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