X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fapi.php;h=9c64b4a3cfc29606dd688a173d8983feaf13440a;hb=3e4af13772b189392272c9dfaad90054fe1d7173;hp=8a29c82dd8a4174f4a60da9ae786320b23b57847;hpb=57f8496c536ff8531b271e6012164ffdd01fe060;p=friendica.git diff --git a/include/api.php b/include/api.php index 8a29c82dd8..9c64b4a3cf 100644 --- a/include/api.php +++ b/include/api.php @@ -470,12 +470,12 @@ function api_rss_extra(App $a, $arr, $user_info) * @return bool|string * Contact url or False if contact id is unknown */ -function api_unique_id_to_url($id) +function api_unique_id_to_nurl($id) { - $r = dba::select('contact', array('url'), array('uid' => 0, 'id' => $id), array('limit' => 1)); + $r = dba::select('contact', array('nurl'), array('uid' => 0, 'id' => $id), array('limit' => 1)); if (DBM::is_result($r)) { - return $r["url"]; + return $r["nurl"]; } else { return false; } @@ -486,9 +486,8 @@ function api_unique_id_to_url($id) * * @param object $a App * @param int|string $contact_id Contact ID or URL - * @param string $type Return type (for errors) */ -function api_get_user(App $a, $contact_id = null, $type = "json") +function api_get_user(App $a, $contact_id = null) { global $called_api; @@ -511,7 +510,7 @@ function api_get_user(App $a, $contact_id = null, $type = "json") // Searching for contact id with uid = 0 if (!is_null($contact_id) && (intval($contact_id) != 0)) { - $user = dbesc(api_unique_id_to_url($contact_id)); + $user = dbesc(api_unique_id_to_nurl($contact_id)); if ($user == "") { throw new BadRequestException("User not found."); @@ -525,7 +524,7 @@ function api_get_user(App $a, $contact_id = null, $type = "json") } if (is_null($user) && x($_GET, 'user_id')) { - $user = dbesc(api_unique_id_to_url($_GET['user_id'])); + $user = dbesc(api_unique_id_to_nurl($_GET['user_id'])); if ($user == "") { throw new BadRequestException("User not found."); @@ -559,7 +558,7 @@ function api_get_user(App $a, $contact_id = null, $type = "json") $argid = count($called_api); list($user, $null) = explode(".", $a->argv[$argid]); if (is_numeric($user)) { - $user = dbesc(api_unique_id_to_url($user)); + $user = dbesc(api_unique_id_to_nurl($user)); if ($user == "") { return false; @@ -1325,8 +1324,9 @@ function api_status_show($type) 'retweeted' => false, 'possibly_sensitive' => false, 'lang' => "", - 'statusnet_html' => $converted["html"], - 'statusnet_conversation_id' => $lastwall['parent'], + 'statusnet_html' => $converted["html"], + 'statusnet_conversation_id' => $lastwall['parent'], + 'external_url' => System::baseUrl() . "/display/" . $lastwall['guid'], ); if (count($converted["attachments"]) > 0) { @@ -1414,7 +1414,8 @@ function api_users_show($type) $geo => null, 'favorited' => $lastwall['starred'] ? true : false, 'statusnet_html' => $converted["html"], - 'statusnet_conversation_id' => $lastwall['parent'], + 'statusnet_conversation_id' => $lastwall['parent'], + 'external_url' => System::baseUrl() . "/display/" . $lastwall['guid'], ); if (count($converted["attachments"]) > 0) { @@ -1611,40 +1612,131 @@ function api_statuses_public_timeline($type) $start = $page * $count; - if ($max_id > 0) { - $sql_extra = 'AND `item`.`id` <= ' . intval($max_id); + if ($exclude_replies && !$conversation_id) { + if ($max_id > 0) { + $sql_extra = 'AND `thread`.`iid` <= ' . intval($max_id); + } + + $r = dba::p("SELECT " . item_fieldlists() . " + FROM `thread` + STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid` + " . item_joins() . " + STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` + AND NOT `user`.`hidewall` + AND `verb` = ? + AND NOT `thread`.`private` + AND `thread`.`wall` + AND `thread`.`visible` + AND NOT `thread`.`deleted` + AND NOT `thread`.`moderated` + AND `thread`.`iid` > ? + $sql_extra + ORDER BY `thread`.`iid` DESC + LIMIT " . intval($start) . ", " . intval($count), + ACTIVITY_POST, + $since_id + ); + + $r = dba::inArray($r); + } else { + if ($max_id > 0) { + $sql_extra = 'AND `item`.`id` <= ' . intval($max_id); + } + if ($conversation_id > 0) { + $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id); + } + + $r = dba::p("SELECT " . item_fieldlists() . " + FROM `item` + " . item_joins() . " + STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid` + AND NOT `user`.`hidewall` + AND `verb` = ? + AND NOT `item`.`private` + AND `item`.`wall` + AND `item`.`visible` + AND NOT `item`.`deleted` + AND NOT `item`.`moderated` + AND `item`.`id` > ? + $sql_extra + ORDER BY `item`.`id` DESC + LIMIT " . intval($start) . ", " . intval($count), + ACTIVITY_POST, + $since_id + ); + + $r = dba::inArray($r); } - if ($exclude_replies > 0) { - $sql_extra .= ' AND `item`.`parent` = `item`.`id`'; + + $ret = api_format_items($r, $user_info, false, $type); + + $data = array('status' => $ret); + switch ($type) { + case "atom": + case "rss": + $data = api_rss_extra($a, $data, $user_info); + break; } - if ($conversation_id > 0) { - $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id); + + return api_format_data("statuses", $type, $data); +} + +/// @TODO move to top of file or somewhere better +api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline', true); + +/** + * @brief Returns the list of public federated posts this node knows about + * + * @param string $type Return format: json, xml, atom, rss + * @return array|string + * @throws ForbiddenException + */ +function api_statuses_networkpublic_timeline($type) +{ + $a = get_app(); + + if (api_user() === false) { + throw new ForbiddenException(); } - $r = q( - "SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, - `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, - `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`, - `contact`.`id` AS `cid`, - `user`.`nickname`, `user`.`hidewall` - FROM `item` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` = `item`.`uid` - AND (NOT `contact`.`blocked` OR `contact`.`pending`) - STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid` - AND NOT `user`.`hidewall` - WHERE `verb` = '%s' AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` - AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' - AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' - AND NOT `item`.`private` AND `item`.`wall` + $user_info = api_get_user($a); + + $since_id = x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0; + $max_id = x($_REQUEST, 'max_id') ? $_REQUEST['max_id'] : 0; + + // pagination + $count = x($_REQUEST, 'count') ? $_REQUEST['count'] : 20; + $page = x($_REQUEST, 'page') ? $_REQUEST['page'] : 1; + if ($page < 1) { + $page = 1; + } + $start = ($page - 1) * $count; + + $sql_extra = ''; + if ($max_id > 0) { + $sql_extra = 'AND `thread`.`iid` <= ' . intval($max_id); + } + + $r = dba::p("SELECT " . item_fieldlists() . " + FROM `thread` + STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid` + " . item_joins() . " + WHERE `thread`.`uid` = 0 + AND `verb` = ? + AND NOT `thread`.`private` + AND `thread`.`visible` + AND NOT `thread`.`deleted` + AND NOT `thread`.`moderated` + AND `thread`.`iid` > ? $sql_extra - AND `item`.`id`>%d - ORDER BY `item`.`id` DESC LIMIT %d, %d ", - dbesc(ACTIVITY_POST), - intval($since_id), - intval($start), - intval($count) + ORDER BY `thread`.`iid` DESC + LIMIT " . intval($start) . ", " . intval($count), + ACTIVITY_POST, + $since_id ); + $r = dba::inArray($r); + $ret = api_format_items($r, $user_info, false, $type); $data = array('status' => $ret); @@ -1659,7 +1751,7 @@ function api_statuses_public_timeline($type) } /// @TODO move to top of file or somewhere better -api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline', true); +api_register_func('api/statuses/networkpublic_timeline', 'api_statuses_networkpublic_timeline', true); /** * @TODO nothing to say? @@ -2019,6 +2111,13 @@ function api_statuses_mentions($type) api_register_func('api/statuses/mentions', 'api_statuses_mentions', true); api_register_func('api/statuses/replies', 'api_statuses_mentions', true); +/** + * @brief Returns a user's public timeline + * + * @param string $type Either "json" or "xml" + * @return string|array + * @throws ForbiddenException + */ function api_statuses_user_timeline($type) { $a = get_app(); @@ -2028,7 +2127,6 @@ function api_statuses_user_timeline($type) } $user_info = api_get_user($a); - // get last network messages logger( "api_statuses_user_timeline: api_user: ". api_user() . @@ -2037,18 +2135,18 @@ function api_statuses_user_timeline($type) LOGGER_DEBUG ); - // params - $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20); - $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] -1 : 0); - if ($page < 0) { - $page = 0; - } - $since_id = (x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0); - //$since_id = 0;//$since_id = (x($_REQUEST, 'since_id')?$_REQUEST['since_id'] : 0); - $exclude_replies = (x($_REQUEST, 'exclude_replies') ? 1 : 0); - $conversation_id = (x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0); + $since_id = x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0; + $max_id = x($_REQUEST, 'max_id') ? $_REQUEST['max_id'] : 0; + $exclude_replies = x($_REQUEST, 'exclude_replies') ? 1 : 0; + $conversation_id = x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0; - $start = $page * $count; + // pagination + $count = x($_REQUEST, 'count') ? $_REQUEST['count'] : 20; + $page = x($_REQUEST, 'page') ? $_REQUEST['page'] : 1; + if ($page < 1) { + $page = 1; + } + $start = ($page - 1) * $count; $sql_extra = ''; if ($user_info['self'] == 1) { @@ -2058,10 +2156,15 @@ function api_statuses_user_timeline($type) if ($exclude_replies > 0) { $sql_extra .= ' AND `item`.`parent` = `item`.`id`'; } + if ($conversation_id > 0) { $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id); } + if ($max_id > 0) { + $sql_extra .= ' AND `item`.`id` <= ' . intval($max_id); + } + $r = q( "SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, @@ -2074,7 +2177,7 @@ function api_statuses_user_timeline($type) AND `item`.`contact-id` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND NOT `item`.`deleted` $sql_extra - AND `item`.`id`>%d + AND `item`.`id` > %d ORDER BY `item`.`id` DESC LIMIT %d ,%d ", intval(api_user()), dbesc(ACTIVITY_POST), @@ -2769,8 +2872,9 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json") 'user' => $status_user , 'friendica_owner' => $owner_user, //'entities' => NULL, - 'statusnet_html' => $converted["html"], - 'statusnet_conversation_id' => $item['parent'], + 'statusnet_html' => $converted["html"], + 'statusnet_conversation_id' => $item['parent'], + 'external_url' => System::baseUrl() . "/display/" . $item['guid'], 'friendica_activities' => api_format_items_activities($item, $type), ); @@ -2901,11 +3005,16 @@ function api_lists_list($type) api_register_func('api/lists/list', 'api_lists_list', true); /** - * https://dev.twitter.com/docs/api/1/get/statuses/friends - * This function is deprecated by Twitter - * returns: json, xml + * @brief Returns either the friends of the follower list + * + * Note: Considers friends and followers lists to be private and won't return + * anything if any user_id parameter is passed. + * + * @param string $qtype Either "friends" or "followers" + * @return boolean|array + * @throws ForbiddenException */ -function api_statuses_f($type, $qtype) +function api_statuses_f($qtype) { $a = get_app(); @@ -2913,9 +3022,17 @@ function api_statuses_f($type, $qtype) throw new ForbiddenException(); } + // pagination + $count = x($_GET, 'count') ? $_GET['count'] : 20; + $page = x($_GET, 'page') ? $_GET['page'] : 1; + if ($page < 1) { + $page = 1; + } + $start = ($page - 1) * $count; + $user_info = api_get_user($a); - if (x($_GET, 'cursor') && $_GET['cursor']=='undefined') { + if (x($_GET, 'cursor') && $_GET['cursor'] == 'undefined') { /* this is to stop Hotot to load friends multiple times * I'm not sure if I'm missing return something or * is a bug in hotot. Workaround, meantime @@ -2938,9 +3055,25 @@ function api_statuses_f($type, $qtype) $sql_extra = " AND false "; } + if ($qtype == 'blocks') { + $sql_blocked = 'AND `blocked`'; + } else { + $sql_blocked = 'AND NOT `blocked`'; + } + $r = q( - "SELECT `nurl` FROM `contact` WHERE `uid` = %d AND NOT `self` AND (NOT `blocked` OR `pending`) $sql_extra ORDER BY `nick`", - intval(api_user()) + "SELECT `nurl` + FROM `contact` + WHERE `uid` = %d + AND NOT `self` + $sql_blocked + AND NOT `pending` + $sql_extra + ORDER BY `nick` + LIMIT %d, %d", + intval(api_user()), + intval($start), + intval($count) ); $ret = array(); @@ -2956,21 +3089,37 @@ function api_statuses_f($type, $qtype) } return array('user' => $ret); - } + +/** + * @brief Returns the list of friends of the provided user + * + * @deprecated By Twitter API in favor of friends/list + * + * @param string $type Either "json" or "xml" + * @return boolean|string|array + */ function api_statuses_friends($type) { - $data = api_statuses_f($type, "friends"); + $data = api_statuses_f("friends"); if ($data === false) { return false; } return api_format_data("users", $type, $data); } +/** + * @brief Returns the list of friends of the provided user + * + * @deprecated By Twitter API in favor of friends/list + * + * @param string $type Either "json" or "xml" + * @return boolean|string|array + */ function api_statuses_followers($type) { - $data = api_statuses_f($type, "followers"); + $data = api_statuses_f("followers"); if ($data === false) { return false; } @@ -2981,6 +3130,28 @@ function api_statuses_followers($type) api_register_func('api/statuses/friends', 'api_statuses_friends', true); api_register_func('api/statuses/followers', 'api_statuses_followers', true); +/** + * Returns the list of blocked users + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/mute-block-report-users/api-reference/get-blocks-list + * + * @param string $type Either "json" or "xml" + * + * @return boolean|string|array + * @throws UnauthorizedException + */ +function api_blocks_list($type) +{ + $data = api_statuses_f('blocks'); + if ($data === false) { + return false; + } + return api_format_data("users", $type, $data); +} + +/// @TODO move to top of file or somewhere better +api_register_func('api/blocks/list', 'api_blocks_list', true); + function api_statusnet_config($type) { $a = get_app(); @@ -4783,7 +4954,7 @@ function api_friendica_group_create($type) intval($uid) ); if (count($contact)) - $result = Group::create_member($uid, $name, $cid, $gid); + $result = Group::addMember($gid, $cid); else { $erroraddinguser = true; $errorusers[] = $cid; @@ -4846,7 +5017,7 @@ function api_friendica_group_update($type) ); if (count($contact)) { - $result = Group::create_member($uid, $name, $cid, $gid); + $result = Group::addMember($gid, $cid); } else { $erroraddinguser = true; $errorusers[] = $cid;