inboxnoticestream.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 for a profile's "all" feed
  18. *
  19. * @category NoticeStream
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Mikael Nordfeldth <mmn@hethane.se>
  23. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  24. * @author Stephane Berube <chimo@chromic.org>
  25. * @copyright 2011 StatusNet, Inc.
  26. * @copyright 2014 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. defined('GNUSOCIAL') || die();
  30. /**
  31. * @category General
  32. * @copyright 2011 StatusNet, Inc.
  33. * @copyright 2014 Free Software Foundation, Inc http://www.fsf.org
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class InboxNoticeStream extends ScopingNoticeStream
  37. {
  38. /**
  39. * Constructor
  40. *
  41. * @param Profile $target Profile to get a stream for
  42. * @param Profile $scoped Currently scoped profile (if null, it is fetched)
  43. */
  44. public function __construct(Profile $target, Profile $scoped = null)
  45. {
  46. parent::__construct(
  47. new CachingNoticeStream(
  48. new RawInboxNoticeStream($target),
  49. 'profileall:' . $target->getID(),
  50. false,
  51. true
  52. ),
  53. $scoped
  54. );
  55. }
  56. }
  57. /**
  58. * Raw stream of notices for the target's inbox
  59. *
  60. * @category General
  61. * @copyright 2011 StatusNet, Inc.
  62. * @copyright 2014 Free Software Foundation, Inc http://www.fsf.org
  63. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  64. */
  65. class RawInboxNoticeStream extends FullNoticeStream
  66. {
  67. protected $target = null;
  68. protected $inbox = null;
  69. /**
  70. * Constructor
  71. *
  72. * @param Profile $target Profile to get a stream for
  73. */
  74. public function __construct(Profile $target)
  75. {
  76. parent::__construct();
  77. $this->target = $target;
  78. }
  79. /**
  80. * Get IDs in a range
  81. *
  82. * @param int $offset Offset from start
  83. * @param int $limit Limit of number to get
  84. * @param int $since_id Since this notice
  85. * @param int $max_id Before this notice
  86. *
  87. * @return array IDs found
  88. */
  89. public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
  90. {
  91. $inner_notice = new Notice();
  92. $inner_notice->whereAdd(sprintf(
  93. "notice.created >= TIMESTAMP '%s'",
  94. $inner_notice->escape($this->target->created)
  95. ));
  96. if (!empty($since_id)) {
  97. $inner_notice->whereAdd("notice.id > {$since_id}");
  98. }
  99. if (!empty($max_id)) {
  100. $inner_notice->whereAdd("notice.id <= {$max_id}");
  101. }
  102. $inner_notice->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
  103. self::filterVerbs($inner_notice, $this->selectVerbs);
  104. // The only purpose of this hack is to allow filterVerbs above
  105. $notice_cond = preg_replace(
  106. '/^\s+WHERE\s+/',
  107. 'AND ',
  108. $inner_notice->_query['condition']
  109. ) . 'ORDER BY notice.id DESC LIMIT ' . ($limit + $offset);
  110. $notice = new Notice();
  111. // Reply:: is a table of mentions
  112. // Subscription:: is a table of subscriptions (every user is subscribed to themselves)
  113. // notice.id will give us even really old posts, which were recently
  114. // imported. For example if a remote instance had problems and just
  115. // managed to post here.
  116. $notice->query(sprintf(
  117. <<<'END'
  118. SELECT DISTINCT id
  119. FROM (
  120. (
  121. SELECT notice.id
  122. FROM notice
  123. INNER JOIN subscription
  124. ON notice.profile_id = subscription.subscribed
  125. WHERE subscription.subscriber = %1$d %2$s
  126. ) UNION ALL (
  127. SELECT notice.id
  128. FROM notice
  129. INNER JOIN reply ON notice.id = reply.notice_id
  130. WHERE reply.profile_id = %1$d %2$s
  131. ) UNION ALL (
  132. SELECT notice.id
  133. FROM notice
  134. INNER JOIN attention ON notice.id = attention.notice_id
  135. WHERE attention.profile_id = %1$d %2$s
  136. ) UNION ALL (
  137. SELECT notice.id
  138. FROM notice
  139. INNER JOIN group_inbox
  140. ON notice.id = group_inbox.notice_id
  141. INNER JOIN group_member
  142. ON group_inbox.group_id = group_member.group_id
  143. WHERE group_member.profile_id = %1$d %2$s
  144. )
  145. ) AS t1
  146. ORDER BY id DESC
  147. LIMIT %3$d OFFSET %4$d;
  148. END,
  149. $this->target->getID(),
  150. $notice_cond,
  151. $limit,
  152. $offset
  153. ));
  154. $ret = [];
  155. while ($notice->fetch()) {
  156. $ret[] = $notice->id;
  157. }
  158. return $ret;
  159. }
  160. }