profilenoticestream.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  17. * Stream of notices by a profile
  18. *
  19. * @category Stream
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Stream of notices by a profile
  28. *
  29. * @category General
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @copyright 2011 StatusNet, Inc.
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class ProfileNoticeStream extends ScopingNoticeStream
  36. {
  37. public $widgetOpts;
  38. public $scoped;
  39. protected $target;
  40. public function __construct(Profile $target, Profile $scoped = null)
  41. {
  42. $this->target = $target;
  43. parent::__construct(
  44. new CachingNoticeStream(
  45. new RawProfileNoticeStream($target),
  46. 'profile:notice_ids:' . $target->getID()
  47. ),
  48. $scoped
  49. );
  50. }
  51. public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
  52. {
  53. if ($this->impossibleStream()) {
  54. return array();
  55. } else {
  56. return parent::getNoticeIds($offset, $limit, $since_id, $max_id);
  57. }
  58. }
  59. public function getNotices($offset, $limit, $since_id = null, $max_id = null)
  60. {
  61. if ($this->impossibleStream()) {
  62. throw new PrivateStreamException($this->target, $this->scoped);
  63. } else {
  64. return parent::getNotices($offset, $limit, $since_id, $max_id);
  65. }
  66. }
  67. public function impossibleStream()
  68. {
  69. if (!$this->target->readableBy($this->scoped)) {
  70. // cannot read because it's a private stream and either noone's logged in or they are not subscribers
  71. return true;
  72. }
  73. // If it's a spammy stream, and no user or not a moderator
  74. if (common_config('notice', 'hidespam')) {
  75. // if this is a silenced user
  76. if ($this->target->hasRole(Profile_role::SILENCED) &&
  77. // and we are either not logged in
  78. (!$this->scoped instanceof Profile ||
  79. // or if we are, we are not logged in as the target, and we don't have right to review spam
  80. (!$this->scoped->sameAs($this->target) && !$this->scoped->hasRight(Right::REVIEWSPAM)))) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. }
  87. /**
  88. * Raw stream of notices by a profile
  89. *
  90. * @category General
  91. * @package GNUsocial
  92. * @author Evan Prodromou <evan@status.net>
  93. * @copyright 2011 StatusNet, Inc.
  94. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  95. */
  96. class RawProfileNoticeStream extends FullNoticeStream
  97. {
  98. protected $target;
  99. public function __construct(Profile $target)
  100. {
  101. parent::__construct();
  102. $this->target = $target;
  103. }
  104. public function getNoticeIds($offset, $limit, $since_id, $max_id)
  105. {
  106. $notice = new Notice();
  107. $notice->profile_id = $this->target->getID();
  108. $notice->selectAdd();
  109. $notice->selectAdd('id');
  110. $notice->whereAdd('scope <> ' . Notice::MESSAGE_SCOPE);
  111. Notice::addWhereSinceId($notice, $since_id);
  112. Notice::addWhereMaxId($notice, $max_id);
  113. self::filterVerbs($notice, $this->selectVerbs);
  114. $notice->orderBy('created DESC, id DESC');
  115. if (!is_null($offset)) {
  116. $notice->limit($offset, $limit);
  117. }
  118. $notice->find();
  119. $ids = array();
  120. while ($notice->fetch()) {
  121. $ids[] = $notice->id;
  122. }
  123. return $ids;
  124. }
  125. }