]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Fave.php
7cd64982cd8e818408bc56adc3dc6b1314301d7d
[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         $stream = new NoticeStream(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
87         return $stream->getNotices($offset, $limit, $since_id, $max_id);
88     }
89
90     function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
91     {
92         $stream = new NoticeStream(array('Fave', '_streamDirect'),
93                                    array($user_id, $own),
94                                    ($own) ? 'fave:ids_by_user_own:'.$user_id :
95                                    'fave:ids_by_user:'.$user_id);
96
97         return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
98     }
99
100     /**
101      * Note that the sorting for this is by order of *fave* not order of *notice*.
102      *
103      * @fixme add since_id, max_id support?
104      *
105      * @param <type> $user_id
106      * @param <type> $own
107      * @param <type> $offset
108      * @param <type> $limit
109      * @param <type> $since_id
110      * @param <type> $max_id
111      * @return <type>
112      */
113     function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id)
114     {
115         $fav = new Fave();
116         $qry = null;
117
118         if ($own) {
119             $qry  = 'SELECT fave.* FROM fave ';
120             $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
121         } else {
122              $qry =  'SELECT fave.* FROM fave ';
123              $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
124              $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
125              $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
126         }
127
128         if ($since_id != 0) {
129             $qry .= 'AND notice_id > ' . $since_id . ' ';
130         }
131
132         if ($max_id != 0) {
133             $qry .= 'AND notice_id <= ' . $max_id . ' ';
134         }
135
136         // NOTE: we sort by fave time, not by notice time!
137
138         $qry .= 'ORDER BY modified DESC ';
139
140         if (!is_null($offset)) {
141             $qry .= "LIMIT $limit OFFSET $offset";
142         }
143
144         $fav->query($qry);
145
146         $ids = array();
147
148         while ($fav->fetch()) {
149             $ids[] = $fav->notice_id;
150         }
151
152         $fav->free();
153         unset($fav);
154
155         return $ids;
156     }
157
158     function asActivity()
159     {
160         $notice  = Notice::staticGet('id', $this->notice_id);
161         $profile = Profile::staticGet('id', $this->user_id);
162
163         $act = new Activity();
164
165         $act->verb = ActivityVerb::FAVORITE;
166
167         // FIXME: rationalize this with URL below
168
169         $act->id   = TagURI::mint('favor:%d:%d:%s',
170                                   $profile->id,
171                                   $notice->id,
172                                   common_date_iso8601($this->modified));
173
174         $act->time    = strtotime($this->modified);
175         // TRANS: Activity title when marking a notice as favorite.
176         $act->title   = _("Favor");
177         // TRANS: Ntofication given when a user marks a notice as favorite.
178         // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
179         $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
180                                $profile->getBestName(),
181                                $notice->uri);
182
183         $act->actor     = ActivityObject::fromProfile($profile);
184         $act->objects[] = ActivityObject::fromNotice($notice);
185
186         $url = common_local_url('AtomPubShowFavorite',
187                                           array('profile' => $this->user_id,
188                                                 'notice'  => $this->notice_id));
189
190         $act->selfLink = $url;
191         $act->editLink = $url;
192
193         return $act;
194     }
195
196     /**
197      * Fetch a stream of favorites by profile
198      *
199      * @param integer $profileId Profile that faved
200      * @param integer $offset    Offset from last
201      * @param integer $limit     Number to get
202      *
203      * @return mixed stream of faves, use fetch() to iterate
204      *
205      * @todo Cache results
206      * @todo integrate with Fave::stream()
207      */
208
209     static function byProfile($profileId, $offset, $limit)
210     {
211         $fav = new Fave();
212
213         $fav->user_id = $profileId;
214
215         $fav->orderBy('modified DESC');
216
217         $fav->limit($offset, $limit);
218
219         $fav->find();
220
221         return $fav;
222     }
223
224     /**
225      * Grab a list of profile who have favored this notice.
226      *
227      * @return ArrayWrapper masquerading as a Fave
228      */
229     static function byNotice($noticeId)
230     {
231         $c = self::memcache();
232         $key = Cache::key('fave:by_notice:' . $noticeId);
233
234         $wrapper = $c->get($key);
235         if (!$wrapper) {
236             // @fixme caching & scalability!
237             $fave = new Fave();
238             $fave->notice_id = $noticeId;
239             $fave->find();
240
241             $list = array();
242             while ($fave->fetch()) {
243                 $list[] = clone($fave);
244             }
245             $wrapper = new ArrayWrapper($list);
246             $c->set($key, $wrapper);
247         }
248         return $wrapper;
249     }
250 }