Class: PostmanPat::PmMail

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
AASM
Defined in:
app/models/postman_pat/pm_mail.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compose(author, recipients, subject, message) ⇒ PmMessage

Creates and sends a new Private Message with subject and message to all recipients

Parameters:

  • user (User)

    the sender of this message.

  • recipients (Array)

    user account ids that are to receive the message

  • subject (optional, String)

    an optional subject for the new message. If no subject is given a default message of "[No Subject]" is provided.

  • message (String)

    the message content

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/postman_pat/pm_mail.rb', line 62

def self.compose(author, recipients, subject, message)
  recipients.map {|r| r.to_i}
  
  # Add default subject if non is provided
  subject = "[No Subject]" if subject.blank?

  msg = PostmanPat::PmMessage.new
  msg.author = author
  msg.subject = subject 
  msg.message = message
  
  transaction do
    msg.save!
    # send a copy of the mail to the creator of the message and mark it as sent
    mail = PostmanPat::PmMail.new
    mail.recipient = author
    mail.pm_message = msg
    mail.mark_sent!
    mail.save!

    # create a new piece of mail for each collaborator in the message
    recipients.each do |recipient|
      mail = PostmanPat::PmMail.new
      mail.recipient_id = recipient
      mail.pm_message = msg
      mail.save! 
    end # each loop
  end # transaction
  msg
end

.get_all_mail(user_id) ⇒ Array

Gets all PmMail's for a user

Parameters:

  • user_id (Integer)

    the user id of the user to fetch all the mail for

Returns:

  • (Array)

    an array of PmMail instances



106
107
108
# File 'app/models/postman_pat/pm_mail.rb', line 106

def self.get_all_mail(user_id)
  where(:recipient_id => user_id)
end

.get_mail(user_id, mail_id) ⇒ PmMail

Gets a specific pm mail by the user_id and mail_id

Parameters:

  • user_id (Integer)

    the user id of the user to collect all mail for

  • mail_id (Integer)

    the PmMail instance id of the mail you're trying to find

Returns:

  • (PmMail)

    PmMail instance or nil if the mail message of the user can not be found



98
99
100
# File 'app/models/postman_pat/pm_mail.rb', line 98

def self.get_mail(user_id, mail_id)
  where(:id => mail_id).where(:recipient_id => user_id).first  
end

.num_deleted_mail(user_id) ⇒ Object

Returns the number of deleted private mail for a particular user

Parameters:

  • user_id (Integer)

    user id of the user to find the number of deleted mails for



140
141
142
# File 'app/models/postman_pat/pm_mail.rb', line 140

def self.num_deleted_mail(user_id)
  where(:recipient_id => user_id).where(:aasm_state => 'deleted').count
end

.num_mail(user_id) ⇒ Object

Returns the total number of private mails for a particular user

Parameters:

  • user_id (Integer)

    user id of the user to find the total number of unread mails for



115
116
117
# File 'app/models/postman_pat/pm_mail.rb', line 115

def self.num_mail(user_id)
  where(:recipient_id => user_id).count
end

.num_read_mail(user_id) ⇒ Object

Returns the number of read private mail for a particular user

Parameters:

  • user_id (Integer)

    user id of the user to find the number of read mails for



132
133
134
# File 'app/models/postman_pat/pm_mail.rb', line 132

def self.num_read_mail(user_id)
  where(:recipient_id => user_id).where(:aasm_state => 'read').count
end

.num_unread_mail(user_id) ⇒ Object

Returns the number of unread private mail for a particular user

Parameters:

  • user_id (Integer)

    user id of the user to find the number of unread mails for



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

def self.num_unread_mail(user_id)
  where(:recipient_id => user_id).where(:aasm_state => 'unread').count
end

Instance Method Details

#all_collaboratorsArray

Returns all the collaborators(users) of the PmMail/Message thread

Returns:

  • (Array)

    array of users who are collaborating on the private message thread



218
219
220
221
222
223
224
225
# File 'app/models/postman_pat/pm_mail.rb', line 218

def all_collaborators
  collaborators = PostmanPat::PmMail.where(:pm_message_id => self.pm_message_id) 
  ids = []
  collaborators.each do |c|
    ids << c.recipient_id
  end
  User.where(:id => ids)
end

#is_deleted?Boolean

Return true or false if the mail has been deleted

Returns:

  • (Boolean)


173
174
175
# File 'app/models/postman_pat/pm_mail.rb', line 173

def is_deleted?
  self.aasm_state == "deleted" 
end

#is_read?Boolean

Return true or false if the mail has been read or not

Returns:

  • (Boolean)


159
160
161
# File 'app/models/postman_pat/pm_mail.rb', line 159

def is_read?
  self.aasm_state == "read"  
end

#is_sent?Boolean

Return true or false if the mail is in a state of sent

Returns:

  • (Boolean)


166
167
168
# File 'app/models/postman_pat/pm_mail.rb', line 166

def is_sent?
  self.aasm_state == "sent"  
end

#is_unread?Boolean

Return true or false if the mail is unread or not

Returns:

  • (Boolean)


152
153
154
# File 'app/models/postman_pat/pm_mail.rb', line 152

def is_unread?
  self.aasm_state == "unread"  
end

#reply(message) ⇒ Object

Reply to the mail message thread.

Parameters:

  • message (String)

    the reply message



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/postman_pat/pm_mail.rb', line 180

def reply(message)
  reply = PostmanPat::PmMessage.new
  reply.parent_id = self.pm_message_id 
  reply.author_id = self.recipient_id
  reply.message = message

  if reply.save!
    # Set the message as unread again for all collaborators of the mail thread because
    # one of them replied, thus making the mail "new" or "unseen" by all collaborators
    set_all_to_unread

    # Set the senders message state to 'sent'
    self.mark_sent!
  end
  
end

#who_last_replied?User

Tell us who the last user was to reply to the PmMail message thread. If nobody has replied, the author will be returned.

Returns:

  • (User)

    the last user to reply. Returns the author of the message if nobody has replied yet



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/models/postman_pat/pm_mail.rb', line 201

def who_last_replied?
  pm_message = PostmanPat::PmMessage.find(self.pm_message_id)
  # [TODO] - raise exception if pm_message is nil
  
  if pm_message.children.count > 0
    # [TODO] - link up the account/user model to be dynamic on whatever model called acts_as_postman_pat
    User.find(pm_message.children.last.author_id)
  else
    # if nobody has replied, return creator
    User.find(pm_message.author_id)
  end
  
end