def do_poll
total_num = total_numi = 0
from_and_subj = []
from_and_subj_inbox = []
loaded_labels = Set.new
@mutex.synchronize do
@poll_sources.each do |source|
begin
yield "Loading from #{source}... " unless source.done? || (source.respond_to?(:has_errors?) && source.has_errors?)
rescue SourceError => e
warn "problem getting messages from #{source}: #{e.message}"
Redwood::report_broken_sources :force_to_top => true
next
end
num = 0
numi = 0
each_message_from source do |m|
old_m = Index.build_message m.id
if old_m
if old_m.source.id != source.id || old_m.source_info != m.source_info
new_labels = old_m.labels + (m.labels - [:unread, :inbox])
yield "Message at #{m.source_info} is an updated of an old message. Updating labels from #{m.labels.to_a * ','} => #{new_labels.to_a * ','}"
m.labels = new_labels
Index.update_message m
else
yield "Skipping already-imported message at #{m.source_info}"
end
else
yield "Found new message at #{m.source_info} with labels #{m.labels.to_a * ','}"
add_new_message m
loaded_labels.merge m.labels
num += 1
from_and_subj << [m.from && m.from.longname, m.subj]
if (m.labels & [:inbox, :spam, :deleted, :killed]) == Set.new([:inbox])
from_and_subj_inbox << [m.from && m.from.longname, m.subj]
numi += 1
end
end
m
end
yield "Found #{num} messages, #{numi} to inbox." unless num == 0
total_num += num
total_numi += numi
end
loaded_labels = loaded_labels - LabelManager::HIDDEN_RESERVED_LABELS - [:inbox, :killed]
yield "Done polling; loaded #{total_num} new messages total"
@last_poll = Time.now
@polling = false
end
[total_num, total_numi, from_and_subj, from_and_subj_inbox, loaded_labels]
end