threadednoticelist.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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') && !defined('STATUSNET')) { 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) {
  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. // Show the repeats-button for this notice. If there are repeats,
  209. // the show() function will return true, definitely setting our
  210. // $threadActive flag, which will be used later to show a reply box.
  211. $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
  212. $threadActive = $item->show() || $threadActive;
  213. Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
  214. }
  215. if (count($notices)>0) {
  216. if ($moreCutoff) {
  217. $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
  218. $item->show();
  219. }
  220. foreach (array_reverse($notices) as $notice) {
  221. if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
  222. $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
  223. $item->show();
  224. Event::handle('EndShowThreadedNoticeSub', array($this, $this->notice, $notice));
  225. }
  226. }
  227. }
  228. if ($threadActive && Profile::current() instanceof Profile) {
  229. // @fixme do a proper can-post check that's consistent
  230. // with the JS side
  231. $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
  232. $item->show();
  233. }
  234. $this->out->elementEnd('ul');
  235. Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
  236. }
  237. }
  238. parent::showEnd();
  239. }
  240. }
  241. // @todo FIXME: needs documentation.
  242. class ThreadedNoticeListSubItem extends NoticeListItem
  243. {
  244. protected $root = null;
  245. function __construct(Notice $notice, $root, $out)
  246. {
  247. $this->root = $root;
  248. parent::__construct($notice, $out);
  249. }
  250. function avatarSize()
  251. {
  252. return AVATAR_STREAM_SIZE; // @fixme would like something in between
  253. }
  254. function showNoticeLocation()
  255. {
  256. //
  257. }
  258. function showNoticeSource()
  259. {
  260. //
  261. }
  262. function getReplyProfiles()
  263. {
  264. $all = parent::getReplyProfiles();
  265. $profiles = array();
  266. $rootAuthor = $this->root->getProfile();
  267. foreach ($all as $profile) {
  268. if ($profile->id != $rootAuthor->id) {
  269. $profiles[] = $profile;
  270. }
  271. }
  272. return $profiles;
  273. }
  274. function showEnd()
  275. {
  276. $threadActive = null; // unused here for now, but maybe in the future?
  277. if (Event::handle('StartShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive))) {
  278. $item = new ThreadedNoticeListInlineRepeatsItem($this->notice, $this->out);
  279. $hasRepeats = $item->show();
  280. Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
  281. }
  282. parent::showEnd();
  283. }
  284. }
  285. /**
  286. * Placeholder for loading more replies...
  287. */
  288. class ThreadedNoticeListMoreItem extends NoticeListItem
  289. {
  290. protected $cnt;
  291. function __construct(Notice $notice, Action $out, $cnt)
  292. {
  293. parent::__construct($notice, $out);
  294. $this->cnt = $cnt;
  295. }
  296. /**
  297. * recipe function for displaying a single notice.
  298. *
  299. * This uses all the other methods to correctly display a notice. Override
  300. * it or one of the others to fine-tune the output.
  301. *
  302. * @return void
  303. */
  304. function show()
  305. {
  306. $this->showStart();
  307. $this->showMiniForm();
  308. $this->showEnd();
  309. }
  310. /**
  311. * start a single notice.
  312. *
  313. * @return void
  314. */
  315. function showStart()
  316. {
  317. $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
  318. }
  319. function showEnd()
  320. {
  321. $this->out->elementEnd('li');
  322. }
  323. function showMiniForm()
  324. {
  325. $id = $this->notice->conversation;
  326. $url = common_local_url('conversation', array('id' => $id));
  327. $n = Conversation::noticeCount($id) - 1;
  328. // TRANS: Link to show replies for a notice.
  329. // TRANS: %d is the number of replies to a notice and used for plural.
  330. $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
  331. $this->out->element('a', array('href' => $url), $msg);
  332. }
  333. }
  334. /**
  335. * Placeholder for reply form...
  336. * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
  337. */
  338. class ThreadedNoticeListReplyItem extends NoticeListItem
  339. {
  340. /**
  341. * recipe function for displaying a single notice.
  342. *
  343. * This uses all the other methods to correctly display a notice. Override
  344. * it or one of the others to fine-tune the output.
  345. *
  346. * @return void
  347. */
  348. function show()
  349. {
  350. $this->showStart();
  351. $this->showMiniForm();
  352. $this->showEnd();
  353. }
  354. /**
  355. * start a single notice.
  356. *
  357. * @return void
  358. */
  359. function showStart()
  360. {
  361. $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
  362. }
  363. function showMiniForm()
  364. {
  365. $this->out->element('input', array('class' => 'placeholder',
  366. // TRANS: Field label for reply mini form.
  367. 'value' => _('Write a reply...')));
  368. }
  369. }
  370. /**
  371. * Placeholder for showing repeats...
  372. */
  373. class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
  374. {
  375. function getProfiles()
  376. {
  377. $repeats = $this->notice->getRepeats();
  378. $profiles = array();
  379. foreach ($repeats as $rep) {
  380. $profiles[] = $rep->profile_id;
  381. }
  382. return $profiles;
  383. }
  384. function magicList($items)
  385. {
  386. if (count($items) > 4) {
  387. return parent::magicList(array_slice($items, 0, 3));
  388. } else {
  389. return parent::magicList($items);
  390. }
  391. }
  392. function getListMessage($count, $you)
  393. {
  394. if ($count == 1 && $you) {
  395. // darn first person being different from third person!
  396. // TRANS: List message for notice repeated by logged in user.
  397. return _m('REPEATLIST', 'You repeated this.');
  398. } else if ($count > 4) {
  399. // TRANS: List message for when more than 4 people repeat something.
  400. // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
  401. // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
  402. return sprintf(_m('%%s and %d other repeated this.',
  403. '%%s and %d others repeated this.',
  404. $count - 3),
  405. $count - 3);
  406. } else {
  407. // TRANS: List message for repeated notices.
  408. // TRANS: %%s is a list of users who have repeated a notice.
  409. // TRANS: Plural is based on the number of of users that have repeated a notice.
  410. return sprintf(_m('%%s repeated this.',
  411. '%%s repeated this.',
  412. $count),
  413. $count);
  414. }
  415. }
  416. function showStart()
  417. {
  418. $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
  419. }
  420. function showEnd()
  421. {
  422. $this->out->elementEnd('li');
  423. }
  424. }
  425. // @todo FIXME: needs documentation.
  426. class ThreadedNoticeListInlineRepeatsItem extends ThreadedNoticeListRepeatsItem
  427. {
  428. function showStart()
  429. {
  430. $this->out->elementStart('div', array('class' => 'notice-repeats'));
  431. }
  432. function showEnd()
  433. {
  434. $this->out->elementEnd('div');
  435. }
  436. }