]> git.cworth.org Git - sup/blobdiff - lib/sup/index.rb
make label-list-mode non-modal
[sup] / lib / sup / index.rb
index 0da441afd3220be194164c805f11531441512f27..367829533581323e28b91ff59bec4449e37609af 100644 (file)
@@ -1,8 +1,14 @@
 ## the index structure for redwood. interacts with ferret.
 
-require 'thread'
 require 'fileutils'
 require 'ferret'
+begin
+  require 'chronic'
+  $have_chronic = true
+rescue LoadError => e
+  Redwood::log "'chronic' library not found. run 'gem install chronic' to install."
+  $have_chronic = false
+end
 
 module Redwood
 
@@ -18,6 +24,7 @@ class Index
   include Singleton
 
   attr_reader :index
+  alias ferret index
   def initialize dir=BASE_DIR
     @dir = dir
     @sources = {}
@@ -171,7 +178,7 @@ EOS
       :date => m.date.to_indexable_s,
       :body => m.content,
       :snippet => m.snippet,
-      :label => m.labels.join(" "),
+      :label => m.labels.uniq.join(" "),
       :from => m.from ? m.from.email : "",
       :to => (m.to + m.cc + m.bcc).map { |x| x.email }.join(" "),
       :subject => wrap_subj(Message.normalize_subj(m.subj)),
@@ -225,7 +232,9 @@ EOS
   ## message-building lambdas, so that building an unwanted message
   ## can be skipped in the block if desired.
   ##
-  ## stops loading any thread if a message with a :killed flag is found.
+  ## only two options, :limit and :skip_killed. if :skip_killed is
+  ## true, stops loading any thread if a message with a :killed flag
+  ## is found.
   SAME_SUBJECT_DATE_LIMIT = 7
   def each_message_in_thread_for m, opts={}
     #Redwood::log "Building thread for #{m.id}: #{m.subj}"
@@ -261,15 +270,13 @@ EOS
       q.add_query Ferret::Search::TermQuery.new(:message_id, id), :should
       q.add_query Ferret::Search::TermQuery.new(:refs, id), :should
 
-      ## load_killed is true so that we can abort if any message in
-      ## the thread has the killed label.
-      q = build_query :qobj => q, :load_killed => true
+      q = build_query :qobj => q
 
       num_queries += 1
       killed = false
       @index.search_each(q, :limit => :all) do |docid, score|
         break if opts[:limit] && messages.size >= opts[:limit]
-        if @index[docid][:label].split(/\s+/).include?("killed") && !opts[:load_killed]
+        if @index[docid][:label].split(/\s+/).include?("killed") && opts[:skip_killed]
           killed = true
           break
         end
@@ -305,7 +312,7 @@ EOS
       "from" => doc[:from],
       "to" => doc[:to],
       "message-id" => doc[:message_id],
-      "references" => doc[:refs],
+      "references" => doc[:refs].split(/\s+/).map { |x| "<#{x}>" }.join(" "),
     }
 
     Message.new :source => source, :source_info => doc[:source_info].to_i, 
@@ -370,8 +377,10 @@ EOS
 
 protected
 
+  ## do any specialized parsing
+  ## returns nil and flashes error message if parsing failed
   def parse_user_query_string str
-    str2 = str.gsub(/(to|from):(\S+)/) do
+    result = str.gsub(/\b(to|from):(\S+)\b/) do
       field, name = $1, $2
       if(p = ContactManager.contact_for(name))
         [field, p.email]
@@ -380,8 +389,34 @@ protected
       end.join(":")
     end
     
-    Redwood::log "translated #{str} to #{str2}" unless str2 == str
-    @qparser.parse str2
+    if $have_chronic
+      chronic_failure = false
+      result = result.gsub(/\b(before|on|in|after):(\((.+?)\)\B|(\S+)\b)/) do
+        break if chronic_failure
+        field, datestr = $1, ($3 || $4)
+        realdate = Chronic.parse(datestr, :guess => false, :context => :none)
+        if realdate
+          case field
+          when "after"
+            Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate.end}"
+            "date:(>= #{sprintf "%012d", realdate.end.to_i})"
+          when "before"
+            Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate.begin}"
+            "date:(<= #{sprintf "%012d", realdate.begin.to_i})"
+          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 "Don't understand date #{datestr.inspect}!"
+          chronic_failure = true
+        end
+      end
+      result = nil if chronic_failure
+    end
+    
+    Redwood::log "translated #{str.inspect} to #{result}" unless result == str
+    @qparser.parse result if result
   end
 
   def build_query opts
@@ -400,7 +435,7 @@ protected
         
     query.add_query Ferret::Search::TermQuery.new("label", "spam"), :must_not unless opts[:load_spam] || labels.include?(:spam)
     query.add_query Ferret::Search::TermQuery.new("label", "deleted"), :must_not unless opts[:load_deleted] || labels.include?(:deleted)
-    query.add_query Ferret::Search::TermQuery.new("label", "killed"), :must_not unless opts[:load_killed] || labels.include?(:killed)
+    query.add_query Ferret::Search::TermQuery.new("label", "killed"), :must_not if opts[:skip_killed]
     query
   end