favenoticestream.php 3.9 KB

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