]> git.notmuchmail.org Git - sup/blobdiff - lib/sup/index.rb
Merge branch 'dont-canonicalize-email-addresses' into next
[sup] / lib / sup / index.rb
index 6e3e2951310957f8fb098e35192bf003e35bb786..838d601347f1f86ffba21ac72e37c3e191ac95ba 100644 (file)
@@ -246,8 +246,17 @@ 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(" ")),
     }
@@ -282,13 +291,15 @@ EOS
     query = build_query opts
     offset = 0
     while true
-      results = @index_mutex.synchronize { @index.search query, :sort => "date DESC", :limit => EACH_BY_DATE_NUM, :offset => offset }
+      limit = (opts[:limit])? [EACH_BY_DATE_NUM, opts[:limit] - offset].min : EACH_BY_DATE_NUM
+      results = @index_mutex.synchronize { @index.search query, :sort => "date DESC", :limit => limit, :offset => offset }
       Redwood::log "got #{results.total_hits} results for query (offset #{offset}) #{query.inspect}"
       results.hits.each do |hit|
         yield @index_mutex.synchronize { @index[hit.doc][:message_id] }, lambda { build_message hit.doc }
       end
-      break if offset >= results.total_hits - EACH_BY_DATE_NUM
-      offset += EACH_BY_DATE_NUM
+      break if opts[:limit] and offset >= opts[:limit] - limit
+      break if offset >= results.total_hits - limit
+      offset += limit
     end
   end
 
@@ -386,23 +397,27 @@ EOS
 
   ## builds a message object from a ferret result
   def build_message docid
-    doc = @index_mutex.synchronize { @index[docid] }
-    source = @source_mutex.synchronize { @sources[doc[:source_id].to_i] }
-    #puts "building message #{doc[:message_id]} (#{source}##{doc[:source_info]})"
-    raise "invalid source #{doc[:source_id]}" unless source
-
-    fake_header = {
-      "date" => Time.at(doc[:date].to_i),
-      "subject" => unwrap_subj(doc[:subject]),
-      "from" => doc[:from],
-      "to" => doc[:to].split(/\s+/).join(", "), # reformat
-      "message-id" => doc[:message_id],
-      "references" => doc[:refs].split(/\s+/).map { |x| "<#{x}>" }.join(" "),
-    }
+    @index_mutex.synchronize do
+      doc = @index[docid]
+
+      source = @source_mutex.synchronize { @sources[doc[:source_id].to_i] }
+      raise "invalid source #{doc[:source_id]}" unless source
+
+      #puts "building message #{doc[:message_id]} (#{source}##{doc[:source_info]})"
+
+      fake_header = {
+        "date" => Time.at(doc[:date].to_i),
+        "subject" => unwrap_subj(doc[:subject]),
+        "from" => doc[:from],
+        "to" => doc[:to].split(/\s+/).join(", "), # reformat
+        "message-id" => doc[:message_id],
+        "references" => doc[:refs].split(/\s+/).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
+      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
+    end
   end
 
   def fresh_thread_id; @next_thread_id += 1; end
@@ -412,10 +427,14 @@ EOS
   def drop_entry docno; @index_mutex.synchronize { @index.delete docno } end
 
   def load_entry_for_id mid
-    results = @index_mutex.synchronize { @index.search Ferret::Search::TermQuery.new(:message_id, mid) }
-    return if results.total_hits == 0
-    docid = results.hits[0].doc
-    [docid, @index_mutex.synchronize { @index[docid] } ]
+    @index_mutex.synchronize do
+      results = @index.search Ferret::Search::TermQuery.new(:message_id, mid)
+      return if results.total_hits == 0
+      docid = results.hits[0].doc
+      entry = @index[docid]
+      entry_dup = entry.fields.inject({}) { |h, f| h[f] = entry[f]; h }
+      [docid, entry_dup]
+    end
   end
 
   def load_contacts emails, h={}
@@ -439,9 +458,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
@@ -461,7 +480,7 @@ EOS
     q = Ferret::Search::BooleanQuery.new
     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 }
+    @index_mutex.synchronize { @index.search(q, :limit => 1).total_hits > 0 }
   end
 
 protected
@@ -553,6 +572,18 @@ protected
       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
+      end
+    end
     
     if subs
       [@qparser.parse(subs), extraopts]