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



125
126
127
128
129
130
131
# File 'app/models/message.rb', line 125

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: User.locate!(resource_h['message']['sender']),
                                content: resource_h['message']['content']
  end
end

.read_all!Object



121
122
123
# File 'app/models/message.rb', line 121

def self.read_all!
  update_all read: true
end

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


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

def approved?
  approved_at?
end

#authorize!(user) ⇒ Object

Raises:

  • (Mumukit::Auth::UnauthorizedAccessError)


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

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

#authorized?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#contextualizationObject



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

def contextualization
  direct? ? assignment : discussion
end

#contextualized?Boolean

Returns:

  • (Boolean)


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

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)


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

def direct?
  submission_id.present?
end

#from_initiator?Boolean

Returns:

  • (Boolean)


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

def from_initiator?
  sender == discussion&.initiator
end

#from_moderator?Boolean

Returns:

  • (Boolean)


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

def from_moderator?
  from_moderator || sender.moderator_here?
end

#from_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def from_user?(user)
  sender == user
end

#mark_from_moderator!Object



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

def mark_from_moderator!
  self.from_moderator = from_moderator?
end

#notify!Object



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

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

#question?Boolean

Returns:

  • (Boolean)


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

def question?
  from_initiator? && !not_actually_a_question?
end

#read!Object



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

def read!
  update! read: true
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)


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

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

#subjectObject

TODO remove this once messages generate notifications



134
135
136
# File 'app/models/message.rb', line 134

def subject
  'message'
end

#targetObject



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

def target
  self
end

#to_resource_hObject



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

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,
                   :from_moderator, :sender_id],
          include: {exercise: {only: [:bibliotheca_id]}})
      .merge(organization: Organization.current.name, sender: sender.uid)
end

#toggle_approved!(user) ⇒ Object



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

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

#toggle_not_actually_a_question!Object



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

def toggle_not_actually_a_question!
  toggle! :not_actually_a_question
end

#update_counters_cache!Object



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

def update_counters_cache!
  discussion&.update_counters!
end

#validated?Boolean

Returns:

  • (Boolean)


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

def validated?
  approved? || from_moderator?
end