X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Ftwitterapi.php;h=9055d8b9820886e6291b19c535e7f1be46c7476c;hb=a08c76a4342cfb4b3d772f6880ba0b8f3153b246;hp=ca8b03cdcd79f6230df08db5bce0495932cd8773;hpb=701a237e8ce8790804da25fe5aaf989d88a05fdf;p=quix0rs-gnu-social.git diff --git a/lib/twitterapi.php b/lib/twitterapi.php index ca8b03cdcd..9055d8b982 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -1,7 +1,7 @@ . */ -if (!defined('LACONICA')) { exit(1); } +if (!defined('STATUSNET') && !defined('LACONICA')) { + exit(1); +} class TwitterapiAction extends Action { @@ -86,8 +88,8 @@ class TwitterapiAction extends Action Avatar::defaultImage(AVATAR_STREAM_SIZE); $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null; - $twitter_user['protected'] = false; # not supported by Laconica yet - $twitter_user['followers_count'] = $this->count_subscriptions($profile); + $twitter_user['protected'] = false; # not supported by StatusNet yet + $twitter_user['followers_count'] = $profile->subscriberCount(); // To be supported soon... $twitter_user['profile_background_color'] = ''; @@ -96,17 +98,11 @@ class TwitterapiAction extends Action $twitter_user['profile_sidebar_fill_color'] = ''; $twitter_user['profile_sidebar_border_color'] = ''; - $subbed = DB_DataObject::factory('subscription'); - $subbed->subscriber = $profile->id; - $subbed_count = (int) $subbed->count() - 1; - $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0; + $twitter_user['friends_count'] = $profile->subscriptionCount(); $twitter_user['created_at'] = $this->date_twitter($profile->created); - $faves = DB_DataObject::factory('fave'); - $faves->user_id = $user->id; - $faves_count = (int) $faves->count(); - $twitter_user['favourites_count'] = $faves_count; // British spelling! + $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling! // Need to pull up the user for some of this $user = User::staticGet($profile->id); @@ -127,11 +123,7 @@ class TwitterapiAction extends Action $twitter_user['profile_background_image_url'] = ''; $twitter_user['profile_background_tile'] = false; - $notices = DB_DataObject::factory('notice'); - $notices->profile_id = $profile->id; - $notice_count = (int) $notices->count(); - - $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0; + $twitter_user['statuses_count'] = $profile->noticeCount(); // Is the requesting user following this user? $twitter_user['following'] = false; @@ -167,7 +159,7 @@ class TwitterapiAction extends Action $twitter_status = array(); $twitter_status['text'] = $notice->content; - $twitter_status['truncated'] = false; # Not possible on Laconica + $twitter_status['truncated'] = false; # Not possible on StatusNet $twitter_status['created_at'] = $this->date_twitter($notice->created); $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : null; @@ -194,6 +186,24 @@ class TwitterapiAction extends Action $twitter_status['favorited'] = false; } + // Enclosures + $attachments = $notice->attachments(); + + if (!empty($attachments)) { + + $twitter_status['attachments'] = array(); + + foreach ($attachments as $attachment) { + if ($attachment->isEnclosure()) { + $enclosure = array(); + $enclosure['url'] = $attachment->url; + $enclosure['mimetype'] = $attachment->mimetype; + $enclosure['size'] = $attachment->size; + $twitter_status['attachments'][] = $enclosure; + } + } + } + if ($include_user) { # Don't get notice (recursive!) $twitter_user = $this->twitter_user_array($profile, false); @@ -203,13 +213,50 @@ class TwitterapiAction extends Action return $twitter_status; } - function twitter_rss_entry_array($notice) + function twitter_group_array($group) + { + $twitter_group=array(); + $twitter_group['id']=$group->id; + $twitter_group['url']=$group->permalink(); + $twitter_group['nickname']=$group->nickname; + $twitter_group['fullname']=$group->fullname; + $twitter_group['homepage_url']=$group->homepage_url; + $twitter_group['original_logo']=$group->original_logo; + $twitter_group['homepage_logo']=$group->homepage_logo; + $twitter_group['stream_logo']=$group->stream_logo; + $twitter_group['mini_logo']=$group->mini_logo; + $twitter_group['homepage']=$group->homepage; + $twitter_group['description']=$group->description; + $twitter_group['location']=$group->location; + $twitter_group['created']=$this->date_twitter($group->created); + $twitter_group['modified']=$this->date_twitter($group->modified); + return $twitter_group; + } + + function twitter_rss_group_array($group) { + $entry = array(); + $entry['content']=$group->description; + $entry['title']=$group->nickname; + $entry['link']=$group->permalink(); + $entry['published']=common_date_iso8601($group->created); + $entry['updated']==common_date_iso8601($group->modified); + $taguribase = common_config('integration', 'groupuri'); + $entry['id'] = "group:$groupuribase:$entry[link]"; + + $entry['description'] = $entry['content']; + $entry['pubDate'] = common_date_rfc2822($group->created); + $entry['guid'] = $entry['link']; + + return $entry; + } + function twitter_rss_entry_array($notice) + { $profile = $notice->getProfile(); $entry = array(); - # We trim() to avoid extraneous whitespace in the output + // We trim() to avoid extraneous whitespace in the output $entry['content'] = common_xml_safe_str(trim($notice->rendered)); $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content)); @@ -222,7 +269,54 @@ class TwitterapiAction extends Action $entry['updated'] = $entry['published']; $entry['author'] = $profile->getBestName(); - # RSS Item specific + // Enclosures + $attachments = $notice->attachments(); + $enclosures = array(); + + foreach ($attachments as $attachment) { + $enclosure_o=$attachment->getEnclosure(); + if ($enclosure_o) { + $enclosure = array(); + $enclosure['url'] = $enclosure_o->url; + $enclosure['mimetype'] = $enclosure_o->mimetype; + $enclosure['size'] = $enclosure_o->size; + $enclosures[] = $enclosure; + } + } + + if (!empty($enclosures)) { + $entry['enclosures'] = $enclosures; + } + +/* + // Enclosure + $attachments = $notice->attachments(); + if($attachments){ + $entry['enclosures']=array(); + foreach($attachments as $attachment){ + if ($attachment->isEnclosure()) { + $enclosure=array(); + $enclosure['url']=$attachment->url; + $enclosure['mimetype']=$attachment->mimetype; + $enclosure['size']=$attachment->size; + $entry['enclosures'][]=$enclosure; + } + } + } +*/ + + // Tags/Categories + $tag = new Notice_tag(); + $tag->notice_id = $notice->id; + if ($tag->find()) { + $entry['tags']=array(); + while ($tag->fetch()) { + $entry['tags'][]=$tag->tag; + } + } + $tag->free(); + + // RSS Item specific $entry['description'] = $entry['content']; $entry['pubDate'] = common_date_rfc2822($notice->created); $entry['guid'] = $entry['link']; @@ -276,6 +370,67 @@ class TwitterapiAction extends Action return $twitter_dm; } + function twitter_relationship_array($source, $target) + { + $relationship = array(); + + $relationship['source'] = + $this->relationship_details_array($source, $target); + $relationship['target'] = + $this->relationship_details_array($target, $source); + + return array('relationship' => $relationship); + } + + function relationship_details_array($source, $target) + { + $details = array(); + + $details['screen_name'] = $source->nickname; + $details['followed_by'] = $target->isSubscribed($source); + $details['following'] = $source->isSubscribed($target); + + $notifications = false; + + if ($source->isSubscribed($target)) { + + $sub = Subscription::pkeyGet(array('subscriber' => + $source->id, 'subscribed' => $target->id)); + + if (!empty($sub)) { + $notifications = ($sub->jabber || $sub->sms); + } + } + + $details['notifications_enabled'] = $notifications; + $details['blocking'] = $source->hasBlocked($target); + $details['id'] = $source->id; + + return $details; + } + + function show_twitter_xml_relationship($relationship) + { + $this->elementStart('relationship'); + + foreach($relationship as $element => $value) { + if ($element == 'source' || $element == 'target') { + $this->elementStart($element); + $this->show_xml_relationship_details($value); + $this->elementEnd($element); + } + } + + $this->elementEnd('relationship'); + } + + function show_xml_relationship_details($details) + { + foreach($details as $element => $value) { + $this->element($element, null, $value); + } + } + function show_twitter_xml_status($twitter_status) { $this->elementStart('status'); @@ -287,6 +442,9 @@ class TwitterapiAction extends Action case 'text': $this->element($element, null, common_xml_safe_str($value)); break; + case 'attachments': + $this->show_xml_attachments($twitter_status['attachments']); + break; default: $this->element($element, null, $value); } @@ -294,6 +452,15 @@ class TwitterapiAction extends Action $this->elementEnd('status'); } + function show_twitter_xml_group($twitter_group) + { + $this->elementStart('group'); + foreach($twitter_group as $element => $value) { + $this->element($element, null, $value); + } + $this->elementEnd('group'); + } + function show_twitter_xml_user($twitter_user, $role='user') { $this->elementStart($role); @@ -307,6 +474,20 @@ class TwitterapiAction extends Action $this->elementEnd($role); } + function show_xml_attachments($attachments) { + if (!empty($attachments)) { + $this->elementStart('attachments', array('type' => 'array')); + foreach ($attachments as $attachment) { + $attrs = array(); + $attrs['url'] = $attachment['url']; + $attrs['mimetype'] = $attachment['mimetype']; + $attrs['size'] = $attachment['size']; + $this->element('enclosure', $attrs, ''); + } + $this->elementEnd('attachments'); + } + } + function show_twitter_rss_item($entry) { $this->elementStart('item'); @@ -315,6 +496,19 @@ class TwitterapiAction extends Action $this->element('pubDate', null, $entry['pubDate']); $this->element('guid', null, $entry['guid']); $this->element('link', null, $entry['link']); + + # RSS only supports 1 enclosure per item + if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){ + $enclosure = $entry['enclosures'][0]; + $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null); + } + + if(array_key_exists('tags', $entry)){ + foreach($entry['tags'] as $tag){ + $this->element('category', null,$tag); + } + } + $this->elementEnd('item'); } @@ -469,6 +663,65 @@ class TwitterapiAction extends Action } + function show_rss_groups($group, $title, $link, $subtitle) + { + + $this->init_document('rss'); + + $this->elementStart('channel'); + $this->element('title', null, $title); + $this->element('link', null, $link); + $this->element('description', null, $subtitle); + $this->element('language', null, 'en-us'); + $this->element('ttl', null, '40'); + + if (is_array($group)) { + foreach ($group as $g) { + $twitter_group = $this->twitter_rss_group_array($g); + $this->show_twitter_rss_item($twitter_group); + } + } else { + while ($group->fetch()) { + $twitter_group = $this->twitter_rss_group_array($group); + $this->show_twitter_rss_item($twitter_group); + } + } + + $this->elementEnd('channel'); + $this->end_twitter_rss(); + } + + function show_atom_groups($group, $title, $id, $link, $subtitle=null, $selfuri=null) + { + + $this->init_document('atom'); + + $this->element('title', null, $title); + $this->element('id', null, $id); + $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null); + + if (!is_null($selfuri)) { + $this->element('link', array('href' => $selfuri, + 'rel' => 'self', 'type' => 'application/atom+xml'), null); + } + + $this->element('updated', null, common_date_iso8601('now')); + $this->element('subtitle', null, $subtitle); + + if (is_array($group)) { + foreach ($group as $g) { + $this->raw($g->asAtomEntry()); + } + } else { + while ($group->fetch()) { + $this->raw($group->asAtomEntry()); + } + } + + $this->end_document('atom'); + + } + function show_json_timeline($notice) { @@ -493,6 +746,68 @@ class TwitterapiAction extends Action $this->end_document('json'); } + function show_json_groups($group) + { + + $this->init_document('json'); + + $groups = array(); + + if (is_array($group)) { + foreach ($group as $g) { + $twitter_group = $this->twitter_group_array($g); + array_push($groups, $twitter_group); + } + } else { + while ($group->fetch()) { + $twitter_group = $this->twitter_group_array($group); + array_push($groups, $twitter_group); + } + } + + $this->show_json_objects($groups); + + $this->end_document('json'); + } + + function show_xml_groups($group) + { + + $this->init_document('xml'); + $this->elementStart('groups', array('type' => 'array')); + + if (is_array($group)) { + foreach ($group as $g) { + $twitter_group = $this->twitter_group_array($g); + $this->show_twitter_xml_group($twitter_group); + } + } else { + while ($group->fetch()) { + $twitter_group = $this->twitter_group_array($group); + $this->show_twitter_xml_group($twitter_group); + } + } + + $this->elementEnd('groups'); + $this->end_document('xml'); + } + + function show_single_json_group($group) + { + $this->init_document('json'); + $twitter_group = $this->twitter_group_array($group); + $this->show_json_objects($twitter_group); + $this->end_document('json'); + } + + function show_single_xml_group($group) + { + $this->init_document('xml'); + $twitter_group = $this->twitter_group_array($group); + $this->show_twitter_xml_group($twitter_group); + $this->end_document('xml'); + } + // Anyone know what date format this is? // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach function date_twitter($dt) @@ -521,11 +836,11 @@ class TwitterapiAction extends Action function init_document($type='xml') { switch ($type) { - case 'xml': + case 'xml': header('Content-Type: application/xml; charset=utf-8'); $this->startXML(); break; - case 'json': + case 'json': header('Content-Type: application/json; charset=utf-8'); // Check for JSONP callback @@ -534,16 +849,16 @@ class TwitterapiAction extends Action print $callback . '('; } break; - case 'rss': + case 'rss': header("Content-Type: application/rss+xml; charset=utf-8"); $this->init_twitter_rss(); break; - case 'atom': + case 'atom': header('Content-Type: application/atom+xml; charset=utf-8'); $this->init_twitter_atom(); break; - default: - $this->client_error(_('Not a supported data format.')); + default: + $this->clientError(_('Not a supported data format.')); break; } @@ -553,10 +868,10 @@ class TwitterapiAction extends Action function end_document($type='xml') { switch ($type) { - case 'xml': + case 'xml': $this->endXML(); break; - case 'json': + case 'json': // Check for JSONP callback $callback = $this->arg('callback'); @@ -564,20 +879,20 @@ class TwitterapiAction extends Action print ')'; } break; - case 'rss': + case 'rss': $this->end_twitter_rss(); break; - case 'atom': + case 'atom': $this->end_twitter_rss(); break; - default: - $this->client_error(_('Not a supported data format.')); + default: + $this->clientError(_('Not a supported data format.')); break; } return; } - function client_error($msg, $code = 400, $content_type = 'json') + function clientError($msg, $code = 400, $content_type = 'json') { static $status = array(400 => 'Bad Request', @@ -653,18 +968,18 @@ class TwitterapiAction extends Action $this->endXML(); } - function show_profile($profile, $content_type='xml', $notice=null) + function show_profile($profile, $content_type='xml', $notice=null, $includeStatuses=true) { - $profile_array = $this->twitter_user_array($profile, true); + $profile_array = $this->twitter_user_array($profile, $includeStatuses); switch ($content_type) { - case 'xml': + case 'xml': $this->show_twitter_xml_user($profile_array); break; - case 'json': + case 'json': $this->show_json_objects($profile_array); break; - default: - $this->client_error(_('Not a supported data format.')); + default: + $this->clientError(_('Not a supported data format.')); return; } return; @@ -672,8 +987,8 @@ class TwitterapiAction extends Action function get_user($id, $apidata=null) { - if (!$id) { - + if (empty($id)) { + // Twitter supports these other ways of passing the user ID if (is_numeric($this->arg('id'))) { return User::staticGet($this->arg('id')); @@ -681,7 +996,7 @@ class TwitterapiAction extends Action $nickname = common_canonical_nickname($this->arg('id')); return User::staticGet('nickname', $nickname); } else if ($this->arg('user_id')) { - // This is to ensure that a non-numeric user_id still + // This is to ensure that a non-numeric user_id still // overrides screen_name even if it doesn't get used if (is_numeric($this->arg('user_id'))) { return User::staticGet('id', $this->arg('user_id')); @@ -693,7 +1008,7 @@ class TwitterapiAction extends Action // Fall back to trying the currently authenticated user return $apidata['user']; } - + } else if (is_numeric($id)) { return User::staticGet($id); } else { @@ -702,6 +1017,34 @@ class TwitterapiAction extends Action } } + function get_group($id, $apidata=null) + { + if (empty($id)) { + + if (is_numeric($this->arg('id'))) { + return User_group::staticGet($this->arg('id')); + } else if ($this->arg('id')) { + $nickname = common_canonical_nickname($this->arg('id')); + return User_group::staticGet('nickname', $nickname); + } else if ($this->arg('group_id')) { + // This is to ensure that a non-numeric user_id still + // overrides screen_name even if it doesn't get used + if (is_numeric($this->arg('group_id'))) { + return User_group::staticGet('id', $this->arg('group_id')); + } + } else if ($this->arg('group_name')) { + $nickname = common_canonical_nickname($this->arg('group_name')); + return User_group::staticGet('nickname', $nickname); + } + + } else if (is_numeric($id)) { + return User_group::staticGet($id); + } else { + $nickname = common_canonical_nickname($id); + return User_group::staticGet('nickname', $nickname); + } + } + function get_profile($id) { if (is_numeric($id)) { @@ -720,13 +1063,13 @@ class TwitterapiAction extends Action { $source_name = _($source); switch ($source) { - case 'web': - case 'xmpp': - case 'mail': - case 'omb': - case 'api': + case 'web': + case 'xmpp': + case 'mail': + case 'omb': + case 'api': break; - default: + default: $ns = Notice_source::staticGet($source); if ($ns) { $source_name = '' . $ns->name . ''; @@ -736,4 +1079,49 @@ class TwitterapiAction extends Action return $source_name; } + /** + * Returns query argument or default value if not found. Certain + * parameters used throughout the API are lightly scrubbed and + * bounds checked. This overrides Action::arg(). + * + * @param string $key requested argument + * @param string $def default value to return if $key is not provided + * + * @return var $var + */ + function arg($key, $def=null) + { + + // XXX: Do even more input validation/scrubbing? + + if (array_key_exists($key, $this->args)) { + switch($key) { + case 'page': + $page = (int)$this->args['page']; + return ($page < 1) ? 1 : $page; + case 'count': + $count = (int)$this->args['count']; + if ($count < 1) { + return 20; + } elseif ($count > 200) { + return 200; + } else { + return $count; + } + case 'since_id': + $since_id = (int)$this->args['since_id']; + return ($since_id < 1) ? 0 : $since_id; + case 'max_id': + $max_id = (int)$this->args['max_id']; + return ($max_id < 1) ? 0 : $max_id; + case 'since': + return strtotime($this->args['since']); + default: + return parent::arg($key, $def); + } + } else { + return $def; + } + } + }