X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fapiaction.php;h=6f2f43ab9cffa4294ae03191ac0637822587929a;hb=349e842078c5d69901df6ec9205cf7edcb4c4636;hp=d151f4c51fd7f461f639235d9db39d87d4d11211;hpb=9b6633698c5abda07df3102b62c37cfb0847e1ea;p=quix0rs-gnu-social.git diff --git a/lib/apiaction.php b/lib/apiaction.php index d151f4c51f..6f2f43ab9c 100644 --- a/lib/apiaction.php +++ b/lib/apiaction.php @@ -54,7 +54,7 @@ @subsection usermethods_sec User Methods - @subsection directmessagemethods_sec Direct Message Methods + @subsection directmessagemethods_sec Direct Message Methods (now a plugin) @subsection friendshipmethods_sec Friendship Methods @@ -123,10 +123,13 @@ class ApiAction extends Action var $auth_user = null; var $page = null; var $count = null; + var $offset = null; + var $limit = null; var $max_id = null; var $since_id = null; var $source = null; var $callback = null; + var $format = null; var $access = self::READ_ONLY; // read (default) or read-write @@ -141,7 +144,7 @@ class ApiAction extends Action */ protected function prepare(array $args=array()) { - StatusNet::setApi(true); // reduce exception reports to aid in debugging + GNUsocial::setApi(true); // reduce exception reports to aid in debugging parent::prepare($args); $this->format = $this->arg('format'); @@ -151,8 +154,12 @@ class ApiAction extends Action $this->max_id = (int)$this->arg('max_id', 0); $this->since_id = (int)$this->arg('since_id', 0); + // These two are not used everywhere, mainly just AtompubAction extensions + $this->offset = ($this->page-1) * $this->count; + $this->limit = $this->count + 1; + if ($this->arg('since')) { - header('X-StatusNet-Warning: since parameter is disabled; use since_id'); + header('X-GNUsocial-Warning: since parameter is disabled; use since_id'); } $this->source = $this->trimmed('source'); @@ -207,14 +214,16 @@ class ApiAction extends Action $user = null; } - $twitter_user['id'] = intval($profile->id); + $twitter_user['id'] = $profile->getID(); $twitter_user['name'] = $profile->getBestName(); - $twitter_user['screen_name'] = $profile->nickname; - $twitter_user['location'] = ($profile->location) ? $profile->location : null; - $twitter_user['description'] = ($profile->bio) ? $profile->bio : null; + $twitter_user['screen_name'] = $profile->getNickname(); + $twitter_user['location'] = $profile->location; + $twitter_user['description'] = $profile->getDescription(); // TODO: avatar url template (example.com/user/avatar?size={x}x{y}) $twitter_user['profile_image_url'] = Avatar::urlByProfile($profile, AVATAR_STREAM_SIZE); + $twitter_user['profile_image_url_https'] = $twitter_user['profile_image_url']; + // START introduced by qvitter API, not necessary for StatusNet API $twitter_user['profile_image_url_profile_size'] = Avatar::urlByProfile($profile, AVATAR_PROFILE_SIZE); try { @@ -239,9 +248,7 @@ class ApiAction extends Action $twitter_user['friends_count'] = $profile->subscriptionCount(); - $twitter_user['created_at'] = $this->dateTwitter($profile->created); - - $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling! + $twitter_user['created_at'] = self::dateTwitter($profile->created); $timezone = 'UTC'; @@ -257,23 +264,21 @@ class ApiAction extends Action $twitter_user['statuses_count'] = $profile->noticeCount(); // Is the requesting user following this user? + // These values might actually also mean "unknown". Ambiguity issues? $twitter_user['following'] = false; $twitter_user['statusnet_blocking'] = false; $twitter_user['notifications'] = false; - if (isset($this->auth_user)) { - - $twitter_user['following'] = $this->auth_user->isSubscribed($profile); - $twitter_user['statusnet_blocking'] = $this->auth_user->hasBlocked($profile); - - // Notifications on? - $sub = Subscription::pkeyGet(array('subscriber' => - $this->auth_user->id, - 'subscribed' => $profile->id)); - - if ($sub) { + if ($this->scoped instanceof Profile) { + try { + $sub = Subscription::getSubscription($this->scoped, $profile); + // Notifications on? + $twitter_user['following'] = true; $twitter_user['notifications'] = ($sub->jabber || $sub->sms); + } catch (NoResultException $e) { + // well, the values are already false... } + $twitter_user['statusnet_blocking'] = $this->scoped->hasBlocked($profile); } if ($get_notice) { @@ -288,6 +293,9 @@ class ApiAction extends Action $twitter_user['statusnet_profile_url'] = $profile->profileurl; + // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array + Event::handle('TwitterUserArray', array($profile, &$twitter_user, $this->scoped, array())); + return $twitter_user; } @@ -295,11 +303,12 @@ class ApiAction extends Action { $base = $this->twitterSimpleStatusArray($notice, $include_user); + // FIXME: MOVE TO SHARE PLUGIN if (!empty($notice->repeat_of)) { $original = Notice::getKV('id', $notice->repeat_of); - if (!empty($original)) { - $original_array = $this->twitterSimpleStatusArray($original, $include_user); - $base['retweeted_status'] = $original_array; + if ($original instanceof Notice) { + $orig_array = $this->twitterSimpleStatusArray($original, $include_user); + $base['retweeted_status'] = $orig_array; } } @@ -313,13 +322,16 @@ class ApiAction extends Action $twitter_status = array(); $twitter_status['text'] = $notice->content; $twitter_status['truncated'] = false; # Not possible on StatusNet - $twitter_status['created_at'] = $this->dateTwitter($notice->created); + $twitter_status['created_at'] = self::dateTwitter($notice->created); try { // We could just do $notice->reply_to but maybe the future holds a // different story for parenting. $parent = $notice->getParent(); $in_reply_to = $parent->id; - } catch (Exception $e) { + } catch (NoParentNoticeException $e) { + $in_reply_to = null; + } catch (NoResultException $e) { + // the in_reply_to message has probably been deleted $in_reply_to = null; } $twitter_status['in_reply_to_status_id'] = $in_reply_to; @@ -327,7 +339,7 @@ class ApiAction extends Action $source = null; $ns = $notice->getSource(); - if ($ns) { + if ($ns instanceof Notice_source) { if (!empty($ns->name) && !empty($ns->url)) { $source = 'getBestName(); - $entry['author-uri'] = $from->homepage; - - $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE); - try { - $avatar = $from->getAvatar(AVATAR_STREAM_SIZE); - $entry['avatar-type'] = $avatar->mediatype; - } catch (Exception $e) { - $entry['avatar-type'] = 'image/png'; - } - - // RSS item specific - - $entry['description'] = $entry['content']; - $entry['pubDate'] = common_date_rfc2822($message->created); - $entry['guid'] = $entry['link']; - - return $entry; - } - - function showSingleXmlDirectMessage($message) - { - $this->initDocument('xml'); - $dmsg = $this->directMessageArray($message); - $this->showXmlDirectMessage($dmsg, true); - $this->endDocument('xml'); - } - - function showSingleJsonDirectMessage($message) - { - $this->initDocument('json'); - $dmsg = $this->directMessageArray($message); - $this->showJsonObjects($dmsg); - $this->endDocument('json'); - } - function showAtomGroups($group, $title, $id, $link, $subtitle=null, $selfuri=null) { $this->initDocument('atom'); @@ -1089,7 +1028,12 @@ class ApiAction extends Action $statuses = array(); if (is_array($notice)) { - $notice = new ArrayWrapper($notice); + //FIXME: make everything calling showJsonTimeline use only Notice objects + $ids = array(); + foreach ($notice as $n) { + $ids[] = $n->getID(); + } + $notice = Notice::multiGet('id', $ids); } while ($notice->fetch()) { @@ -1288,7 +1232,7 @@ class ApiAction extends Action $this->endDocument('xml'); } - function dateTwitter($dt) + static function dateTwitter($dt) { $dateStr = date('d F Y H:i:s', strtotime($dt)); $d = new DateTime($dateStr, new DateTimeZone('UTC')); @@ -1322,7 +1266,6 @@ class ApiAction extends Action default: // TRANS: Client error on an API request with an unsupported data format. $this->clientError(_('Not a supported data format.')); - break; } return; @@ -1349,7 +1292,6 @@ class ApiAction extends Action default: // TRANS: Client error on an API request with an unsupported data format. $this->clientError(_('Not a supported data format.')); - break; } return; } @@ -1404,7 +1346,6 @@ class ApiAction extends Action default: // TRANS: Client error on an API request with an unsupported data format. $this->clientError(_('Not a supported data format.')); - return; } return; } @@ -1434,7 +1375,7 @@ class ApiAction extends Action return User::getKV('nickname', $nickname); } else { // Fall back to trying the currently authenticated user - return $this->auth_user; + return $this->scoped->getUser(); } } else if (self::is_decimal($id)) { @@ -1463,20 +1404,21 @@ class ApiAction extends Action if (self::is_decimal($this->arg('user_id'))) { return Profile::getKV('id', $this->arg('user_id')); } - } else if ($this->arg('screen_name')) { + } elseif (mb_strlen($this->arg('screen_name')) > 0) { $nickname = common_canonical_nickname($this->arg('screen_name')); - $user = User::getKV('nickname', $nickname); - return $user ? $user->getProfile() : null; + $user = User::getByNickname($nickname); + return $user->getProfile(); } else { // Fall back to trying the currently authenticated user return $this->scoped; } - } else if (self::is_decimal($id)) { - return Profile::getKV($id); + } else if (self::is_decimal($id) && intval($id) > 0) { + return Profile::getByID($id); } else { + // FIXME: check if isAcct to identify remote profiles and not just local nicknames $nickname = common_canonical_nickname($id); - $user = User::getKV('nickname', $nickname); - return $user ? $user->getProfile() : null; + $user = User::getByNickname($nickname); + return $user->getProfile(); } } @@ -1530,7 +1472,7 @@ class ApiAction extends Action } if (!empty($list) && $list->private) { - if ($this->auth_user->id == $list->tagger) { + if ($this->scoped->id == $list->tagger) { return $list; } } else { @@ -1598,6 +1540,11 @@ class ApiAction extends Action $aargs['id'] = $id; } + $user = $this->arg('user'); + if (!empty($user)) { + $aargs['user'] = $user; + } + $tag = $this->arg('tag'); if (!empty($tag)) { $aargs['tag'] = $tag;