inboxnoticestream.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall:'.$target->getID()), $scoped);
  47. }
  48. }
  49. /**
  50. * Raw stream of notices for the target's inbox
  51. *
  52. * @category General
  53. * @copyright 2011 StatusNet, Inc.
  54. * @copyright 2014 Free Software Foundation, Inc http://www.fsf.org
  55. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  56. */
  57. class RawInboxNoticeStream extends FullNoticeStream
  58. {
  59. protected $target = null;
  60. protected $inbox = null;
  61. /**
  62. * Constructor
  63. *
  64. * @param Profile $target Profile to get a stream for
  65. */
  66. public function __construct(Profile $target)
  67. {
  68. parent::__construct();
  69. $this->target = $target;
  70. }
  71. /**
  72. * Get IDs in a range
  73. *
  74. * @param int $offset Offset from start
  75. * @param int $limit Limit of number to get
  76. * @param int $since_id Since this notice
  77. * @param int $max_id Before this notice
  78. *
  79. * @return array IDs found
  80. */
  81. public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
  82. {
  83. $notice = new Notice();
  84. $notice->selectAdd();
  85. $notice->selectAdd('id');
  86. // Reply:: is a table of mentions
  87. // Subscription:: is a table of subscriptions (every user is subscribed to themselves)
  88. $notice->_join .= sprintf(
  89. "\n" . <<<'END'
  90. LEFT JOIN (
  91. SELECT notice.id
  92. FROM notice INNER JOIN subscription
  93. ON notice.profile_id = subscription.subscribed
  94. WHERE subscription.subscriber = %1$d
  95. UNION ALL
  96. SELECT notice_id AS id FROM reply WHERE profile_id = %1$d
  97. UNION ALL
  98. SELECT notice_id AS id FROM attention WHERE profile_id = %1$d
  99. UNION ALL
  100. SELECT notice_id AS id FROM group_inbox INNER JOIN group_member USING (group_id)
  101. WHERE group_member.profile_id = %1$d
  102. ) AS t1 USING (id)
  103. END,
  104. $this->target->getID()
  105. );
  106. $notice->whereAdd('t1.id IS NOT NULL');
  107. $notice->whereAdd(sprintf(
  108. "notice.created > TIMESTAMP '%s'",
  109. $notice->escape($this->target->created)
  110. ));
  111. if (!empty($since_id)) {
  112. $notice->whereAdd('id > ' . $since_id);
  113. }
  114. if (!empty($max_id)) {
  115. $notice->whereAdd('id <= ' . $max_id);
  116. }
  117. $notice->whereAdd('scope <> ' . Notice::MESSAGE_SCOPE);
  118. self::filterVerbs($notice, $this->selectVerbs);
  119. // notice.id will give us even really old posts, which were recently
  120. // imported. For example if a remote instance had problems and just
  121. // managed to post here.
  122. $notice->orderBy('id DESC');
  123. $notice->limit($offset, $limit);
  124. if (!$notice->find()) {
  125. return [];
  126. }
  127. return $notice->fetchAll('id');
  128. }
  129. }