Merge remote-tracking branch 'upstream/develop' into 1607-api-generic-xml

This commit is contained in:
Michael Vogel 2016-07-15 16:15:20 +02:00
commit 4e25d08a77
6 changed files with 168 additions and 130 deletions

View file

@ -658,6 +658,13 @@
} }
/**
* @brief return api-formatted array for item's author and owner
*
* @param App $a
* @param array $item : item from db
* @return array(array:author, array:owner)
*/
function api_item_get_user(&$a, $item) { function api_item_get_user(&$a, $item) {
// Make sure that there is an entry in the global contacts for author and owner // Make sure that there is an entry in the global contacts for author and owner
@ -667,20 +674,16 @@
get_gcontact_id(array("url" => $item['owner-link'], "network" => $item['network'], get_gcontact_id(array("url" => $item['owner-link'], "network" => $item['network'],
"photo" => $item['owner-avatar'], "name" => $item['owner-name'])); "photo" => $item['owner-avatar'], "name" => $item['owner-name']));
// Comments in threads may appear as wall-to-wall postings.
// So only take the owner at the top posting.
if ($item["id"] == $item["parent"])
$status_user = api_get_user($a,$item["owner-link"]);
else
$status_user = api_get_user($a,$item["author-link"]); $status_user = api_get_user($a,$item["author-link"]);
$status_user["protected"] = (($item["allow_cid"] != "") OR $status_user["protected"] = (($item["allow_cid"] != "") OR
($item["allow_gid"] != "") OR ($item["allow_gid"] != "") OR
($item["deny_cid"] != "") OR ($item["deny_cid"] != "") OR
($item["deny_gid"] != "") OR ($item["deny_gid"] != "") OR
$item["private"]); $item["private"]);
return ($status_user); $owner_user = api_get_user($a,$item["owner-link"]);
return (array($status_user, $owner_user));
} }
@ -693,8 +696,9 @@
function api_array_to_xml($data, $ename="") { function api_array_to_xml($data, $ename="") {
$attrs=""; $attrs="";
$childs=""; $childs="";
if (count($data)==1 && !is_array($data[0])) { if (count($data)==1 && !is_array($data[array_keys($data)[0]])) {
$ename = array_keys($data)[0]; $ename = array_keys($data)[0];
$ename = trim($ename,'$');
$v = $data[$ename]; $v = $data[$ename];
return "<$ename>$v</$ename>"; return "<$ename>$v</$ename>";
} }
@ -2109,7 +2113,6 @@
} }
function api_convert_item($item) { function api_convert_item($item) {
$body = $item['body']; $body = $item['body'];
$attachments = api_get_attachments($body); $attachments = api_get_attachments($body);
@ -2147,7 +2150,12 @@
$entities = api_get_entitities($statustext, $body); $entities = api_get_entitities($statustext, $body);
return(array("text" => $statustext, "html" => $statushtml, "attachments" => $attachments, "entities" => $entities)); return array(
"text" => $statustext,
"html" => $statushtml,
"attachments" => $attachments,
"entities" => $entities
);
} }
function api_get_attachments(&$body) { function api_get_attachments(&$body) {
@ -2342,6 +2350,33 @@
return $text; return $text;
} }
/**
* @brief return <a href='url'>name</a> as array
*
* @param string $txt
* @return array
* name => 'name'
* 'url => 'url'
*/
function api_contactlink_to_array($txt) {
$match = array();
$r = preg_match_all('|<a href="([^"]*)">([^<]*)</a>|', $txt, $match);
if ($r && count($match)==3) {
$res = array(
'name' => $match[2],
'url' => $match[1]
);
} else {
$res = array(
'name' => $text,
'url' => ""
);
}
return $res;
}
/** /**
* @brief return likes, dislikes and attend status for item * @brief return likes, dislikes and attend status for item
* *
@ -2350,7 +2385,7 @@
* likes => int count * likes => int count
* dislikes => int count * dislikes => int count
*/ */
function api_format_items_likes(&$item) { function api_format_items_activities(&$item) {
$activities = array( $activities = array(
'like' => array(), 'like' => array(),
'dislike' => array(), 'dislike' => array(),
@ -2367,9 +2402,9 @@
} }
$res = array(); $res = array();
$uri = $item['uri']; $uri = $item['uri']."-l";
foreach($activities as $k => $v) { foreach($activities as $k => $v) {
$res[$k] = (x($v,$uri)?$v[$uri]:0); $res[$k] = ( x($v,$uri) ? array_map("api_contactlink_to_array", $v[$uri]) : array() );
} }
return $res; return $res;
@ -2388,10 +2423,9 @@
$ret = Array(); $ret = Array();
foreach($r as $item) { foreach($r as $item) {
api_share_as_retweet($item);
localize_item($item); localize_item($item);
$status_user = api_item_get_user($a,$item); list($status_user, $owner_user) = api_item_get_user($a,$item);
// Look if the posts are matching if they should be filtered by user id // Look if the posts are matching if they should be filtered by user id
if ($filter_user AND ($status_user["id"] != $user_info["id"])) if ($filter_user AND ($status_user["id"] != $user_info["id"]))
@ -2452,10 +2486,11 @@
'geo' => NULL, 'geo' => NULL,
'favorited' => $item['starred'] ? true : false, 'favorited' => $item['starred'] ? true : false,
'user' => $status_user , 'user' => $status_user ,
'friendica_owner' => $owner_user,
//'entities' => NULL, //'entities' => NULL,
'statusnet_html' => $converted["html"], 'statusnet_html' => $converted["html"],
'statusnet_conversation_id' => $item['parent'], 'statusnet_conversation_id' => $item['parent'],
'friendica_activities' => api_format_items_likes($item), 'friendica_activities' => api_format_items_activities($item),
); );
if (count($converted["attachments"]) > 0) if (count($converted["attachments"]) > 0)
@ -2472,15 +2507,31 @@
// Retweets are only valid for top postings // Retweets are only valid for top postings
// It doesn't work reliable with the link if its a feed // It doesn't work reliable with the link if its a feed
$IsRetweet = ($item['owner-link'] != $item['author-link']); #$IsRetweet = ($item['owner-link'] != $item['author-link']);
if ($IsRetweet) #if ($IsRetweet)
$IsRetweet = (($item['owner-name'] != $item['author-name']) OR ($item['owner-avatar'] != $item['author-avatar'])); # $IsRetweet = (($item['owner-name'] != $item['author-name']) OR ($item['owner-avatar'] != $item['author-avatar']));
if ($IsRetweet AND ($item["id"] == $item["parent"])) {
if ($item["id"] == $item["parent"]) {
$retweeted_item = api_share_as_retweet($item);
if ($retweeted_item !== false) {
$retweeted_status = $status; $retweeted_status = $status;
$retweeted_status["user"] = api_get_user($a,$item["author-link"]); try {
$retweeted_status["user"] = api_get_user($a,$retweeted_item["author-link"]);
} catch( BadRequestException $e ) {
// user not found. should be found?
/// @todo check if the user should be always found
$retweeted_status["user"] = array();
}
$status["retweeted_status"] = $retweeted_status; $rt_converted = api_convert_item($retweeted_item);
$retweeted_status['text'] = $rt_converted["text"];
$retweeted_status['statusnet_html'] = $rt_converted["html"];
$retweeted_status['friendica_activities'] = api_format_items_activities($retweeted_item);
$retweeted_status['created_at'] = api_date($retweeted_item['created']);
$status['retweeted_status'] = $retweeted_status;
}
} }
// "uid" and "self" are only needed for some internal stuff, so remove it from here // "uid" and "self" are only needed for some internal stuff, so remove it from here
@ -3039,23 +3090,29 @@
} }
api_register_func('api/friendica/remoteauth', 'api_friendica_remoteauth', true); api_register_func('api/friendica/remoteauth', 'api_friendica_remoteauth', true);
/**
* @brief Return the item shared, if the item contains only the [share] tag
*
* @param array $item Sharer item
* @return array Shared item or false if not a reshare
*/
function api_share_as_retweet(&$item) { function api_share_as_retweet(&$item) {
$body = trim($item["body"]); $body = trim($item["body"]);
// Skip if it isn't a pure repeated messages if (diaspora::is_reshare($body, false)===false) {
// Does it start with a share? return false;
if (strpos($body, "[share") > 0) }
return(false);
// Does it end with a share?
if (strlen($body) > (strrpos($body, "[/share]") + 8))
return(false);
$attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body); $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
// Skip if there is no shared message in there // Skip if there is no shared message in there
// we already checked this in diaspora::is_reshare()
// but better one more than one less...
if ($body == $attributes) if ($body == $attributes)
return(false); return false;
// build the fake reshared item
$reshared_item = $item;
$author = ""; $author = "";
preg_match("/author='(.*?)'/ism", $attributes, $matches); preg_match("/author='(.*?)'/ism", $attributes, $matches);
@ -3093,18 +3150,31 @@
if ($matches[1] != "") if ($matches[1] != "")
$link = $matches[1]; $link = $matches[1];
$posted = "";
preg_match("/posted='(.*?)'/ism", $attributes, $matches);
if ($matches[1] != "")
$posted= $matches[1];
preg_match('/posted="(.*?)"/ism', $attributes, $matches);
if ($matches[1] != "")
$posted = $matches[1];
$shared_body = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$2",$body); $shared_body = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$2",$body);
if (($shared_body == "") OR ($profile == "") OR ($author == "") OR ($avatar == "")) if (($shared_body == "") || ($profile == "") || ($author == "") || ($avatar == "") || ($posted == ""))
return(false); return false;
$item["body"] = $shared_body;
$item["author-name"] = $author;
$item["author-link"] = $profile;
$item["author-avatar"] = $avatar;
$item["plink"] = $link;
return(true);
$reshared_item["body"] = $shared_body;
$reshared_item["author-name"] = $author;
$reshared_item["author-link"] = $profile;
$reshared_item["author-avatar"] = $avatar;
$reshared_item["plink"] = $link;
$reshared_item["created"] = $posted;
$reshared_item["edited"] = $posted;
return $reshared_item;
} }

