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_temporary_token

Class Method Details

.import_from_resource_h!(json) ⇒ Object



99
100
101
102
103
104
105
106
# File 'app/models/message.rb', line 99

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



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

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

.read_all!Object



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

def self.read_all!
  update_all read: true
end

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


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

def approved?
  approved_at?
end

#authorize!(user) ⇒ Object

Raises:

  • (Mumukit::Auth::UnauthorizedAccessError)


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

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

#authorized?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#from_initiator?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/message.rb', line 21

def from_initiator?
  sender_user == discussion&.initiator
end

#from_moderator?Boolean

Returns:

  • (Boolean)


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

def from_moderator?
  sender_user.moderator_here?
end

#from_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def from_user?(user)
  sender_user == user
end

#notify!Object



17
18
19
# File 'app/models/message.rb', line 17

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

#question?Boolean

Returns:

  • (Boolean)


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

def question?
  from_initiator? && !not_actually_a_question?
end

#read!Object



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

def read!
  update! read: true
end

#sender_userObject



33
34
35
# File 'app/models/message.rb', line 33

def sender_user
  User.find_by(uid: sender)
end

#targetObject



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

def target
  self
end

#to_resource_hObject



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

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



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

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

#toggle_not_actually_a_question!Object



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

def toggle_not_actually_a_question!
  toggle! :not_actually_a_question
end

#update_counters_cache!Object



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

def update_counters_cache!
  discussion&.update_counters!
end

#validated?Boolean

Returns:

  • (Boolean)


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

def validated?
  approved? || from_moderator?
end