Class: Conversation

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/models/conversation.rb

Instance Method Summary collapse

Instance Method Details

#count_messagesObject

Returns the number of messages of the conversation



102
103
104
# File 'app/models/conversation.rb', line 102

def count_messages
	return Message.conversation(self).count
end

#is_completely_trashed?(participant) ⇒ Boolean

Returns true if the participant has trashed all the messages of the conversation

Returns:

  • (Boolean)


119
120
121
122
# File 'app/models/conversation.rb', line 119

def is_completely_trashed?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).trash.count == self.receipts_for(participant).count
end

#is_participant?(participant) ⇒ Boolean

Returns true if the messageable is a participant of the conversation

Returns:

  • (Boolean)


107
108
109
110
# File 'app/models/conversation.rb', line 107

def is_participant?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).count != 0
end

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/conversation.rb', line 124

def is_read?(participant)
	!self.is_unread?(participant)
end

#is_trashed?(participant) ⇒ Boolean

Returns true if the participant has at least one trashed message of the conversation

Returns:

  • (Boolean)


113
114
115
116
# File 'app/models/conversation.rb', line 113

def is_trashed?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).trash.count!=0
end

#is_unread?(participant) ⇒ Boolean

Returns true if the participant has at least one unread message of the conversation

Returns:

  • (Boolean)


129
130
131
132
# File 'app/models/conversation.rb', line 129

def is_unread?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).not_trash.is_unread.count!=0
end

#last_messageObject

Last message in the conversation.



91
92
93
94
# File 'app/models/conversation.rb', line 91

def last_message
	@last_message = self.messages.order('created_at DESC').first if @last_message.nil?
	return @last_message
end

#last_senderObject

Sender of the last message.



85
86
87
88
# File 'app/models/conversation.rb', line 85

def last_sender
	@last_sender = self.last_message.sender if @last_sender.nil?
	return @last_sender
end

#mark_as_read(participant) ⇒ Object

Mark the conversation as read for one of the participants



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

def mark_as_read(participant)
	return if participant.nil?
	return self.receipts_for(participant).mark_as_read
end

#mark_as_unread(participant) ⇒ Object

Mark the conversation as unread for one of the participants



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

def mark_as_unread(participant)
	return if participant.nil?
	return self.receipts_for(participant).mark_as_unread
end

#move_to_trash(participant) ⇒ Object

Move the conversation to the trash for one of the participants



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

def move_to_trash(participant)
	return if participant.nil?
	return self.receipts_for(participant).move_to_trash
end

#original_messageObject

First message of the conversation.



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

def original_message
	@original_message = self.messages.order('created_at').first if @original_message.nil?
	return @original_message
end

#originatorObject

Originator of the conversation.



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

def originator
	@originator = self.original_message.sender if @originator.nil?
	return @originator
end

#participantsObject

Returns an array of participants



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

def participants
  return recipients
end

#receipts_for(participant) ⇒ Object

Returns the receipts of the conversation for one participants



97
98
99
# File 'app/models/conversation.rb', line 97

def receipts_for(participant)
  return Receipt.conversation(self).recipient(participant)
end

#recipientsObject

Returns an array of participants



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

def recipients
	if self.last_message
		recps = self.last_message.recipients
		recps = recps.is_a?(Array) ? recps : [recps]
	return recps
	end
	return []
end

#untrash(participant) ⇒ Object

Takes the conversation out of the trash for one of the participants



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

def untrash(participant)
	return if participant.nil?
	return self.receipts_for(participant).untrash
end