View file

@ -1,8 +1,6 @@
{{* used in include/api.php 'api_statuses_friends' and 'api_statuses_followers' *}}
<!-- TEMPLATE APPEARS UNUSED -->
<users type="array"> <users type="array">
{{foreach $users as $u}} {{foreach $users as $u}}
{{include file="api_user_xml.tpl" user=$u}} <user>{{include file="api_user_xml.tpl" user=$u}}</user>
{{/foreach}} {{/foreach}}
</users> </users>

View file

@ -0,0 +1,25 @@
{{* shared structure for statuses. includers must define root element *}}
<text>{{$status.text}}</text>
<truncated>{{$status.truncated}}</truncated>
<created_at>{{$status.created_at}}</created_at>
<in_reply_to_status_id>{{$status.in_reply_to_status_id}}</in_reply_to_status_id>
<source>{{$status.source}}</source>
<id>{{$status.id}}</id>
<in_reply_to_user_id>{{$status.in_reply_to_user_id}}</in_reply_to_user_id>
<in_reply_to_screen_name>{{$status.in_reply_to_screen_name}}</in_reply_to_screen_name>
<geo>{{$status.geo}}</geo>
<favorited>{{$status.favorited}}</favorited>
<user>{{include file="api_user_xml.tpl" user=$status.user}}</user>
<friendica:owner>{{include file="api_user_xml.tpl" user=$status.friendica_owner}}</friendica:owner>
<statusnet:html>{{$status.statusnet_html}}</statusnet:html>
<statusnet:conversation_id>{{$status.statusnet_conversation_id}}</statusnet:conversation_id>
<url>{{$status.url}}</url>
<coordinates>{{$status.coordinates}}</coordinates>
<place>{{$status.place}}</place>
<contributors>{{$status.contributors}}</contributors>
{{if $status.retweeted_status}}<retweeted_status>{{include file="api_single_status_xml.tpl" status=$status.retweeted_status}}</retweeted_status>{{/if}}
<friendica:activities>
{{foreach $status.friendica_activities as $k=>$v}}
<friendica:{{$k}}>{{$v|count}}</friendica:{{$k}}>
{{/foreach}}
</friendica:activities>

