Class: Mailboxer::Conversation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/mailboxer/conversation.rb,
app/models/mailboxer/conversation/opt_out.rb

Defined Under Namespace

Classes: OptOut

Instance Method Summary collapse

Instance Method Details

#add_participant(participant) ⇒ Object

Adds a new participant to the conversation


119
120
121
122
123
124
125
126
127
128
# File 'app/models/mailboxer/conversation.rb', line 119

def add_participant(participant)
	messages.each do |message|
     Mailboxer::ReceiptBuilder.new({
       :notification => message,
       :receiver     => participant,
       :updated_at   => message.updated_at,
       :created_at   => message.created_at
     }).build.save
	end
end

#count_messagesObject

Returns the number of messages of the conversation


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

def count_messages
  Mailboxer::Message.conversation(self).count
end

#has_subscriber?(participant) ⇒ Boolean

tells if participant is opt in

Returns:

  • (Boolean)

179
180
181
# File 'app/models/mailboxer/conversation.rb', line 179

def has_subscriber?(participant)
  !opt_outs.unsubscriber(participant).any?
end

#is_completely_trashed?(participant) ⇒ Boolean

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

Returns:

  • (Boolean)

150
151
152
153
# File 'app/models/mailboxer/conversation.rb', line 150

def is_completely_trashed?(participant)
  return false unless participant
  receipts_for(participant).trash.count == receipts_for(participant).count
end

#is_deleted?(participant) ⇒ Boolean

Returns true if the participant has deleted the conversation

Returns:

  • (Boolean)

137
138
139
140
# File 'app/models/mailboxer/conversation.rb', line 137

def is_deleted?(participant)
  return false unless participant
  return receipts_for(participant).deleted.count == receipts_for(participant).count
end

#is_orphaned?Boolean

Returns true if both participants have deleted the conversation

Returns:

  • (Boolean)

143
144
145
146
147
# File 'app/models/mailboxer/conversation.rb', line 143

def is_orphaned?
  participants.reduce(true) do |is_orphaned, participant|
    is_orphaned && is_deleted?(participant)
  end
end

#is_participant?(participant) ⇒ Boolean

Returns true if the messageable is a participant of the conversation

Returns:

  • (Boolean)

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

def is_participant?(participant)
  return false unless participant
  receipts_for(participant).any?
end

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)

155
156
157
# File 'app/models/mailboxer/conversation.rb', line 155

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

#is_trashed?(participant) ⇒ Boolean

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

Returns:

  • (Boolean)

131
132
133
134
# File 'app/models/mailboxer/conversation.rb', line 131

def is_trashed?(participant)
  return false unless participant
  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)

160
161
162
163
# File 'app/models/mailboxer/conversation.rb', line 160

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

#last_messageObject

Last message in the conversation.


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

def last_message
  @last_message ||= messages.order('created_at DESC').first
end

#last_senderObject

Sender of the last message.


93
94
95
# File 'app/models/mailboxer/conversation.rb', line 93

def last_sender
  @last_sender ||= last_message.sender
end

#mark_as_deleted(participant) ⇒ Object

Mark the conversation as deleted for one of the participants


61
62
63
64
65
66
67
68
69
# File 'app/models/mailboxer/conversation.rb', line 61

def mark_as_deleted(participant)
  return unless participant
  deleted_receipts = receipts_for(participant).mark_as_deleted
  if is_orphaned?
    destroy
  else
    deleted_receipts
  end
end

#mark_as_read(participant) ⇒ Object

Mark the conversation as read for one of the participants


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

def mark_as_read(participant)
  return unless participant
  receipts_for(participant).mark_as_read
end

#mark_as_unread(participant) ⇒ Object

Mark the conversation as unread for one of the participants


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

def mark_as_unread(participant)
  return unless participant
  receipts_for(participant).mark_as_unread
end

#move_to_trash(participant) ⇒ Object

Move the conversation to the trash for one of the participants


49
50
51
52
# File 'app/models/mailboxer/conversation.rb', line 49

def move_to_trash(participant)
  return unless participant
  receipts_for(participant).move_to_trash
end

#opt_in(participant) ⇒ Object

Destroys opt out object if any a participant outside of the discussion is, yet, not meant to optin


174
175
176
# File 'app/models/mailboxer/conversation.rb', line 174

def opt_in(participant)
  opt_outs.unsubscriber(participant).destroy_all
end

#opt_out(participant) ⇒ Object

Creates a opt out object because by default all particpants are opt in


167
168
169
170
# File 'app/models/mailboxer/conversation.rb', line 167

def opt_out(participant)
  return unless has_subscriber?(participant)
  opt_outs.create(:unsubscriber => participant)
end

#original_messageObject

First message of the conversation.


88
89
90
# File 'app/models/mailboxer/conversation.rb', line 88

def original_message
  @original_message ||= messages.order('created_at').first
end

#originatorObject

Originator of the conversation.


83
84
85
# File 'app/models/mailboxer/conversation.rb', line 83

def originator
  @originator ||= original_message.sender
end

#participantsObject

Returns an array of participants


78
79
80
# File 'app/models/mailboxer/conversation.rb', line 78

def participants
  recipients
end

#receipts_for(participant) ⇒ Object

Returns the receipts of the conversation for one participants


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

def receipts_for(participant)
  Mailboxer::Receipt.conversation(self).recipient(participant)
end

#recipientsObject

Returns an array of participants


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

def recipients
  return [] unless original_message
  Array original_message.recipients
end

#untrash(participant) ⇒ Object

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


55
56
57
58
# File 'app/models/mailboxer/conversation.rb', line 55

def untrash(participant)
  return unless participant
  receipts_for(participant).untrash
end