]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Fave.php
Merge branch 'extprofile' into 0.9.x
[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
48             Event::handle('EndFavorNotice', array($profile, $notice));
49         }
50
51         return $fave;
52     }
53
54     function delete()
55     {
56         $profile = Profile::staticGet('id', $this->user_id);
57         $notice  = Notice::staticGet('id', $this->notice_id);
58
59         $result = null;
60
61         if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
62
63             $result = parent::delete();
64
65             if ($result) {
66                 Event::handle('EndDisfavorNotice', array($profile, $notice));
67             }
68         }
69
70         return $result;
71     }
72
73     function pkeyGet($kv)
74     {
75         return Memcached_DataObject::pkeyGet('Fave', $kv);
76     }
77
78     function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
79     {
80         $ids = Notice::stream(array('Fave', '_streamDirect'),
81                               array($user_id, $own),
82                               ($own) ? 'fave:ids_by_user_own:'.$user_id :
83                               'fave:ids_by_user:'.$user_id,
84                               $offset, $limit, $since_id, $max_id);
85         return $ids;
86     }
87
88     /**
89      * Note that the sorting for this is by order of *fave* not order of *notice*.
90      *
91      * @fixme add since_id, max_id support?
92      *
93      * @param <type> $user_id
94      * @param <type> $own
95      * @param <type> $offset
96      * @param <type> $limit
97      * @param <type> $since_id
98      * @param <type> $max_id
99      * @return <type>
100      */
101     function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id)
102     {
103         $fav = new Fave();
104         $qry = null;
105
106         if ($own) {
107             $qry  = 'SELECT fave.* FROM fave ';
108             $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
109         } else {
110              $qry =  'SELECT fave.* FROM fave ';
111              $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
112              $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
113              $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
114         }
115
116         if ($since_id != 0) {
117             $qry .= 'AND notice_id > ' . $since_id . ' ';
118         }
119
120         if ($max_id != 0) {
121             $qry .= 'AND notice_id <= ' . $max_id . ' ';
122         }
123
124         // NOTE: we sort by fave time, not by notice time!
125
126         $qry .= 'ORDER BY modified DESC ';
127
128         if (!is_null($offset)) {
129             $qry .= "LIMIT $limit OFFSET $offset";
130         }
131
132         $fav->query($qry);
133
134         $ids = array();
135
136         while ($fav->fetch()) {
137             $ids[] = $fav->notice_id;
138         }
139
140         $fav->free();
141         unset($fav);
142
143         return $ids;
144     }
145
146     function asActivity()
147     {
148         $notice  = Notice::staticGet('id', $this->notice_id);
149         $profile = Profile::staticGet('id', $this->user_id);
150
151         $act = new Activity();
152
153         $act->verb = ActivityVerb::FAVORITE;
154
155         // FIXME: rationalize this with URL below
156
157         $act->id   = TagURI::mint('favor:%d:%d:%s',
158                                   $profile->id,
159                                   $notice->id,
160                                   common_date_iso8601($this->modified));
161
162         $act->time    = strtotime($this->modified);
163         // TRANS: Activity title when marking a notice as favorite.
164         $act->title   = _("Favor");
165         // TRANS: Ntofication given when a user marks a notice as favorite.
166         // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
167         $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
168                                $profile->getBestName(),
169                                $notice->uri);
170
171         $act->actor     = ActivityObject::fromProfile($profile);
172         $act->objects[] = ActivityObject::fromNotice($notice);
173
174         $url = common_local_url('AtomPubShowFavorite',
175                                           array('profile' => $this->user_id,
176                                                 'notice'  => $this->notice_id));
177
178         $act->selfLink = $url;
179         $act->editLink = $url;
180
181         return $act;
182     }
183
184     /**
185      * Fetch a stream of favorites by profile
186      *
187      * @param integer $profileId Profile that faved
188      * @param integer $offset    Offset from last
189      * @param integer $limit     Number to get
190      *
191      * @return mixed stream of faves, use fetch() to iterate
192      *
193      * @todo Cache results
194      * @todo integrate with Fave::stream()
195      */
196
197     static function byProfile($profileId, $offset, $limit)
198     {
199         $fav = new Fave();
200
201         $fav->user_id = $profileId;
202
203         $fav->orderBy('modified DESC');
204
205         $fav->limit($offset, $limit);
206
207         $fav->find();
208
209         return $fav;
210     }
211 }