Method: Redwood::ThreadSet#add_message

Defined in:
lib/sup/thread.rb

#add_message(message) ⇒ Object

the heart of the threading code



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/sup/thread.rb', line 339

def add_message message
  el = @messages[message.id]
  return if el.message # we've seen it before

  el.message = message
  oldroot = el.root

  ## link via references:
  prev = nil
  message.refs.each do |ref_id|
    ref = @messages[ref_id]
    link prev, ref if prev
    prev = ref
  end
  link prev, el, true if prev

  ## link via in-reply-to:
  message.replytos.each do |ref_id|
    ref = @messages[ref_id]
    link ref, el, true
    break # only do the first one
  end

  root = el.root

  ## new root. need to drop old one and put this one in its place
  if root != oldroot && oldroot.thread
    oldroot.thread.drop oldroot
    oldroot.thread = nil
  end

  key =
    if thread_by_subj?
      Message.normalize_subj root.subj
    else
      root.id
    end

  ## check to see if the subject is still the same (in the case
  ## that we first added a child message with a different
  ## subject)
  if root.thread
    unless @threads[key] == root.thread
      if @threads[key]
        root.thread.empty!
        @threads[key] << root
        root.thread = @threads[key]
      else
        @threads[key] = root.thread
      end
    end
  else
    thread = @threads[key]
    thread << root
    root.thread = thread
  end

  ## last bit
  @num_messages += 1
end