]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Favorite/lib/favenoticestream.php
Use NoticeStream::filterVerbs for filtering in noticestreams
[quix0rs-gnu-social.git] / plugins / Favorite / 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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Notice stream for favorites
35  *
36  * @category  Stream
37  * @package   StatusNet
38  * @author    Evan Prodromou <evan@status.net>
39  * @copyright 2011 StatusNet, Inc.
40  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41  * @link      http://status.net/
42  */
43 class FaveNoticeStream extends ScopingNoticeStream
44 {
45     function __construct($user_id, $own, $profile = -1)
46     {
47         $stream = new RawFaveNoticeStream($user_id, $own);
48         if ($own) {
49             $key = 'fave:ids_by_user_own:'.$user_id;
50         } else {
51             $key = 'fave:ids_by_user:'.$user_id;
52         }
53         if (is_int($profile) && $profile == -1) {
54             $profile = Profile::current();
55         }
56         parent::__construct(new CachingNoticeStream($stream, $key),
57                             $profile);
58     }
59 }
60
61 /**
62  * Raw notice stream for favorites
63  *
64  * @category  Stream
65  * @package   StatusNet
66  * @author    Evan Prodromou <evan@status.net>
67  * @copyright 2011 StatusNet, Inc.
68  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
69  * @link      http://status.net/
70  */
71 class RawFaveNoticeStream extends NoticeStream
72 {
73     protected $user_id;
74     protected $own;
75
76     protected $selectVerbs = array();
77
78     function __construct($user_id, $own)
79     {
80         parent::__construct();
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