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 $actor the local or remote Profile who favorites
46 * @param Notice $target the notice that is favorited
47 * @return Fave record on success
48 * @throws Exception on failure
50 static function addNew(Profile $actor, Notice $target) {
51 $act = new Activity();
52 $act->verb = ActivityVerb::FAVORITE;
54 $act->id = self::newUri($actor, $target, common_sql_date($act->time));
55 $act->title = _("Favor");
56 // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
57 // notice's nickname and %3$s is the content of the favorited notice.)
58 $act->content = sprintf(_('%1$s favorited something by %2$s: %3$s'),
59 $actor->getNickname(), $target->getProfile()->getNickname(),
60 $target->rendered ?: $target->content);
61 $act->actor = $actor->asActivityObject();
62 $act->target = $target->asActivityObject();
63 $act->objects = array(clone($act->target));
65 $url = common_local_url('AtomPubShowFavorite', array('profile'=>$actor->id, 'notice'=>$target->id));
66 $act->selfLink = $url;
67 $act->editLink = $url;
69 // saveActivity will in turn also call Fave::saveActivityObject which does
70 // what this function used to do before this commit.
71 $stored = Notice::saveActivity($act, $actor);
76 // exception throwing takeover!
77 public function insert()
79 if (parent::insert()===false) {
80 common_log_db_error($this, 'INSERT', __FILE__);
81 throw new ServerException(sprintf(_m('Could not store new object of type %s'), get_called_class()));
83 self::blowCacheForProfileId($this->user_id);
84 self::blowCacheForNoticeId($this->notice_id);
88 public function delete($useWhere=false)
90 $profile = Profile::getKV('id', $this->user_id);
91 $notice = Notice::getKV('id', $this->notice_id);
95 if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
97 $result = parent::delete($useWhere);
99 self::blowCacheForProfileId($this->user_id);
100 self::blowCacheForNoticeId($this->notice_id);
101 self::blow('popular');
104 Event::handle('EndDisfavorNotice', array($profile, $notice));
111 function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
113 $stream = new FaveNoticeStream($user_id, $own);
115 return $stream->getNotices($offset, $limit, $since_id, $max_id);
118 function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
120 $stream = new FaveNoticeStream($user_id, $own);
122 return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
125 function asActivity()
127 $target = $this->getTarget();
128 $actor = $this->getActor();
130 $act = new Activity();
132 $act->verb = ActivityVerb::FAVORITE;
134 // FIXME: rationalize this with URL below
136 $act->id = $this->getUri();
138 $act->time = strtotime($this->created);
139 // TRANS: Activity title when marking a notice as favorite.
140 $act->title = _("Favor");
141 // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
142 // notice's nickname and %3$s is the content of the favorited notice.)
143 $act->content = sprintf(_('%1$s favorited something by %2$s: %3$s'),
144 $actor->getNickname(), $target->getProfile()->getNickname(),
145 $target->rendered ?: $target->content);
147 $act->actor = $actor->asActivityObject();
148 $act->target = $target->asActivityObject();
149 $act->objects = array(clone($act->target));
151 $url = common_local_url('AtomPubShowFavorite',
152 array('profile' => $actor->id,
153 'notice' => $target->id));
155 $act->selfLink = $url;
156 $act->editLink = $url;
161 static function existsForProfile($notice, Profile $scoped)
163 $fave = self::pkeyGet(array('user_id'=>$scoped->id, 'notice_id'=>$notice->id));
165 return ($fave instanceof Fave);
169 * Fetch a stream of favorites by profile
171 * @param integer $profileId Profile that faved
172 * @param integer $offset Offset from last
173 * @param integer $limit Number to get
175 * @return mixed stream of faves, use fetch() to iterate
177 * @todo Cache results
178 * @todo integrate with Fave::stream()
181 static function byProfile($profileId, $offset, $limit)
185 $fav->user_id = $profileId;
187 $fav->orderBy('modified DESC');
189 $fav->limit($offset, $limit);
196 static function countByProfile(Profile $profile)
198 $c = Cache::instance();
200 $cnt = $c->get(Cache::key('fave:count_by_profile:'.$profile->id));
201 if (is_integer($cnt)) {
207 $faves->user_id = $profile->id;
208 $cnt = (int) $faves->count('notice_id');
211 $c->set(Cache::key('fave:count_by_profile:'.$profile->id), $cnt);
217 static protected $_faves = array();
220 * All faves of this notice
222 * @param Notice $notice A notice we wish to get faves for (may still be ArrayWrapper)
224 * @return array Array of Fave objects
226 static public function byNotice($notice)
228 if (!isset(self::$_faves[$notice->id])) {
229 self::fillFaves(array($notice->id));
231 return self::$_faves[$notice->id];
234 static public function fillFaves(array $notice_ids)
236 $faveMap = Fave::listGet('notice_id', $notice_ids);
237 self::$_faves = array_replace(self::$_faves, $faveMap);
240 static public function blowCacheForProfileId($profile_id)
242 $cache = Cache::instance();
244 // Faves don't happen chronologically, so we need to blow
246 $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id));
247 $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id.';last'));
248 $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id));
249 $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id.';last'));
250 $cache->delete(Cache::key('fave:count_by_profile:'.$profile_id));
253 static public function blowCacheForNoticeId($notice_id)
255 $cache = Cache::instance();
257 $cache->delete(Cache::key('fave:list-ids:notice_id:'.$notice_id));
261 // Remember that we want the _activity_ notice here, not faves applied
262 // to the supplied Notice (as with byNotice)!
263 static public function fromStored(Notice $stored)
265 $class = get_called_class();
266 $object = new $class;
267 $object->uri = $stored->uri;
268 if (!$object->find(true)) {
269 throw new NoResultException($object);
275 * Retrieves the _targeted_ notice of a verb (such as the notice that was
276 * _favorited_, but not the favorite activity itself).
278 * @param Notice $stored The activity notice.
280 * @throws NoResultException when it can't find what it's looking for.
282 static public function getTargetFromStored(Notice $stored)
284 return self::fromStored($stored)->getTarget();
287 static public function getObjectType()
292 public function asActivityObject(Profile $scoped=null)
294 $actobj = new ActivityObject();
295 $actobj->id = $this->getUri();
296 $actobj->type = ActivityUtils::resolveUri(self::getObjectType());
297 $actobj->actor = $this->getActorObject();
298 $actobj->target = $this->getTargetObject();
299 $actobj->objects = array(clone($actobj->target));
300 $actobj->verb = ActivityVerb::FAVORITE;
301 $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
302 $actobj->content = $this->getTarget()->rendered ?: $this->getTarget()->content;
307 * @param ActivityObject $actobj The _favored_ notice (which we're "in-reply-to")
308 * @param Notice $stored The _activity_ notice, i.e. the favor itself.
310 static public function parseActivityObject(ActivityObject $actobj, Notice $stored)
312 $local = ActivityUtils::findLocalObject($actobj->getIdentifiers());
313 if (!$local instanceof Notice) {
314 // $local always returns something, but this was not what we expected. Something is wrong.
315 throw new Exception('Something other than a Notice was returned from findLocalObject');
318 $actor = $stored->getProfile();
319 $object = new Fave();
320 $object->user_id = $stored->getProfile()->id;
321 $object->notice_id = $local->id;
322 $object->uri = $stored->uri;
323 $object->created = $stored->created;
324 $object->modified = $stored->modified;
328 static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
330 $target = self::getTargetFromStored($stored);
332 // The following logic was copied from StatusNet's Activity plugin
333 if (ActivityUtils::compareTypes($target->verb, array(ActivityVerb::POST))) {
334 // "I like the thing you posted"
335 $act->objects = $target->asActivity()->objects;
337 // "I like that you did whatever you did"
338 $act->target = $target->asActivityObject();
339 $act->objects = array(clone($act->target));
341 $act->context->replyToID = $target->getUri();
342 $act->context->replyToUrl = $target->getUrl();
343 $act->title = ActivityUtils::verbToTitle($act->verb);
346 static function saveActivityObject(ActivityObject $actobj, Notice $stored)
348 $object = self::parseActivityObject($actobj, $stored);
349 $object->insert(); // exception throwing in Fave's case!
351 self::blowCacheForProfileId($object->user_id);
352 self::blowCacheForNoticeId($object->notice_id);
353 self::blow('popular');
355 Event::handle('EndFavorNotice', array($stored->getProfile(), $object->getTarget()));
359 public function getAttentionArray() {
360 // not all objects can/should carry attentions, so we don't require extending this
361 // the format should be an array with URIs to mentioned profiles
365 public function getTarget()
367 // throws exception on failure
368 $target = new Notice();
369 $target->id = $this->notice_id;
370 if (!$target->find(true)) {
371 throw new NoResultException($target);
377 public function getTargetObject()
379 return $this->getTarget()->asActivityObject();
382 protected $_stored = array();
384 public function getStored()
386 if (!isset($this->_stored[$this->uri])) {
387 $stored = new Notice();
388 $stored->uri = $this->uri;
389 if (!$stored->find(true)) {
390 throw new NoResultException($stored);
392 $this->_stored[$this->uri] = $stored;
394 return $this->_stored[$this->uri];
397 public function getActor()
399 $profile = new Profile();
400 $profile->id = $this->user_id;
401 if (!$profile->find(true)) {
402 throw new NoResultException($profile);
407 public function getActorObject()
409 return $this->getActor()->asActivityObject();
412 public function getUri()
414 if (!empty($this->uri)) {
418 // We (should've in this case) created it ourselves, so we tag it ourselves
419 return self::newUri($this->getActor(), $this->getTarget(), $this->created);
422 static function newUri(Profile $actor, Managed_DataObject $target, $created=null)
424 if (is_null($created)) {
425 $created = common_sql_now();
427 return TagURI::mint(strtolower(get_called_class()).':%d:%s:%d:%s',
429 ActivityUtils::resolveUri(self::getObjectType(), true),
431 common_date_iso8601($created));