Class: Message

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithSoftDeletion

#deleted?, #soft_delete!

Methods inherited from ApplicationRecord

active_between, aggregate_of, all_except, defaults, #delete, #destroy!, enum_prefixed_translations_for, 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, with_pg_retry, with_temporary_token

Methods included from WithPgLock

#with_pg_lock

Class Method Details

.import_from_resource_h!(resource_h) ⇒ Object



122
123
124
125
126
127
128
# File 'app/models/message.rb', line 122

def self.import_from_resource_h!(resource_h)
  if resource_h['submission_id'].present?
    assignment = Assignment.find_by(submission_id: resource_h['submission_id'])
    assignment&.receive_answer! sender: resource_h['message']['sender'],
                                content: resource_h['message']['content']
  end
end

.read_all!Object



118
119
120
# File 'app/models/message.rb', line 118

def self.read_all!
  update_all read: true
end

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/models/message.rb', line 98

def approved?
  approved_at?
end

#authorize!(user) ⇒ Object

Raises:

  • (Mumukit::Auth::UnauthorizedAccessError)


71
72
73
# File 'app/models/message.rb', line 71

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

#authorized?(user) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/message.rb', line 67

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

#contextualizationObject



25
26
27
# File 'app/models/message.rb', line 25

def contextualization
  direct? ? assignment : discussion
end

#contextualized?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/message.rb', line 29

def contextualized?
  assignment_id.present? ^ discussion_id.present?
end

#direct?Boolean

Whether this message is direct, that is, whether it comes from rise-hand feature. Forum messages are non-direct.

Returns:

  • (Boolean)


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

def direct?
  submission_id.present?
end

#from_initiator?Boolean

Returns:

  • (Boolean)


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

def from_initiator?
  sender_user == discussion&.initiator
end

#from_moderator?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/message.rb', line 55

def from_moderator?
  sender_user.moderator_here?
end

#from_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/message.rb', line 59

def from_user?(user)
  sender_user == user
end

#notify!Object



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

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

#question?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/message.rb', line 110

def question?
  from_initiator? && !not_actually_a_question?
end

#read!Object



82
83
84
# File 'app/models/message.rb', line 82

def read!
  update! read: true
end

#sender_userObject



63
64
65
# File 'app/models/message.rb', line 63

def sender_user
  User.find_by(uid: sender)
end

#stale?Boolean

Whether this message is stale, that is, it targets a submission that is not the latest one.

Only direct messages may become stale.

Returns:

  • (Boolean)


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

def stale?
  direct? && assignment.submission_id != submission_id
end

#subjectObject

TODO remove this once messages generate notifications



131
132
133
# File 'app/models/message.rb', line 131

def subject
  'message'
end

#targetObject



114
115
116
# File 'app/models/message.rb', line 114

def target
  self
end

#to_resource_hObject



75
76
77
78
79
80
# File 'app/models/message.rb', line 75

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

#toggle_approved!(user) ⇒ Object



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

def toggle_approved!(user)
  if approved?
    disapprove!
  else
    approve!(user)
  end
end

#toggle_not_actually_a_question!Object



94
95
96
# File 'app/models/message.rb', line 94

def toggle_not_actually_a_question!
  toggle! :not_actually_a_question
end

#update_counters_cache!Object



106
107
108
# File 'app/models/message.rb', line 106

def update_counters_cache!
  discussion&.update_counters!
end

#validated?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/message.rb', line 102

def validated?
  approved? || from_moderator?
end