3 * Table Definition for fave
6 class Fave extends Managed_DataObject
8 public $__table = 'fave'; // table name
9 public $notice_id; // int(4) primary_key not_null
10 public $user_id; // int(4) primary_key not_null
11 public $uri; // varchar(255)
12 public $created; // datetime multiple_key not_null
13 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
15 public static function schemaDef()
19 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the favorite'),
20 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user who likes this notice'),
21 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier, usually a tag URI'),
22 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
23 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
25 'primary key' => array('notice_id', 'user_id'),
26 'unique keys' => array(
27 'fave_uri_key' => array('uri'),
29 'foreign keys' => array(
30 'fave_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
31 'fave_user_id_fkey' => array('profile', array('user_id' => 'id')), // note: formerly referenced notice.id, but we can now record remote users' favorites
34 'fave_notice_id_idx' => array('notice_id'),
35 'fave_user_id_idx' => array('user_id', 'modified'),
36 'fave_modified_idx' => array('modified'),
42 * Save a favorite record.
43 * @fixme post-author notification should be moved here
45 * @param Profile $profile the local or remote user who likes
46 * @param Notice $notice the notice that is liked
47 * @return mixed false on failure, or Fave record on success
49 static function addNew(Profile $profile, Notice $notice) {
53 if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {
57 $fave->user_id = $profile->id;
58 $fave->notice_id = $notice->id;
59 $fave->created = common_sql_now();
60 $fave->modified = common_sql_now();
61 $fave->uri = self::newUri($profile,
67 } catch (ServerException $e) {
68 common_log_db_error($fave, 'INSERT', __FILE__);
71 self::blowCacheForProfileId($fave->user_id);
72 self::blowCacheForNoticeId($fave->notice_id);
73 self::blow('popular');
75 Event::handle('EndFavorNotice', array($profile, $notice));
81 // exception throwing takeover!
82 public function insert()
84 if (!parent::insert()) {
85 throw new ServerException(sprintf(_m('Could not store new object of type %s'), get_called_class()));
87 self::blowCacheForProfileId($this->user_id);
88 self::blowCacheForNoticeId($this->notice_id);
92 public function delete($useWhere=false)
94 $profile = Profile::getKV('id', $this->user_id);
95 $notice = Notice::getKV('id', $this->notice_id);
99 if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
101 $result = parent::delete($useWhere);
103 self::blowCacheForProfileId($this->user_id);
104 self::blowCacheForNoticeId($this->notice_id);
105 self::blow('popular');
108 Event::handle('EndDisfavorNotice', array($profile, $notice));
115 function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
117 $stream = new FaveNoticeStream($user_id, $own);
119 return $stream->getNotices($offset, $limit, $since_id, $max_id);
122 function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
124 $stream = new FaveNoticeStream($user_id, $own);
126 return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
129 function asActivity()
131 $notice = $this->getTarget();
132 $profile = $this->getActor();
134 $act = new Activity();
136 $act->verb = ActivityVerb::FAVORITE;
138 // FIXME: rationalize this with URL below
140 $act->id = $this->getUri();
142 $act->time = strtotime($this->modified);
143 // TRANS: Activity title when marking a notice as favorite.
144 $act->title = _("Favor");
145 // TRANS: Ntofication given when a user marks a notice as favorite.
146 // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
147 $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
148 $profile->getBestName(),
151 $act->actor = $profile->asActivityObject();
152 // $act->target = $notice->asActivityObject();
153 // $act->objects = array(clone($act->target));
154 $act->objects[] = $notice->asActivityObject();
156 $url = common_local_url('AtomPubShowFavorite',
157 array('profile' => $profile->id,
158 'notice' => $notice->id));
160 $act->selfLink = $url;
161 $act->editLink = $url;
166 static function existsForProfile($notice, Profile $scoped)
168 $fave = self::pkeyGet(array('user_id'=>$scoped->id, 'notice_id'=>$notice->id));
170 return ($fave instanceof Fave);
174 * Fetch a stream of favorites by profile
176 * @param integer $profileId Profile that faved
177 * @param integer $offset Offset from last
178 * @param integer $limit Number to get
180 * @return mixed stream of faves, use fetch() to iterate
182 * @todo Cache results
183 * @todo integrate with Fave::stream()
186 static function byProfile($profileId, $offset, $limit)
190 $fav->user_id = $profileId;
192 $fav->orderBy('modified DESC');
194 $fav->limit($offset, $limit);
201 static function countByProfile(Profile $profile)
203 $c = Cache::instance();
205 $cnt = $c->get(Cache::key('fave:count_by_profile:'.$profile->id));
206 if (is_integer($cnt)) {
212 $faves->user_id = $profile->id;
213 $cnt = (int) $faves->count('notice_id');
216 $c->set(Cache::key('fave:count_by_profile:'.$profile->id), $cnt);
222 static protected $_faves = array();
225 * All faves of this notice
227 * @param Notice $notice A notice we wish to get faves for (may still be ArrayWrapper)
229 * @return array Array of Fave objects
231 static public function byNotice($notice)
233 if (!isset(self::$_faves[$notice->id])) {
234 self::fillFaves(array($notice->id));
236 return self::$_faves[$notice->id];
239 static public function fillFaves(array $notice_ids)
241 $faveMap = Fave::listGet('notice_id', $notice_ids);
242 self::$_faves = array_replace(self::$_faves, $faveMap);
245 static public function blowCacheForProfileId($profile_id)
247 $cache = Cache::instance();
249 // Faves don't happen chronologically, so we need to blow
251 $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id));
252 $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id.';last'));
253 $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id));
254 $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id.';last'));
255 $cache->delete(Cache::key('fave:count_by_profile:'.$profile_id));
258 static public function blowCacheForNoticeId($notice_id)
260 $cache = Cache::instance();
262 $cache->delete(Cache::key('fave:list-ids:notice_id:'.$notice_id));
266 // Remember that we want the _activity_ notice here, not faves applied
267 // to the supplied Notice (as with byNotice)!
268 static public function fromStored(Notice $stored)
270 $class = get_called_class();
271 $object = new $class;
272 $object->uri = $stored->uri;
273 if (!$object->find(true)) {
274 throw new NoResultException($object);
279 static public function getObjectType()
284 public function asActivityObject(Profile $scoped=null)
286 $actobj = new ActivityObject();
287 $actobj->id = $this->getUri();
288 $actobj->type = ActivityUtils::resolveUri(self::getObjectType());
289 $actobj->actor = $this->getActorObject();
290 $actobj->target = $this->getTarget()->asActivityObject();
291 $actobj->objects = array(clone($actobj->target));
292 $actobj->verb = ActivityVerb::FAVORITE;
293 $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
298 * @param ActivityObject $actobj The _favored_ notice (which we're "in-reply-to")
299 * @param Notice $stored The _activity_ notice, i.e. the favor itself.
301 static public function parseActivityObject(ActivityObject $actobj, Notice $stored)
303 $local = ActivityUtils::findLocalObject($actobj->getIdentifiers());
304 if (!$local instanceof Notice) {
305 // $local always returns something, but this was not what we expected. Something is wrong.
306 throw new Exception('Something other than a Notice was returned from findLocalObject');
309 $actor = $stored->getProfile();
310 $object = new Fave();
311 $object->user_id = $stored->getProfile()->id;
312 $object->notice_id = $local->id;
313 $object->uri = $stored->uri;
314 $object->created = $stored->created;
315 $object->modified = $stored->modified;
319 static function saveActivityObject(ActivityObject $actobj, Notice $stored)
321 $object = self::parseActivityObject($actobj, $stored);
322 $object->insert(); // exception throwing!
323 Event::handle('EndFavorNotice', array($stored->getProfile(), $object->getTarget()));
328 public function getTarget()
330 // throws exception on failure
331 $target = new Notice();
332 $target->id = $this->notice_id;
333 if (!$target->find(true)) {
334 throw new NoResultException($target);
340 public function getTargetObject()
342 return $this->getTarget()->asActivityObject();
345 protected $_stored = array();
347 public function getStored()
349 if (!isset($this->_stored[$this->uri])) {
350 $stored = new Notice();
351 $stored->uri = $this->uri;
352 if (!$stored->find(true)) {
353 throw new NoResultException($stored);
355 $this->_stored[$this->uri] = $stored;
357 return $this->_stored[$this->uri];
360 public function getActor()
362 $profile = new Profile();
363 $profile->id = $this->user_id;
364 if (!$profile->find(true)) {
365 throw new NoResultException($profile);
370 public function getActorObject()
372 return $this->getActor()->asActivityObject();
375 public function getUri()
377 if (!empty($this->uri)) {
381 // We (should've in this case) created it ourselves, so we tag it ourselves
382 return self::newUri($this->getActor(), $this->getTarget(), $this->created);
385 static function newUri(Profile $actor, Managed_DataObject $target, $created=null)
387 if (is_null($created)) {
388 $created = common_sql_now();
390 return TagURI::mint(strtolower(get_called_class()).':%d:%s:%d:%s',
392 ActivityUtils::resolveUri(self::getObjectType(), true),
394 common_date_iso8601($created));