我正在使用 Ruby 和 Rails 开发 IMAP 客户端。我可以成功导入邮件、邮箱等... 但是,在初始导入之后,如何检测自上次同步以来发生的任何更改?
目前,我将 UID 和 UID 有效性值存储在数据库中,比较它们并进行适当的搜索。这可行,但它不会检测到已删除的消息或对消息标志的更改等。
我是否必须每次都提取所有消息才能检测到这些更改?其他 IMAP 客户端是如何做到这么快的(即 Apple Mail 和 Postbox)。我的脚本每个帐户已经花费了 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!