3 * Table Definition for fave
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
7 class Fave extends Managed_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 $uri; // varchar(255)
16 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
18 /* the code above is auto generated do not remove the tag below */
21 public static function schemaDef()
25 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the favorite'),
26 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user who likes this notice'),
27 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier, usually a tag URI'),
28 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
30 'primary key' => array('notice_id', 'user_id'),
31 'unique keys' => array(
32 'fave_uri_key' => array('uri'),
34 'foreign keys' => array(
35 'fave_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
36 'fave_user_id_fkey' => array('profile', array('user_id' => 'id')), // note: formerly referenced notice.id, but we can now record remote users' favorites
39 'fave_notice_id_idx' => array('notice_id'),
40 'fave_user_id_idx' => array('user_id', 'modified'),
41 'fave_modified_idx' => array('modified'),
47 * Save a favorite record.
48 * @fixme post-author notification should be moved here
50 * @param Profile $profile the local or remote user who likes
51 * @param Notice $notice the notice that is liked
52 * @return mixed false on failure, or Fave record on success
54 static function addNew(Profile $profile, Notice $notice) {
58 if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {
62 $fave->user_id = $profile->id;
63 $fave->notice_id = $notice->id;
64 $fave->modified = common_sql_now();
65 $fave->uri = self::newURI($fave->user_id,
68 if (!$fave->insert()) {
69 common_log_db_error($fave, 'INSERT', __FILE__);
72 self::blowCacheForProfileId($fave->user_id);
73 self::blowCacheForNoticeId($fave->notice_id);
74 self::blow('popular');
76 Event::handle('EndFavorNotice', array($profile, $notice));
82 function delete($useWhere=false)
84 $profile = Profile::getKV('id', $this->user_id);
85 $notice = Notice::getKV('id', $this->notice_id);
89 if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
91 $result = parent::delete($useWhere);
93 self::blowCacheForProfileId($this->user_id);
94 self::blowCacheForNoticeId($this->notice_id);
95 self::blow('popular');
98 Event::handle('EndDisfavorNotice', array($profile, $notice));
105 function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
107 $stream = new FaveNoticeStream($user_id, $own);
109 return $stream->getNotices($offset, $limit, $since_id, $max_id);
112 function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
114 $stream = new FaveNoticeStream($user_id, $own);
116 return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
119 function asActivity()
121 $notice = Notice::getKV('id', $this->notice_id);
124 throw new Exception("Fave for non-existent notice: " . $this->notice_id);
127 $profile = Profile::getKV('id', $this->user_id);
130 throw new Exception("Fave by non-existent profile: " . $this->user_id);
133 $act = new Activity();
135 $act->verb = ActivityVerb::FAVORITE;
137 // FIXME: rationalize this with URL below
139 $act->id = $this->getURI();
141 $act->time = strtotime($this->modified);
142 // TRANS: Activity title when marking a notice as favorite.
143 $act->title = _("Favor");
144 // TRANS: Ntofication given when a user marks a notice as favorite.
145 // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
146 $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
147 $profile->getBestName(),
150 $act->actor = ActivityObject::fromProfile($profile);
151 $act->objects[] = ActivityObject::fromNotice($notice);
153 $url = common_local_url('AtomPubShowFavorite',
154 array('profile' => $this->user_id,
155 'notice' => $this->notice_id));
157 $act->selfLink = $url;
158 $act->editLink = $url;
163 static function existsForProfile($notice, Profile $scoped) {
164 $fave = self::pkeyGet(array('user_id'=>$scoped->id, 'notice_id'=>$notice->id));
166 return ($fave instanceof Fave);
170 * Fetch a stream of favorites by profile
172 * @param integer $profileId Profile that faved
173 * @param integer $offset Offset from last
174 * @param integer $limit Number to get
176 * @return mixed stream of faves, use fetch() to iterate
178 * @todo Cache results
179 * @todo integrate with Fave::stream()
182 static function byProfile($profileId, $offset, $limit)
186 $fav->user_id = $profileId;
188 $fav->orderBy('modified DESC');
190 $fav->limit($offset, $limit);
197 static function countByProfile(Profile $profile)
199 $c = Cache::instance();
201 $cnt = $c->get(Cache::key('fave:count_by_profile:'.$profile->id));
202 if (is_integer($cnt)) {
208 $faves->user_id = $profile->id;
209 $cnt = (int) $faves->count('notice_id');
212 $c->set(Cache::key('fave:count_by_profile:'.$profile->id), $cnt);
220 if (!empty($this->uri)) {
223 return self::newURI($this->user_id, $this->notice_id, $this->modified);
227 static function newURI($profile_id, $notice_id, $modified)
229 return TagURI::mint('favor:%d:%d:%s',
232 common_date_iso8601($modified));
236 static protected $_faves = array();
239 * All faves of this notice
241 * @param Notice $notice A notice we wish to get faves for (may still be ArrayWrapper)
243 * @return array Array of Fave objects
245 static public function byNotice($notice)
247 if (!isset(self::$_faves[$notice->id])) {
248 self::fillFaves(array($notice->id));
250 return self::$_faves[$notice->id];
253 static public function fillFaves(array $notice_ids)
255 $faveMap = Fave::listGet('notice_id', $notice_ids);
256 self::$_faves = array_replace(self::$_faves, $faveMap);
259 static public function blowCacheForProfileId($profile_id)
261 $cache = Cache::instance();
263 // Faves don't happen chronologically, so we need to blow
265 $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id));
266 $cache->delete(Cache::key('fave:ids_by_user:'.$profile_id.';last'));
267 $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id));
268 $cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id.';last'));
269 $cache->delete(Cache::key('fave:count_by_profile:'.$profile_id));
272 static public function blowCacheForNoticeId($notice_id)
274 $cache = Cache::instance();
276 $cache->delete(Cache::key('fave:list-ids:notice_id:'.$notice_id));