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



129
130
131
132
133
134
135
136
137
138
# File 'app/models/mailboxer/conversation.rb', line 129

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



118
119
120
# File 'app/models/mailboxer/conversation.rb', line 118

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

#has_subscriber?(participant) ⇒ Boolean

tells if participant is opt in

Returns:

  • (Boolean)


189
190
191
# File 'app/models/mailboxer/conversation.rb', line 189

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)


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

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)


147
148
149
150
# File 'app/models/mailboxer/conversation.rb', line 147

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)


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

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)


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

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

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/models/mailboxer/conversation.rb', line 165

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)


141
142
143
144
# File 'app/models/mailboxer/conversation.rb', line 141

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)


170
171
172
173
# File 'app/models/mailboxer/conversation.rb', line 170

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.



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

def last_message
  @last_message ||= messages.order(:created_at => :desc, :id => :desc).first
end

#last_senderObject

Sender of the last message.



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

def last_sender
  @last_sender ||= last_message.sender
end

#mark_as_deleted(participant) ⇒ Object

Mark the conversation as deleted for one of the participants



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

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



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

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



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

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



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

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



184
185
186
# File 'app/models/mailboxer/conversation.rb', line 184

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

#opt_out(participant) ⇒ Object

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



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

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

#original_messageObject

First message of the conversation.



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

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

#originatorObject

Originator of the conversation.



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

def originator
  @originator ||= original_message.sender
end

#participantsObject

Returns an array of participants



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

def participants
  recipients
end

#receipts_for(participant) ⇒ Object

Returns the receipts of the conversation for one participants



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

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

#recipientsObject

Returns an array of participants



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

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



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

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