inboxnoticestream.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Stream of notices for a profile's "all" feed
  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 NoticeStream
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Mikael Nordfeldth <mmn@hethane.se>
  27. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  28. * @copyright 2011 StatusNet, Inc.
  29. * @copyright 2014 Free Software Foundation, Inc.
  30. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  31. * @link http://status.net/
  32. */
  33. if (!defined('GNUSOCIAL')) {
  34. exit(1);
  35. }
  36. /**
  37. * Stream of notices for a profile's "all" feed
  38. *
  39. * @category General
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @author Mikael Nordfeldth <mmn@hethane.se>
  43. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  44. * @copyright 2011 StatusNet, Inc.
  45. * @copyright 2014 Free Software Foundation, Inc.
  46. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  47. * @link http://status.net/
  48. */
  49. class InboxNoticeStream extends ScopingNoticeStream
  50. {
  51. /**
  52. * Constructor
  53. *
  54. * @param Profile $target Profile to get a stream for
  55. * @param Profile $scoped Currently scoped profile (if null, it is fetched)
  56. */
  57. public function __construct(Profile $target, Profile $scoped = null)
  58. {
  59. // FIXME: we don't use CachingNoticeStream - but maybe we should?
  60. parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall'), $scoped);
  61. }
  62. }
  63. /**
  64. * Raw stream of notices for the target's inbox
  65. *
  66. * @category General
  67. * @package StatusNet
  68. * @author Evan Prodromou <evan@status.net>
  69. * @author Mikael Nordfeldth <mmn@hethane.se>
  70. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  71. * @copyright 2011 StatusNet, Inc.
  72. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  73. * @link http://status.net/
  74. */
  75. class RawInboxNoticeStream extends FullNoticeStream
  76. {
  77. protected $target = null;
  78. protected $inbox = null;
  79. /**
  80. * Constructor
  81. *
  82. * @param Profile $target Profile to get a stream for
  83. */
  84. public function __construct(Profile $target)
  85. {
  86. parent::__construct();
  87. $this->target = $target;
  88. }
  89. /**
  90. * Get IDs in a range
  91. *
  92. * @param int $offset Offset from start
  93. * @param int $limit Limit of number to get
  94. * @param int $since_id Since this notice
  95. * @param int $max_id Before this notice
  96. *
  97. * @return array IDs found
  98. */
  99. public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
  100. {
  101. $notice_ids = [];
  102. // Grab all the profiles target is subscribed to (every user is subscribed to themselves)
  103. $subscription = new Subscription();
  104. $subscription->selectAdd();
  105. $subscription->selectAdd('subscribed');
  106. $subscription->whereAdd(sprintf('subscriber = %1$d', $this->target->id));
  107. $subscription_profile_ids = $subscription->fetchAll('subscribed');
  108. // Grab all the notices were target was mentioned
  109. $reply = new Reply();
  110. $reply->selectAdd();
  111. $reply->selectAdd('notice_id');
  112. $reply->whereAdd(sprintf('profile_id = %1$d', $this->target->id));
  113. $notice_ids += $reply->fetchAll('notice_id');
  114. // Grab all the notices that require target's attention
  115. $attention = new Attention();
  116. $attention->selectAdd();
  117. $attention->selectAdd('notice_id');
  118. $attention->whereAdd(sprintf('profile_id = %1$d', $this->target->id));
  119. $notice_ids += $attention->fetchAll('notice_id');
  120. // Grab all the notices posted on groups target is a member of
  121. $group_inbox = new Group_inbox();
  122. $group_inbox->selectAdd();
  123. $group_inbox->selectAdd('notice_id');
  124. $group_inbox->whereAdd(
  125. sprintf(
  126. 'group_id IN (SELECT group_id FROM group_member WHERE profile_id = %1$d)',
  127. $this->target->id
  128. )
  129. );
  130. $notice_ids += $group_inbox->fetchAll('notice_id');
  131. $query_ids = '';
  132. if (!empty($notice_ids)) { // Replies, Attentions and Groups
  133. $query_ids .= 'notice.id IN (' . implode(', ', $notice_ids) . ') OR ';
  134. }
  135. // every user is at least subscribed to themselves
  136. $query_ids .= 'notice.profile_id IN (' . implode(', ', $subscription_profile_ids) . ')';
  137. $notice = new Notice();
  138. $notice->selectAdd();
  139. $notice->selectAdd('id');
  140. $notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created)));
  141. $notice->whereAdd($query_ids);
  142. if (!empty($since_id)) {
  143. $notice->whereAdd(sprintf('notice.id > %d', $since_id));
  144. }
  145. if (!empty($max_id)) {
  146. $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
  147. }
  148. self::filterVerbs($notice, $this->selectVerbs);
  149. $notice->limit($offset, $limit);
  150. // notice.id will give us even really old posts, which were
  151. // recently imported. For example if a remote instance had
  152. // problems and just managed to post here. Another solution
  153. // would be to have a 'notice.imported' field and order by it.
  154. $notice->orderBy('notice.id DESC');
  155. if (!$notice->find()) {
  156. return [];
  157. }
  158. return $notice->fetchAll('id');
  159. }
  160. }