Class: Conversation

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Diaspora::Federated::Base, Diaspora::Guid
Defined in:
app/models/conversation.rb

Instance Method Summary collapse

Methods included from Diaspora::Guid

included, #set_guid

Methods included from Diaspora::Federated::Base

included

Instance Method Details

#diaspora_handleObject


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

def diaspora_handle
  self.author.diaspora_handle
end

#diaspora_handle=(nh) ⇒ Object


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

def diaspora_handle= nh
  self.author = Webfinger.new(nh).fetch
end

#first_unread_message(user) ⇒ Object


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

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

#last_authorObject


56
57
58
59
60
# File 'app/models/conversation.rb', line 56

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

#max_participantsObject


19
20
21
# File 'app/models/conversation.rb', line 19

def max_participants
  errors.add(:max_participants, "too many participants") if participants.count > 20
end

#participant_handlesObject


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

def participant_handles
  self.participants.map{|p| p.diaspora_handle}.join(";")
end

#participant_handles=(handles) ⇒ Object


50
51
52
53
54
# File 'app/models/conversation.rb', line 50

def participant_handles= handles
  handles.split(';').each do |handle|
    self.participants << Webfinger.new(handle).fetch
  end
end

#public?Boolean


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

def public?
  false
end

#receive(user, person) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/conversation.rb', line 70

def receive(user, person)
  cnv = Conversation.find_or_create_by_guid(self.attributes)

  self.participants.each do |participant|
    ConversationVisibility.find_or_create_by_conversation_id_and_person_id(cnv.id, participant.id)
  end

  self.messages.each do |msg|
    msg.conversation_id = cnv.id
    received_msg = msg.receive(user, person)
    Notification.notify(user, received_msg, person) if msg.respond_to?(:notification_type)
  end
end

#recipientsObject


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

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

#subjectObject


62
63
64
# File 'app/models/conversation.rb', line 62

def subject
  self[:subject].blank? ? "no subject" : self[:subject]
end

#subscribers(user) ⇒ Object


66
67
68
# File 'app/models/conversation.rb', line 66

def subscribers(user)
  self.recipients
end