Sha256: 3117fefbe3f983d1dda1a51590ec253f75578b723ef7c03429ac406006795125

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 KB

Contents

class Message < ApplicationRecord

  belongs_to :discussion, optional: true
  belongs_to :assignment, foreign_key: :submission_id, primary_key: :submission_id, optional: true
  has_one :exercise, through: :assignment

  validates_presence_of :content, :sender
  validates_presence_of :submission_id, :unless => :discussion_id?
  markdown_on :content

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

  def from_initiator?
    sender_user == discussion&.initiator
  end

  def from_user?(user)
    sender_user == user
  end

  def sender_user
    User.find_by(uid: sender)
  end

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

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

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

  def read!
    update! read: true
  end

  def toggle_approved!
    toggle! :approved
  end

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

  def self.read_all!
    update_all read: true
  end

  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
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mumuki-domain-6.5.1 app/models/message.rb
mumuki-domain-6.5.0 app/models/message.rb
mumuki-domain-6.4.3 app/models/message.rb
mumuki-domain-6.4.2 app/models/message.rb
mumuki-domain-6.4.1 app/models/message.rb
mumuki-domain-6.4.0 app/models/message.rb
mumuki-domain-6.2.2 app/models/message.rb
mumuki-domain-6.2.1 app/models/message.rb
mumuki-domain-6.3.0 app/models/message.rb
mumuki-domain-6.2.0 app/models/message.rb