]> git.cworth.org Git - sup/blobdiff - lib/sup/thread.rb
Merge branches 'prev-next-improv', 'fix-warnings', 'mime-view', 'charset', 'join...
[sup] / lib / sup / thread.rb
index f54c935339c84841d22d59edb98fca7bd9da1457..32002c4062cb0e9d7aabf317375cf22eafdeee65 100644 (file)
@@ -207,7 +207,7 @@ class Container
   def subj; find_attr :subj; end
   def date; find_attr :date; end
 
-  def is_reply?; subj && Message.subject_is_reply?(subj); end
+  def is_reply?; subj && Message.subj_is_reply?(subj); end
 
   def to_s
     [ "<#{id}",
@@ -289,7 +289,10 @@ class ThreadSet
     remove_container c
     p.children << c
     c.parent = p
-    update_threading_for c
+
+    ## if the child was previously a top-level container, it now ain't,
+    ## so ditch our thread and kill it if necessary
+    prune_thread_of c
   end
   private :link
 
@@ -298,20 +301,18 @@ class ThreadSet
   end
   private :remove_container
 
-  def update_threading_for c
-    ## if the child was previously a top-level container, but now is not,
-    ## ditch our thread and kill it if necessary
-    if c.thread && !c.root?
-      c.thread.drop c
-      @threads.delete_if { |k, v| v == c.thread } if c.thread.empty?
-      c.thread = nil
-    end
+  def prune_thread_of c
+    return unless c.thread
+    c.thread.drop c
+    @threads.delete_if { |k, v| v == c.thread } if c.thread.empty?
+    c.thread = nil
   end
-  private :update_threading_for
+  private :prune_thread_of
 
   def remove_id mid
     return unless(c = @messages[mid])
     remove_container c
+    prune_thread_of c
   end
 
   def remove_thread_containing_id mid
@@ -348,6 +349,32 @@ class ThreadSet
     t.each { |m, *o| add_message m }
   end
 
+  ## merges two threads together. both must be members of this threadset.
+  ## does its best, heuristically, to determine which is the parent.
+  def join_threads threads
+    return if threads.size < 2
+
+    containers = threads.map do |t|
+      c = @messages[t.first.id]
+      raise "not in threadset: #{t.first.id}" unless c && c.message
+      c
+    end
+
+    ## use subject headers heuristically
+    parent = containers.find { |c| !c.is_reply? }
+
+    ## no thread was rooted by a non-reply, so make a fake parent
+    parent ||= @messages["joining-ref-" + containers.map { |c| c.id }.join("-")]
+
+    containers.each do |c|
+      next if c == parent
+      c.message.add_ref parent.id
+      link parent, c
+    end
+
+    true
+  end
+
   def is_relevant? m
     m.refs.any? { |ref_id| @messages.member? ref_id }
   end