inboxnoticestream.php 4.4 KB

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