threadednoticelist.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * widget for displaying a list of notices
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category UI
  23. * @package StatusNet
  24. * @author Brion Vibber <brion@status.net>
  25. * @copyright 2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * widget for displaying a list of notices
  32. *
  33. * There are a number of actions that display a list of notices, in
  34. * reverse chronological order. This widget abstracts out most of the
  35. * code for UI for notice lists. It's overridden to hide some
  36. * data for e.g. the profile page.
  37. *
  38. * @category UI
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  42. * @link http://status.net/
  43. * @see Notice
  44. * @see NoticeListItem
  45. * @see ProfileNoticeList
  46. */
  47. class ThreadedNoticeList extends NoticeList
  48. {
  49. protected $userProfile;
  50. function __construct(Notice $notice, Action $out=null, $profile=-1)
  51. {
  52. parent::__construct($notice, $out);
  53. if (is_int($profile) && $profile == -1) {
  54. $profile = Profile::current();
  55. }
  56. $this->userProfile = $profile;
  57. }
  58. /**
  59. * show the list of notices
  60. *
  61. * "Uses up" the stream by looping through it. So, probably can't
  62. * be called twice on the same list.
  63. *
  64. * @return int count of notices listed.
  65. */
  66. function show()
  67. {
  68. $this->out->elementStart('div', array('id' =>'notices_primary'));
  69. // TRANS: Header for Notices section.
  70. $this->out->element('h2', null, _m('HEADER','Notices'));
  71. $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
  72. $notices = $this->notice->fetchAll();
  73. $total = count($notices);
  74. $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
  75. $allnotices = self::_allNotices($notices);
  76. self::prefill($allnotices);
  77. $conversations = array();
  78. foreach ($notices as $notice) {
  79. // Collapse repeats into their originals...
  80. if ($notice->repeat_of) {
  81. $orig = Notice::getKV('id', $notice->repeat_of);
  82. if ($orig instanceof Notice) {
  83. $notice = $orig;
  84. }
  85. }
  86. $convo = $notice->conversation;
  87. if (!empty($conversations[$convo])) {
  88. // Seen this convo already -- skip!
  89. continue;
  90. }
  91. $conversations[$convo] = true;
  92. // Get the convo's root notice
  93. $root = $notice->conversationRoot($this->userProfile);
  94. if ($root instanceof Notice) {
  95. $notice = $root;
  96. }
  97. try {
  98. $item = $this->newListItem($notice);
  99. $item->show();
  100. } catch (Exception $e) {
  101. // we log exceptions and continue
  102. common_log(LOG_ERR, $e->getMessage());
  103. continue;
  104. }
  105. }
  106. $this->out->elementEnd('ol');
  107. $this->out->elementEnd('div');
  108. return $total;
  109. }
  110. function _allNotices($notices)
  111. {
  112. $convId = array();
  113. foreach ($notices as $notice) {
  114. $convId[] = $notice->conversation;
  115. }
  116. $convId = array_unique($convId);
  117. $allMap = Notice::listGet('conversation', $convId);
  118. $allArray = array();
  119. foreach ($allMap as $convId => $convNotices) {
  120. $allArray = array_merge($allArray, $convNotices);
  121. }
  122. return $allArray;
  123. }
  124. /**
  125. * returns a new list item for the current notice
  126. *
  127. * Recipe (factory?) method; overridden by sub-classes to give
  128. * a different list item class.
  129. *
  130. * @param Notice $notice the current notice
  131. *
  132. * @return NoticeListItem a list item for displaying the notice
  133. */
  134. function newListItem(Notice $notice)
  135. {
  136. return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
  137. }
  138. }
  139. /**
  140. * widget for displaying a single notice
  141. *
  142. * This widget has the core smarts for showing a single notice: what to display,
  143. * where, and under which circumstances. Its key method is show(); this is a recipe
  144. * that calls all the other show*() methods to build up a single notice. The
  145. * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
  146. * author info (since that's implicit by the data in the page).
  147. *
  148. * @category UI
  149. * @package StatusNet
  150. * @author Evan Prodromou <evan@status.net>
  151. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  152. * @link http://status.net/
  153. * @see NoticeList
  154. * @see ProfileNoticeListItem
  155. */
  156. class ThreadedNoticeListItem extends NoticeListItem
  157. {
  158. protected $userProfile = null;
  159. function __construct(Notice $notice, Action $out=null, $profile=null)
  160. {
  161. parent::__construct($notice, $out);
  162. $this->userProfile = $profile;
  163. }
  164. function initialItems()
  165. {
  166. return 3;
  167. }
  168. /**
  169. * finish the notice
  170. *
  171. * Close the last elements in the notice list item
  172. *
  173. * @return void
  174. */
  175. function showEnd()
  176. {
  177. $max = $this->initialItems();
  178. if (!$this->repeat instanceof Notice) {
  179. $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
  180. $notice = $stream->getNotices(0, $max + 2);
  181. $notices = array();
  182. $cnt = 0;
  183. $moreCutoff = null;
  184. while ($notice->fetch()) {
  185. if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
  186. // Don't list repeats as separate notices in a conversation
  187. if (!empty($notice->repeat_of)) {
  188. continue;
  189. }
  190. if ($notice->id == $this->notice->id) {
  191. // Skip!
  192. continue;
  193. }
  194. $cnt++;
  195. if ($cnt > $max) {
  196. // boo-yah
  197. $moreCutoff = clone($notice);
  198. break;
  199. }
  200. $notices[] = clone($notice); // *grumble* inefficient as hell
  201. Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
  202. }
  203. }
  204. if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
  205. $threadActive = count($notices) > 0; // has this thread had any activity?
  206. $this->out->elementStart('ul', 'notices threaded-replies xoxo');
  207. if (Event::handle('StartShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive))) {
  208. // Repeats and Faves/Likes are handled in plugins.
  209. Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
  210. }
  211. if (count($notices)>0) {
  212. if ($moreCutoff) {
  213. $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
  214. $item->show();
  215. }
  216. foreach (array_reverse($notices) as $notice) {
  217. if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
  218. $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
  219. $item->show();
  220. Event::handle('EndShowThreadedNoticeSub', array($this, $this->notice, $notice));
  221. }
  222. }
  223. }
  224. Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
  225. $this->out->elementEnd('ul');
  226. }
  227. }
  228. parent::showEnd();
  229. }
  230. }
  231. // @todo FIXME: needs documentation.
  232. class ThreadedNoticeListSubItem extends NoticeListItem
  233. {
  234. protected $root = null;
  235. function __construct(Notice $notice, $root, $out)
  236. {
  237. $this->root = $root;
  238. parent::__construct($notice, $out);
  239. }
  240. function avatarSize()
  241. {
  242. return AVATAR_STREAM_SIZE; // @fixme would like something in between
  243. }
  244. function showNoticeLocation()
  245. {
  246. //
  247. }
  248. function showNoticeSource()
  249. {
  250. //
  251. }
  252. function getAttentionProfiles()
  253. {
  254. $all = parent::getAttentionProfiles();
  255. $profiles = array();
  256. $rootAuthor = $this->root->getProfile();
  257. foreach ($all as $profile) {
  258. if ($profile->id != $rootAuthor->id) {
  259. $profiles[] = $profile;
  260. }
  261. }
  262. return $profiles;
  263. }
  264. function showEnd()
  265. {
  266. $threadActive = null; // unused here for now, but maybe in the future?
  267. if (Event::handle('StartShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive))) {
  268. // Repeats and Faves/Likes are handled in plugins.
  269. Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
  270. }
  271. parent::showEnd();
  272. }
  273. }
  274. /**
  275. * Placeholder for loading more replies...
  276. */
  277. class ThreadedNoticeListMoreItem extends NoticeListItem
  278. {
  279. protected $cnt;
  280. function __construct(Notice $notice, Action $out, $cnt)
  281. {
  282. parent::__construct($notice, $out);
  283. $this->cnt = $cnt;
  284. }
  285. /**
  286. * recipe function for displaying a single notice.
  287. *
  288. * This uses all the other methods to correctly display a notice. Override
  289. * it or one of the others to fine-tune the output.
  290. *
  291. * @return void
  292. */
  293. function show()
  294. {
  295. $this->showStart();
  296. $this->showMiniForm();
  297. $this->showEnd();
  298. }
  299. /**
  300. * start a single notice.
  301. *
  302. * @return void
  303. */
  304. function showStart()
  305. {
  306. $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
  307. }
  308. function showEnd()
  309. {
  310. $this->out->elementEnd('li');
  311. }
  312. function showMiniForm()
  313. {
  314. $id = $this->notice->conversation;
  315. $url = common_local_url('conversation', array('id' => $id));
  316. $n = Conversation::noticeCount($id) - 1;
  317. // TRANS: Link to show replies for a notice.
  318. // TRANS: %d is the number of replies to a notice and used for plural.
  319. $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
  320. $this->out->element('a', array('href' => $url), $msg);
  321. }
  322. }