inboxnoticestream.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Sort in descending order as id will give us even really old posts,
  106. // which were recently imported. For example, if a remote instance had
  107. // problems and just managed to post here.
  108. $notice->whereAdd(
  109. sprintf('id IN (SELECT DISTINCT id FROM (' .
  110. '(SELECT id FROM notice WHERE profile_id IN (SELECT subscribed FROM subscription WHERE subscriber = %1$d)) UNION ' .
  111. '(SELECT notice_id AS id FROM reply WHERE profile_id = %1$d) UNION ' .
  112. '(SELECT notice_id AS id FROM attention WHERE profile_id = %1$d) UNION ' .
  113. '(SELECT notice_id AS id FROM group_inbox WHERE group_id IN (SELECT group_id FROM group_member WHERE profile_id = %1$d)) ' .
  114. 'ORDER BY id DESC) AS T)',
  115. $this->target->getID())
  116. );
  117. if (!empty($since_id)) {
  118. $notice->whereAdd(sprintf('notice.id > %d', $since_id));
  119. }
  120. if (!empty($max_id)) {
  121. $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
  122. }
  123. self::filterVerbs($notice, $this->selectVerbs);
  124. $notice->limit($offset, $limit);
  125. if (!$notice->find()) {
  126. return [];
  127. }
  128. return $notice->fetchAll('id');
  129. }
  130. }