X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Findex.rb;h=e403570a74b39774d7ceefa046722d3dae3c12bb;hb=8f2c6a2081d78732ac2e0cfe5c713cb580595c33;hp=8af4edd89e9a3c04799ac3d7a6774097d52d1630;hpb=7cf401d88e72af8f49f4cce8ba323214ce5542b2;p=sup diff --git a/lib/sup/index.rb b/lib/sup/index.rb index 8af4edd..e403570 100644 --- a/lib/sup/index.rb +++ b/lib/sup/index.rb @@ -177,31 +177,31 @@ EOS 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, @@ -226,7 +226,7 @@ EOS ## 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})" @@ -246,21 +246,29 @@ EOS :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") @@ -324,7 +332,7 @@ EOS 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 @@ -369,7 +377,7 @@ EOS 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 @@ -380,7 +388,7 @@ EOS 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 @@ -400,14 +408,16 @@ EOS "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 @@ -449,9 +459,9 @@ EOS 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 @@ -474,10 +484,27 @@ EOS @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 = {} @@ -539,11 +566,9 @@ protected 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" @@ -557,11 +582,9 @@ protected "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 @@ -571,15 +594,14 @@ protected 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