Class: Message

Inherits:
ApplicationRecord show all
Defined in:
app/models/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

aggregate_of, all_except, defaults, name_model_as, numbered, #save, #save_and_notify!, #save_and_notify_changes!, #update_and_notify!, update_or_create!

Class Method Details

.import_from_json!(json) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/models/message.rb', line 56

def self.import_from_json!(json)
  message_data = parse_json json
  Organization.find_by!(name: message_data.delete('organization')).switch!

  if message_data['submission_id'].present?
    Assignment.find_by(submission_id: message_data.delete('submission_id'))&.receive_answer! message_data
  end
end

.parse_json(json) ⇒ Object



45
46
47
48
49
50
# File 'app/models/message.rb', line 45

def self.parse_json(json)
  message = json.delete 'message'
  json
    .except('uid', 'exercise_id')
    .merge(message)
end

.read_all!Object



52
53
54
# File 'app/models/message.rb', line 52

def self.read_all!
  update_all read: true
end

Instance Method Details

#as_platform_jsonObject



35
36
37
38
39
# File 'app/models/message.rb', line 35

def as_platform_json
  as_json(except: [:id, :type, :discussion_id],
          include: {exercise: {only: [:bibliotheca_id]}})
    .merge(organization: Organization.current.name)
end

#authorize!Object

Raises:

  • (Mumukit::Auth::UnauthorizedAccessError)


31
32
33
# File 'app/models/message.rb', line 31

def authorize!
  raise Mumukit::Auth::UnauthorizedAccessError unless authorized?(current_user)
end

#authorized?(user) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/message.rb', line 27

def authorized?(user)
  from_user?(user) || user&.moderator?
end

#from_initiator?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/models/message.rb', line 15

def from_initiator?
  sender_user == discussion&.initiator
end

#from_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/models/message.rb', line 19

def from_user?(user)
  sender_user == user
end

#notify!Object



11
12
13
# File 'app/models/message.rb', line 11

def notify!
  Mumukit::Nuntius.notify! 'student-messages', as_platform_json
end

#read!Object



41
42
43
# File 'app/models/message.rb', line 41

def read!
  update! read: true
end

#sender_userObject



23
24
25
# File 'app/models/message.rb', line 23

def sender_user
  User.find_by(uid: sender)
end