threadednoticelistitem.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Widget for displaying a single notice.
  19. *
  20. * This widget has the core smarts for showing a single notice: what to display,
  21. * where, and under which circumstances. Its key method is show(); this is a recipe
  22. * that calls all the other show*() methods to build up a single notice. The
  23. * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
  24. * author info (since that's implicit by the data in the page).
  25. *
  26. * @category UI
  27. * @package GNUsocial
  28. * @author Evan Prodromou <evan@status.net>
  29. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  30. * @see NoticeList
  31. * @see ProfileNoticeListItem
  32. */
  33. class ThreadedNoticeListItem extends NoticeListItem
  34. {
  35. public $widgetOpts;
  36. public $scoped;
  37. protected $userProfile = null;
  38. public function __construct(Notice $notice, Action $out = null, $profile = null)
  39. {
  40. parent::__construct($notice, $out);
  41. $this->userProfile = $profile;
  42. }
  43. public function initialItems()
  44. {
  45. return 3;
  46. }
  47. /**
  48. * finish the notice
  49. *
  50. * Close the last elements in the notice list item
  51. *
  52. * @return void
  53. */
  54. public function showEnd()
  55. {
  56. $max = $this->initialItems();
  57. if (!$this->repeat instanceof Notice) {
  58. $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
  59. $notice = $stream->getNotices(0, $max + 2);
  60. $notices = [];
  61. $cnt = 0;
  62. $moreCutoff = null;
  63. while ($notice->fetch()) {
  64. if (Event::handle('StartAddNoticeReply', [$this, $this->notice, $notice])) {
  65. // Don't list repeats as separate notices in a conversation
  66. if (!empty($notice->repeat_of)) {
  67. continue;
  68. }
  69. if ($notice->id == $this->notice->id) {
  70. // Skip!
  71. continue;
  72. }
  73. if (!$notice->isVerb([ActivityVerb::POST])) {
  74. continue;
  75. }
  76. $cnt++;
  77. if ($cnt > $max) {
  78. // boo-yah
  79. $moreCutoff = clone $notice;
  80. break;
  81. }
  82. $notices[] = clone $notice; // *grumble* inefficient as hell
  83. Event::handle('EndAddNoticeReply', [$this, $this->notice, $notice]);
  84. }
  85. }
  86. if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
  87. $threadActive = count($notices) > 0; // has this thread had any activity?
  88. $this->out->elementStart('ul', 'notices threaded-replies xoxo');
  89. if (Event::handle('StartShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive))) {
  90. // Repeats and Faves/Likes are handled in plugins.
  91. Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
  92. }
  93. if (count($notices) > 0) {
  94. if ($moreCutoff) {
  95. $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
  96. $item->show();
  97. }
  98. foreach (array_reverse($notices) as $notice) {
  99. if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
  100. $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
  101. $item->show();
  102. Event::handle('EndShowThreadedNoticeSub', array($this, $this->notice, $notice));
  103. }
  104. }
  105. }
  106. Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
  107. $this->out->elementEnd('ul');
  108. }
  109. }
  110. parent::showEnd();
  111. }
  112. }