/**
  * Handle the request
  *
  * Get favs and return them as json object
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     $fave = new Fave();
     $fave->selectAdd();
     $fave->selectAdd('user_id');
     $fave->notice_id = $this->original->id;
     $fave->orderBy('modified');
     if (!is_null($this->cnt)) {
         $fave->limit(0, $this->cnt);
     }
     $ids = $fave->fetchAll('user_id');
     // get nickname and profile image
     $ids_with_profile_data = array();
     $i = 0;
     foreach ($ids as $id) {
         $profile = Profile::getKV('id', $id);
         $ids_with_profile_data[$i]['user_id'] = $id;
         $ids_with_profile_data[$i]['nickname'] = $profile->nickname;
         $ids_with_profile_data[$i]['fullname'] = $profile->fullname;
         $ids_with_profile_data[$i]['profileurl'] = $profile->profileurl;
         $profile = new Profile();
         $profile->id = $id;
         $avatarurl = $profile->avatarUrl(24);
         $ids_with_profile_data[$i]['avatarurl'] = $avatarurl;
         $i++;
     }
     $this->initDocument('json');
     $this->showJsonObjects($ids_with_profile_data);
     $this->endDocument('json');
 }
Example #2
0
 static function addNew($user, $notice)
 {
     $fave = new Fave();
     $fave->user_id = $user->id;
     $fave->notice_id = $notice->id;
     if (!$fave->insert()) {
         common_log_db_error($fave, 'INSERT', __FILE__);
         return false;
     }
     return $fave;
 }
Example #3
0
 /**
  * Class handler.
  *
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if (!common_logged_in()) {
         // TRANS: Client error displayed when trying to remove a favorite while not logged in.
         $this->clientError(_('Not logged in.'));
         return;
     }
     $user = common_current_user();
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)));
         return;
     }
     $id = $this->trimmed('notice');
     $notice = Notice::staticGet($id);
     $token = $this->trimmed('token-' . $notice->id);
     if (!$token || $token != common_session_token()) {
         // TRANS: Client error displayed when the session token does not match or is not given.
         $this->clientError(_('There was a problem with your session token. Try again, please.'));
         return;
     }
     $fave = new Fave();
     $fave->user_id = $user->id;
     $fave->notice_id = $notice->id;
     if (!$fave->find(true)) {
         // TRANS: Client error displayed when trying to remove favorite status for a notice that is not a favorite.
         $this->clientError(_('This notice is not a favorite!'));
         return;
     }
     $result = $fave->delete();
     if (!$result) {
         common_log_db_error($fave, 'DELETE', __FILE__);
         // TRANS: Server error displayed when removing a favorite from the database fails.
         $this->serverError(_('Could not delete favorite.'));
         return;
     }
     $user->blowFavesCache();
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Title for page on which favorites can be added.
         $this->element('title', null, _('Add to favorites'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $favor = new FavorForm($this, $notice);
         $favor->show();
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)), 303);
     }
 }
Example #4
0
 protected function getNotices()
 {
     // is this our own stream?
     $own = $this->scoped instanceof Profile ? $this->target->getID() === $this->scoped->getID() : false;
     $stream = Fave::stream($this->target->getID(), 0, $this->limit, $own);
     return $stream->fetchAll();
 }
Example #5
0
 /**
  * Class handler.
  *
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     $profile = AnonymousFavePlugin::getAnonProfile();
     if (empty($profile) || $_SERVER['REQUEST_METHOD'] != 'POST') {
         // TRANS: Client error.
         $this->clientError(_m('Could not favor notice! Please make sure your browser has cookies enabled.'));
     }
     $id = $this->trimmed('notice');
     $notice = Notice::getKV($id);
     $token = $this->checkSessionToken();
     // Throws exception
     $stored = Fave::addNew($profile, $notice);
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Title.
         $this->element('title', null, _m('Disfavor favorite'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $disfavor = new AnonDisFavorForm($this, $notice);
         $disfavor->show();
         $this->elementEnd('body');
         $this->endHTML();
     } else {
         $this->returnToPrevious();
     }
 }
Example #6
0
 function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id)
 {
     $fav = new Fave();
     $qry = null;
     if ($own) {
         $qry = 'SELECT fave.* FROM fave ';
         $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
     } else {
         $qry = 'SELECT fave.* FROM fave ';
         $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
         $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
         $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
     }
     if ($since_id != 0) {
         $qry .= 'AND notice_id > ' . $since_id . ' ';
     }
     if ($max_id != 0) {
         $qry .= 'AND notice_id <= ' . $max_id . ' ';
     }
     // NOTE: we sort by fave time, not by notice time!
     $qry .= 'ORDER BY modified DESC ';
     if (!is_null($offset)) {
         $qry .= "LIMIT {$limit} OFFSET {$offset}";
     }
     $fav->query($qry);
     $ids = array();
     while ($fav->fetch()) {
         $ids[] = $fav->notice_id;
     }
     $fav->free();
     unset($fav);
     return $ids;
 }
Example #7
0
 /**
  * Class handler.
  * 
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if (!common_logged_in()) {
         $this->clientError(_('Not logged in.'));
         return;
     }
     $user = common_current_user();
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)));
         return;
     }
     $id = $this->trimmed('notice');
     $notice = Notice::staticGet($id);
     $token = $this->trimmed('token-' . $notice->id);
     if (!$token || $token != common_session_token()) {
         $this->clientError(_("There was a problem with your session token. Try again, please."));
         return;
     }
     $fave = new Fave();
     $fave->user_id = $this->id;
     $fave->notice_id = $notice->id;
     if (!$fave->find(true)) {
         $this->clientError(_('This notice is not a favorite!'));
         return;
     }
     $result = $fave->delete();
     if (!$result) {
         common_log_db_error($fave, 'DELETE', __FILE__);
         $this->serverError(_('Could not delete favorite.'));
         return;
     }
     $user->blowFavesCache();
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         $this->element('title', null, _('Add to favorites'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $favor = new FavorForm($this, $notice);
         $favor->show();
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)));
     }
 }
Example #8
0
 /**
  * Class handler.
  *
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     $profile = AnonymousFavePlugin::getAnonProfile();
     if (empty($profile) || $_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_m('Could not disfavor notice! Please make sure your browser has cookies enabled.'));
         return;
     }
     $id = $this->trimmed('notice');
     $notice = Notice::staticGet($id);
     $token = $this->trimmed('token-' . $notice->id);
     if (!$token || $token != common_session_token()) {
         // TRANS: Client error.
         $this->clientError(_m('There was a problem with your session token. Try again, please.'));
         return;
     }
     $fave = new Fave();
     $fave->user_id = $profile->id;
     $fave->notice_id = $notice->id;
     if (!$fave->find(true)) {
         // TRANS: Client error.
         $this->clientError(_m('This notice is not a favorite!'));
         return;
     }
     $result = $fave->delete();
     if (!$result) {
         common_log_db_error($fave, 'DELETE', __FILE__);
         // TRANS: Server error.
         $this->serverError(_m('Could not delete favorite.'));
         return;
     }
     $profile->blowFavesCache();
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Title.
         $this->element('title', null, _m('Add to favorites'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $favor = new AnonFavorForm($this, $notice);
         $favor->show();
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         $this->returnToPrevious();
     }
 }
Example #9
0
 /**
  * Get notices
  *
  * @param integer $limit max number of notices to return
  *
  * @return array notices
  */
 function getNotices($limit = 0)
 {
     $notice = Fave::stream($this->user->id, 0, $limit, $false);
     $notices = array();
     while ($notice->fetch()) {
         $notices[] = clone $notice;
     }
     return $notices;
 }
 function getProfiles()
 {
     $faves = Fave::byNotice($this->notice);
     $profiles = array();
     foreach ($faves as $fave) {
         $profiles[] = $fave->user_id;
     }
     return $profiles;
 }