View file

@ -1,47 +1,8 @@
{{* used in api.php to return a single status *}}
<status>{{if $status}} <status
<created_at>{{$status.created_at}}</created_at> xmlns:statusnet="http://status.net/schema/api/1/"
<id>{{$status.id}}</id> xmlns:friendica="http://friendi.ca/schema/api/1/">
<text>{{$status.text}}</text> {{if $status}}
<source>{{$status.source}}</source> {{include file="api_single_status_xml.tpl" status=$status}}
<truncated>{{$status.truncated}}</truncated> {{/if}}
<in_reply_to_status_id>{{$status.in_reply_to_status_id}}</in_reply_to_status_id> </status>
<in_reply_to_user_id>{{$status.in_reply_to_user_id}}</in_reply_to_user_id>
<favorited>{{$status.favorited}}</favorited>
<in_reply_to_screen_name>{{$status.in_reply_to_screen_name}}</in_reply_to_screen_name>
<geo>{{$status.geo}}</geo>
<coordinates>{{$status.coordinates}}</coordinates>
<place>{{$status.place}}</place>
<contributors>{{$status.contributors}}</contributors>
<user>
<id>{{$status.user.id}}</id>
<name>{{$status.user.name}}</name>
<screen_name>{{$status.user.screen_name}}</screen_name>
<location>{{$status.user.location}}</location>
<description>{{$status.user.description}}</description>
<profile_image_url>{{$status.user.profile_image_url}}</profile_image_url>
<url>{{$status.user.url}}</url>
<protected>{{$status.user.protected}}</protected>
<followers_count>{{$status.user.followers}}</followers_count>
<profile_background_color>{{$status.user.profile_background_color}}</profile_background_color>
<profile_text_color>{{$status.user.profile_text_color}}</profile_text_color>
<profile_link_color>{{$status.user.profile_link_color}}</profile_link_color>
<profile_sidebar_fill_color>{{$status.user.profile_sidebar_fill_color}}</profile_sidebar_fill_color>
<profile_sidebar_border_color>{{$status.user.profile_sidebar_border_color}}</profile_sidebar_border_color>
<friends_count>{{$status.user.friends_count}}</friends_count>
<created_at>{{$status.user.created_at}}</created_at>
<favourites_count>{{$status.user.favourites_count}}</favourites_count>
<utc_offset>{{$status.user.utc_offset}}</utc_offset>
<time_zone>{{$status.user.time_zone}}</time_zone>
<profile_background_image_url>{{$status.user.profile_background_image_url}}</profile_background_image_url>
<profile_background_tile>{{$status.user.profile_background_tile}}</profile_background_tile>
<profile_use_background_image>{{$status.user.profile_use_background_image}}</profile_use_background_image>
<notifications></notifications>
<geo_enabled>{{$status.user.geo_enabled}}</geo_enabled>
<verified>{{$status.user.verified}}</verified>
<following></following>
<statuses_count>{{$status.user.statuses_count}}</statuses_count>
<lang>{{$status.user.lang}}</lang>
<contributors_enabled>{{$status.user.contributors_enabled}}</contributors_enabled>
</user>
{{/if}}</status>

