favenoticestream.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Notice stream for favorites
  37. *
  38. * @category Stream
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class FaveNoticeStream extends ScopingNoticeStream
  46. {
  47. function __construct($user_id, $own, $profile = -1)
  48. {
  49. $stream = new RawFaveNoticeStream($user_id, $own);
  50. if ($own) {
  51. $key = 'fave:ids_by_user_own:'.$user_id;
  52. } else {
  53. $key = 'fave:ids_by_user:'.$user_id;
  54. }
  55. if (is_int($profile) && $profile == -1) {
  56. $profile = Profile::current();
  57. }
  58. parent::__construct(new CachingNoticeStream($stream, $key),
  59. $profile);
  60. }
  61. }
  62. /**
  63. * Raw notice stream for favorites
  64. *
  65. * @category Stream
  66. * @package StatusNet
  67. * @author Evan Prodromou <evan@status.net>
  68. * @copyright 2011 StatusNet, Inc.
  69. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  70. * @link http://status.net/
  71. */
  72. class RawFaveNoticeStream extends NoticeStream
  73. {
  74. protected $user_id;
  75. protected $own;
  76. function __construct($user_id, $own)
  77. {
  78. parent::__construct();
  79. $this->user_id = $user_id;
  80. $this->own = $own;
  81. $this->selectVerbs = array();
  82. }
  83. /**
  84. * Note that the sorting for this is by order of *fave* not order of *notice*.
  85. *
  86. * @fixme add since_id, max_id support?
  87. *
  88. * @param <type> $user_id
  89. * @param <type> $own
  90. * @param <type> $offset
  91. * @param <type> $limit
  92. * @param <type> $since_id
  93. * @param <type> $max_id
  94. * @return <type>
  95. */
  96. function getNoticeIds($offset, $limit, $since_id, $max_id)
  97. {
  98. $fav = new Fave();
  99. $qry = null;
  100. if ($this->own) {
  101. $qry = 'SELECT fave.* FROM fave ';
  102. $qry .= 'WHERE fave.user_id = ' . $this->user_id . ' ';
  103. } else {
  104. $qry = 'SELECT fave.* FROM fave ';
  105. $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
  106. $qry .= 'WHERE fave.user_id = ' . $this->user_id . ' ';
  107. $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
  108. }
  109. if ($since_id != 0) {
  110. $qry .= 'AND notice_id > ' . $since_id . ' ';
  111. }
  112. if ($max_id != 0) {
  113. $qry .= 'AND notice_id <= ' . $max_id . ' ';
  114. }
  115. // NOTE: we sort by fave time, not by notice time!
  116. $qry .= 'ORDER BY modified DESC ';
  117. if (!is_null($offset)) {
  118. $qry .= "LIMIT $limit OFFSET $offset";
  119. }
  120. $fav->query($qry);
  121. $ids = array();
  122. while ($fav->fetch()) {
  123. $ids[] = $fav->notice_id;
  124. }
  125. $fav->free();
  126. unset($fav);
  127. return $ids;
  128. }
  129. }