Class: Conversation

Inherits:
ApplicationRecord show all
Includes:
Diaspora::Federated::Base, Diaspora::Fields::Author, Diaspora::Fields::Guid
Defined in:
app/models/conversation.rb

Instance Method Summary collapse

Methods included from Diaspora::Fields::Author

included

Methods included from Diaspora::Fields::Guid

included, #set_guid

Methods included from Diaspora::Federated::Base

#object_to_receive

Instance Method Details

#first_unread_message(user) ⇒ Object


31
32
33
34
35
# File 'app/models/conversation.rb', line 31

def first_unread_message(user)
  if visibility = self.conversation_visibilities.where(:person_id => user.person.id).where('unread > 0').first
    self.messages.to_a[-visibility.unread]
  end
end

#last_authorObject


58
59
60
61
62
# File 'app/models/conversation.rb', line 58

def last_author
  return unless @last_author.present? || messages.size > 0
  @last_author_id ||= messages.pluck(:author_id).last
  @last_author ||= Person.includes(:profile).find_by(id: @last_author_id)
end

#local_recipientsObject


14
15
16
17
18
19
20
21
22
23
# File 'app/models/conversation.rb', line 14

def local_recipients
  recipients.each do |recipient|
    if recipient.local?
      unless recipient.owner.contacts.where(person_id: author.id).any? ||
          (author.owner && author.owner.podmin_account?)
        errors.add(:all_recipients, "recipient not allowed")
      end
    end
  end
end

#ordered_participantsObject


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

def ordered_participants
  @ordered_participants ||= (messages.map(&:author).reverse + participants).uniq
end

#participant_handlesObject


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

def participant_handles
  participants.map(&:diaspora_handle).join(";")
end

#participant_handles=(handles) ⇒ Object


52
53
54
55
56
# File 'app/models/conversation.rb', line 52

def participant_handles=(handles)
  handles.split(";").each do |handle|
    participants << Person.find_or_fetch_by_identifier(handle)
  end
end

#recipientsObject


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

def recipients
  self.participants - [self.author]
end

#set_read(user) ⇒ Object


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

def set_read(user)
  update_read_for(user, read: true)
end

#subjectObject


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

def subject
  self[:subject].blank? ? I18n.t("conversations.new.subject_default") : self[:subject]
end

#subscribersObject


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

def subscribers
  recipients
end

#update_read_for(user, read:) ⇒ Object


41
42
43
44
45
46
# File 'app/models/conversation.rb', line 41

def update_read_for(user, read:)
  visibility = conversation_visibilities.find_by(person_id: user.person.id)
  return unless visibility
  visibility.unread = read ? 0 : 1
  visibility.save
end