lib/sup/index.rb in sup-0.7 vs lib/sup/index.rb in sup-0.8

- old
+ new

@@ -175,35 +175,35 @@ @index = Ferret::Index::Index.new(:path => dir, :analyzer => @analyzer) end end end - ## Syncs the message to the index: deleting if it's already there, - ## and adding either way. Index state will be determined by m.labels. + ## Syncs the message to the index, replacing any previous version. adding + ## either way. Index state will be determined by the message's #labels + ## accessor. ## - ## docid and entry can be specified if they're already known. - def sync_message m, docid=nil, entry=nil, opts={} - docid, entry = load_entry_for_id m.id unless docid && entry + ## if need_load is false, docid and entry are assumed to be set to the + ## result of load_entry_for_id (which can be nil). + def sync_message m, need_load=true, docid=nil, entry=nil, opts={} + docid, entry = load_entry_for_id m.id if need_load raise "no source info for message #{m.id}" unless m.source && m.source_info @index_mutex.synchronize do raise "trying to delete non-corresponding entry #{docid} with index message-id #{@index[docid][:message_id].inspect} and parameter message id #{m.id.inspect}" if docid && @index[docid][:message_id] != m.id end - source_id = - if m.source.is_a? Integer - m.source - else - m.source.id or raise "unregistered source #{m.source} (id #{m.source.id.inspect})" - end + source_id = if m.source.is_a? Integer + m.source + else + m.source.id or raise "unregistered source #{m.source} (id #{m.source.id.inspect})" + end - snippet = - if m.snippet_contains_encrypted_content? && $config[:discard_snippets_from_encrypted_messages] - "" - else - m.snippet - end + snippet = if m.snippet_contains_encrypted_content? && $config[:discard_snippets_from_encrypted_messages] + "" + else + m.snippet + end ## write the new document to the index. if the entry already exists in the ## index, reuse it (which avoids having to reload the entry from the source, ## which can be quite expensive for e.g. large threads of IMAP actions.) ## @@ -224,11 +224,11 @@ ## if we are a later version of a message, ignore what's in the index, ## but merge in the labels. if entry[:source_id] && entry[:source_info] && entry[:label] && ((entry[:source_id].to_i > source_id) || (entry[:source_info].to_i < m.source_info)) - labels = (entry[:label].split(/\s+/).map { |l| l.intern } + m.labels).uniq + labels = (entry[:label].symbolistize + m.labels).uniq #Redwood::log "found updated version of message #{m.id}: #{m.subj}" #Redwood::log "previous version was at #{entry[:source_id].inspect}:#{entry[:source_info].inspect}, this version at #{source_id.inspect}:#{m.source_info.inspect}" #Redwood::log "merged labels are #{labels.inspect} (index #{entry[:label].inspect}, message #{m.labels.inspect})" entry = {} end @@ -244,25 +244,33 @@ :date => (entry[:date] || m.date.to_indexable_s), :body => (entry[:body] || m.indexable_content), :snippet => snippet, # always override :label => labels.uniq.join(" "), :attachments => (entry[:attachments] || m.attachments.uniq.join(" ")), - :from => (entry[:from] || (m.from ? m.from.indexable_content : "")), - :to => (entry[:to] || (m.to + m.cc + m.bcc).map { |x| x.indexable_content }.join(" ")), + + ## always override :from and :to. + ## older versions of Sup would often store the wrong thing in the index + ## (because they were canonicalizing email addresses, resulting in the + ## wrong name associated with each.) the correct address is read from + ## the original header when these messages are opened in thread-view-mode, + ## so this allows people to forcibly update the address in the index by + ## marking those threads for saving. + :from => (m.from ? m.from.indexable_content : ""), + :to => (m.to + m.cc + m.bcc).map { |x| x.indexable_content }.join(" "), + :subject => (entry[:subject] || wrap_subj(Message.normalize_subj(m.subj))), :refs => (entry[:refs] || (m.refs + m.replytos).uniq.join(" ")), } - @index_mutex.synchronize do + @index_mutex.synchronize do @index.delete docid if docid @index.add_document d end - docid, entry = load_entry_for_id m.id - ## this hasn't been triggered in a long time. TODO: decide whether it's still a problem. - raise "just added message #{m.id.inspect} but couldn't find it in a search" unless docid - true + ## this hasn't been triggered in a long time. + ## docid, entry = load_entry_for_id m.id + ## raise "just added message #{m.id.inspect} but couldn't find it in a search" unless docid end def save_index fn=File.join(@dir, "ferret") # don't have to do anything, apparently end @@ -322,11 +330,11 @@ date_min = m.date - (SAME_SUBJECT_DATE_LIMIT * 12 * 3600) date_max = m.date + (SAME_SUBJECT_DATE_LIMIT * 12 * 3600) q = Ferret::Search::BooleanQuery.new true sq = Ferret::Search::PhraseQuery.new(:subject) - wrap_subj(Message.normalize_subj(m.subj)).split(/\s+/).each do |t| + wrap_subj(Message.normalize_subj(m.subj)).split.each do |t| sq.add_term t end q.add_query sq, :must q.add_query Ferret::Search::RangeQuery.new(:date, :>= => date_min.to_indexable_s, :<= => date_max.to_indexable_s), :must @@ -367,22 +375,22 @@ end mid = @index[docid][:message_id] unless messages.member?(mid) #Redwood::log "got #{mid} as a child of #{id}" messages[mid] ||= lambda { build_message docid } - refs = @index[docid][:refs].split(" ") + refs = @index[docid][:refs].split pending += refs.select { |id| !searched[id] } end end end end if killed Redwood::log "thread for #{m.id} is killed, ignoring" false else - Redwood::log "ran #{num_queries} queries to build thread of #{messages.size + 1} messages for #{m.id}: #{m.subj}" if num_queries > 0 + Redwood::log "ran #{num_queries} queries to build thread of #{messages.size} messages for #{m.id}: #{m.subj}" if num_queries > 0 messages.each { |mid, builder| yield mid, builder } true end end @@ -398,18 +406,20 @@ fake_header = { "date" => Time.at(doc[:date].to_i), "subject" => unwrap_subj(doc[:subject]), "from" => doc[:from], - "to" => doc[:to].split(/\s+/).join(", "), # reformat + "to" => doc[:to].split.join(", "), # reformat "message-id" => doc[:message_id], - "references" => doc[:refs].split(/\s+/).map { |x| "<#{x}>" }.join(" "), + "references" => doc[:refs].split.map { |x| "<#{x}>" }.join(" "), } - Message.new :source => source, :source_info => doc[:source_info].to_i, - :labels => doc[:label].split(" ").map { |s| s.intern }, - :snippet => doc[:snippet], :header => fake_header + m = Message.new :source => source, :source_info => doc[:source_info].to_i, + :labels => doc[:label].symbolistize, + :snippet => doc[:snippet] + m.parse_header fake_header + m end end def fresh_thread_id; @next_thread_id += 1; end def wrap_subj subj; "__START_SUBJECT__ #{subj} __END_SUBJECT__"; end @@ -447,13 +457,13 @@ #Redwood::log "got message #{docid} to: #{@index[docid][:to].inspect} and from: #{@index[docid][:from].inspect}" f = @index[docid][:from] t = @index[docid][:to] if AccountManager.is_account_email? f - t.split(" ").each { |e| contacts[PersonManager.person_for(e)] = true } + t.split(" ").each { |e| contacts[Person.from_address(e)] = true } else - contacts[PersonManager.person_for(f)] = true + contacts[Person.from_address(f)] = true end end end contacts.keys.compact @@ -472,14 +482,31 @@ q.add_query Ferret::Search::TermQuery.new("source_id", source.id.to_s), :must q.add_query Ferret::Search::TermQuery.new("label", label.to_s), :must @index_mutex.synchronize { @index.search(q, :limit => 1).total_hits > 0 } end + ## takes a user query string and returns the list of docids for messages + ## that match the query. + ## + ## messages can then be loaded from the index with #build_message. + ## + ## raises a ParseError if the parsing failed. + def run_query query + qobj, opts = Redwood::Index.parse_user_query_string query + query = Redwood::Index.build_query opts.merge(:qobj => qobj) + results = @index.search query, :limit => (opts[:limit] || :all) + results.hits.map { |hit| hit.doc } + end + protected - ## do any specialized parsing - ## returns nil and flashes error message if parsing failed + class ParseError < StandardError; end + + ## parse a query string from the user. returns a query object and a set of + ## extra flags; both of these are meant to be passed to #build_query. + ## + ## raises a ParseError if something went wrong. def parse_user_query_string s extraopts = {} subs = s.gsub(/\b(to|from):(\S+)\b/) do field, name = $1, $2 @@ -537,15 +564,13 @@ "attachments:(*.#{name.downcase})" end end if $have_chronic - chronic_failure = false subs = subs.gsub(/\b(before|on|in|during|after):(\((.+?)\)\B|(\S+)\b)/) do - break if chronic_failure field, datestr = $1, ($3 || $4) - realdate = Chronic.parse(datestr, :guess => false, :context => :none) + realdate = Chronic.parse datestr, :guess => false, :context => :past if realdate case field when "after" Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate.end}" "date:(>= #{sprintf "%012d", realdate.end.to_i})" @@ -555,32 +580,29 @@ else Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate}" "date:(<= #{sprintf "%012d", realdate.end.to_i}) date:(>= #{sprintf "%012d", realdate.begin.to_i})" end else - BufferManager.flash "Can't understand date #{datestr.inspect}!" - chronic_failure = true + raise ParseError, "can't understand date #{datestr.inspect}" end end - subs = nil if chronic_failure end ## limit:42 restrict the search to 42 results subs = subs.gsub(/\blimit:(\S+)\b/) do lim = $1 if lim =~ /^\d+$/ extraopts[:limit] = lim.to_i '' else - BufferManager.flash "Can't understand limit #{lim.inspect}!" - subs = nil + raise ParseError, "non-numeric limit #{lim.inspect}" end end - if subs + begin [@qparser.parse(subs), extraopts] - else - nil + rescue Ferret::QueryParser::QueryParseException => e + raise ParseError, e.message end end def build_query opts query = Ferret::Search::BooleanQuery.new