Created
July 14, 2009 22:11
-
-
Save shwoodard/147241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message < ActiveRecord::Base | |
belongs_to :reply_of, :class_name => 'Message', :foreign_key => 'parent_id' | |
belongs_to :replies, :class_name => 'Message', :foreign_key => 'parent_id' | |
belongs_to :sender, :class_name => 'CommunityUser', :foreign_key => 'sender_id' | |
belongs_to :recipient, :class_name => 'CommunityUser', :foreign_key => 'recipient_id' | |
belongs_to :conversation | |
named_scope :deleted, lambda { |user| | |
{:conditions => ["(sender_deleted_at IS NOT NULL AND sender_id = #{user.id} AND sender_deleted_permanently != 1) OR (recipient_deleted_at IS NOT NULL AND recipient_id = #{user.id} AND recipient_deleted_permanently != 1)"]} | |
} | |
named_scope :unread_received, lambda { |user| | |
{:conditions => ["recipient_id = #{user.id} AND recipient_read_at IS NULL AND recipient_deleted_at IS NULL"]} | |
} | |
validates_presence_of :subject, :content | |
validates_presence_of :anonymous_email, :sender_name, :if => :from_anonymous? | |
validate :recipient_is_a_participant? | |
validate :sender_or_recipient_are_valid? | |
before_create :assign_conversation | |
attr_accessor :anonymous_sender_name | |
# after_create :send_receipt_reminder | |
# define_index do | |
# indexes :subject | |
# indexes :content | |
# indexes :sender_name | |
# indexes [sender.first_name, sender.last_name, sender.email, sender.title], :as => :sender | |
# indexes [recipient.first_name, recipient.last_name, recipient.email, recipient.title], :as => :recipient | |
# indexes [:anonymous_email, :anonymous_phone], :as => :anonymous | |
# | |
# has :sender_deleted_at | |
# has :sender_read_at | |
# has :recipient_deleted_at | |
# has :recipient_read_at | |
# has :replied_at | |
# has :created_at | |
# has :updated_at | |
# has :anonymous | |
# has sender(:id), :as => :sender_ids | |
# has recipient(:id), :as => :recipient_ids | |
# | |
# set_property :delta => true | |
# end | |
def from | |
sender_name | |
end | |
def to | |
recipient.full_name | |
end | |
def reply? | |
!reply_of.nil? | |
end | |
# Return true if a message has been read. | |
def read? | |
!recipient_read_at.nil? | |
end | |
def other_member(member) | |
member == sender ? recipient : sender | |
end | |
#TODO refactor so gets first message from conversation rather than uses recursion | |
def anonymous_sender_name | |
@anonymous_sender_name ||= parent_id.nil? ? sender_name : reply_of.anonymous_sender_name | |
end | |
# Put the message in the trash for the given person. | |
def trash(member, time=Time.zone.now) | |
if sender == member && recipient == member | |
self.sender_deleted_at = time | |
self.recipient_deleted_at = time | |
else | |
case member | |
when sender | |
self.sender_deleted_at = time | |
when recipient | |
self.recipient_deleted_at = time | |
else | |
# Given our controller before filters, this should never happen... | |
raise ArgumentError, "Unauthorized member" | |
end | |
end | |
save! | |
end | |
# Put the message in the trash for the given person. | |
def restore(member) | |
if sender == member && recipient == member | |
self.sender_deleted_at = nil | |
self.recipient_deleted_at = nil | |
else | |
case member | |
when sender | |
self.sender_deleted_at = nil | |
when recipient | |
self.recipient_deleted_at = nil | |
else | |
# Given our controller before filters, this should never happen... | |
raise ArgumentError, "Unauthorized member" | |
end | |
end | |
save! | |
end | |
# Return true if the message has been trashed. | |
def trashed?(member) | |
case member | |
when sender | |
!sender_deleted_at.nil? | |
when recipient | |
!recipient_deleted_at.nil? | |
end | |
end | |
def anonymous_reply? | |
anonymous? && reply_of | |
end | |
def sender_with_deleted | |
# no sender_id for anonymous case | |
return CommunityUser.find_with_destroyed(self.sender_id) unless sender_without_deleted or !self.sender_id | |
sender_without_deleted | |
end | |
alias_method_chain :sender, :deleted | |
def recipient_with_deleted | |
return CommunityUser.find_with_destroyed(self.recipient_id) unless recipient_without_deleted or !self.recipient_id | |
recipient_without_deleted | |
end | |
alias_method_chain :recipient, :deleted | |
protected | |
# Assign the conversation id. | |
# This is the parent message's conversation unless there is no parent, | |
# in which case we create a new conversation. | |
def assign_conversation | |
self.conversation = reply_of.nil? ? Conversation.create : reply_of.conversation | |
end | |
def from_anonymous? | |
anonymous? | |
end | |
def recipient_is_a_participant? | |
return true if recipient.blank? #anonymous msg | |
if !recipient.messaging_participant? && !reply? | |
errors.add(:recipient, "This recipient can't recieve messages") | |
end | |
end | |
def sender_or_recipient_are_valid? | |
if recipient_id.blank? && sender_id.blank? | |
raise ActiveRecord::RecordNotFoundException | |
elsif anonymous_reply? && !sender | |
raise ActiveRecord::RecordNotFoundException | |
elsif anonymous? && !anonymous_reply? && !recipient | |
raise ActiveRecord::RecordNotFoundException | |
elsif !sender || !recipein | |
raise ActiveRecord::RecordNotFoundException | |
end | |
end | |
def self.send_receipt_reminder(msg, url) | |
return if msg.sender == msg.recipient | |
MessageMailer.deliver_message_notification(msg, url) | |
end | |
def self.send_recommendation(recommendation) | |
MessageMailer.deliver_recommendation(recommendation) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment