favenoticestream.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Notice stream for favorites
  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 Stream
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Notice stream for favorites
  33. *
  34. * @category Stream
  35. * @package StatusNet
  36. * @author Evan Prodromou <evan@status.net>
  37. * @copyright 2011 StatusNet, Inc.
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  39. * @link http://status.net/
  40. */
  41. class FaveNoticeStream extends ScopingNoticeStream
  42. {
  43. function __construct(Profile $target, Profile $scoped=null)
  44. {
  45. $stream = new RawFaveNoticeStream($target, $scoped);
  46. if ($target->sameAs($scoped)) {
  47. $key = 'fave:ids_by_user_own:'.$target->getID();
  48. } else {
  49. $key = 'fave:ids_by_user:'.$target->getID();
  50. }
  51. parent::__construct(new CachingNoticeStream($stream, $key), $scoped);
  52. }
  53. }
  54. /**
  55. * Raw notice stream for favorites
  56. *
  57. * @category Stream
  58. * @package StatusNet
  59. * @author Evan Prodromou <evan@status.net>
  60. * @copyright 2011 StatusNet, Inc.
  61. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  62. * @link http://status.net/
  63. */
  64. class RawFaveNoticeStream extends NoticeStream
  65. {
  66. protected $user_id;
  67. protected $own;
  68. protected $selectVerbs = array();
  69. function __construct(Profile $target, Profile $scoped=null)
  70. {
  71. parent::__construct();
  72. $this->user_id = $target->getID();
  73. $this->own = $target->sameAs($scoped);
  74. }
  75. /**
  76. * Note that the sorting for this is by order of *fave* not order of *notice*.
  77. *
  78. * @fixme add since_id, max_id support?
  79. *
  80. * @param <type> $user_id
  81. * @param <type> $own
  82. * @param <type> $offset
  83. * @param <type> $limit
  84. * @param <type> $since_id
  85. * @param <type> $max_id
  86. * @return <type>
  87. */
  88. function getNoticeIds($offset, $limit, $since_id, $max_id)
  89. {
  90. $fav = new Fave();
  91. $qry = null;
  92. if ($this->own) {
  93. $qry = 'SELECT fave.* FROM fave ';
  94. $qry .= 'WHERE fave.user_id = ' . $this->user_id . ' ';
  95. } else {
  96. $qry = 'SELECT fave.* FROM fave ';
  97. $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
  98. $qry .= 'WHERE fave.user_id = ' . $this->user_id . ' ';
  99. $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
  100. }
  101. if ($since_id != 0) {
  102. $qry .= 'AND notice_id > ' . $since_id . ' ';
  103. }
  104. if ($max_id != 0) {
  105. $qry .= 'AND notice_id <= ' . $max_id . ' ';
  106. }
  107. // NOTE: we sort by fave time, not by notice time!
  108. $qry .= 'ORDER BY modified DESC ';
  109. if (!is_null($offset)) {
  110. $qry .= "LIMIT $limit OFFSET $offset";
  111. }
  112. $fav->query($qry);
  113. $ids = array();
  114. while ($fav->fetch()) {
  115. $ids[] = $fav->notice_id;
  116. }
  117. $fav->free();
  118. unset($fav);
  119. return $ids;
  120. }
  121. }