3 * Table Definition for fave
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
7 class Fave extends Memcached_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
18 function staticGet($k,$v=null)
19 { return Memcached_DataObject::staticGet('Fave',$k,$v); }
21 /* the code above is auto generated do not remove the tag below */
25 * Save a favorite record.
26 * @fixme post-author notification should be moved here
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
32 static function addNew(Profile $profile, Notice $notice) {
36 if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {
40 $fave->user_id = $profile->id;
41 $fave->notice_id = $notice->id;
43 if (!$fave->insert()) {
44 common_log_db_error($fave, 'INSERT', __FILE__);
47 self::blow('fave:by_notice', $fave->notice_id);
49 Event::handle('EndFavorNotice', array($profile, $notice));
57 $profile = Profile::staticGet('id', $this->user_id);
58 $notice = Notice::staticGet('id', $this->notice_id);
62 if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
64 $result = parent::delete();
65 self::blow('fave:by_notice', $this->notice_id);
68 Event::handle('EndDisfavorNotice', array($profile, $notice));
77 return Memcached_DataObject::pkeyGet('Fave', $kv);
80 function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
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);
91 * Note that the sorting for this is by order of *fave* not order of *notice*.
93 * @fixme add since_id, max_id support?
95 * @param <type> $user_id
97 * @param <type> $offset
98 * @param <type> $limit
99 * @param <type> $since_id
100 * @param <type> $max_id
103 function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id)
109 $qry = 'SELECT fave.* FROM fave ';
110 $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
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 . ' ';
118 if ($since_id != 0) {
119 $qry .= 'AND notice_id > ' . $since_id . ' ';
123 $qry .= 'AND notice_id <= ' . $max_id . ' ';
126 // NOTE: we sort by fave time, not by notice time!
128 $qry .= 'ORDER BY modified DESC ';
130 if (!is_null($offset)) {
131 $qry .= "LIMIT $limit OFFSET $offset";
138 while ($fav->fetch()) {
139 $ids[] = $fav->notice_id;
148 function asActivity()
150 $notice = Notice::staticGet('id', $this->notice_id);
151 $profile = Profile::staticGet('id', $this->user_id);
153 $act = new Activity();
155 $act->verb = ActivityVerb::FAVORITE;
157 // FIXME: rationalize this with URL below
159 $act->id = TagURI::mint('favor:%d:%d:%s',
162 common_date_iso8601($this->modified));
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(),
173 $act->actor = ActivityObject::fromProfile($profile);
174 $act->objects[] = ActivityObject::fromNotice($notice);
176 $url = common_local_url('AtomPubShowFavorite',
177 array('profile' => $this->user_id,
178 'notice' => $this->notice_id));
180 $act->selfLink = $url;
181 $act->editLink = $url;
187 * Fetch a stream of favorites by profile
189 * @param integer $profileId Profile that faved
190 * @param integer $offset Offset from last
191 * @param integer $limit Number to get
193 * @return mixed stream of faves, use fetch() to iterate
195 * @todo Cache results
196 * @todo integrate with Fave::stream()
199 static function byProfile($profileId, $offset, $limit)
203 $fav->user_id = $profileId;
205 $fav->orderBy('modified DESC');
207 $fav->limit($offset, $limit);
215 * Grab a list of profile who have favored this notice.
217 * @return ArrayWrapper masquerading as a Fave
219 static function byNotice($noticeId)
221 $c = self::memcache();
222 $key = Cache::key('fave:by_notice', $noticeId);
224 $wrapper = $c->get($key);
226 // @fixme caching & scalability!
228 $fave->notice_id = $noticeId;
232 while ($fave->fetch()) {
233 $list[] = clone($fave);
235 $wrapper = new ArrayWrapper($list);
236 $c->set($key, $wrapper);