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:%d', $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:%d', $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 $stream = new FaveNoticeStream($user_id, $own);
84 return $stream->getNotices($offset, $limit, $since_id, $max_id);
87 function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
89 $stream = new FaveNoticeStream($user_id, $own);
91 return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
96 $notice = Notice::staticGet('id', $this->notice_id);
97 $profile = Profile::staticGet('id', $this->user_id);
99 $act = new Activity();
101 $act->verb = ActivityVerb::FAVORITE;
103 // FIXME: rationalize this with URL below
105 $act->id = TagURI::mint('favor:%d:%d:%s',
108 common_date_iso8601($this->modified));
110 $act->time = strtotime($this->modified);
111 // TRANS: Activity title when marking a notice as favorite.
112 $act->title = _("Favor");
113 // TRANS: Ntofication given when a user marks a notice as favorite.
114 // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
115 $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
116 $profile->getBestName(),
119 $act->actor = ActivityObject::fromProfile($profile);
120 $act->objects[] = ActivityObject::fromNotice($notice);
122 $url = common_local_url('AtomPubShowFavorite',
123 array('profile' => $this->user_id,
124 'notice' => $this->notice_id));
126 $act->selfLink = $url;
127 $act->editLink = $url;
133 * Fetch a stream of favorites by profile
135 * @param integer $profileId Profile that faved
136 * @param integer $offset Offset from last
137 * @param integer $limit Number to get
139 * @return mixed stream of faves, use fetch() to iterate
141 * @todo Cache results
142 * @todo integrate with Fave::stream()
145 static function byProfile($profileId, $offset, $limit)
149 $fav->user_id = $profileId;
151 $fav->orderBy('modified DESC');
153 $fav->limit($offset, $limit);
161 * Grab a list of profile who have favored this notice.
163 * @return ArrayWrapper masquerading as a Fave
165 static function byNotice($noticeId)
167 $c = self::memcache();
168 $key = Cache::key('fave:by_notice:' . $noticeId);
170 $wrapper = $c->get($key);
172 // @fixme caching & scalability!
174 $fave->notice_id = $noticeId;
178 while ($fave->fetch()) {
179 $list[] = clone($fave);
181 $wrapper = new ArrayWrapper($list);
182 $c->set($key, $wrapper);