8 attr_reader :containers
10 raise "wrong thread, buddy!" if block_given?
18 def empty?; @containers.empty?; end
21 raise "bad drop" unless @containers.member? c
26 puts "=== start thread #{self} with #{@containers.length} trees ==="
27 @containers.each { |c| c.dump_recursive }
28 puts "=== end thread ==="
31 ## yields each message and some stuff
32 def each fake_root=false
34 root = @containers.find_all { |c| !Message.subj_is_reply?(c) }.argmin { |c| c.date }
38 root.first_useful_descendant.each_with_stuff do |c, d, par|
39 yield c.message, d, (par ? par.message : nil)
41 elsif @containers.length > 1 && fake_root
43 yield :fake_root, 0, nil
46 @containers.each do |cont|
48 fud = cont.first_useful_descendant
49 fud.each_with_stuff do |c, d, par|
50 ## special case here: if we're an empty root that's already
51 ## been joined by a fake root, don't emit
52 yield c.message, d + adj, (par ? par.message : nil) unless
53 fake_root && c.message.nil? && root.nil? && c == fud
58 def dirty?; any? { |m, *o| m && m.dirty? }; end
59 def date; map { |m, *o| m.date if m }.compact.max; end
60 def snippet; argfind { |m, *o| m && m.snippet }; end
61 def authors; map { |m, *o| m.from if m }.compact.uniq; end
63 def apply_label t; each { |m, *o| m && m.add_label(t) }; end
65 each { |m, *o| m && m.remove_label(t) }
68 def toggle_label label
78 def set_labels l; each { |m, *o| m && m.labels = l }; end
80 def has_label? t; any? { |m, *o| m && m.has_label?(t) }; end
81 def save index; each { |m, *o| m && m.save(index) }; end
83 def direct_participants
84 map { |m, *o| [m.from] + m.to if m }.flatten.compact.uniq
88 map { |m, *o| [m.from] + m.to + m.cc + m.bcc if m }.flatten.compact.uniq
91 def size; map { |m, *o| m ? 1 : 0 }.sum; end
92 def subj; argfind { |m, *o| m && m.subj }; end
94 map { |m, *o| m && m.labels }.flatten.compact.uniq.sort_by { |t| t.to_s }
97 each { |m, *o| m && m.labels = l.clone }
101 inject(nil) do |a, b|
108 b.date > a.date ? b : a
114 "<thread containing: #{@containers.join ', '}>"
118 ## recursive structure used internally to represent message trees as
119 ## described by reply-to: and references: headers.
121 ## the 'id' field is the same as the message id. but the message might
122 ## be empty, in the case that we represent a message that was referenced
123 ## by another message (as an ancestor) but never received.
125 attr_accessor :message, :parent, :children, :id, :thread
128 raise "non-String #{id.inspect}" unless id.is_a? String
130 @message, @parent, @thread = nil, nil, nil
134 def each_with_stuff parent=nil
135 yield self, 0, parent
136 @children.each do |c|
137 c.each_with_stuff(self) { |cc, d, par| yield cc, d + 1, par }
145 @parent && @parent.descendant_of?(o)
149 def == o; Container === o && id == o.id; end
151 def empty?; @message.nil?; end
152 def root?; @parent.nil?; end
153 def root; root? ? self : @parent.root; end
155 def first_useful_descendant
156 if empty? && @children.size == 1
157 @children.first.first_useful_descendant
165 @children.argfind { |c| c.find_attr attr }
170 def subj; find_attr :subj; end
171 def date; find_attr :date; end
173 def is_reply?; subj && Message.subject_is_reply?(subj); end
177 (@parent.nil? ? nil : "parent=#{@parent.id}"),
178 (@children.empty? ? nil : "children=#{@children.map { |c| c.id }.inspect}"),
179 ].compact.join(" ") + ">"
182 def dump_recursive indent=0, root=true, parent=nil
183 raise "inconsistency" unless parent.nil? || parent.children.include?(self)
188 line = #"[#{useful? ? 'U' : ' '}] " +
190 "[#{thread}] #{@message.subj} " ##{@message.refs.inspect} / #{@message.replytos.inspect}"
195 puts "#{id} #{line}"#[0 .. (105 - indent)]
197 @children.each { |c| c.dump_recursive indent, false, self }
201 ## a set of threads (so a forest). builds the thread structures by
202 ## reading messages from an index.
204 attr_reader :num_messages
209 @messages = {} ## map from message ids to container objects
210 @subj_thread = {} ## map from subject strings to thread objects
213 def contains_id? id; @messages.member?(id) && !@messages[id].empty?; end
215 (c = @messages[m.id]) && c.root.thread
219 @subj_thread.each { |k, v| @subj_thread.delete(k) if v.empty? }
221 private :delete_empties
223 def threads; delete_empties; @subj_thread.values; end
224 def size; delete_empties; @subj_thread.size; end
227 @subj_thread.each do |s, t|
228 puts "**********************"
229 puts "** for subject #{s} **"
230 puts "**********************"
235 def link p, c, overwrite=false
236 if p == c || p.descendant_of?(c) || c.descendant_of?(p) # would create a loop
237 # puts "*** linking parent #{p} and child #{c} would create a loop"
241 if c.parent.nil? || overwrite
242 c.parent.children.delete c if overwrite && c.parent
254 return unless(c = @messages[mid])
256 c.parent.children.delete c if c.parent
263 ## load in (at most) num number of threads from the index
264 def load_n_threads num, opts={}
265 @index.each_id_by_date opts do |mid, builder|
267 next if contains_id? mid
271 load_thread_for_message m
272 yield @subj_thread.size if block_given?
276 ## loads in all messages needed to thread m
277 def load_thread_for_message m
278 @index.each_message_in_thread_for m, :limit => 100 do |mid, builder|
279 next if contains_id? mid
280 add_message builder.call
285 m.refs.any? { |ref_id| @messages[ref_id] }
288 ## an "online" version of the jwz threading algorithm.
289 def add_message message
291 el = (@messages[id] ||= Container.new id)
292 return if @messages[id].message # we've seen it before
297 ## link via references:
299 message.refs.each do |ref_id|
300 raise "non-String ref id #{ref_id.inspect} (full: #{message.refs.inspect})" unless ref_id.is_a?(String)
301 ref = (@messages[ref_id] ||= Container.new ref_id)
302 link prev, ref if prev
305 link prev, el, true if prev
307 ## link via in-reply-to:
308 message.replytos.each do |ref_id|
309 ref = (@messages[ref_id] ||= Container.new ref_id)
311 break # only do the first one
314 ## update subject grouping
316 # puts "> have #{el}, root #{root}, oldroot #{oldroot}"
321 # puts "*** root (#{root.subj}) == oldroot (#{oldroot.subj}); ignoring"
323 ## to disable subject grouping, use the next line instead
324 ## (and the same for below)
325 #Redwood::log "[1] normalized subject for #{id} is #{Message.normalize_subj(root.subj)}"
326 thread = (@subj_thread[Message.normalize_subj(root.subj)] ||= Thread.new)
327 #thread = (@subj_thread[root.id] ||= Thread.new)
331 # puts "# (1) added #{root} to #{thread}"
335 ## new root. need to drop old one and put this one in its place
336 # puts "*** DROPPING #{oldroot} from #{oldroot.thread}"
337 oldroot.thread.drop oldroot
342 # puts "*** IGNORING cuz root already has a thread"
344 ## to disable subject grouping, use the next line instead
345 ## (and the same above)
346 #Redwood::log "[2] normalized subject for #{id} is #{Message.normalize_subj(root.subj)}"
347 thread = (@subj_thread[Message.normalize_subj(root.subj)] ||= Thread.new)
348 #thread = (@subj_thread[root.id] ||= Thread.new)
352 # puts "# (2) added #{root} to #{thread}"