2011-08-26 22:59:43 +02:00
|
|
|
require 'iconv'
|
|
|
|
|
2011-07-27 20:34:30 +02:00
|
|
|
class Message < ActiveRecord::Base
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-08-02 23:12:17 +02:00
|
|
|
belongs_to :user
|
2011-08-16 20:05:58 +02:00
|
|
|
belongs_to :folder
|
2011-08-02 23:12:17 +02:00
|
|
|
|
2011-08-26 22:59:43 +02:00
|
|
|
set_primary_key :uid
|
|
|
|
attr_accessible :unseen, :to_addr, :size, :content_type, :folder_id, :subject, :date, :uid, :from_addr, :user_id, :msg_id
|
|
|
|
|
|
|
|
def self.addr_to_db(addr)
|
|
|
|
ret = ""
|
|
|
|
name = addr.name
|
2011-08-28 21:52:17 +02:00
|
|
|
name.nil? ? ret : ret << ApplicationController.decode_quoted(name)
|
2011-08-26 22:59:43 +02:00
|
|
|
ret << "<" + addr.mailbox + "@" + addr.host
|
|
|
|
ret
|
|
|
|
end
|
2011-08-24 19:20:13 +02:00
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
def self.getPageForUser(user,folder,page,sort_field,sort_dir)
|
2011-08-02 23:12:17 +02:00
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
order = 'date desc'
|
|
|
|
if sort_field
|
|
|
|
if Message.attribute_method?(sort_field) == true
|
|
|
|
order = sort_field
|
|
|
|
sort_dir == 'desc' ? order += ' desc' : sort_dir
|
|
|
|
end
|
|
|
|
end
|
2011-08-02 23:12:17 +02:00
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
Message.paginate :page => page , :per_page => user.prefs.msgs_per_page.to_i, :conditions=> ['user_id = ? and folder_id = ?', user.id,folder.id],:order => order
|
|
|
|
end
|
|
|
|
|
2011-08-26 22:59:43 +02:00
|
|
|
def self.createForUser(user,folder,imap_message)
|
|
|
|
|
|
|
|
envelope = imap_message.attr['ENVELOPE']
|
|
|
|
|
|
|
|
from = addr_to_db(envelope.from[0])
|
|
|
|
to = addr_to_db(envelope.to[0])
|
|
|
|
|
2011-08-28 21:52:17 +02:00
|
|
|
envelope.subject.nil? ? subject = "" : subject = ApplicationController.decode_quoted(envelope.subject)
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
create(
|
|
|
|
:user_id => user.id,
|
|
|
|
:folder_id => folder.id,
|
2011-08-26 22:59:43 +02:00
|
|
|
:msg_id => envelope.message_id,
|
|
|
|
:uid => imap_message.attr['UID'].to_i,
|
|
|
|
:from_addr => from,
|
|
|
|
:to_addr => to,
|
|
|
|
:subject => subject,
|
|
|
|
:content_type => imap_message.attr['BODYSTRUCTURE'].media_type.downcase,
|
|
|
|
:date => envelope.date,
|
|
|
|
:unseen => !(imap_message.attr['FLAGS'].member? :Seen),
|
|
|
|
:size => imap_message.attr['RFC822.SIZE']
|
|
|
|
)
|
|
|
|
end
|
2011-08-16 20:05:58 +02:00
|
|
|
|
|
|
|
def change_folder(folder)
|
|
|
|
update_attributes(:folder_id => folder.id)
|
2011-08-02 23:12:17 +02:00
|
|
|
end
|
|
|
|
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-07-27 20:34:30 +02:00
|
|
|
end
|