我正在使用Ruby和Rails的IMAP客户端上。 我可以成功地导入邮件,邮箱和更多...然而,最初的导入后,我怎么能发现自从我上次同步发生的任何变化?
目前我储存的UID和UID有效性的值在数据库中,比较它们,并适当地进行搜索。 这工作,但它不检测已删除的邮件或更改信息标志等。
我必须每天来检测这些变化的时间把所有的消息? 如何做其他的IMAP客户端这么快做(即Apple Mail和邮箱)。 我的剧本已经采取每个账户10+秒的极少数的电子邮件地址:
# select ourself as the current mailbox
@imap_connection.examine(self.location)
# grab all new messages and update them in the database
# if the uid's are still valid, we will just fetch the newest UIDs
# otherwise, we need to search when we last synced, which is slower :(
if self.uid_validity.nil? || uid_validity == self.uid_validity
# for some IMAP servers, if a mailbox is empty, a uid_fetch will fail, so then
begin
messages = @imap_connection.uid_fetch(uid_range, ['UID', 'RFC822', 'FLAGS'])
rescue
# gmail cries if the folder is empty
uids = @imap_connection.uid_search(['ALL'])
messages = @imap_connection.uid_fetch(uids, ['UID', 'RFC822', 'FLAGS']) unless uids.empty?
end
messages.each do |imap_message|
Message.create_from_imap!(imap_message, self.id)
end unless messages.nil?
else
query = self.last_synced.nil? ? ['All'] : ['SINCE', Net::IMAP.format_datetime(self.last_synced)]
@imap_connection.search(query).each do |message_id|
imap_message = @imap_connection.fetch(message_id, ['RFC822', 'FLAGS', 'UID'])[0]
# don't mark the messages as read
#@imap_connection.store(message_id, '-FLAGS', [:Seen])
Message.create_from_imap!(imap_message, self.id)
end
end
# now assume all UIDs are valid
self.uid_validity = uid_validity
# now remember that we just fetched all those messages
self.last_synced = Time.now
self.save!