inboxnoticestream.php 5.6 KB

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