]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Fave.php
efbceee6a83ae8de1b69d8e49ff2095cacf97419
[quix0rs-gnu-social.git] / classes / Fave.php
1 <?php
2 /**
3  * Table Definition for fave
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Fave extends Memcached_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'fave';                            // table name
13     public $notice_id;                       // int(4)  primary_key not_null
14     public $user_id;                         // int(4)  primary_key not_null
15     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
16
17     /* Static get */
18     function staticGet($k,$v=null)
19     { return Memcached_DataObject::staticGet('Fave',$k,$v); }
20
21     /* the code above is auto generated do not remove the tag below */
22     ###END_AUTOCODE
23
24     /**
25      * Save a favorite record.
26      * @fixme post-author notification should be moved here
27      *
28      * @param Profile $profile the local or remote user who likes
29      * @param Notice $notice the notice that is liked
30      * @return mixed false on failure, or Fave record on success
31      */
32     static function addNew(Profile $profile, Notice $notice) {
33
34         $fave = null;
35
36         if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {
37
38             $fave = new Fave();
39
40             $fave->user_id   = $profile->id;
41             $fave->notice_id = $notice->id;
42
43             if (!$fave->insert()) {
44                 common_log_db_error($fave, 'INSERT', __FILE__);
45                 return false;
46             }
47             self::blow('fave:by_notice:%d', $fave->notice_id);
48
49             Event::handle('EndFavorNotice', array($profile, $notice));
50         }
51
52         return $fave;
53     }
54
55     function delete()
56     {
57         $profile = Profile::staticGet('id', $this->user_id);
58         $notice  = Notice::staticGet('id', $this->notice_id);
59
60         $result = null;
61
62         if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
63
64             $result = parent::delete();
65             self::blow('fave:by_notice:%d', $this->notice_id);
66
67             if ($result) {
68                 Event::handle('EndDisfavorNotice', array($profile, $notice));
69             }
70         }
71
72         return $result;
73     }
74
75     function pkeyGet($kv)
76     {
77         return Memcached_DataObject::pkeyGet('Fave', $kv);
78     }
79
80     function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
81     {
82         $ids = Notice::stream(array('Fave', '_streamDirect'),
83                               array($user_id, $own),
84                               ($own) ? 'fave:ids_by_user_own:'.$user_id :
85                               'fave:ids_by_user:'.$user_id,
86                               $offset, $limit, $since_id, $max_id);
87         return $ids;
88     }
89
90     /**
91      * Note that the sorting for this is by order of *fave* not order of *notice*.
92      *
93      * @fixme add since_id, max_id support?
94      *
95      * @param <type> $user_id
96      * @param <type> $own
97      * @param <type> $offset
98      * @param <type> $limit
99      * @param <type> $since_id
100      * @param <type> $max_id
101      * @return <type>
102      */
103     function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id)
104     {
105         $fav = new Fave();
106         $qry = null;
107
108         if ($own) {
109             $qry  = 'SELECT fave.* FROM fave ';
110             $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
111         } else {
112              $qry =  'SELECT fave.* FROM fave ';
113              $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
114              $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
115              $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
116         }
117
118         if ($since_id != 0) {
119             $qry .= 'AND notice_id > ' . $since_id . ' ';
120         }
121
122         if ($max_id != 0) {
123             $qry .= 'AND notice_id <= ' . $max_id . ' ';
124         }
125
126         // NOTE: we sort by fave time, not by notice time!
127
128         $qry .= 'ORDER BY modified DESC ';
129
130         if (!is_null($offset)) {
131             $qry .= "LIMIT $limit OFFSET $offset";
132         }
133
134         $fav->query($qry);
135
136         $ids = array();
137
138         while ($fav->fetch()) {
139             $ids[] = $fav->notice_id;
140         }
141
142         $fav->free();
143         unset($fav);
144
145         return $ids;
146     }
147
148     function asActivity()
149     {
150         $notice  = Notice::staticGet('id', $this->notice_id);
151         $profile = Profile::staticGet('id', $this->user_id);
152
153         $act = new Activity();
154
155         $act->verb = ActivityVerb::FAVORITE;
156
157         // FIXME: rationalize this with URL below
158
159         $act->id   = TagURI::mint('favor:%d:%d:%s',
160                                   $profile->id,
161                                   $notice->id,
162                                   common_date_iso8601($this->modified));
163
164         $act->time    = strtotime($this->modified);
165         // TRANS: Activity title when marking a notice as favorite.
166         $act->title   = _("Favor");
167         // TRANS: Ntofication given when a user marks a notice as favorite.
168         // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
169         $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
170                                $profile->getBestName(),
171                                $notice->uri);
172
173         $act->actor     = ActivityObject::fromProfile($profile);
174         $act->objects[] = ActivityObject::fromNotice($notice);
175
176         $url = common_local_url('AtomPubShowFavorite',
177                                           array('profile' => $this->user_id,
178                                                 'notice'  => $this->notice_id));
179
180         $act->selfLink = $url;
181         $act->editLink = $url;
182
183         return $act;
184     }
185
186     /**
187      * Fetch a stream of favorites by profile
188      *
189      * @param integer $profileId Profile that faved
190      * @param integer $offset    Offset from last
191      * @param integer $limit     Number to get
192      *
193      * @return mixed stream of faves, use fetch() to iterate
194      *
195      * @todo Cache results
196      * @todo integrate with Fave::stream()
197      */
198
199     static function byProfile($profileId, $offset, $limit)
200     {
201         $fav = new Fave();
202
203         $fav->user_id = $profileId;
204
205         $fav->orderBy('modified DESC');
206
207         $fav->limit($offset, $limit);
208
209         $fav->find();
210
211         return $fav;
212     }
213
214     /**
215      * Grab a list of profile who have favored this notice.
216      *
217      * @return ArrayWrapper masquerading as a Fave
218      */
219     static function byNotice($noticeId)
220     {
221         $c = self::memcache();
222         $key = Cache::key('fave:by_notice:' . $noticeId);
223
224         $wrapper = $c->get($key);
225         if (!$wrapper) {
226             // @fixme caching & scalability!
227             $fave = new Fave();
228             $fave->notice_id = $noticeId;
229             $fave->find();
230
231             $list = array();
232             while ($fave->fetch()) {
233                 $list[] = clone($fave);
234             }
235             $wrapper = new ArrayWrapper($list);
236             $c->set($key, $wrapper);
237         }
238         return $wrapper;
239     }
240 }