]> git.cworth.org Git - sup/blob - lib/sup/thread.rb
better comments
[sup] / lib / sup / thread.rb
1 ## Herein is all the code responsible for threading messages. I use an
2 ## online version of the JWZ threading algorithm:
3 ## http://www.jwz.org/doc/threading.html
4 ##
5 ## I certainly didn't implement it for efficiency, but thanks to our
6 ## search engine backend, it's typically not applied to very many
7 ## messages at once.
8
9 ## At the top level, we have a ThreadSet. A ThreadSet represents a set
10 ## of threads, e.g. a message folder or an inbox. Each ThreadSet
11 ## contains zero or more Threads. A Thread represents all the message
12 ## related to a particular subject. Each Thread has one or more
13 ## Containers. A Container is a recursive structure that holds the
14 ## tree structure as determined by the references: and in-reply-to:
15 ## headers. A Thread with multiple Containers occurs if they have the
16 ## same subject, but (most likely due to someone using a primitive
17 ## MUA) we don't have evidence from in-reply-to: or references:
18 ## headers, only subject: (and thus our tree is probably broken). A
19 ## Container holds zero or one message. In the case of no message, it
20 ## means we've seen a reference to the message but haven't seen the
21 ## message itself (yet).
22
23 module Redwood
24
25 class Thread
26   include Enumerable
27
28   attr_reader :containers
29   def initialize
30     ## ah, the joys of a multithreaded application with a class called
31     ## "Thread". i keep instantiating the wrong one...
32     raise "wrong Thread class, buddy!" if block_given?
33     @containers = []
34   end
35
36   def << c
37     @containers << c
38   end
39
40   def empty?; @containers.empty?; end
41
42   def drop c
43     raise "bad drop" unless @containers.member? c
44     @containers.delete c
45   end
46
47   def dump
48     puts "=== start thread #{self} with #{@containers.length} trees ==="
49     @containers.each { |c| c.dump_recursive }
50     puts "=== end thread ==="
51   end
52
53   ## yields each message, its depth, and its parent.  note that the
54   ## message can be a Message object, or :fake_root, or nil.
55   def each fake_root=false
56     adj = 0
57     root = @containers.find_all { |c| !Message.subj_is_reply?(c) }.argmin { |c| c.date }
58
59     if root
60       adj = 1
61       root.first_useful_descendant.each_with_stuff do |c, d, par|
62         yield c.message, d, (par ? par.message : nil)
63       end
64     elsif @containers.length > 1 && fake_root
65       adj = 1
66       yield :fake_root, 0, nil
67     end
68
69     @containers.each do |cont|
70       next if cont == root
71       fud = cont.first_useful_descendant
72       fud.each_with_stuff do |c, d, par|
73         ## special case here: if we're an empty root that's already
74         ## been joined by a fake root, don't emit
75         yield c.message, d + adj, (par ? par.message : nil) unless
76           fake_root && c.message.nil? && root.nil? && c == fud 
77       end
78     end
79   end
80
81   def first; each { |m, *o| return m if m }; nil; end
82   def dirty?; any? { |m, *o| m && m.dirty? }; end
83   def date; map { |m, *o| m.date if m }.compact.max; end
84   def snippet; argfind { |m, *o| m && m.snippet }; end
85   def authors; map { |m, *o| m.from if m }.compact.uniq; end
86
87   def apply_label t; each { |m, *o| m && m.add_label(t) }; end
88   def remove_label t
89     each { |m, *o| m && m.remove_label(t) }
90   end
91
92   def toggle_label label
93     if has_label? label
94       remove_label label
95       return false
96     else
97       apply_label label
98       return true
99     end
100   end
101
102   def set_labels l; each { |m, *o| m && m.labels = l }; end
103   
104   def has_label? t; any? { |m, *o| m && m.has_label?(t) }; end
105   def dirty?; any? { |m, *o| m && m.dirty? }; end
106   def save index; each { |m, *o| m && m.save(index) }; end
107
108   def direct_participants
109     map { |m, *o| [m.from] + m.to if m }.flatten.compact.uniq
110   end
111
112   def participants
113     map { |m, *o| [m.from] + m.to + m.cc + m.bcc if m }.flatten.compact.uniq
114   end
115
116   def size; map { |m, *o| m ? 1 : 0 }.sum; end
117   def subj; argfind { |m, *o| m && m.subj }; end
118   def labels
119       map { |m, *o| m && m.labels }.flatten.compact.uniq.sort_by { |t| t.to_s }
120   end
121   def labels= l
122     each { |m, *o| m && m.labels = l.clone }
123   end
124
125   def latest_message
126     inject(nil) do |a, b| 
127       b = b.first
128       if a.nil?
129         b
130       elsif b.nil?
131         a
132       else
133         b.date > a.date ? b : a
134       end
135     end
136   end
137
138   def to_s
139     "<thread containing: #{@containers.join ', '}>"
140   end
141 end
142
143 ## recursive structure used internally to represent message trees as
144 ## described by reply-to: and references: headers.
145 ##
146 ## the 'id' field is the same as the message id. but the message might
147 ## be empty, in the case that we represent a message that was referenced
148 ## by another message (as an ancestor) but never received.
149 class Container
150   attr_accessor :message, :parent, :children, :id, :thread
151
152   def initialize id
153     raise "non-String #{id.inspect}" unless id.is_a? String
154     @id = id
155     @message, @parent, @thread = nil, nil, nil
156     @children = []
157   end      
158
159   def each_with_stuff parent=nil
160     yield self, 0, parent
161     @children.each do |c|
162       c.each_with_stuff(self) { |cc, d, par| yield cc, d + 1, par }
163     end
164   end
165
166   def descendant_of? o
167     if o == self
168       true
169     else
170       @parent && @parent.descendant_of?(o)
171     end
172   end
173
174   def == o; Container === o && id == o.id; end
175
176   def empty?; @message.nil?; end
177   def root?; @parent.nil?; end
178   def root; root? ? self : @parent.root; end
179
180   def first_useful_descendant
181     if empty? && @children.size == 1
182       @children.first.first_useful_descendant
183     else
184       self
185     end
186   end
187
188   def find_attr attr
189     if empty?
190       @children.argfind { |c| c.find_attr attr }
191     else
192       @message.send attr
193     end
194   end
195   def subj; find_attr :subj; end
196   def date; find_attr :date; end
197
198   def is_reply?; subj && Message.subject_is_reply?(subj); end
199
200   def to_s
201     [ "<#{id}",
202       (@parent.nil? ? nil : "parent=#{@parent.id}"),
203       (@children.empty? ? nil : "children=#{@children.map { |c| c.id }.inspect}"),
204     ].compact.join(" ") + ">"
205   end
206
207   def dump_recursive indent=0, root=true, parent=nil
208     raise "inconsistency" unless parent.nil? || parent.children.include?(self)
209     unless root
210       print " " * indent
211       print "+->"
212     end
213     line = #"[#{useful? ? 'U' : ' '}] " +
214       if @message
215         "[#{thread}] #{@message.subj} " ##{@message.refs.inspect} / #{@message.replytos.inspect}"
216       else
217         "<no message>"
218       end
219
220     puts "#{id} #{line}"#[0 .. (105 - indent)]
221     indent += 3
222     @children.each { |c| c.dump_recursive indent, false, self }
223   end
224 end
225
226 ## a set of threads (so a forest). builds the thread structures by
227 ## reading messages from an index.
228 class ThreadSet
229   attr_reader :num_messages
230
231   def initialize index
232     @index = index
233     @num_messages = 0
234     @messages = {} ## map from message ids to container objects
235     @subj_thread = {} ## map from subject strings to thread objects
236   end
237
238   def contains_id? id; @messages.member?(id) && !@messages[id].empty?; end
239   def thread_for m
240     (c = @messages[m.id]) && c.root.thread
241   end
242
243   def delete_cruft
244     @subj_thread.each { |k, v| @subj_thread.delete(k) if v.empty? || v.subj != k }
245   end
246   private :delete_cruft
247
248   def threads; delete_cruft; @subj_thread.values; end
249   def size; delete_cruft; @subj_thread.size; end
250
251   def dump
252     @subj_thread.each do |s, t|
253       puts "**********************"
254       puts "** for subject #{s} **"
255       puts "**********************"
256       t.dump
257     end
258   end
259
260   def link p, c, overwrite=false
261     if p == c || p.descendant_of?(c) || c.descendant_of?(p) # would create a loop
262 #      puts "*** linking parent #{p} and child #{c} would create a loop"
263       return
264     end
265
266     if c.parent.nil? || overwrite
267       c.parent.children.delete c if overwrite && c.parent
268       if c.thread
269         c.thread.drop c 
270         c.thread = nil
271       end
272       p.children << c
273       c.parent = p
274     end
275   end
276   private :link
277
278   def remove mid
279     return unless(c = @messages[mid])
280
281     c.parent.children.delete c if c.parent
282     if c.thread
283       c.thread.drop c
284       c.thread = nil
285     end
286   end
287
288   ## load in (at most) num number of threads from the index
289   def load_n_threads num, opts={}
290     @index.each_id_by_date opts do |mid, builder|
291       break if size >= num
292       next if contains_id? mid
293
294       m = builder.call
295       add_message m
296       load_thread_for_message m
297       yield @subj_thread.size if block_given?
298     end
299   end
300
301   ## loads in all messages needed to thread m
302   def load_thread_for_message m
303     @index.each_message_in_thread_for m, :limit => 100 do |mid, builder|
304       next if contains_id? mid
305       add_message builder.call
306     end
307   end
308
309   def is_relevant? m
310     m.refs.any? { |ref_id| @messages[ref_id] }
311   end
312
313   ## an "online" version of the jwz threading algorithm.
314   def add_message message
315     id = message.id
316     el = (@messages[id] ||= Container.new id)
317     return if @messages[id].message # we've seen it before
318
319     el.message = message
320     oldroot = el.root
321
322     ## link via references:
323     prev = nil
324     message.refs.each do |ref_id|
325       raise "non-String ref id #{ref_id.inspect} (full: #{message.refs.inspect})" unless ref_id.is_a?(String)
326       ref = (@messages[ref_id] ||= Container.new ref_id)
327       link prev, ref if prev
328       prev = ref
329     end
330     link prev, el, true if prev
331
332     ## link via in-reply-to:
333     message.replytos.each do |ref_id|
334       ref = (@messages[ref_id] ||= Container.new ref_id)
335       link ref, el, true
336       break # only do the first one
337     end
338
339     ## update subject grouping
340     root = el.root
341     #    puts "> have #{el}, root #{root}, oldroot #{oldroot}"
342     #    el.dump_recursive
343
344     if root == oldroot
345       if oldroot.thread
346         ## check to see if the subject is still the same (in the case
347         ## that we first added a child message with a different
348         ## subject)
349
350         ## this code is duplicated below. sorry! TODO: refactor
351         s = Message.normalize_subj(root.subj)
352         unless @subj_thread[s] == root.thread
353           ## Redwood::log "[1] moving thread to new subject #{root.subj}"
354           if @subj_thread[s]
355             @subj_thread[s] << root
356             root.thread = @subj_thread[s]
357           else
358             @subj_thread[s] = root.thread
359           end
360         end
361
362       else
363         ## to disable subject grouping, use the next line instead
364         ## (and the same for below)
365         #Redwood::log "[1] for #{root}, subject #{Message.normalize_subj(root.subj)} has #{@subj_thread[Message.normalize_subj(root.subj)] ? 'a' : 'no'} thread"
366         thread = (@subj_thread[Message.normalize_subj(root.subj)] ||= Thread.new)
367         #thread = (@subj_thread[root.id] ||= Thread.new)
368
369         thread << root
370         root.thread = thread
371         # Redwood::log "[1] added #{root} to #{thread}"
372       end
373     else
374       if oldroot.thread
375         ## new root. need to drop old one and put this one in its place
376         oldroot.thread.drop oldroot
377         oldroot.thread = nil
378       end
379
380       if root.thread
381         ## check to see if the subject is still the same (in the case
382         ## that we first added a child message with a different
383         ## subject)
384         s = Message.normalize_subj(root.subj)
385         unless @subj_thread[s] == root.thread
386           # Redwood::log "[2] moving thread to new subject #{root.subj}"
387           if @subj_thread[s]
388             @subj_thread[s] << root
389             root.thread = @subj_thread[s]
390           else
391             @subj_thread[s] = root.thread
392           end
393         end
394
395       else
396         ## to disable subject grouping, use the next line instead
397         ## (and the same above)
398         
399         ## this code is duplicated above. sorry! TODO: refactor
400         # Redwood::log "[2] for #{root}, subject '#{Message.normalize_subj(root.subj)}' has #{@subj_thread[Message.normalize_subj(root.subj)] ? 'a' : 'no'} thread"
401
402         thread = (@subj_thread[Message.normalize_subj(root.subj)] ||= Thread.new)
403         #thread = (@subj_thread[root.id] ||= Thread.new)
404
405         thread << root
406         root.thread = thread
407         # Redwood::log "[2] added #{root} to #{thread}"
408       end
409     end
410
411     ## last bit
412     @num_messages += 1
413   end
414 end
415
416 end