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 $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 */
24 public static function schemaDef()
28 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the favorite'),
29 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user who likes this notice'),
30 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
32 'primary key' => array('notice_id', 'user_id'),
33 'foreign keys' => array(
34 'fave_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
35 'fave_user_id_fkey' => array('profile', array('user_id' => 'id')), // note: formerly referenced notice.id, but we can now record remote users' favorites
38 'fave_notice_id_idx' => array('notice_id'),
39 'fave_user_id_idx' => array('user_id', 'modified'),
40 'fave_modified_idx' => array('modified'),
46 * Save a favorite record.
47 * @fixme post-author notification should be moved here
49 * @param Profile $profile the local or remote user who likes
50 * @param Notice $notice the notice that is liked
51 * @return mixed false on failure, or Fave record on success
53 static function addNew(Profile $profile, Notice $notice) {
57 if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {
61 $fave->user_id = $profile->id;
62 $fave->notice_id = $notice->id;
64 if (!$fave->insert()) {
65 common_log_db_error($fave, 'INSERT', __FILE__);
68 self::blow('fave:list-ids:notice_id:%d', $fave->notice_id);
70 Event::handle('EndFavorNotice', array($profile, $notice));
78 $profile = Profile::staticGet('id', $this->user_id);
79 $notice = Notice::staticGet('id', $this->notice_id);
83 if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
85 $result = parent::delete();
86 self::blow('fave:list-ids:notice_id:%d', $this->notice_id);
89 Event::handle('EndDisfavorNotice', array($profile, $notice));
98 return Memcached_DataObject::pkeyGet('Fave', $kv);
101 function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
103 $stream = new FaveNoticeStream($user_id, $own);
105 return $stream->getNotices($offset, $limit, $since_id, $max_id);
108 function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
110 $stream = new FaveNoticeStream($user_id, $own);
112 return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
115 function asActivity()
117 $notice = Notice::staticGet('id', $this->notice_id);
118 $profile = Profile::staticGet('id', $this->user_id);
120 $act = new Activity();
122 $act->verb = ActivityVerb::FAVORITE;
124 // FIXME: rationalize this with URL below
126 $act->id = TagURI::mint('favor:%d:%d:%s',
129 common_date_iso8601($this->modified));
131 $act->time = strtotime($this->modified);
132 // TRANS: Activity title when marking a notice as favorite.
133 $act->title = _("Favor");
134 // TRANS: Ntofication given when a user marks a notice as favorite.
135 // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
136 $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
137 $profile->getBestName(),
140 $act->actor = ActivityObject::fromProfile($profile);
141 $act->objects[] = ActivityObject::fromNotice($notice);
143 $url = common_local_url('AtomPubShowFavorite',
144 array('profile' => $this->user_id,
145 'notice' => $this->notice_id));
147 $act->selfLink = $url;
148 $act->editLink = $url;
154 * Fetch a stream of favorites by profile
156 * @param integer $profileId Profile that faved
157 * @param integer $offset Offset from last
158 * @param integer $limit Number to get
160 * @return mixed stream of faves, use fetch() to iterate
162 * @todo Cache results
163 * @todo integrate with Fave::stream()
166 static function byProfile($profileId, $offset, $limit)
170 $fav->user_id = $profileId;
172 $fav->orderBy('modified DESC');
174 $fav->limit($offset, $limit);