Welcome to mirror list, hosted at ThFree Co, Russian Federation.

message.rb « models « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e0c738d00022b94f66c061b02a297515a8817b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

class Message < ApplicationRecord
  include Diaspora::Federated::Base
  include Diaspora::Fields::Guid
  include Diaspora::Fields::Author

  include Reference::Source

  belongs_to :conversation, touch: true

  delegate :name, to: :author, prefix: true

  validates :text, presence: true
  validate :participant_of_parent_conversation

  def conversation_guid=(guid)
    self.conversation_id = Conversation.where(guid: guid).ids.first
  end

  def increase_unread(user)
    vis = ConversationVisibility.find_by(conversation_id: conversation_id, person_id: user.person.id)
    return unless vis
    vis.unread += 1
    vis.save
  end

  def message
    @message ||= Diaspora::MessageRenderer.new text
  end

  # @return [Array<Person>]
  def subscribers
    conversation.participants
  end

  private

  def participant_of_parent_conversation
    if conversation && !conversation.participants.include?(author)
      errors[:base] << "Author is not participating in the conversation"
    else
      true
    end
  end
end