a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
a20af62000-09-26Fredrik Hübinette (Hubbe) 
1db8651999-10-05Johan Schön import "."; class Thread { object root; multiset unread_numbers=(<>);
ea37871999-10-10Johan Schön  mapping(int:object) textno_to_node;
1db8651999-10-05Johan Schön  mapping(int:object) unread_texts;
3524712015-05-26Martin Nilsson  int max_follow;
1db8651999-10-05Johan Schön  class Node { Session.Text text;
ea37871999-10-10Johan Schön  int conf_no;
1db8651999-10-05Johan Schön  Node parent; int unread;
3524712015-05-26Martin Nilsson 
1db8651999-10-05Johan Schön  array(Node) children = ({ });
ee4d382000-04-10Johan Schön  string get_aux_item_author(object text) { string name, email; if(sizeof(text->aux_items["mx-author"])) name=text->aux_items["mx-author"][0]->data; if(sizeof(text->aux_items["mx-from"])) email=text->aux_items["mx-from"][0]->data; if(name && email) return sprintf("%s <%s>",name,email); if(name) return name; if(email) return email; return 0; }
ea37871999-10-10Johan Schön  array(mapping) flatten(int depth)
5c3ad31999-10-05Johan Schön  {
eb48fb1999-10-17Johan Schön  string author_name; catch { author_name=text->author->name; }; if(!author_name) author_name="Deleted person";
ea37871999-10-10Johan Schön  return ({ ([ "no": (string)text->no,
eb48fb1999-10-17Johan Schön  "author_no": (string)text->author->no,
ee4d382000-04-10Johan Schön  "author_name":get_aux_item_author(text)||author_name,
ea37871999-10-10Johan Schön  "subject": text->subject, "unread": (unread?"un":""), "depth": (string)depth ]), children->flatten(depth+1) });
5c3ad31999-10-05Johan Schön  }
3524712015-05-26Martin Nilsson 
5c3ad31999-10-05Johan Schön 
1db8651999-10-05Johan Schön  Node possible_parent(int follow) { foreach(text->misc->comm_to, Session.Text _parent) {
67f4791999-10-11Johan Schön  if(catch(_parent->misc))
5c3ad31999-10-05Johan Schön  continue;
1db8651999-10-05Johan Schön  foreach( ({ @_parent->misc->recpt->conf, @_parent->misc->ccrecpt->conf, @_parent->misc->bccrecpt->conf }), Session.Conference rcpt_conf)
ea37871999-10-10Johan Schön  if(rcpt_conf->no == conf_no)
1db8651999-10-05Johan Schön  if(textno_to_node[_parent->no]) return textno_to_node[_parent->no]; else if(follow || unread_numbers[_parent->no])
ea37871999-10-10Johan Schön  return Node(_parent, conf_no, follow); return 0;
1db8651999-10-05Johan Schön  } }
3524712015-05-26Martin Nilsson 
1db8651999-10-05Johan Schön  array(Node) possible_children(int follow) { foreach(text->misc->comm_in, Session.Text child) {
67f4791999-10-11Johan Schön  if(catch(child->misc))
5c3ad31999-10-05Johan Schön  continue;
1db8651999-10-05Johan Schön  foreach( ({ @child->misc->recpt->conf, @child->misc->ccrecpt->conf, @child->misc->bccrecpt->conf }), Session.Conference rcpt_conf)
ea37871999-10-10Johan Schön  if(rcpt_conf->no == conf_no)
1db8651999-10-05Johan Schön  if(textno_to_node[child->no]) children += ({ textno_to_node[child->no] }); else if(follow || unread_numbers[child->no])
ea37871999-10-10Johan Schön  children += ({ Node(child, conf_no, follow) });
1db8651999-10-05Johan Schön  } return children; }
3524712015-05-26Martin Nilsson 
ea37871999-10-10Johan Schön  void create(Session.Text _text, int _conf_no, int follow)
1db8651999-10-05Johan Schön  { text=_text;
ea37871999-10-10Johan Schön  conf_no=_conf_no;
3524712015-05-26Martin Nilsson 
563bd72004-01-11Martin Nilsson  textno_to_node[text->no]=this;
dbb0a11999-10-18Johan Schön  _text->clear_stat;
1db8651999-10-05Johan Schön  unread=unread_numbers[text->no]; if(!unread) follow--; else { m_delete(unread_texts, text->no); follow=max_follow; }
3524712015-05-26Martin Nilsson 
1db8651999-10-05Johan Schön  parent=possible_parent(follow); children=possible_children(follow);
67f4791999-10-11Johan Schön  // werror("Parent to %d: %d\n",_text->no, parent && parent->text->no); // werror("Children to %d: %s\n",_text->no, ((array(string))(children->text->no))*", ");
1db8651999-10-05Johan Schön  } }
ea37871999-10-10Johan Schön  void create(mapping(int:Session.Text) _unread_texts, int conf_no, Session.Text start_from, int _max_follow, mapping _textno_to_node)
1db8651999-10-05Johan Schön  { unread_texts=_unread_texts;
ea37871999-10-10Johan Schön  textno_to_node=_textno_to_node;
1db8651999-10-05Johan Schön  unread_numbers=(< @indices(unread_texts) >); max_follow=_max_follow;
3524712015-05-26Martin Nilsson 
ea37871999-10-10Johan Schön  Node start_node=Node(start_from, conf_no, max_follow);
1db8651999-10-05Johan Schön  Node temp; temp=start_node; do { root=temp; } while(temp=temp->parent);
ea37871999-10-10Johan Schön  unread_numbers=0;
1db8651999-10-05Johan Schön  } }
ea37871999-10-10Johan Schön array(Thread) children; object parent=0;
1db8651999-10-05Johan Schön 
ea37871999-10-10Johan Schön void create(array(Session.Text) unread_texts, int conf_no, int max_follow, mapping(int:object) textno_to_node)
1db8651999-10-05Johan Schön {
ea37871999-10-10Johan Schön  children=({ }); mapping m_unread_texts=mkmapping(unread_texts->no, unread_texts);
5c3ad31999-10-05Johan Schön  m_delete(m_unread_texts,0);
3524712015-05-26Martin Nilsson 
5c3ad31999-10-05Johan Schön  foreach(unread_texts->no-({0}), int no) { if(!(m_unread_texts[no]->misc)) m_delete(m_unread_texts,no); }
3524712015-05-26Martin Nilsson 
1db8651999-10-05Johan Schön  foreach(unread_texts->no, array(int) unread_texts_no) { if(!m_unread_texts[unread_texts_no]) continue;
ea37871999-10-10Johan Schön  object t=Thread(m_unread_texts, conf_no, m_unread_texts[unread_texts_no], max_follow, textno_to_node); children += ({ t->root });
1db8651999-10-05Johan Schön  }
ea37871999-10-10Johan Schön  foreach(children, object thread)
563bd72004-01-11Martin Nilsson  thread->parent=this;
1db8651999-10-05Johan Schön }