threadednoticelistitem.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. protected $userProfile = null;
  36. public function __construct(Notice $notice, Action $out = null, $profile = null)
  37. {
  38. parent::__construct($notice, $out);
  39. $this->userProfile = $profile;
  40. }
  41. public function initialItems()
  42. {
  43. return 3;
  44. }
  45. /**
  46. * finish the notice
  47. *
  48. * Close the last elements in the notice list item
  49. *
  50. * @return void
  51. */
  52. public function showEnd()
  53. {
  54. $max = $this->initialItems();
  55. if (!$this->repeat instanceof Notice) {
  56. $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
  57. $notice = $stream->getNotices(0, $max + 2);
  58. $notices = [];
  59. $cnt = 0;
  60. $moreCutoff = null;
  61. while ($notice->fetch()) {
  62. if (Event::handle('StartAddNoticeReply', [$this, $this->notice, $notice])) {
  63. // Don't list repeats as separate notices in a conversation
  64. if (!empty($notice->repeat_of)) {
  65. continue;
  66. }
  67. if ($notice->id == $this->notice->id) {
  68. // Skip!
  69. continue;
  70. }
  71. if (!$notice->isVerb([ActivityVerb::POST])) {
  72. continue;
  73. }
  74. $cnt++;
  75. if ($cnt > $max) {
  76. // boo-yah
  77. $moreCutoff = clone $notice;
  78. break;
  79. }
  80. $notices[] = clone $notice; // *grumble* inefficient as hell
  81. Event::handle('EndAddNoticeReply', [$this, $this->notice, $notice]);
  82. }
  83. }
  84. if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
  85. $threadActive = count($notices) > 0; // has this thread had any activity?
  86. $this->out->elementStart('ul', 'notices threaded-replies xoxo');
  87. if (Event::handle('StartShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive))) {
  88. // Repeats and Faves/Likes are handled in plugins.
  89. Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
  90. }
  91. if (count($notices) > 0) {
  92. if ($moreCutoff) {
  93. $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
  94. $item->show();
  95. }
  96. foreach (array_reverse($notices) as $notice) {
  97. if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
  98. $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
  99. $item->show();
  100. Event::handle('EndShowThreadedNoticeSub', array($this, $this->notice, $notice));
  101. }
  102. }
  103. }
  104. Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
  105. $this->out->elementEnd('ul');
  106. }
  107. }
  108. parent::showEnd();
  109. }
  110. }