profilenoticestream.php 4.3 KB

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