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