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, #delete, #destroy!, numbered, organic_on, resource_fields, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes

Class Method Details

.import_from_resource_h!(json) ⇒ Object



84
85
86
87
88
89
90
91
# File 'app/models/message.rb', line 84

def self.import_from_resource_h!(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



73
74
75
76
77
78
# File 'app/models/message.rb', line 73

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

.read_all!Object



80
81
82
# File 'app/models/message.rb', line 80

def self.read_all!
  update_all read: true
end

Instance Method Details

#authorize!(user) ⇒ Object

Raises:

  • (Mumukit::Auth::UnauthorizedAccessError)


39
40
41
# File 'app/models/message.rb', line 39

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

#authorized?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#from_initiator?Boolean

Returns:

  • (Boolean)


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

def from_initiator?
  sender_user == discussion&.initiator
end

#from_moderator?Boolean

Returns:

  • (Boolean)


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

def from_moderator?
  sender_user.moderator_here?
end

#from_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def from_user?(user)
  sender_user == user
end

#notify!Object



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

def notify!
  Mumukit::Nuntius.notify! 'student-messages', to_resource_h unless Organization.silenced?
end

#question?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/message.rb', line 69

def question?
  from_initiator? && !not_actually_a_question?
end

#read!Object



49
50
51
# File 'app/models/message.rb', line 49

def read!
  update! read: true
end

#sender_userObject



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

def sender_user
  User.find_by(uid: sender)
end

#to_resource_hObject



43
44
45
46
47
# File 'app/models/message.rb', line 43

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

#toggle_approved!Object



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

def toggle_approved!
  toggle! :approved
end

#toggle_not_actually_a_question!Object



57
58
59
# File 'app/models/message.rb', line 57

def toggle_not_actually_a_question!
  toggle! :not_actually_a_question
end

#update_counters_cache!Object



65
66
67
# File 'app/models/message.rb', line 65

def update_counters_cache!
  discussion&.update_counters!
end

#validated?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/message.rb', line 61

def validated?
  approved? || from_moderator?
end