View file

@ -2,25 +2,9 @@
<statuses type="array" <statuses type="array"
xmlns:statusnet="http://status.net/schema/api/1/" xmlns:statusnet="http://status.net/schema/api/1/"
xmlns:friendica="http://friendi.ca/schema/api/1/"> xmlns:friendica="http://friendi.ca/schema/api/1/">
{{foreach $statuses as $status}} <status> {{foreach $statuses as $status}}
<text>{{$status.text}}</text> <status>
<truncated>{{$status.truncated}}</truncated> {{include file="api_single_status_xml.tpl" status=$status}}
<created_at>{{$status.created_at}}</created_at>
<in_reply_to_status_id>{{$status.in_reply_to_status_id}}</in_reply_to_status_id>
<source>{{$status.source}}</source>
<id>{{$status.id}}</id>
<in_reply_to_user_id>{{$status.in_reply_to_user_id}}</in_reply_to_user_id>
<in_reply_to_screen_name>{{$status.in_reply_to_screen_name}}</in_reply_to_screen_name>
<geo>{{$status.geo}}</geo>
<favorited>{{$status.favorited}}</favorited>
{{include file="api_user_xml.tpl" user=$status.user}} <statusnet:html>{{$status.statusnet_html}}</statusnet:html>
<statusnet:conversation_id>{{$status.statusnet_conversation_id}}</statusnet:conversation_id>
<url>{{$status.url}}</url>
<coordinates>{{$status.coordinates}}</coordinates>
<place>{{$status.place}}</place>
<contributors>{{$status.contributors}}</contributors>
<friendica:activities>{{foreach $status.friendica_activities as $k=>$v}}
<friendica:{{$k}}>{{$v}}</friendica:{{$k}}>
{{/foreach}}</friendica:activities>
</status> </status>
{{/foreach}}</statuses> {{/foreach}}
</statuses>

View file

@ -1,5 +1,5 @@
{{* includer template MUST provide root element *}}
<user>
<id>{{$user.id}}</id> <id>{{$user.id}}</id>
<name>{{$user.name}}</name> <name>{{$user.name}}</name>
<screen_name>{{$user.screen_name}}</screen_name> <screen_name>{{$user.screen_name}}</screen_name>
@ -44,4 +44,4 @@
<place>{{$user.status.place}}</place> <place>{{$user.status.place}}</place>
<contributors>{{$user.status.contributors}}</contributors> <contributors>{{$user.status.contributors}}</contributors>
{{/if}}</status> {{/if}}</status>
</user>