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



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

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



123
124
125
# File 'app/models/message.rb', line 123

def self.read_all!
  update_all read: true
end

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


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

def approved?
  approved_at?
end

#authorize!(user) ⇒ Object

Raises:

  • (Mumukit::Auth::UnauthorizedAccessError)


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

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

#authorized?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#contextualizationObject



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

def contextualization
  direct? ? assignment : discussion
end

#contextualized?Boolean

Returns:

  • (Boolean)


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

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)


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

def direct?
  submission_id.present?
end

#from_initiator?Boolean

Returns:

  • (Boolean)


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

def from_initiator?
  sender_user == discussion&.initiator
end

#from_moderator?Boolean

Returns:

  • (Boolean)


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

def from_moderator?
  from_moderator || sender_user.moderator_here?
end

#from_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def from_user?(user)
  sender_user == user
end

#mark_from_moderator!Object



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

def mark_from_moderator!
  self.from_moderator = from_moderator?
end

#notify!Object



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

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

#question?Boolean

Returns:

  • (Boolean)


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

def question?
  from_initiator? && !not_actually_a_question?
end

#read!Object



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

def read!
  update! read: true
end

#sender_userObject



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

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)


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

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

#subjectObject

TODO remove this once messages generate notifications



136
137
138
# File 'app/models/message.rb', line 136

def subject
  'message'
end

#targetObject



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

def target
  self
end

#to_resource_hObject



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

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

#toggle_approved!(user) ⇒ Object



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

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

#toggle_not_actually_a_question!Object



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

def toggle_not_actually_a_question!
  toggle! :not_actually_a_question
end

#update_counters_cache!Object



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

def update_counters_cache!
  discussion&.update_counters!
end

#validated?Boolean

Returns:

  • (Boolean)


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

def validated?
  approved? || from_moderator?
end