inboxnoticestream.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $notice = new Notice();
  92. $notice->selectAdd();
  93. $notice->selectAdd('id');
  94. // Reply:: is a table of mentions
  95. // Subscription:: is a table of subscriptions (every user is subscribed to themselves)
  96. $notice->_join .= "\n" . <<<'END'
  97. LEFT JOIN (
  98. SELECT notice.id, subscription.subscriber AS profile_id
  99. FROM notice INNER JOIN subscription
  100. ON notice.profile_id = subscription.subscribed
  101. UNION ALL
  102. SELECT reply.id, notice.profile_id
  103. FROM notice AS reply INNER JOIN notice ON reply.reply_to = notice.id
  104. UNION ALL
  105. SELECT notice_id, profile_id FROM attention
  106. UNION ALL
  107. SELECT group_inbox.notice_id, group_member.profile_id
  108. FROM group_inbox INNER JOIN group_member USING (group_id)
  109. ) AS t1 USING (id)
  110. END;
  111. $notice->whereAdd('t1.id IS NOT NULL');
  112. $notice->whereAdd('t1.profile_id = ' . $this->target->getID());
  113. $notice->whereAdd(sprintf(
  114. "notice.created > TIMESTAMP '%s'",
  115. $notice->escape($this->target->created)
  116. ));
  117. if (!empty($since_id)) {
  118. $notice->whereAdd('id > ' . $since_id);
  119. }
  120. if (!empty($max_id)) {
  121. $notice->whereAdd('id <= ' . $max_id);
  122. }
  123. $notice->whereAdd('scope <> ' . Notice::MESSAGE_SCOPE);
  124. self::filterVerbs($notice, $this->selectVerbs);
  125. // notice.id will give us even really old posts, which were recently
  126. // imported. For example if a remote instance had problems and just
  127. // managed to post here.
  128. $notice->orderBy('id DESC');
  129. $notice->limit($offset, $limit);
  130. if (!$notice->find()) {
  131. return [];
  132. }
  133. return $notice->fetchAll('id');
  134. }
  135. }