inboxnoticestream.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Stream of notices for a profile's "all" feed
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @category NoticeStream
  22. * @package StatusNet
  23. * @author Evan Prodromou <evan@status.net>
  24. * @author Mikael Nordfeldth <mmn@hethane.se>
  25. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @copyright 2014 Free Software Foundation, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('GNUSOCIAL')) {
  32. exit(1);
  33. }
  34. /**
  35. * Stream of notices for a profile's "all" feed
  36. *
  37. * @category General
  38. * @package StatusNet
  39. * @author Evan Prodromou <evan@status.net>
  40. * @author Mikael Nordfeldth <mmn@hethane.se>
  41. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  42. * @author chimo <chimo@chromic.org>
  43. * @copyright 2011 StatusNet, Inc.
  44. * @copyright 2014 Free Software Foundation, Inc.
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  46. * @link http://status.net/
  47. */
  48. class InboxNoticeStream extends ScopingNoticeStream
  49. {
  50. /**
  51. * Constructor
  52. *
  53. * @param Profile $target Profile to get a stream for
  54. * @param Profile $scoped Currently scoped profile (if null, it is fetched)
  55. */
  56. public function __construct(Profile $target, Profile $scoped = null)
  57. {
  58. parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall:'.$target->getID()), $scoped);
  59. }
  60. }
  61. /**
  62. * Raw stream of notices for the target's inbox
  63. *
  64. * @category General
  65. * @package StatusNet
  66. * @author Evan Prodromou <evan@status.net>
  67. * @author Mikael Nordfeldth <mmn@hethane.se>
  68. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  69. * @copyright 2011 StatusNet, Inc.
  70. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  71. * @link http://status.net/
  72. */
  73. class RawInboxNoticeStream extends FullNoticeStream
  74. {
  75. protected $target = null;
  76. protected $inbox = null;
  77. /**
  78. * Constructor
  79. *
  80. * @param Profile $target Profile to get a stream for
  81. */
  82. public function __construct(Profile $target)
  83. {
  84. parent::__construct();
  85. $this->target = $target;
  86. }
  87. /**
  88. * Get IDs in a range
  89. *
  90. * @param int $offset Offset from start
  91. * @param int $limit Limit of number to get
  92. * @param int $since_id Since this notice
  93. * @param int $max_id Before this notice
  94. *
  95. * @return array IDs found
  96. */
  97. public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
  98. {
  99. $notice = new Notice();
  100. $notice->selectAdd();
  101. $notice->selectAdd('id');
  102. $notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created)));
  103. // Reply:: is a table of mentions
  104. // Subscription:: is a table of subscriptions (every user is subscribed to themselves)
  105. $notice->_join .= sprintf(
  106. "\n" . <<<'END'
  107. LEFT JOIN (
  108. SELECT id FROM notice
  109. WHERE profile_id
  110. IN (SELECT subscribed FROM subscription WHERE subscriber = %1$d)
  111. UNION ALL
  112. SELECT notice_id AS id FROM reply WHERE profile_id = %1$d
  113. UNION ALL
  114. SELECT notice_id AS id FROM attention WHERE profile_id = %1$d
  115. UNION ALL
  116. SELECT notice_id AS id FROM group_inbox INNER JOIN group_member USING (group_id)
  117. WHERE group_member.profile_id = %1$d
  118. ) AS t1 USING (id)
  119. END,
  120. $this->target->getID()
  121. );
  122. $notice->whereAdd('t1.id IS NOT NULL');
  123. if (!empty($since_id)) {
  124. $notice->whereAdd(sprintf('notice.id > %d', $since_id));
  125. }
  126. if (!empty($max_id)) {
  127. $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
  128. }
  129. self::filterVerbs($notice, $this->selectVerbs);
  130. // notice.id will give us even really old posts, which were recently
  131. // imported. For example if a remote instance had problems and just
  132. // managed to post here.
  133. $notice->orderBy('id DESC');
  134. $notice->limit($offset, $limit);
  135. if (!$notice->find()) {
  136. return [];
  137. }
  138. return $notice->fetchAll('id');
  139. }
  140. }