X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fapiaction.php;h=18ec44bb2bcc7ec0560206d9428571e16f12d074;hb=945920f24dba53d8367160b221bc842db0768fc4;hp=d85029f4336ade4e9418afad4e7df32fcdf0eac2;hpb=c7df5594d07867b54a1e698dee35602ad0dabbd2;p=quix0rs-gnu-social.git diff --git a/lib/apiaction.php b/lib/apiaction.php old mode 100755 new mode 100644 index d85029f433..18ec44bb2b --- a/lib/apiaction.php +++ b/lib/apiaction.php @@ -144,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'); @@ -159,7 +159,7 @@ class ApiAction extends Action $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'); @@ -214,11 +214,11 @@ 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); @@ -248,7 +248,7 @@ class ApiAction extends Action $twitter_user['friends_count'] = $profile->subscriptionCount(); - $twitter_user['created_at'] = $this->dateTwitter($profile->created); + $twitter_user['created_at'] = self::dateTwitter($profile->created); $timezone = 'UTC'; @@ -264,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) { @@ -305,6 +303,7 @@ 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 ($original instanceof Notice) { @@ -323,34 +322,37 @@ 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; $source = null; + $source_link = null; $ns = $notice->getSource(); if ($ns instanceof Notice_source) { - if (!empty($ns->name) && !empty($ns->url)) { - $source = '' - . htmlspecialchars($ns->name) - . ''; - } else { - $source = $ns->code; + $source = $ns->code; + if (!empty($ns->url)) { + $source_link = $ns->url; + if (!empty($ns->name)) { + $source = $ns->name; + } } } $twitter_status['uri'] = $notice->getUri(); $twitter_status['source'] = $source; + $twitter_status['source_link'] = $source_link; $twitter_status['id'] = intval($notice->id); $replier_profile = null; @@ -367,21 +369,16 @@ class ApiAction extends Action $twitter_status['in_reply_to_screen_name'] = ($replier_profile) ? $replier_profile->nickname : null; - if (isset($notice->lat) && isset($notice->lon)) { + try { + $notloc = Notice_location::locFromStored($notice); // This is the format that GeoJSON expects stuff to be in $twitter_status['geo'] = array('type' => 'Point', - 'coordinates' => array((float) $notice->lat, - (float) $notice->lon)); - } else { + 'coordinates' => array((float) $notloc->lat, + (float) $notloc->lon)); + } catch (ServerException $e) { $twitter_status['geo'] = null; } - if (!is_null($this->scoped)) { - $twitter_status['repeated'] = $this->scoped->hasRepeated($notice); - } else { - $twitter_status['repeated'] = false; - } - // Enclosures $attachments = $notice->attachments(); @@ -411,7 +408,7 @@ class ApiAction extends Action // StatusNet-specific - $twitter_status['statusnet_html'] = $notice->rendered; + $twitter_status['statusnet_html'] = $notice->getRendered(); $twitter_status['statusnet_conversation_id'] = intval($notice->conversation); // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array @@ -430,11 +427,11 @@ class ApiAction extends Action $twitter_group['nickname'] = $group->nickname; $twitter_group['fullname'] = $group->fullname; - if (isset($this->auth_user)) { - $twitter_group['member'] = $this->auth_user->isMember($group); + if ($this->scoped instanceof Profile) { + $twitter_group['member'] = $this->scoped->isMember($group); $twitter_group['blocked'] = Group_block::isBlocked( $group, - $this->auth_user->getProfile() + $this->scoped ); } @@ -447,8 +444,8 @@ class ApiAction extends Action $twitter_group['homepage'] = $group->homepage; $twitter_group['description'] = $group->description; $twitter_group['location'] = $group->location; - $twitter_group['created'] = $this->dateTwitter($group->created); - $twitter_group['modified'] = $this->dateTwitter($group->modified); + $twitter_group['created'] = self::dateTwitter($group->created); + $twitter_group['modified'] = self::dateTwitter($group->modified); return $twitter_group; } @@ -485,8 +482,8 @@ class ApiAction extends Action $twitter_list['member_count'] = $list->taggedCount(); $twitter_list['uri'] = $list->getUri(); - if (isset($this->auth_user)) { - $twitter_list['following'] = $list->hasSubscriber($this->auth_user); + if ($this->scoped instanceof Profile) { + $twitter_list['following'] = $list->hasSubscriber($this->scoped); } else { $twitter_list['following'] = false; } @@ -506,7 +503,7 @@ class ApiAction extends Action // We trim() to avoid extraneous whitespace in the output - $entry['content'] = common_xml_safe_str(trim($notice->rendered)); + $entry['content'] = common_xml_safe_str(trim($notice->getRendered())); $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content)); $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id)); $entry['published'] = common_date_iso8601($notice->created); @@ -554,13 +551,14 @@ class ApiAction extends Action $entry['pubDate'] = common_date_rfc2822($notice->created); $entry['guid'] = $entry['link']; - if (isset($notice->lat) && isset($notice->lon)) { + try { + $notloc = Notice_location::locFromStored($notice); // This is the format that GeoJSON expects stuff to be in. // showGeoRSS() below uses it for XML output, so we reuse it $entry['geo'] = array('type' => 'Point', - 'coordinates' => array((float) $notice->lat, - (float) $notice->lon)); - } else { + 'coordinates' => array((float) $notloc->lat, + (float) $notloc->lon)); + } catch (ServerException $e) { $entry['geo'] = null; } @@ -575,37 +573,30 @@ class ApiAction extends Action $relationship = array(); $relationship['source'] = - $this->relationshipDetailsArray($source, $target); + $this->relationshipDetailsArray($source->getProfile(), $target->getProfile()); $relationship['target'] = - $this->relationshipDetailsArray($target, $source); + $this->relationshipDetailsArray($target->getProfile(), $source->getProfile()); return array('relationship' => $relationship); } - function relationshipDetailsArray($source, $target) + function relationshipDetailsArray(Profile $source, Profile $target) { $details = array(); - $source_profile = $source->getProfile(); - $target_profile = $target->getProfile(); - - $details['screen_name'] = $source->nickname; - $details['followed_by'] = $target->isSubscribed($source_profile); - $details['following'] = $source->isSubscribed($target_profile); + $details['screen_name'] = $source->getNickname(); + $details['followed_by'] = $target->isSubscribed($source); - $notifications = false; - - if ($source->isSubscribed($target_profile)) { - $sub = Subscription::pkeyGet(array('subscriber' => - $source->id, 'subscribed' => $target->id)); - - if (!empty($sub)) { - $notifications = ($sub->jabber || $sub->sms); - } + try { + $sub = Subscription::getSubscription($source, $target); + $details['following'] = true; + $details['notifications_enabled'] = ($sub->jabber || $sub->sms); + } catch (NoResultException $e) { + $details['following'] = false; + $details['notifications_enabled'] = false; } - $details['notifications_enabled'] = $notifications; - $details['blocking'] = $source->hasBlocked($target_profile); + $details['blocking'] = $source->hasBlocked($target); $details['id'] = intval($source->id); return $details; @@ -655,10 +646,16 @@ class ApiAction extends Action $this->showGeoXML($value); break; case 'retweeted_status': + // FIXME: MOVE TO SHARE PLUGIN $this->showTwitterXmlStatus($value, 'retweeted_status'); break; default: if (strncmp($element, 'statusnet_', 10) == 0) { + if ($element === 'statusnet_in_groups' && is_array($value)) { + // QVITTERFIX because it would cause an array to be sent as $value + // THIS IS UNDOCUMENTED AND SHOULD NEVER BE RELIED UPON (qvitter uses json output) + $value = json_encode($value); + } $this->element('statusnet:'.substr($element, 10), null, $value); } else { $this->element($element, null, $value); @@ -774,9 +771,15 @@ class ApiAction extends Action function showJsonObjects($objects) { - print(json_encode($objects)); + $json_objects = json_encode($objects); + if($json_objects === false) { + $this->clientError(_('JSON encoding failed. Error: ').json_last_error_msg()); + } else { + print $json_objects; + } } + function showSingleXmlStatus($notice) { $this->initDocument('xml'); @@ -787,8 +790,9 @@ class ApiAction extends Action function showSingleAtomStatus($notice) { - header('Content-Type: application/atom+xml; charset=utf-8'); - print $notice->asAtomEntry(true, true, true, $this->auth_user); + header('Content-Type: application/atom+xml;type=entry;charset="utf-8"'); + print '' . "\n"; + print $notice->asAtomEntry(true, true, true, $this->scoped); } function show_single_json_status($notice) @@ -806,7 +810,12 @@ class ApiAction extends Action 'xmlns:statusnet' => 'http://status.net/schema/api/1/')); 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()) { @@ -862,7 +871,12 @@ class ApiAction extends Action $this->element('ttl', null, '40'); 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()) { @@ -906,7 +920,12 @@ class ApiAction extends Action $this->element('subtitle', null, $subtitle); 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()) { @@ -1010,7 +1029,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()) { @@ -1209,7 +1233,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')); @@ -1352,7 +1376,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)) { @@ -1381,20 +1405,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 instanceof 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(); } } @@ -1448,7 +1473,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 { @@ -1516,6 +1541,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;