inboxnoticestream.php 4.7 KB

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