Example #11
0
 function moveActivity($act, $sink, $user, $remote)
 {
     if (empty($user)) {
         // TRANS: Exception thrown if a non-existing user is provided. %s is a user ID.
         throw new Exception(sprintf(_('No such user "%s".'), $act->actor->id));
     }
     switch ($act->verb) {
         case ActivityVerb::FAVORITE:
             $this->log(LOG_INFO, "Moving favorite of {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             // push it, then delete local
             $sink->postActivity($act);
             $notice = Notice::staticGet('uri', $act->objects[0]->id);
             if (!empty($notice)) {
                 $fave = Fave::pkeyGet(array('user_id' => $user->id, 'notice_id' => $notice->id));
                 $fave->delete();
             }
             break;
         case ActivityVerb::POST:
             $this->log(LOG_INFO, "Moving notice {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             // XXX: send a reshare, not a post
             $sink->postActivity($act);
             $notice = Notice::staticGet('uri', $act->objects[0]->id);
             if (!empty($notice)) {
                 $notice->delete();
             }
             break;
         case ActivityVerb::JOIN:
             $this->log(LOG_INFO, "Moving group join of {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
             $sink->postActivity($act);
             $group = User_group::staticGet('uri', $act->objects[0]->id);
             if (!empty($group)) {
                 $user->leaveGroup($group);
             }
             break;
         case ActivityVerb::FOLLOW:
             if ($act->actor->id == $user->uri) {
                 $this->log(LOG_INFO, "Moving subscription to {$act->objects[0]->id} by " . "{$act->actor->id} to {$remote->nickname}.");
                 $sink->postActivity($act);
                 $other = Profile::fromURI($act->objects[0]->id);
                 if (!empty($other)) {
                     Subscription::cancel($user->getProfile(), $other);
                 }
             } else {
                 $otherUser = User::staticGet('uri', $act->actor->id);
                 if (!empty($otherUser)) {
                     $this->log(LOG_INFO, "Changing sub to {$act->objects[0]->id}" . "by {$act->actor->id} to {$remote->nickname}.");
                     $otherProfile = $otherUser->getProfile();
                     Subscription::start($otherProfile, $remote);
                     Subscription::cancel($otherProfile, $user->getProfile());
                 } else {
                     $this->log(LOG_NOTICE, "Not changing sub to {$act->objects[0]->id}" . "by remote {$act->actor->id} " . "to {$remote->nickname}.");
                 }
             }
             break;
     }
 }
 function getFaves()
 {
     $faves = array();
     $fave = new Fave();
     $fave->user_id = $this->user->id;
     if ($fave->find()) {
         while ($fave->fetch()) {
             $faves[] = clone $fave;
         }
     }
     return $faves;
 }
Example #13
0
 function handle($channel)
 {
     $notice = $this->getNotice($this->other);
     try {
         $fave = Fave::addNew($this->user->getProfile(), $notice);
     } catch (Exception $e) {
         $channel->error($this->user, $e->getMessage());
         return;
     }
     // TRANS: Text shown when a notice has been marked as favourite successfully.
     $channel->output($this->user, _('Notice marked as fave.'));
 }
 protected function prepare(array $args = array())
 {
     parent::prepare($args);
     $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
     if (!$this->_profile instanceof Profile) {
         // TRANS: Client exception thrown when requesting a favorite feed for a non-existing profile.
         throw new ClientException(_('No such profile.'), 404);
     }
     $offset = ($this->page - 1) * $this->count;
     $limit = $this->count + 1;
     $this->_faves = Fave::byProfile($this->_profile->id, $offset, $limit);
     return true;
 }
 function getNoticeIds($offset, $limit, $since_id, $max_id)
 {
     $weightexpr = common_sql_weight('modified', common_config('popular', 'dropoff'));
     $cutoff = sprintf("modified > '%s'", common_sql_date(time() - common_config('popular', 'cutoff')));
     $fave = new Fave();
     $fave->selectAdd();
     $fave->selectAdd('notice_id');
     $fave->selectAdd("{$weightexpr} as weight");
     $fave->whereAdd($cutoff);
     $fave->orderBy('weight DESC');
     $fave->groupBy('notice_id');
     if (!is_null($offset)) {
         $fave->limit($offset, $limit);
     }
     // FIXME: $since_id, $max_id are ignored
     $ids = array();
     if ($fave->find()) {
         while ($fave->fetch()) {
             $ids[] = $fave->notice_id;
         }
     }
     return $ids;
 }
Example #16
0
 /**
  * Class handler.
  *
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if (!common_logged_in()) {
         // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
         $this->clientError(_('Not logged in.'));
         return;
     }
     $user = common_current_user();
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)));
         return;
     }
     $id = $this->trimmed('notice');
     $notice = Notice::staticGet($id);
     $token = $this->trimmed('token-' . $notice->id);
     if (!$token || $token != common_session_token()) {
         // TRANS: Client error displayed when the session token does not match or is not given.
         $this->clientError(_('There was a problem with your session token. Try again, please.'));
         return;
     }
     if ($user->hasFave($notice)) {
         // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite.
         $this->clientError(_('This notice is already a favorite!'));
         return;
     }
     $fave = Fave::addNew($user->getProfile(), $notice);
     if (!$fave) {
         // TRANS: Server error displayed when trying to mark a notice as favorite fails in the database.
         $this->serverError(_('Could not create favorite.'));
         return;
     }
     $this->notify($notice, $user);
     $user->blowFavesCache();
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Page title for page on which favorite notices can be unfavourited.
         $this->element('title', null, _('Disfavor favorite.'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $disfavor = new DisFavorForm($this, $notice);
         $disfavor->show();
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)), 303);
     }
 }
Example #17
0
 /**
  * Class handler.
  *
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     $profile = AnonymousFavePlugin::getAnonProfile();
     if (empty($profile) || $_SERVER['REQUEST_METHOD'] != 'POST') {
         // TRANS: Client error.
         $this->clientError(_m('Could not favor notice! Please make sure your browser has cookies enabled.'));
         return;
     }
     $id = $this->trimmed('notice');
     $notice = Notice::staticGet($id);
     $token = $this->trimmed('token-' . $notice->id);
     if (empty($token) || $token != common_session_token()) {
         // TRANS: Client error.
         $this->clientError(_m('There was a problem with your session token. Try again, please.'));
         return;
     }
     if ($profile->hasFave($notice)) {
         // TRANS: Client error.
         $this->clientError(_m('This notice is already a favorite!'));
         return;
     }
     $fave = Fave::addNew($profile, $notice);
     if (!$fave) {
         // TRANS: Server error.
         $this->serverError(_m('Could not create favorite.'));
         return;
     }
     $profile->blowFavesCache();
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Title.
         $this->element('title', null, _m('Disfavor favorite'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $disfavor = new AnonDisFavorForm($this, $notice);
         $disfavor->show();
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         $this->returnToPrevious();
     }
 }
Example #18
0
 /**
  * Class handler.
  * 
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if (!common_logged_in()) {
         $this->clientError(_('Not logged in.'));
         return;
     }
     $user = common_current_user();
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)));
         return;
     }
     $id = $this->trimmed('notice');
     $notice = Notice::staticGet($id);
     $token = $this->trimmed('token-' . $notice->id);
     if (!$token || $token != common_session_token()) {
         $this->clientError(_("There was a problem with your session token. Try again, please."));
         return;
     }
     if ($user->hasFave($notice)) {
         $this->clientError(_('This notice is already a favorite!'));
         return;
     }
     $fave = Fave::addNew($user, $notice);
     if (!$fave) {
         $this->serverError(_('Could not create favorite.'));
         return;
     }
     $this->notify($notice, $user);
     $user->blowFavesCache();
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         $this->element('title', null, _('Disfavor favorite'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $disfavor = new DisFavorForm($this, $notice);
         $disfavor->show();
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)));
     }
 }
 protected function handle()
 {
     parent::handle();
     if (!in_array($this->format, array('xml', 'json'))) {
         $this->clientError(_('API method not found.'), 404, $this->format);
     }
     if (empty($this->notice)) {
         $this->clientError(_('No status found with that ID.'), 404, $this->format);
     }
     try {
         $stored = Fave::addNew($this->scoped, $this->notice);
     } catch (AlreadyFulfilledException $e) {
         // Note: Twitter lets you fave things repeatedly via API.
         $this->clientError($e->getMessage(), 403);
     }
     if ($this->format == 'xml') {
         $this->showSingleXmlStatus($this->notice);
     } elseif ($this->format == 'json') {
         $this->show_single_json_status($this->notice);
     }
 }
 /**
  * For initializing members of the class.
  *
  * @param array $args misc. arguments
  *
  * @return boolean true
  */
 protected function prepare(array $args = array())
 {
     parent::prepare($args);
     $profileId = $this->trimmed('profile');
     $noticeId = $this->trimmed('notice');
     $this->_profile = Profile::getKV('id', $profileId);
     if (empty($this->_profile)) {
         // TRANS: Client exception.
         throw new ClientException(_('No such profile.'), 404);
     }
     $this->_notice = Notice::getKV('id', $noticeId);
     if (empty($this->_notice)) {
         // TRANS: Client exception thrown when referencing a non-existing notice.
         throw new ClientException(_('No such notice.'), 404);
     }
     $this->_fave = Fave::pkeyGet(array('user_id' => $profileId, 'notice_id' => $noticeId));
     if (empty($this->_fave)) {
         // TRANS: Client exception thrown when referencing a non-existing favorite.
         throw new ClientException(_('No such favorite.'), 404);
     }
     return true;
 }
Example #21
0
 /**
  * Class handler.
  *
  * @param array $args query arguments
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     $profile = AnonymousFavePlugin::getAnonProfile();
     if (empty($profile) || $_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_m('Could not disfavor notice! Please make sure your browser has cookies enabled.'));
     }
     $id = $this->trimmed('notice');
     $notice = Notice::getKV($id);
     $token = $this->checkSessionToken();
     $fave = new Fave();
     $fave->user_id = $profile->id;
     $fave->notice_id = $notice->id;
     if (!$fave->find(true)) {
         throw new NoResultException($fave);
     }
     $result = $fave->delete();
     if (!$result) {
         common_log_db_error($fave, 'DELETE', __FILE__);
         // TRANS: Server error.
         $this->serverError(_m('Could not delete favorite.'));
     }
     Fave::blowCacheForProfileId($profile->id);
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Title.
         $this->element('title', null, _m('Add to favorites'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $favor = new AnonFavorForm($this, $notice);
         $favor->show();
         $this->elementEnd('body');
         $this->endHTML();
     } else {
         $this->returnToPrevious();
     }
 }
Example #22
0
 /**
  * Count the number of faves a notice already has. Used to initalize
  * a tally for a notice.
  *
  * @param integer $noticeID ID of the notice to count faves for
  *
  * @return integer $total total number of time the notice has been favored
  */
 static function countExistingFaves($noticeID)
 {
     $fave = new Fave();
     $fave->notice_id = $noticeID;
     $total = $fave->count();
     return $total;
 }
Example #23
0
 /**
  * Prepare the object
  *
  * Check the input values and initialize the object.
  * Shows an error page on bad input.
  *
  * @param array $args $_REQUEST data
  *
  * @return boolean success flag
  */
 function prepare($args)
 {
     parent::prepare($args);
     $nickname = common_canonical_nickname($this->arg('nickname'));
     $this->user = User::getKV('nickname', $nickname);
     if (!$this->user) {
         // TRANS: Client error displayed when trying to display favourite notices for a non-existing user.
         $this->clientError(_('No such user.'));
     }
     $this->page = $this->trimmed('page');
     if (!$this->page) {
         $this->page = 1;
     }
     common_set_returnto($this->selfUrl());
     $cur = common_current_user();
     // Show imported/gateway notices as well as local if
     // the user is looking at their own favorites, otherwise not.
     $this->notice = Fave::stream($this->user->id, ($this->page - 1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, !empty($cur) && $cur->id == $this->user->id);
     if (empty($this->notice)) {
         // TRANS: Server error displayed when favourite notices could not be retrieved from the database.
         $this->serverError(_('Could not retrieve favorite notices.'));
     }
     if ($this->page > 1 && $this->notice->N == 0) {
         // TRANS: Client error when page not found (404)
         $this->clientError(_('No such page.'), 404);
     }
     return true;
 }
Example #24
0
 /**
  * Add stuff to notices in API responses
  *
  * @param Action $action action being executed
  *
  * @return boolean hook return
  */
 function onNoticeSimpleStatusArray($notice, &$twitter_status, $scoped)
 {
     // groups
     $notice_groups = $notice->getGroups();
     $group_addressees = false;
     foreach ($notice_groups as $g) {
         $group_addressees[] = array('nickname' => $g->nickname, 'url' => $g->mainpage);
     }
     $twitter_status['statusnet_in_groups'] = $group_addressees;
     // for older verions of gnu social: include the repeat-id, which we need when unrepeating later
     if (array_key_exists('repeated', $twitter_status) && $twitter_status['repeated'] === true) {
         $repeated = Notice::pkeyGet(array('profile_id' => $scoped->id, 'repeat_of' => $notice->id, 'verb' => 'http://activitystrea.ms/schema/1.0/share'));
         $twitter_status['repeated_id'] = $repeated->id;
     }
     // more metadata about attachments
     // get all attachments first, and put all the extra meta data in an array
     $attachments = $notice->attachments();
     $attachment_url_to_id = array();
     if (!empty($attachments)) {
         foreach ($attachments as $attachment) {
             if (is_object($attachment)) {
                 try {
                     $enclosure_o = $attachment->getEnclosure();
                     // add id to all attachments
                     $attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
                     // add data about thumbnails
                     $thumb = $attachment->getThumbnail();
                     $large_thumb = $attachment->getThumbnail(1000, 3000, false);
                     if (method_exists('File_thumbnail', 'url')) {
                         $thumb_url = File_thumbnail::url($thumb->filename);
                         $large_thumb_url = File_thumbnail::url($large_thumb->filename);
                     } else {
                         $thumb_url = $thumb->getUrl();
                         $large_thumb_url = $large_thumb->getUrl();
                     }
                     $attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url;
                     $attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $large_thumb_url;
                     $attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width;
                     $attachment_url_to_id[$enclosure_o->url]['height'] = $attachment->height;
                     // animated gif?
                     if ($attachment->mimetype == 'image/gif') {
                         $image = ImageFile::fromFileObject($attachment);
                         if ($image->animated == 1) {
                             $attachment_url_to_id[$enclosure_o->url]['animated'] = true;
                         } else {
                             $attachment_url_to_id[$enclosure_o->url]['animated'] = false;
                         }
                     }
                     // this applies to older versions of gnu social, i think
                 } catch (ServerException $e) {
                     $thumb = File_thumbnail::getKV('file_id', $attachment->id);
                     if ($thumb instanceof File_thumbnail) {
                         $thumb_url = $thumb->getUrl();
                         $attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
                         $attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url;
                         $attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $thumb_url;
                         $attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width;
                         $attachment_url_to_id[$enclosure_o->url]['height'] = $attachment->height;
                         // animated gif?
                         if ($attachment->mimetype == 'image/gif') {
                             $image = ImageFile::fromFileObject($attachment);
                             if ($image->animated == 1) {
                                 $attachment_url_to_id[$enclosure_o->url]['animated'] = true;
                             } else {
                                 $attachment_url_to_id[$enclosure_o->url]['animated'] = false;
                             }
                         }
                     }
                 }
             }
         }
     }
     // add the extra meta data to $twitter_status
     if (!empty($twitter_status['attachments'])) {
         foreach ($twitter_status['attachments'] as &$attachment) {
             if (!empty($attachment_url_to_id[$attachment['url']])) {
                 $attachment = array_merge($attachment, $attachment_url_to_id[$attachment['url']]);
             }
         }
     }
     // reply-to profile url
     try {
         $reply = $notice->getParent();
         $twitter_status['in_reply_to_profileurl'] = $reply->getProfile()->getUrl();
     } catch (ServerException $e) {
         $twitter_status['in_reply_to_profileurl'] = null;
     }
     // fave number
     $faves = Fave::byNotice($notice);
     $favenum = count($faves);
     $twitter_status['fave_num'] = $favenum;
     // repeat number
     $repeats = $notice->repeatStream();
     $repeatnum = 0;
     while ($repeats->fetch()) {
         if ($repeats->verb == ActivityVerb::SHARE) {
             // i.e. not deleted repeats
             $repeatnum++;
         }
     }
     $twitter_status['repeat_num'] = $repeatnum;
     // some more metadata about notice
     if ($notice->is_local == '1') {
         $twitter_status['is_local'] = true;
     } else {
         $twitter_status['is_local'] = false;
         if ($notice->object_type != 'activity') {
             try {
                 $twitter_status['external_url'] = $notice->getUrl(true);
             } catch (InvalidUrlException $e) {
                 common_debug('Qvitter: No URL available for external notice: id=' . $notice->id);
             }
         }
     }
     if ($notice->source == 'activity' || $notice->object_type == 'activity' || $notice->object_type == 'http://activitystrea.ms/schema/1.0/activity' || $notice->verb == 'delete') {
         $twitter_status['is_activity'] = true;
     } else {
         $twitter_status['is_activity'] = false;
     }
     if (ActivityUtils::compareTypes($notice->verb, array('qvitter-delete-notice', 'delete'))) {
         $twitter_status['qvitter_delete_notice'] = true;
     }
     return true;
 }
Example #25
0
 function execute($channel)
 {
     $recipient = common_relative_profile($this->user, common_canonical_nickname($this->other));
     if (!$recipient) {
         $channel->error($this->user, _('No such user.'));
         return;
     }
     $notice = $recipient->getCurrentNotice();
     if (!$notice) {
         $channel->error($this->user, _('User has no last notice'));
         return;
     }
     $fave = Fave::addNew($this->user, $notice);
     if (!$fave) {
         $channel->error($this->user, _('Could not create favorite.'));
         return;
     }
     $other = User::staticGet('id', $recipient->id);
     if ($other && $other->id != $user->id) {
         if ($other->email && $other->emailnotifyfav) {
             mail_notify_fave($other, $this->user, $notice);
         }
     }
     $this->user->blowFavesCache();
     $channel->output($this->user, _('Notice marked as fave.'));
 }
 protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
 {
     return Fave::existsForProfile($target, $scoped) ? new DisfavorForm($action, $target) : new FavorForm($action, $target);
 }
Example #27
0
 /**
  * Add stuff to notices in API responses
  *
  * @param Action $action action being executed
  *
  * @return boolean hook return
  */
 function onNoticeSimpleStatusArray($notice, &$twitter_status, $scoped)
 {
     // groups
     $notice_groups = $notice->getGroups();
     $group_addressees = false;
     foreach ($notice_groups as $g) {
         $group_addressees[] = array('nickname' => $g->nickname, 'url' => $g->mainpage);
     }
     $twitter_status['statusnet_in_groups'] = $group_addressees;
     // for older verions of gnu social: include the repeat-id, which we need when unrepeating later
     if (array_key_exists('repeated', $twitter_status) && $twitter_status['repeated'] === true) {
         $repeated = Notice::pkeyGet(array('profile_id' => $scoped->id, 'repeat_of' => $notice->id, 'verb' => 'http://activitystrea.ms/schema/1.0/share'));
         $twitter_status['repeated_id'] = $repeated->id;
     }
     // more metadata about attachments
     // get all attachments first, and put all the extra meta data in an array
     $attachments = $notice->attachments();
     $attachment_url_to_id = array();
     if (!empty($attachments)) {
         foreach ($attachments as $attachment) {
             if (is_object($attachment)) {
                 try {
                     $enclosure_o = $attachment->getEnclosure();
                     // add id to all attachments
                     $attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
                     // add data about thumbnails
                     $thumb = $attachment->getThumbnail();
                     $large_thumb = $attachment->getThumbnail(1000, 3000, false);
                     if (method_exists('File_thumbnail', 'url')) {
                         $thumb_url = File_thumbnail::url($thumb->filename);
                         $large_thumb_url = File_thumbnail::url($large_thumb->filename);
                     } else {
                         $thumb_url = $thumb->getUrl();
                         $large_thumb_url = $large_thumb->getUrl();
                     }
                     $attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url;
                     $attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $large_thumb_url;
                     $attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width;
                     $attachment_url_to_id[$enclosure_o->url]['height'] = $attachment->height;
                     // animated gif?
                     if ($attachment->mimetype == 'image/gif') {
                         $image = ImageFile::fromFileObject($attachment);
                         if ($image->animated == 1) {
                             $attachment_url_to_id[$enclosure_o->url]['animated'] = true;
                         } else {
                             $attachment_url_to_id[$enclosure_o->url]['animated'] = false;
                         }
                     }
                     // this applies to older versions of gnu social, i think
                 } catch (ServerException $e) {
                     $thumb = File_thumbnail::getKV('file_id', $attachment->id);
                     if ($thumb instanceof File_thumbnail) {
                         $thumb_url = $thumb->getUrl();
                         $attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
                         $attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url;
                         $attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $thumb_url;
                         $attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width;
                         $attachment_url_to_id[$enclosure_o->url]['height'] = $attachment->height;
                         // animated gif?
                         if ($attachment->mimetype == 'image/gif') {
                             $image = ImageFile::fromFileObject($attachment);
                             if ($image->animated == 1) {
                                 $attachment_url_to_id[$enclosure_o->url]['animated'] = true;
                             } else {
                                 $attachment_url_to_id[$enclosure_o->url]['animated'] = false;
                             }
                         }
                     }
                 }
             }
         }
     }
     // add the extra meta data to $twitter_status
     if (!empty($twitter_status['attachments'])) {
         foreach ($twitter_status['attachments'] as &$attachment) {
             if (!empty($attachment_url_to_id[$attachment['url']])) {
                 $attachment = array_merge($attachment, $attachment_url_to_id[$attachment['url']]);
             }
         }
     }
     // quoted notices
     if (!empty($twitter_status['attachments'])) {
         foreach ($twitter_status['attachments'] as &$attachment) {
             $quoted_notice = false;
             // if this attachment has an url this might be a notice url
             if (isset($attachment['url'])) {
                 $noticeurl = common_path('notice/', StatusNet::isHTTPS());
                 $instanceurl = common_path('', StatusNet::isHTTPS());
                 // remove protocol for the comparison below
                 $noticeurl_wo_protocol = preg_replace('(^https?://)', '', $noticeurl);
                 $instanceurl_wo_protocol = preg_replace('(^https?://)', '', $instanceurl);
                 $attachment_url_wo_protocol = preg_replace('(^https?://)', '', $attachment['url']);
                 // local notice urls
                 if (strpos($attachment_url_wo_protocol, $noticeurl_wo_protocol) === 0) {
                     $possible_notice_id = str_replace($noticeurl_wo_protocol, '', $attachment_url_wo_protocol);
                     if (ctype_digit($possible_notice_id)) {
                         $quoted_notice = Notice::getKV('id', $possible_notice_id);
                     }
                 } elseif (strpos($attachment_url_wo_protocol, $instanceurl_wo_protocol) !== 0 && stristr($attachment_url_wo_protocol, '/notice/')) {
                     $quoted_notice = Notice::getKV('url', $attachment['url']);
                     // try with http<->https if no match. applies to quitter.se notices mostly
                     if (!$quoted_notice instanceof Notice) {
                         if (strpos($attachment['url'], 'https://') === 0) {
                             $quoted_notice = Notice::getKV('url', str_replace('https://', 'http://', $attachment['url']));
                         } else {
                             $quoted_notice = Notice::getKV('url', str_replace('http://', 'https://', $attachment['url']));
                         }
                     }
                 }
                 // include the quoted notice in the attachment
                 if ($quoted_notice instanceof Notice) {
                     $quoted_notice_author = Profile::getKV('id', $quoted_notice->profile_id);
                     if ($quoted_notice_author instanceof Profile) {
                         $attachment['quoted_notice']['id'] = $quoted_notice->id;
                         $attachment['quoted_notice']['content'] = $quoted_notice->content;
                         $attachment['quoted_notice']['nickname'] = $quoted_notice_author->nickname;
                         $attachment['quoted_notice']['fullname'] = $quoted_notice_author->fullname;
                         $quoted_notice_attachments = $quoted_notice->attachments();
                         foreach ($quoted_notice_attachments as $q_attach) {
                             if (is_object($q_attach)) {
                                 try {
                                     $qthumb = $q_attach->getThumbnail();
                                     if (method_exists('File_thumbnail', 'url')) {
                                         $thumb_url = File_thumbnail::url($qthumb->filename);
                                     } else {
                                         $thumb_url = $qthumb->getUrl();
                                     }
                                     $attachment['quoted_notice']['attachments'][] = array('thumb_url' => $thumb_url, 'attachment_id' => $q_attach->id);
                                 } catch (Exception $e) {
                                     common_debug('Qvitter: could not get thumbnail for attachment id=' . $q_attach->id . ' in quoted notice id=' . $quoted_notice->id);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     try {
         $twitter_status['external_url'] = $notice->getUrl(true);
     } catch (InvalidUrlException $e) {
         common_debug('Qvitter: No URL available for external notice: id=' . $notice->id);
     }
     // reply-to profile url
     try {
         $reply = $notice->getParent();
         $twitter_status['in_reply_to_profileurl'] = $reply->getProfile()->getUrl();
     } catch (ServerException $e) {
         $twitter_status['in_reply_to_profileurl'] = null;
     }
     // fave number
     $faves = Fave::byNotice($notice);
     $favenum = count($faves);
     $twitter_status['fave_num'] = $favenum;
     // repeat number
     $repeats = $notice->repeatStream();
     $repeatnum = 0;
     while ($repeats->fetch()) {
         if ($repeats->verb == ActivityVerb::SHARE) {
             // i.e. not deleted repeats
             $repeatnum++;
         }
     }
     $twitter_status['repeat_num'] = $repeatnum;
     // is this a post? (previously is_activity)
     if (method_exists('ActivityUtils', 'compareVerbs')) {
         $twitter_status['is_post_verb'] = ActivityUtils::compareVerbs($notice->verb, array(ActivityVerb::POST));
     } else {
         $twitter_status['is_post_verb'] = $notice->verb == ActivityVerb::POST ? true : false;
     }
     // some more metadata about notice
     if ($notice->is_local == '1') {
         $twitter_status['is_local'] = true;
     } else {
         $twitter_status['is_local'] = false;
         if ($twitter_status['is_post_verb'] === true) {
             try {
                 $twitter_status['external_url'] = $notice->getUrl(true);
             } catch (InvalidUrlException $e) {
                 common_debug('Qvitter: No URL available for external notice: id=' . $notice->id);
             }
         }
     }
     if (ActivityUtils::compareTypes($notice->verb, array('qvitter-delete-notice', 'delete'))) {
         $twitter_status['qvitter_delete_notice'] = true;
     }
     return true;
 }
 function handle($channel)
 {
     $notice = $this->getNotice($this->other);
     $fave = Fave::addNew($this->user->getProfile(), $notice);
     if (!$fave) {
         $channel->error($this->user, _('Could not create favorite.'));
         return;
     }
     // @fixme favorite notification should be triggered
     // at a lower level
     $other = User::staticGet('id', $notice->profile_id);
     if ($other && $other->id != $user->id) {
         if ($other->email && $other->emailnotifyfav) {
             mail_notify_fave($other, $this->user, $notice);
         }
     }
     $this->user->blowFavesCache();
     $channel->output($this->user, _('Notice marked as fave.'));
 }
 function dumpFaves($user, $dir)
 {
     common_log(LOG_INFO, 'dumping faves by ' . $user->nickname . ' to directory ' . $dir);
     $page = 1;
     do {
         $fave = Fave::byProfile($user->id, ($page - 1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
         while ($fave->fetch()) {
             try {
                 $fname = $dir . '/' . common_date_iso8601($fave->modified) . '-fave-' . $fave->notice_id . '.atom';
                 $act = $fave->asActivity();
                 $data = $act->asString(false, false, false);
                 common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname);
                 file_put_contents($fname, $data);
                 $data = null;
             } catch (Exception $e) {
                 common_log(LOG_ERR, "Error backing up fave of " . $fave->notice_id . ": " . $e->getMessage());
                 continue;
             }
         }
         $page++;
     } while ($fave->N > NOTICES_PER_PAGE);
 }
 /**
  * Handle the request
  *
  * Check the format and show the user info
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     if (!in_array($this->format, array('xml', 'json'))) {
         $this->clientError(_('API method not found.'), 404, $this->format);
         return;
     }
     if (empty($this->notice)) {
         $this->clientError(_('No status found with that ID.'), 404, $this->format);
         return;
     }
     $fave = new Fave();
     $fave->user_id = $this->user->id;
     $fave->notice_id = $this->notice->id;
     if (!$fave->find(true)) {
         $this->clientError(_('That status is not a favorite.'), 403, $this->favorite);
         return;
     }
     $result = $fave->delete();
     if (!$result) {
         common_log_db_error($fave, 'DELETE', __FILE__);
         $this->clientError(_('Could not delete favorite.'), 404, $this->format);
         return;
     }
     $this->user->blowFavesCache();
     if ($this->format == 'xml') {
         $this->showSingleXmlStatus($this->notice);
     } elseif ($this->format == 'json') {
         $this->show_single_json_status($this->notice);
     }
 }