Module: PrivateMail::InstanceMethods

Defined in:
lib/private_mail.rb

Overview

Adds instance methods.

Instance Method Summary collapse

Instance Method Details

#mailboxObject

returns an instance of class type Mailbox - this object essentially wraps the user’s mail messages and provides a clean interface for accessing them. see Mailbox for more details.

example:

phil = User.find(3123)
phil.mailbox[:inbox].unread_mail      #returns all unread mail in your inbox
phil.mailbox[:sentbox].mail         #returns all sent mail messages


52
53
54
55
56
# File 'lib/private_mail.rb', line 52

def mailbox()
  @mailbox = Mailbox.new(self) if @mailbox.nil?
  @mailbox.type = :all
  return @mailbox
end

#read_conversation(conversation, options = {}) ⇒ Object

returns an array of the user’s Mail associated with the given conversation. All mail is marked as read but the returning array is built before this so you can see which messages were unread when viewing the conversation.

This returns deleted/trashed messages as well for the purpose of reading trashed convos, to disable this send the option ‘:conditions => “mail.trashed != true”’

params:

conversation

the Conversation object that you want to read.

options

any options to filter the conversation, these are used as find options so all valid options for find will work.

returns:

array of Mail belonging to the given conversation.



192
193
194
195
196
# File 'lib/private_mail.rb', line 192

def read_conversation(conversation, options = {})
  convo_mail = self.mailbox.mail(options.merge(:conversation => conversation))
  self.mailbox.mark_as_read(:conversation => conversation)
  return convo_mail
end

#read_mail(mail) ⇒ Object

returns the mail given as the parameter, marked as read.



175
176
177
# File 'lib/private_mail.rb', line 175

def read_mail(mail)
  return mail.mark_as_read()
end

#reply(conversation, recipients, reply_body, subject = nil) ⇒ Object

creates a new Message associated with the given conversation and delivers the reply to each of the given recipients.

*explicitly calling this method is rare unless you are replying to a subset of the users involved in the conversation or if you are including someone that is not currently in the conversation. reply_to_sender, reply_to_all, and reply_to_conversation will suffice in most cases.

params:

conversation

the Conversation object that the mail you are responding to belongs.

recipients

a single User object or array of Users to deliver the reply message to.

reply_body

the body of the reply message.

subject

the subject of the message, defaults to ‘RE: [original subject]’ if one isnt given.

returns:

the sent Mail.



99
100
101
102
103
104
105
106
# File 'lib/private_mail.rb', line 99

def reply(conversation, recipients, reply_body, subject = nil)
  return nil if(reply_body.blank?)
  subject = subject || "RE: #{conversation.subject}"
  response = Message.create({:sender => self, :conversation => conversation, :body => reply_body, :subject => subject})
  response.recipients = recipients.is_a?(Array) ? recipients : [recipients]
  response.deliver(self.mailbox_types[:received])
  return mailbox[self.mailbox_types[:sent]] << response
end

#reply_to_all(mail, reply_body, subject = nil) ⇒ Object

sends a Mail to all of the recipients of the given mail message (excluding yourself).

params:

mail

the Mail object that you are replying to.

reply_body

the body of the reply message.

subject

the subject of the message, defaults to ‘RE: [original subject]’ if one isnt given.

returns:

the sent Mail.



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/private_mail.rb', line 134

def reply_to_all(mail, reply_body, subject = nil)
  msg = mail.message
  recipients = msg.recipients.clone()
  if(msg.sender != self)
    recipients.delete(self)
    if(!recipients.include?(msg.sender))
      recipients << msg.sender
    end
  end
  return reply(mail.conversation, recipients, reply_body, subject)
end

#reply_to_conversation(conversation, reply_body, subject = nil) ⇒ Object

sends a Mail to all users involved in the given conversation (excluding yourself).

*this may have undesired effects if users have been added to the conversation after it has begun.

params:

conversation

the Conversation object that the mail you are responding to belongs.

reply_body

the body of the reply message.

subject

the subject of the message, defaults to ‘RE: [original subject]’ if one isnt given.

returns:

the sent Mail.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/private_mail.rb', line 159

def reply_to_conversation(conversation, reply_body, subject = nil)
  #move conversation to inbox if it is currently in the trash - doesnt make much sense replying to a trashed convo.
  if((mailbox[self.mailbox_types[:deleted]].has_conversation?(conversation)))
    mailbox.move_to(self.mailbox_types[:received], :conversation => conversation)
  end
  #remove self from recipients unless you are the originator of the convo
  recipients = conversation.original_message.recipients.clone()
  if(conversation.originator != self)
    recipients.delete(self)
    if(!recipients.include?(conversation.originator))
      recipients << conversation.originator
    end
  end
  return reply(conversation,recipients, reply_body, subject)
end

#reply_to_sender(mail, reply_body, subject = nil) ⇒ Object

sends a Mail to the sender of the given mail message.

params:

mail

the Mail object that you are replying to.

reply_body

the body of the reply message.

subject

the subject of the message, defaults to ‘RE: [original subject]’ if one isnt given.

returns:

the sent Mail.



119
120
121
# File 'lib/private_mail.rb', line 119

def reply_to_sender(mail, reply_body, subject = nil)
  return reply(mail.conversation, mail.message.sender, reply_body, subject)
end

#send_message(recipients, msg_body, subject = '') ⇒ Object

creates new Message and Conversation objects from the given parameters and delivers Mail to each of the recipients’ inbox.

params:

recipients

a single user object or array of users to deliver the message to.

msg_body

the body of the message.

subject

the subject of the message, defaults to empty string if not provided.

returns:

the sent Mail.

example:

phil = User.find(3123)
todd = User.find(4141)
phil.send_message(todd, 'whats up for tonight?', 'hey guy')      #sends a Mail message to todd's inbox, and a Mail message to phil's sentbox


74
75
76
77
78
79
80
# File 'lib/private_mail.rb', line 74

def send_message(recipients, msg_body, subject = '')
  convo = Conversation.create({:subject => subject})
  message = Message.create({:sender => self, :conversation => convo,  :body => msg_body, :subject => subject})
  message.recipients = recipients.is_a?(Array) ? recipients : [recipients]
  message.deliver(self.mailbox_types[:received])
  return mailbox[:sentbox] << message
end