profilenoticestream.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Stream of notices by a profile
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Stream
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Stream of notices by a profile
  33. *
  34. * @category General
  35. * @package StatusNet
  36. * @author Evan Prodromou <evan@status.net>
  37. * @copyright 2011 StatusNet, Inc.
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  39. * @link http://status.net/
  40. */
  41. class ProfileNoticeStream extends ScopingNoticeStream
  42. {
  43. protected $target;
  44. function __construct(Profile $target, Profile $scoped=null)
  45. {
  46. $this->target = $target;
  47. parent::__construct(new CachingNoticeStream(new RawProfileNoticeStream($target),
  48. 'profile:notice_ids:' . $target->getID()),
  49. $scoped);
  50. }
  51. 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. 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. 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. )) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. }
  88. /**
  89. * Raw stream of notices by a profile
  90. *
  91. * @category General
  92. * @package StatusNet
  93. * @author Evan Prodromou <evan@status.net>
  94. * @copyright 2011 StatusNet, Inc.
  95. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  96. * @link http://status.net/
  97. */
  98. class RawProfileNoticeStream extends NoticeStream
  99. {
  100. protected $target;
  101. protected $selectVerbs = array(); // select all verbs
  102. function __construct(Profile $target)
  103. {
  104. parent::__construct();
  105. $this->target = $target;
  106. }
  107. function getNoticeIds($offset, $limit, $since_id, $max_id)
  108. {
  109. $notice = new Notice();
  110. $notice->profile_id = $this->target->getID();
  111. $notice->selectAdd();
  112. $notice->selectAdd('id');
  113. Notice::addWhereSinceId($notice, $since_id);
  114. Notice::addWhereMaxId($notice, $max_id);
  115. self::filterVerbs($notice, $this->selectVerbs);
  116. $notice->orderBy('created DESC, id DESC');
  117. if (!is_null($offset)) {
  118. $notice->limit($offset, $limit);
  119. }
  120. $notice->find();
  121. $ids = array();
  122. while ($notice->fetch()) {
  123. $ids[] = $notice->id;
  124. }
  125. return $ids;
  126. }
  127. }