inboxnoticestream.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. INNER JOIN (
  91. (SELECT id FROM notice
  92. WHERE profile_id
  93. IN (SELECT subscribed FROM subscription WHERE subscriber = %1$d))
  94. UNION
  95. (SELECT notice_id AS id FROM reply WHERE profile_id = %1$d)
  96. UNION
  97. (SELECT notice_id AS id FROM attention WHERE profile_id = %1$d)
  98. UNION
  99. (SELECT notice_id AS id FROM group_inbox INNER JOIN group_member USING (group_id)
  100. WHERE group_member.profile_id = %1$d)
  101. ) AS t1 USING (id)
  102. END,
  103. $this->target->getID()
  104. );
  105. $notice->whereAdd(sprintf(
  106. "notice.created > TIMESTAMP '%s'",
  107. $notice->escape($this->target->created)
  108. ));
  109. if (!empty($since_id)) {
  110. $notice->whereAdd('id > ' . $since_id);
  111. }
  112. if (!empty($max_id)) {
  113. $notice->whereAdd('id <= ' . $max_id);
  114. }
  115. $notice->whereAdd('scope <> ' . Notice::MESSAGE_SCOPE);
  116. self::filterVerbs($notice, $this->selectVerbs);
  117. // notice.id will give us even really old posts, which were recently
  118. // imported. For example if a remote instance had problems and just
  119. // managed to post here.
  120. $notice->orderBy('id DESC');
  121. $notice->limit($offset, $limit);
  122. if (!$notice->find()) {
  123. return [];
  124. }
  125. return $notice->fetchAll('id');
  126. }
  127. }