From d576e920d5c0f02386b51c514bb82c6bbc136875 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 19 Nov 2021 20:15:12 +0000 Subject: [PATCH 01/24] Fix legacy API --- include/api.php | 52 ++++-------------------------- src/Module/Api/Friendica/Index.php | 1 + tests/legacy/ApiTest.php | 23 ------------- 3 files changed, 8 insertions(+), 68 deletions(-) diff --git a/include/api.php b/include/api.php index d1ff1458e..a442b1e89 100644 --- a/include/api.php +++ b/include/api.php @@ -221,6 +221,7 @@ function api_call(App $a, App\Arguments $args = null) Logger::warning(API_LOG_PREFIX . 'not implemented', ['module' => 'api', 'action' => 'call', 'query' => DI::args()->getQueryString()]); throw new NotFoundException(); } catch (HTTPException $e) { + Logger::notice(API_LOG_PREFIX . 'got exception', ['module' => 'api', 'action' => 'call', 'query' => DI::args()->getQueryString(), 'error' => $e]); DI::apiResponse()->error($e->getCode(), $e->getDescription(), $e->getMessage(), $type); } } @@ -282,40 +283,6 @@ function api_unique_id_to_nurl($id) } } -/** - * return api-formatted array for item's author and owner - * - * @param App $a App - * @param array $item item from db - * @return array(array:author, array:owner) - * @throws BadRequestException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - */ -function api_item_get_user(App $a, $item) -{ - if (empty($item['author-id'])) { - $item['author-id'] = Contact::getPublicIdByUserId(BaseApi::getCurrentUserID()); - } - $status_user = DI::twitterUser()->createFromContactId($item['author-id'], BaseApi::getCurrentUserID())->toArray(); - - $author_user = $status_user; - - $status_user["protected"] = isset($item['private']) && ($item['private'] == Item::PRIVATE); - - if (($item['thr-parent'] ?? '') == ($item['uri'] ?? '')) { - if (empty($item['owner-id'])) { - $item['owner-id'] = Contact::getPublicIdByUserId(BaseApi::getCurrentUserID()); - } - $owner_user = DI::twitterUser()->createFromContactId($item['owner-id'], BaseApi::getCurrentUserID())->toArray(); - } else { - $owner_user = $author_user; - } - - return ([$status_user, $author_user, $owner_user]); -} - /** * TWITTER API */ @@ -2298,14 +2265,12 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso } foreach ((array)$items as $item) { - [$status_user, $author_user, $owner_user] = api_item_get_user($a, $item); - // Look if the posts are matching if they should be filtered by user id - if ($filter_user && ($status_user["id"] != $user_info["id"])) { + if ($filter_user && ($item["author-id"] != $user_info["id"])) { continue; } - $status = api_format_item($item, $type, $status_user, $author_user, $owner_user); + $status = api_format_item($item, $type); $ret[] = $status; } @@ -2325,13 +2290,10 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso * @throws InternalServerErrorException * @throws UnauthorizedException */ -function api_format_item($item, $type = "json", $status_user = null, $author_user = null, $owner_user = null) +function api_format_item($item, $type = "json") { - $a = DI::app(); - - if (empty($status_user) || empty($author_user) || empty($owner_user)) { - [$status_user, $author_user, $owner_user] = api_item_get_user($a, $item); - } + $author_user = DI::twitterUser()->createFromContactId($item['author-id'], BaseApi::getCurrentUserID())->toArray(); + $owner_user = DI::twitterUser()->createFromContactId($item['owner-id'], BaseApi::getCurrentUserID())->toArray(); DI::contentItem()->localize($item); @@ -2359,7 +2321,7 @@ function api_format_item($item, $type = "json", $status_user = null, $author_use 'in_reply_to_screen_name' => $in_reply_to['screen_name'], $geo => null, 'favorited' => $item['starred'] ? true : false, - 'user' => $status_user, + 'user' => $author_user, 'friendica_author' => $author_user, 'friendica_owner' => $owner_user, 'friendica_private' => $item['private'] == Item::PRIVATE, diff --git a/src/Module/Api/Friendica/Index.php b/src/Module/Api/Friendica/Index.php index dbe45f545..5806f2e48 100644 --- a/src/Module/Api/Friendica/Index.php +++ b/src/Module/Api/Friendica/Index.php @@ -23,6 +23,7 @@ namespace Friendica\Module\Api\Friendica; use Friendica\DI; use Friendica\Module\BaseApi; +require_once __DIR__ . '/../../../../include/api.php'; /** * api/friendica diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 13509bf34..490fac7dd 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -763,29 +763,6 @@ class ApiTest extends FixtureTest self::assertSelfUser(DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); } - /** - * Test the api_item_get_user() function. - * - * @return void - */ - public function testApiItemGetUser() - { - $users = api_item_get_user($this->app, []); - self::assertSelfUser($users[0]); - } - - /** - * Test the api_item_get_user() function with a different item parent. - * - * @return void - */ - public function testApiItemGetUserWithDifferentParent() - { - $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']); - self::assertSelfUser($users[0]); - self::assertEquals($users[0], $users[1]); - } - /** * Test the Arrays::walkRecursive() function. * From 4327ccddcb5fc36ac00c76f8234fa50bd85ac697 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 20 Nov 2021 09:36:17 +0000 Subject: [PATCH 02/24] Some more removed functions and parameters --- include/api.php | 265 ++++++++------------------------------- src/Module/BaseApi.php | 20 ++- tests/legacy/ApiTest.php | 36 +----- 3 files changed, 73 insertions(+), 248 deletions(-) diff --git a/include/api.php b/include/api.php index a442b1e89..e5d963032 100644 --- a/include/api.php +++ b/include/api.php @@ -263,26 +263,6 @@ function api_rss_extra($arr, $user_info) return $arr; } - -/** - * Unique contact to contact url. - * - * @param int $id Contact id - * @return bool|string - * Contact url or False if contact id is unknown - * @throws Exception - */ -function api_unique_id_to_nurl($id) -{ - $r = DBA::selectFirst('contact', ['nurl'], ['id' => $id]); - - if (DBA::isResult($r)) { - return $r["nurl"]; - } else { - return false; - } -} - /** * TWITTER API */ @@ -371,13 +351,9 @@ function api_statuses_mediap($type) BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - $_REQUEST['profile_uid'] = BaseApi::getCurrentUserID(); $_REQUEST['api_source'] = true; $txt = requestdata('status') ?? ''; - /// @TODO old-lost code? - //$txt = urldecode(requestdata('status')); if ((strpos($txt, '<') !== false) || (strpos($txt, '>') !== false)) { $txt = HTML::toBBCodeVideo($txt); @@ -388,8 +364,6 @@ function api_statuses_mediap($type) } $txt = HTML::toBBCode($txt); - DI::args()->getArgv()[1] = $user_info['screen_name']; //should be set to username? - $picture = wall_upload_post($a, false); // now that we have the img url in bbcode we can add it to the status and insert the wall item. @@ -597,8 +571,6 @@ api_register_func('api/statuses/update_with_media', 'api_statuses_update', true, */ function api_media_upload() { - $a = DI::app(); - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); if (empty($_FILES['media'])) { @@ -606,7 +578,7 @@ function api_media_upload() throw new BadRequestException("No media."); } - $media = wall_upload_post($a, false); + $media = wall_upload_post(DI::app(), false); if (!$media) { // Output error throw new InternalServerErrorException(); @@ -845,12 +817,9 @@ function api_users_lookup($type) $users = []; if (!empty($_REQUEST['user_id'])) { - foreach (explode(',', $_REQUEST['user_id']) as $id) { - if (!empty($id)) { - $cid = BaseApi::getContactIDForSearchterm($id); - if (!empty($cid)) { - $users[] = DI::twitterUser()->createFromContactId($cid, BaseApi::getCurrentUserID())->toArray(); - } + foreach (explode(',', $_REQUEST['user_id']) as $cid) { + if (!empty($cid) && is_numeric($cid)) { + $users[] = DI::twitterUser()->createFromContactId((int)$cid, BaseApi::getCurrentUserID())->toArray(); } } } @@ -883,8 +852,6 @@ function api_search($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - if (empty($_REQUEST['q'])) { throw new BadRequestException('q parameter is required.'); } @@ -956,7 +923,7 @@ function api_search($type) $statuses = $statuses ?: Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); - $data['status'] = api_format_items(Post::toArray($statuses), $user_info); + $data['status'] = api_format_items(Post::toArray($statuses), $type); bindComments($data['status']); @@ -987,8 +954,6 @@ function api_statuses_home_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - unset($_REQUEST['user_id']); unset($_GET['user_id']); @@ -1028,7 +993,7 @@ function api_statuses_home_timeline($type) $items = Post::toArray($statuses); - $ret = api_format_items($items, $user_info, false, $type); + $ret = api_format_items($items, $type); // Set all posts from the query above to seen $idarray = []; @@ -1050,7 +1015,7 @@ function api_statuses_home_timeline($type) case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); break; } @@ -1078,8 +1043,6 @@ function api_statuses_public_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - // get last network messages // params @@ -1124,7 +1087,7 @@ function api_statuses_public_timeline($type) $r = Post::toArray($statuses); } - $ret = api_format_items($r, $user_info, false, $type); + $ret = api_format_items($r, $type); bindComments($ret); @@ -1133,7 +1096,7 @@ function api_statuses_public_timeline($type) case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); break; } @@ -1158,8 +1121,6 @@ function api_statuses_networkpublic_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - $since_id = $_REQUEST['since_id'] ?? 0; $max_id = $_REQUEST['max_id'] ?? 0; @@ -1180,7 +1141,7 @@ function api_statuses_networkpublic_timeline($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::toArray(Post::selectForUser(BaseApi::getCurrentUserID(), Item::DISPLAY_FIELDLIST, $condition, $params)); - $ret = api_format_items($statuses, $user_info, false, $type); + $ret = api_format_items($statuses, $type); bindComments($ret); @@ -1189,7 +1150,7 @@ function api_statuses_networkpublic_timeline($type) case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); break; } @@ -1262,7 +1223,7 @@ function api_statuses_show($type) throw new BadRequestException(sprintf("There is no status or conversation with the id %d.", $id)); } - $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); + $ret = api_format_items(Post::toArray($statuses), $type); if ($conversation) { $data = ['status' => $ret]; @@ -1342,7 +1303,7 @@ function api_conversation_show($type) throw new BadRequestException("There is no status with id $id."); } - $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); + $ret = api_format_items(Post::toArray($statuses), $type); $data = ['status' => $ret]; return DI::apiResponse()->formatData("statuses", $type, $data); @@ -1367,8 +1328,6 @@ api_register_func('api/statusnet/conversation', 'api_conversation_show', true); */ function api_statuses_repeat($type) { - $a = DI::app(); - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); // params @@ -1417,7 +1376,7 @@ function api_statuses_repeat($type) $_REQUEST['source'] = api_source(); } - $item_id = item_post($a); + $item_id = item_post(DI::app()); } } else { throw new ForbiddenException(); @@ -1488,8 +1447,6 @@ function api_statuses_mentions($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - unset($_REQUEST['user_id']); unset($_GET['user_id']); @@ -1529,14 +1486,14 @@ function api_statuses_mentions($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); - $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); + $ret = api_format_items(Post::toArray($statuses), $type); $data = ['status' => $ret]; switch ($type) { case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); break; } @@ -1563,10 +1520,11 @@ function api_statuses_user_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $uid = BaseApi::getCurrentUserID(); - Logger::info('api_statuses_user_timeline', ['api_user' => BaseApi::getCurrentUserID(), 'user_info' => $user_info, '_REQUEST' => $_REQUEST]); + Logger::info('api_statuses_user_timeline', ['api_user' => $uid, '_REQUEST' => $_REQUEST]); + $cid = BaseApi::getContactIDForSearchterm($_REQUEST['screen_name'] ?? '', $_REQUEST['user_id'] ?? 0, $uid); $since_id = $_REQUEST['since_id'] ?? 0; $max_id = $_REQUEST['max_id'] ?? 0; $exclude_replies = !empty($_REQUEST['exclude_replies']); @@ -1578,12 +1536,8 @@ function api_statuses_user_timeline($type) $start = max(0, ($page - 1) * $count); - $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ? AND `contact-id` = ?", - BaseApi::getCurrentUserID(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, $user_info['cid']]; - - if ($user_info['self'] == 1) { - $condition[0] .= ' AND `wall` '; - } + $condition = ["(`uid` = ? OR (`uid` = ? AND NOT `global`)) AND `gravity` IN (?, ?) AND `id` > ? AND `author-id` = ?", + 0, $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, $cid]; if ($exclude_replies) { $condition[0] .= ' AND `gravity` = ?'; @@ -1600,9 +1554,9 @@ function api_statuses_user_timeline($type) $condition[] = $max_id; } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); - $ret = api_format_items(Post::toArray($statuses), $user_info, true, $type); + $ret = api_format_items(Post::toArray($statuses), $type); bindComments($ret); @@ -1611,7 +1565,7 @@ function api_statuses_user_timeline($type) case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId($uid)->toArray()); break; } @@ -1679,16 +1633,14 @@ function api_favorites_create_destroy($type) throw new InternalServerErrorException("DB error"); } - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - $rets = api_format_items([$item], $user_info, false, $type); - $ret = $rets[0]; + $ret = api_format_item($item, $type); $data = ['status' => $ret]; switch ($type) { case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); break; } @@ -1715,38 +1667,32 @@ function api_favorites($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - // in friendica starred item are private // return favorites only for self - Logger::info(API_LOG_PREFIX . 'for {self}', ['module' => 'api', 'action' => 'favorites', 'self' => $user_info['self']]); + Logger::info(API_LOG_PREFIX . 'for {self}', ['module' => 'api', 'action' => 'favorites']); - if ($user_info['self'] == 0) { - $ret = []; - } else { - // params - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - $count = $_GET['count'] ?? 20; - $page = $_REQUEST['page'] ?? 1; + // params + $since_id = $_REQUEST['since_id'] ?? 0; + $max_id = $_REQUEST['max_id'] ?? 0; + $count = $_GET['count'] ?? 20; + $page = $_REQUEST['page'] ?? 1; - $start = max(0, ($page - 1) * $count); + $start = max(0, ($page - 1) * $count); - $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ? AND `starred`", - BaseApi::getCurrentUserID(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; + $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ? AND `starred`", + BaseApi::getCurrentUserID(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; - $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; + $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - if ($max_id > 0) { - $condition[0] .= " AND `id` <= ?"; - $condition[] = $max_id; - } - - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); - - $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); + if ($max_id > 0) { + $condition[0] .= " AND `id` <= ?"; + $condition[] = $max_id; } + $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + + $ret = api_format_items(Post::toArray($statuses), $type); + bindComments($ret); $data = ['status' => $ret]; @@ -1754,7 +1700,7 @@ function api_favorites($type) case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); break; } @@ -2245,8 +2191,6 @@ function api_format_items_activities($item, $type = "json") * format items to be returned by api * * @param array $items array of items - * @param array $user_info - * @param bool $filter_user filter items by $user_info * @param string $type Return type (atom, rss, xml, json) * @return array * @throws BadRequestException @@ -2254,25 +2198,11 @@ function api_format_items_activities($item, $type = "json") * @throws InternalServerErrorException * @throws UnauthorizedException */ -function api_format_items($items, $user_info, $filter_user = false, $type = "json") +function api_format_items($items, $type = "json") { - $a = DI::app(); - $ret = []; - - if (empty($items)) { - return $ret; - } - - foreach ((array)$items as $item) { - // Look if the posts are matching if they should be filtered by user id - if ($filter_user && ($item["author-id"] != $user_info["id"])) { - continue; - } - - $status = api_format_item($item, $type); - - $ret[] = $status; + foreach ($items as $item) { + $ret[] = api_format_item($item, $type); } return $ret; @@ -2526,8 +2456,6 @@ function api_lists_statuses($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - unset($_REQUEST['user_id']); unset($_GET['user_id']); @@ -2569,14 +2497,14 @@ function api_lists_statuses($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); - $items = api_format_items(Post::toArray($statuses), $user_info, false, $type); + $items = api_format_items(Post::toArray($statuses), $type); $data = ['status' => $items]; switch ($type) { case "atom": break; case "rss": - $data = api_rss_extra($data, $user_info); + $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); break; } @@ -2794,23 +2722,8 @@ function api_direct_messages_new($type) $sender = DI::twitterUser()->createFromUserId($uid)->toArray(); - $recipient = null; - if (!empty($_POST['screen_name'])) { - $contacts = Contact::selectToArray(['id', 'nurl', 'network'], ['uid' => BaseApi::getCurrentUserID(), 'nick' => $_POST['screen_name']]); - if (DBA::isResult($contacts)) { - // Selecting the id by priority, friendica first - api_best_nickname($contacts); - - $recipient = DI::twitterUser()->createFromContactId($contacts[0]['id'], $uid)->toArray(); - } - } else { - $cid = BaseApi::getContactIDForSearchterm($_POST['user_id']); - if (!empty($cid)) { - $recipient = DI::twitterUser()->createFromContactId($cid, $uid)->toArray(); - } - } - - if (empty($recipient)) { + $cid = BaseApi::getContactIDForSearchterm($_POST['screen_name'] ?? '', $_POST['user_id'] ?? 0, $uid); + if (empty($cid)) { throw new NotFoundException('Recipient not found'); } @@ -2827,11 +2740,11 @@ function api_direct_messages_new($type) } } - $id = Mail::send($recipient['cid'], $_POST['text'], $sub, $replyto); + $id = Mail::send($cid, $_POST['text'], $sub, $replyto); if ($id > -1) { $mail = DBA::selectFirst('mail', [], ['id' => $id]); - $ret = api_format_messages($mail, $recipient, $sender); + $ret = api_format_messages($mail, DI::twitterUser()->createFromContactId($cid, $uid)->toArray(), $sender); } else { $ret = ["error" => $id]; } @@ -3846,7 +3759,7 @@ function prepare_photo_data($type, $scale, $photo_id) $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition); // prepare output of comments - $commentData = api_format_items(Post::toArray($statuses), $user_info, false, $type); + $commentData = api_format_items(Post::toArray($statuses), $type); $comments = []; if ($type == "xml") { $k = 0; @@ -3981,74 +3894,6 @@ function api_clean_plain_items($text) return $text; } -/** - * - * @param array $contacts - * - * @return void - */ -function api_best_nickname(&$contacts) -{ - $best_contact = []; - - if (count($contacts) == 0) { - return; - } - - foreach ($contacts as $contact) { - if ($contact["network"] == "") { - $contact["network"] = "dfrn"; - $best_contact = [$contact]; - } - } - - if (sizeof($best_contact) == 0) { - foreach ($contacts as $contact) { - if ($contact["network"] == "dfrn") { - $best_contact = [$contact]; - } - } - } - - if (sizeof($best_contact) == 0) { - foreach ($contacts as $contact) { - if ($contact["network"] == "dspr") { - $best_contact = [$contact]; - } - } - } - - if (sizeof($best_contact) == 0) { - foreach ($contacts as $contact) { - if ($contact["network"] == "stat") { - $best_contact = [$contact]; - } - } - } - - if (sizeof($best_contact) == 0) { - foreach ($contacts as $contact) { - if ($contact["network"] == "pump") { - $best_contact = [$contact]; - } - } - } - - if (sizeof($best_contact) == 0) { - foreach ($contacts as $contact) { - if ($contact["network"] == "twit") { - $best_contact = [$contact]; - } - } - } - - if (sizeof($best_contact) == 1) { - $contacts = $best_contact; - } else { - $contacts = [$contacts[0]]; - } -} - /** * Return all or a specified group of the user with the containing contacts. * @@ -4438,7 +4283,7 @@ function api_friendica_notification_seen($type) $item = Post::selectFirstForUser(BaseApi::getCurrentUserID(), [], ['id' => $Notify->iid, 'uid' => BaseApi::getCurrentUserID()]); if (DBA::isResult($item)) { // we found the item, return it to the user - $ret = api_format_items([$item], $user_info, false, $type); + $ret = api_format_items([$item], $type); $data = ['status' => $ret]; return DI::apiResponse()->formatData('status', $type, $data); } diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index 2e8e1d15b..eaefdd0bd 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -27,6 +27,7 @@ use Friendica\Core\System; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Post; +use Friendica\Model\User; use Friendica\Network\HTTPException; use Friendica\Security\BasicAuth; use Friendica\Security\OAuth; @@ -292,12 +293,23 @@ class BaseApi extends BaseModule } } - public static function getContactIDForSearchterm($searchterm) + public static function getContactIDForSearchterm(string $screen_name, int $cid, int $uid) { - if (intval($searchterm) == 0) { - $cid = Contact::getIdForURL($searchterm, 0, false); + if (!empty($cid)) { + return $cid; + } + + if (strpos($screen_name, '@') !== false) { + $cid = Contact::getIdForURL($screen_name, 0, false); } else { - $cid = intval($searchterm); + $user = User::getByNickname($screen_name, ['uid']); + if (!empty($user['uid'])) { + $cid = Contact::getPublicIdByUserId($user['uid']); + } + } + + if (empty($cid) && ($uid != 0)) { + $cid = Contact::getPublicIdByUserId($uid); } return $cid; diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 490fac7dd..51b568ed5 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -585,26 +585,6 @@ class ApiTest extends FixtureTest self::assertArrayHasKey('logo', $result['$rss']); } - /** - * Test the api_unique_id_to_nurl() function. - * - * @return void - */ - public function testApiUniqueIdToNurl() - { - self::assertFalse(api_unique_id_to_nurl($this->wrongUserId)); - } - - /** - * Test the api_unique_id_to_nurl() function with a correct ID. - * - * @return void - */ - public function testApiUniqueIdToNurlWithCorrectId() - { - self::assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id'])); - } - /** * Test the api_get_user() function. * @@ -2252,7 +2232,7 @@ class ApiTest extends FixtureTest 'plink' => '', ] ]; - $result = api_format_items($items, ['id' => 0], true); + $result = api_format_items($items); foreach ($result as $status) { self::assertStatus($status); } @@ -2275,7 +2255,7 @@ class ApiTest extends FixtureTest 'plink' => '', ] ]; - $result = api_format_items($items, ['id' => 0], true, 'xml'); + $result = api_format_items($items, 'xml'); foreach ($result as $status) { self::assertStatus($status); } @@ -3138,18 +3118,6 @@ class ApiTest extends FixtureTest self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result); } - /** - * Test the api_best_nickname() function. - * - * @return void - */ - public function testApiBestNickname() - { - $contacts = []; - $result = api_best_nickname($contacts); - self::assertNull($result); - } - /** * Test the api_best_nickname() function with contacts. * From 647ecd4bd3ab25b82dbd6758db60f0412e1eabb7 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 20 Nov 2021 13:44:12 +0000 Subject: [PATCH 03/24] Relocate RSS header stuff, cleaning up the code --- include/api.php | 460 ++++++++++++--------------------- src/Module/Api/ApiResponse.php | 7 +- src/Module/BaseApi.php | 25 ++ tests/legacy/ApiTest.php | 12 +- 4 files changed, 208 insertions(+), 296 deletions(-) diff --git a/include/api.php b/include/api.php index e5d963032..9a619ce52 100644 --- a/include/api.php +++ b/include/api.php @@ -226,43 +226,6 @@ function api_call(App $a, App\Arguments $args = null) } } -/** - * Set values for RSS template - * - * @param array $arr Array to be passed to template - * @param array $user_info User info - * @return array - * @throws BadRequestException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - * @todo find proper type-hints - */ -function api_rss_extra($arr, $user_info) -{ - if (is_null($user_info)) { - $uid = BaseApi::getCurrentUserID(); - if (empty($uid)) { - throw new ForbiddenException(); - } - - $user_info = DI::twitterUser()->createFromUserId($uid)->toArray(); - } - - $arr['$user'] = $user_info; - $arr['$rss'] = [ - 'alternate' => $user_info['url'], - 'self' => DI::baseUrl() . "/" . DI::args()->getQueryString(), - 'base' => DI::baseUrl(), - 'updated' => DateTimeFormat::utc(null, DateTimeFormat::API), - 'atom_updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), - 'language' => $user_info['lang'], - 'logo' => DI::baseUrl() . "/images/friendica-32.png", - ]; - - return $arr; -} - /** * TWITTER API */ @@ -284,6 +247,7 @@ function api_rss_extra($arr, $user_info) function api_account_verify_credentials($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); unset($_REQUEST['user_id']); unset($_GET['user_id']); @@ -293,7 +257,7 @@ function api_account_verify_credentials($type) $skip_status = $_REQUEST['skip_status'] ?? false; - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $user_info = DI::twitterUser()->createFromUserId($uid, $skip_status)->toArray(); // "verified" isn't used here in the standard unset($user_info["verified"]); @@ -307,8 +271,8 @@ function api_account_verify_credentials($type) } // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($user_info["uid"]); - unset($user_info["self"]); + unset($user_info['uid']); + unset($user_info['self']); return DI::apiResponse()->formatData("user", $type, ['user' => $user_info]); } @@ -347,11 +311,12 @@ function requestdata($k) */ function api_statuses_mediap($type) { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + $a = DI::app(); - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - - $_REQUEST['profile_uid'] = BaseApi::getCurrentUserID(); + $_REQUEST['profile_uid'] = $uid; $_REQUEST['api_source'] = true; $txt = requestdata('status') ?? ''; @@ -393,9 +358,10 @@ api_register_func('api/statuses/mediap', 'api_statuses_mediap', true, API_METHOD */ function api_statuses_update($type) { - $a = DI::app(); - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + $a = DI::app(); // convert $_POST array items to the form we use for web posts. if (requestdata('htmlstatus')) { @@ -433,7 +399,7 @@ function api_statuses_update($type) if (requestdata('lat') && requestdata('long')) { $_REQUEST['coord'] = sprintf("%s %s", requestdata('lat'), requestdata('long')); } - $_REQUEST['profile_uid'] = BaseApi::getCurrentUserID(); + $_REQUEST['profile_uid'] = $uid; if (!$parent) { // Check for throttling (maximum posts per day, week and month) @@ -441,11 +407,11 @@ function api_statuses_update($type) if ($throttle_day > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60); - $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, BaseApi::getCurrentUserID(), $datefrom]; + $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom]; $posts_day = Post::count($condition); if ($posts_day > $throttle_day) { - logger::info('Daily posting limit reached for user '.BaseApi::getCurrentUserID()); + logger::info('Daily posting limit reached for user ' . $uid); // die(api_error($type, DI::l10n()->t("Daily posting limit of %d posts reached. The post was rejected.", $throttle_day)); throw new TooManyRequestsException(DI::l10n()->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day)); } @@ -455,11 +421,11 @@ function api_statuses_update($type) if ($throttle_week > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7); - $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, BaseApi::getCurrentUserID(), $datefrom]; + $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom]; $posts_week = Post::count($condition); if ($posts_week > $throttle_week) { - logger::info('Weekly posting limit reached for user '.BaseApi::getCurrentUserID()); + logger::info('Weekly posting limit reached for user ' . $uid); // die(api_error($type, DI::l10n()->t("Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week))); throw new TooManyRequestsException(DI::l10n()->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week)); } @@ -469,11 +435,11 @@ function api_statuses_update($type) if ($throttle_month > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30); - $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, BaseApi::getCurrentUserID(), $datefrom]; + $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom]; $posts_month = Post::count($condition); if ($posts_month > $throttle_month) { - logger::info('Monthly posting limit reached for user '.BaseApi::getCurrentUserID()); + logger::info('Monthly posting limit reached for user ' . $uid); // die(api_error($type, DI::l10n()->t("Monthly posting limit of %d posts reached. The post was rejected.", $throttle_month)); throw new TooManyRequestsException(DI::l10n()->t("Monthly posting limit of %d post reached. The post was rejected.", "Monthly posting limit of %d posts reached. The post was rejected.", $throttle_month)); } @@ -498,7 +464,7 @@ function api_statuses_update($type) $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `nickname`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo` INNER JOIN `user` ON `user`.`uid` = `photo`.`uid` WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ? - ORDER BY `photo`.`width` DESC LIMIT 2", $id, BaseApi::getCurrentUserID())); + ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid)); if (!empty($media)) { $ressources[] = $media[0]['resource-id']; @@ -546,7 +512,7 @@ function api_statuses_update($type) if (!empty($ressources) && !empty($item_id)) { $item = Post::selectFirst(['uri-id', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'], ['id' => $item_id]); foreach ($ressources as $ressource) { - Photo::setPermissionForRessource($ressource, BaseApi::getCurrentUserID(), $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']); + Photo::setPermissionForRessource($ressource, $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']); } } @@ -620,6 +586,7 @@ api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST) function api_media_metadata_create($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); $postdata = Network::postdata(); @@ -642,7 +609,7 @@ function api_media_metadata_create($type) Logger::info('Updating metadata', ['media_id' => $data['media_id']]); - $condition = ['id' => $data['media_id'], 'uid' => BaseApi::getCurrentUserID()]; + $condition = ['id' => $data['media_id'], 'uid' => $uid]; $photo = DBA::selectFirst('photo', ['resource-id'], $condition); if (!DBA::isResult($photo)) { throw new BadRequestException("Metadata not found."); @@ -726,8 +693,9 @@ function api_get_item(array $condition) function api_users_show($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $user_info = DI::twitterUser()->createFromUserId($uid)->toArray(); $item = api_get_last_status($user_info['pid'], $user_info['uid']); if (!empty($item)) { @@ -759,6 +727,9 @@ api_register_func('api/externalprofile/show', 'api_users_show'); */ function api_users_search($type) { + BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); + $userlist = []; if (!empty($_GET['q'])) { @@ -776,7 +747,7 @@ function api_users_search($type) if (DBA::isResult($contacts)) { $k = 0; foreach ($contacts as $contact) { - $user_info = DI::twitterUser()->createFromContactId($contact['id'], BaseApi::getCurrentUserID())->toArray(); + $user_info = DI::twitterUser()->createFromContactId($contact['id'], $uid)->toArray(); if ($type == 'xml') { $userlist[$k++ . ':user'] = $user_info; @@ -814,12 +785,15 @@ api_register_func('api/users/search', 'api_users_search'); */ function api_users_lookup($type) { + BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); + $users = []; if (!empty($_REQUEST['user_id'])) { foreach (explode(',', $_REQUEST['user_id']) as $cid) { if (!empty($cid) && is_numeric($cid)) { - $users[] = DI::twitterUser()->createFromContactId((int)$cid, BaseApi::getCurrentUserID())->toArray(); + $users[] = DI::twitterUser()->createFromContactId((int)$cid, $uid)->toArray(); } } } @@ -851,6 +825,7 @@ api_register_func('api/users/lookup', 'api_users_lookup', true); function api_search($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); if (empty($_REQUEST['q'])) { throw new BadRequestException('q parameter is required.'); @@ -877,7 +852,7 @@ function api_search($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; if (preg_match('/^#(\w+)$/', $searchTerm, $matches) === 1 && isset($matches[1])) { $searchTerm = $matches[1]; - $condition = ["`iid` > ? AND `name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $since_id, $searchTerm, BaseApi::getCurrentUserID()]; + $condition = ["`iid` > ? AND `name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $since_id, $searchTerm, $uid]; $tags = DBA::select('tag-search-view', ['uri-id'], $condition); $uriids = []; while ($tag = DBA::fetch($tags)) { @@ -900,7 +875,7 @@ function api_search($type) " . ($exclude_replies ? " AND `gravity` = " . GRAVITY_PARENT : ' ') . " AND (`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND `body` LIKE CONCAT('%',?,'%')", - $since_id, BaseApi::getCurrentUserID(), $_REQUEST['q']]; + $since_id, $uid, $_REQUEST['q']]; if ($max_id > 0) { $condition[0] .= ' AND `id` <= ?'; $condition[] = $max_id; @@ -910,7 +885,7 @@ function api_search($type) $statuses = []; if (parse_url($searchTerm, PHP_URL_SCHEME) != '') { - $id = Item::fetchByLink($searchTerm, BaseApi::getCurrentUserID()); + $id = Item::fetchByLink($searchTerm, $uid); if (!$id) { // Public post $id = Item::fetchByLink($searchTerm); @@ -921,7 +896,7 @@ function api_search($type) } } - $statuses = $statuses ?: Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = $statuses ?: Post::selectForUser($uid, [], $condition, $params); $data['status'] = api_format_items(Post::toArray($statuses), $type); @@ -953,6 +928,7 @@ api_register_func('api/search', 'api_search', true); function api_statuses_home_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); unset($_REQUEST['user_id']); unset($_GET['user_id']); @@ -973,7 +949,7 @@ function api_statuses_home_timeline($type) $start = max(0, ($page - 1) * $count); $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ?", - BaseApi::getCurrentUserID(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; + $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; if ($max_id > 0) { $condition[0] .= " AND `id` <= ?"; @@ -989,7 +965,7 @@ function api_statuses_home_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); $items = Post::toArray($statuses); @@ -1010,16 +986,7 @@ function api_statuses_home_timeline($type) bindComments($ret); - $data = ['status' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); - break; - } - - return DI::apiResponse()->formatData("statuses", $type, $data); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } @@ -1042,6 +1009,7 @@ api_register_func('api/statuses/friends_timeline', 'api_statuses_home_timeline', function api_statuses_public_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); // get last network messages @@ -1065,7 +1033,7 @@ function api_statuses_public_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); $r = Post::toArray($statuses); } else { @@ -1082,7 +1050,7 @@ function api_statuses_public_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); $r = Post::toArray($statuses); } @@ -1091,16 +1059,7 @@ function api_statuses_public_timeline($type) bindComments($ret); - $data = ['status' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); - break; - } - - return DI::apiResponse()->formatData("statuses", $type, $data); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1120,9 +1079,10 @@ api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline' function api_statuses_networkpublic_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; + $since_id = $_REQUEST['since_id'] ?? 0; + $max_id = $_REQUEST['max_id'] ?? 0; // pagination $count = $_REQUEST['count'] ?? 20; @@ -1139,22 +1099,13 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::toArray(Post::selectForUser(BaseApi::getCurrentUserID(), Item::DISPLAY_FIELDLIST, $condition, $params)); + $statuses = Post::toArray(Post::selectForUser($uid, Item::DISPLAY_FIELDLIST, $condition, $params)); $ret = api_format_items($statuses, $type); bindComments($ret); - $data = ['status' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); - break; - } - - return DI::apiResponse()->formatData("statuses", $type, $data); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1176,8 +1127,7 @@ api_register_func('api/statuses/networkpublic_timeline', 'api_statuses_networkpu function api_statuses_show($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $uid = BaseApi::getCurrentUserID(); // params $id = intval(DI::args()->getArgv()[3] ?? 0); @@ -1201,7 +1151,7 @@ function api_statuses_show($type) throw new BadRequestException(sprintf("There is no status with the id %d", $id)); } - $item = Post::selectFirst(['id'], ['uri-id' => $uri_item['uri-id'], 'uid' => [0, BaseApi::getCurrentUserID()]], ['order' => ['uid' => true]]); + $item = Post::selectFirst(['id'], ['uri-id' => $uri_item['uri-id'], 'uid' => [0, $uid]], ['order' => ['uid' => true]]); if (!DBA::isResult($item)) { throw new BadRequestException(sprintf("There is no status with the uri-id %d for the given user.", $uri_item['uri-id'])); } @@ -1216,7 +1166,7 @@ function api_statuses_show($type) $params = []; } - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); /// @TODO How about copying this to above methods which don't check $r ? if (!DBA::isResult($statuses)) { @@ -1252,8 +1202,7 @@ api_register_func('api/statuses/show', 'api_statuses_show', true); function api_conversation_show($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $uid = BaseApi::getCurrentUserID(); // params $id = intval(DI::args()->getArgv()[3] ?? 0); @@ -1281,7 +1230,7 @@ function api_conversation_show($type) throw new BadRequestException("There is no status with the id $id."); } - $parent = Post::selectFirst(['id'], ['uri-id' => $item['parent-uri-id'], 'uid' => [0, BaseApi::getCurrentUserID()]], ['order' => ['uid' => true]]); + $parent = Post::selectFirst(['id'], ['uri-id' => $item['parent-uri-id'], 'uid' => [0, $uid]], ['order' => ['uid' => true]]); if (!DBA::isResult($parent)) { throw new BadRequestException("There is no status with this id."); } @@ -1289,7 +1238,7 @@ function api_conversation_show($type) $id = $parent['id']; $condition = ["`parent` = ? AND `uid` IN (0, ?) AND `gravity` IN (?, ?) AND `id` > ?", - $id, BaseApi::getCurrentUserID(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; + $id, $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; if ($max_id > 0) { $condition[0] .= " AND `id` <= ?"; @@ -1297,7 +1246,7 @@ function api_conversation_show($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); if (!DBA::isResult($statuses)) { throw new BadRequestException("There is no status with id $id."); @@ -1329,6 +1278,7 @@ api_register_func('api/statusnet/conversation', 'api_conversation_show', true); function api_statuses_repeat($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // params $id = intval(DI::args()->getArgv()[3] ?? 0); @@ -1349,7 +1299,7 @@ function api_statuses_repeat($type) if (DBA::isResult($item) && !empty($item['body'])) { if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::TWITTER])) { - if (!Item::performActivity($id, 'announce', BaseApi::getCurrentUserID())) { + if (!Item::performActivity($id, 'announce', $uid)) { throw new InternalServerErrorException(); } @@ -1369,7 +1319,7 @@ function api_statuses_repeat($type) $post .= "[/share]"; } $_REQUEST['body'] = $post; - $_REQUEST['profile_uid'] = BaseApi::getCurrentUserID(); + $_REQUEST['profile_uid'] = $uid; $_REQUEST['api_source'] = true; if (empty($_REQUEST['source'])) { @@ -1405,6 +1355,7 @@ api_register_func('api/statuses/retweet', 'api_statuses_repeat', true, API_METHO function api_statuses_destroy($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // params $id = intval(DI::args()->getArgv()[3] ?? 0); @@ -1422,7 +1373,7 @@ function api_statuses_destroy($type) $ret = api_statuses_show($type); - Item::deleteForUser(['id' => $id], BaseApi::getCurrentUserID()); + Item::deleteForUser(['id' => $id], $uid); return $ret; } @@ -1446,6 +1397,7 @@ api_register_func('api/statuses/destroy', 'api_statuses_destroy', true, API_METH function api_statuses_mentions($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); unset($_REQUEST['user_id']); unset($_GET['user_id']); @@ -1469,11 +1421,11 @@ function api_statuses_mentions($type) $condition = [ GRAVITY_PARENT, GRAVITY_COMMENT, - BaseApi::getCurrentUserID(), + $uid, Post\UserNotification::TYPE_EXPLICIT_TAGGED | Post\UserNotification::TYPE_IMPLICIT_TAGGED | Post\UserNotification::TYPE_THREAD_COMMENT | Post\UserNotification::TYPE_DIRECT_COMMENT | Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, - BaseApi::getCurrentUserID(), $since_id, + $uid, $since_id, ]; if ($max_id > 0) { @@ -1484,20 +1436,11 @@ function api_statuses_mentions($type) array_unshift($condition, $query); $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); $ret = api_format_items(Post::toArray($statuses), $type); - $data = ['status' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); - break; - } - - return DI::apiResponse()->formatData("statuses", $type, $data); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1519,7 +1462,6 @@ api_register_func('api/statuses/replies', 'api_statuses_mentions', true); function api_statuses_user_timeline($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $uid = BaseApi::getCurrentUserID(); Logger::info('api_statuses_user_timeline', ['api_user' => $uid, '_REQUEST' => $_REQUEST]); @@ -1560,16 +1502,7 @@ function api_statuses_user_timeline($type) bindComments($ret); - $data = ['status' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId($uid)->toArray()); - break; - } - - return DI::apiResponse()->formatData("statuses", $type, $data); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1592,6 +1525,7 @@ api_register_func('api/statuses/user_timeline', 'api_statuses_user_timeline', tr function api_favorites_create_destroy($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // for versioned api. /// @TODO We need a better global soluton @@ -1610,7 +1544,7 @@ function api_favorites_create_destroy($type) $itemid = intval($_REQUEST['id'] ?? 0); } - $item = Post::selectFirstForUser(BaseApi::getCurrentUserID(), [], ['id' => $itemid, 'uid' => BaseApi::getCurrentUserID()]); + $item = Post::selectFirstForUser($uid, [], ['id' => $itemid, 'uid' => $uid]); if (!DBA::isResult($item)) { throw new BadRequestException("Invalid item."); @@ -1635,16 +1569,7 @@ function api_favorites_create_destroy($type) $ret = api_format_item($item, $type); - $data = ['status' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); - break; - } - - return DI::apiResponse()->formatData("status", $type, $data); + return DI::apiResponse()->formatData("status", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1666,6 +1591,7 @@ api_register_func('api/favorites/destroy', 'api_favorites_create_destroy', true, function api_favorites($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); // in friendica starred item are private // return favorites only for self @@ -1680,7 +1606,7 @@ function api_favorites($type) $start = max(0, ($page - 1) * $count); $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ? AND `starred`", - BaseApi::getCurrentUserID(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; + $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $since_id]; $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; @@ -1689,22 +1615,13 @@ function api_favorites($type) $condition[] = $max_id; } - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); $ret = api_format_items(Post::toArray($statuses), $type); bindComments($ret); - $data = ['status' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); - break; - } - - return DI::apiResponse()->formatData("statuses", $type, $data); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -2143,7 +2060,7 @@ function api_format_items_activities($item, $type = "json") //builtin_activity_puller($i, $activities); // get user data and add it to the array of the activity - $user = DI::twitterUser()->createFromContactId($parent_item['author-id'], BaseApi::getCurrentUserID())->toArray(); + $user = DI::twitterUser()->createFromContactId($parent_item['author-id'], $item['uid'])->toArray(); switch ($parent_item['verb']) { case Activity::LIKE: $activities['like'][] = $user; @@ -2222,8 +2139,8 @@ function api_format_items($items, $type = "json") */ function api_format_item($item, $type = "json") { - $author_user = DI::twitterUser()->createFromContactId($item['author-id'], BaseApi::getCurrentUserID())->toArray(); - $owner_user = DI::twitterUser()->createFromContactId($item['owner-id'], BaseApi::getCurrentUserID())->toArray(); + $author_user = DI::twitterUser()->createFromContactId($item['author-id'], $item['uid'])->toArray(); + $owner_user = DI::twitterUser()->createFromContactId($item['owner-id'], $item['uid'])->toArray(); DI::contentItem()->localize($item); @@ -2286,7 +2203,7 @@ function api_format_item($item, $type = "json") if (!empty($announce)) { $retweeted_item = $item; $item = $announce; - $status['friendica_owner'] = DI::twitterUser()->createFromContactId($announce['author-id'], BaseApi::getCurrentUserID())->toArray(); + $status['friendica_owner'] = DI::twitterUser()->createFromContactId($announce['author-id'], $item['uid'])->toArray(); } } @@ -2305,7 +2222,7 @@ function api_format_item($item, $type = "json") $quoted_status['text'] = $conv_quoted['text']; $quoted_status['statusnet_html'] = $conv_quoted['html']; try { - $quoted_status["user"] = DI::twitterUser()->createFromContactId($quoted_item['author-id'], BaseApi::getCurrentUserID())->toArray(); + $quoted_status["user"] = DI::twitterUser()->createFromContactId($quoted_item['author-id'], $item['uid'])->toArray(); } catch (BadRequestException $e) { // user not found. should be found? /// @todo check if the user should be always found @@ -2327,7 +2244,7 @@ function api_format_item($item, $type = "json") unset($retweeted_status['statusnet_conversation_id']); $status['user'] = $status['friendica_owner']; try { - $retweeted_status["user"] = DI::twitterUser()->createFromContactId($retweeted_item['author-id'], BaseApi::getCurrentUserID())->toArray(); + $retweeted_status["user"] = DI::twitterUser()->createFromContactId($retweeted_item['author-id'], $item['uid'])->toArray(); } catch (BadRequestException $e) { // user not found. should be found? /// @todo check if the user should be always found @@ -2356,8 +2273,8 @@ function api_format_item($item, $type = "json") } // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($status["user"]["uid"]); - unset($status["user"]["self"]); + unset($status["user"]['uid']); + unset($status["user"]['self']); if ($item["coord"] != "") { $coords = explode(' ', $item["coord"]); @@ -2410,10 +2327,10 @@ api_register_func('api/lists/subscriptions', 'api_lists_list', true); function api_lists_ownerships($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - $uid = $user_info['uid']; + $user_info = DI::twitterUser()->createFromUserId($uid)->toArray(); $groups = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid]); @@ -2455,6 +2372,7 @@ api_register_func('api/lists/ownerships', 'api_lists_ownerships', true); function api_lists_statuses($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); unset($_REQUEST['user_id']); unset($_GET['user_id']); @@ -2478,7 +2396,7 @@ function api_lists_statuses($type) $groups = DBA::selectToArray('group_member', ['contact-id'], ['gid' => 1]); $gids = array_column($groups, 'contact-id'); - $condition = ['uid' => BaseApi::getCurrentUserID(), 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'group-id' => $gids]; + $condition = ['uid' => $uid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'group-id' => $gids]; $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]); if ($max_id > 0) { @@ -2495,20 +2413,11 @@ function api_lists_statuses($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition, $params); + $statuses = Post::selectForUser($uid, [], $condition, $params); $items = api_format_items(Post::toArray($statuses), $type); - $data = ['status' => $items]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); - break; - } - - return DI::apiResponse()->formatData("statuses", $type, $data); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $items], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -2531,6 +2440,7 @@ api_register_func('api/lists/statuses', 'api_lists_statuses', true); function api_statuses_f($qtype) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); // pagination $count = $_GET['count'] ?? 20; @@ -2538,7 +2448,7 @@ function api_statuses_f($qtype) $start = max(0, ($page - 1) * $count); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $user_info = DI::twitterUser()->createFromUserId($uid)->toArray(); if (!empty($_GET['cursor']) && $_GET['cursor'] == 'undefined') { /* this is to stop Hotot to load friends multiple times @@ -2558,11 +2468,6 @@ function api_statuses_f($qtype) $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(Contact::FOLLOWER), intval(Contact::FRIEND)); } - // friends and followers only for self - if ($user_info['self'] == 0) { - $sql_extra = " AND false "; - } - if ($qtype == 'blocks') { $sql_filter = 'AND `blocked` AND NOT `pending`'; } elseif ($qtype == 'incoming') { @@ -2581,17 +2486,17 @@ function api_statuses_f($qtype) $sql_extra ORDER BY `nick` LIMIT ?, ?", - BaseApi::getCurrentUserID(), + $uid, $start, $count )); $ret = []; foreach ($r as $cid) { - $user = DI::twitterUser()->createFromContactId($cid['id'], BaseApi::getCurrentUserID())->toArray(); + $user = DI::twitterUser()->createFromContactId($cid['id'], $uid)->toArray(); // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($user["uid"]); - unset($user["self"]); + unset($user['uid']); + unset($user['self']); if ($user) { $ret[] = $user; @@ -2713,7 +2618,6 @@ api_register_func('api/friendships/incoming', 'api_friendships_incoming', true); function api_direct_messages_new($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); if (empty($_POST["text"]) || empty($_POST['screen_name']) && empty($_POST['user_id'])) { @@ -2729,7 +2633,7 @@ function api_direct_messages_new($type) $replyto = ''; if (!empty($_REQUEST['replyto'])) { - $mail = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uid' => BaseApi::getCurrentUserID(), 'id' => $_REQUEST['replyto']]); + $mail = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uid' => $uid, 'id' => $_REQUEST['replyto']]); $replyto = $mail['parent-uri']; $sub = $mail['title']; } else { @@ -2749,17 +2653,7 @@ function api_direct_messages_new($type) $ret = ["error" => $id]; } - $data = ['direct_message'=>$ret]; - - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, $sender); - break; - } - - return DI::apiResponse()->formatData("direct-messages", $type, $data); + return DI::apiResponse()->formatData("direct-messages", $type, ['direct_message' => $ret], Contact::createSelfFromUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -2780,9 +2674,8 @@ api_register_func('api/direct_messages/new', 'api_direct_messages_new', true, AP function api_direct_messages_destroy($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); - // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); //required $id = $_REQUEST['id'] ?? 0; // optional @@ -2790,7 +2683,6 @@ function api_direct_messages_destroy($type) $verbose = (!empty($_GET['friendica_verbose']) ? strtolower($_GET['friendica_verbose']) : "false"); /// @todo optional parameter 'include_entities' from Twitter API not yet implemented - $uid = $user_info['uid']; // error if no id or parenturi specified (for clients posting parent-uri as well) if ($verbose == "true" && ($id == 0 || $parenturi == "")) { $answer = ['result' => 'error', 'message' => 'message id or parenturi not specified']; @@ -2928,6 +2820,7 @@ api_register_func('api/friendships/destroy', 'api_friendships_destroy', true, AP function api_direct_messages_box($type, $box, $verbose) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); // params $count = $_GET['count'] ?? 20; @@ -2946,7 +2839,7 @@ function api_direct_messages_box($type, $box, $verbose) unset($_REQUEST['screen_name']); unset($_GET['screen_name']); - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $user_info = DI::twitterUser()->createFromUserId($uid)->toArray(); $profile_url = $user_info["url"]; @@ -2978,7 +2871,7 @@ function api_direct_messages_box($type, $box, $verbose) $r = DBA::toArray(DBA::p( "SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid` = ? AND $sql_extra AND `mail`.`id` > ? ORDER BY `mail`.`id` DESC LIMIT ?,?", - BaseApi::getCurrentUserID(), + $uid, $since_id, $start, $count @@ -2992,9 +2885,9 @@ function api_direct_messages_box($type, $box, $verbose) foreach ($r as $item) { if ($box == "inbox" || $item['from-url'] != $profile_url) { $recipient = $user_info; - $sender = DI::twitterUser()->createFromContactId($item['contact-id'], BaseApi::getCurrentUserID())->toArray(); + $sender = DI::twitterUser()->createFromContactId($item['contact-id'], $uid)->toArray(); } elseif ($box == "sentbox" || $item['from-url'] == $profile_url) { - $recipient = DI::twitterUser()->createFromContactId($item['contact-id'], BaseApi::getCurrentUserID())->toArray(); + $recipient = DI::twitterUser()->createFromContactId($item['contact-id'], $uid)->toArray(); $sender = $user_info; } @@ -3003,17 +2896,7 @@ function api_direct_messages_box($type, $box, $verbose) } } - - $data = ['direct_message' => $ret]; - switch ($type) { - case "atom": - break; - case "rss": - $data = api_rss_extra($data, $user_info); - break; - } - - return DI::apiResponse()->formatData("direct-messages", $type, $data); + return DI::apiResponse()->formatData("direct-messages", $type, ['direct_message' => $ret], Contact::createSelfFromUserId($uid)); } /** @@ -3093,12 +2976,13 @@ api_register_func('api/direct_messages', 'api_direct_messages_inbox', true); function api_fr_photos_list($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); $r = DBA::toArray(DBA::p( "SELECT `resource-id`, MAX(scale) AS `scale`, `album`, `filename`, `type`, MAX(`created`) AS `created`, MAX(`edited`) AS `edited`, MAX(`desc`) AS `desc` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) GROUP BY `resource-id`, `album`, `filename`, `type`", - BaseApi::getCurrentUserID(), Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER + $uid, Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER )); $typetoext = [ 'image/jpeg' => 'jpg', @@ -3143,6 +3027,7 @@ function api_fr_photos_list($type) function api_fr_photo_create_update($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // input params $photo_id = $_REQUEST['photo_id'] ?? null; @@ -3176,24 +3061,24 @@ function api_fr_photo_create_update($type) $mode = "update"; // check if photo is existing in databasei - if (!Photo::exists(['resource-id' => $photo_id, 'uid' => BaseApi::getCurrentUserID(), 'album' => $album])) { + if (!Photo::exists(['resource-id' => $photo_id, 'uid' => $uid, 'album' => $album])) { throw new BadRequestException("photo not available"); } } // checks on acl strings provided by clients $acl_input_error = false; - $acl_input_error |= check_acl_input($allow_cid); - $acl_input_error |= check_acl_input($deny_cid); - $acl_input_error |= check_acl_input($allow_gid); - $acl_input_error |= check_acl_input($deny_gid); + $acl_input_error |= check_acl_input($allow_cid, $uid); + $acl_input_error |= check_acl_input($deny_cid, $uid); + $acl_input_error |= check_acl_input($allow_gid, $uid); + $acl_input_error |= check_acl_input($deny_gid, $uid); if ($acl_input_error) { throw new BadRequestException("acl data invalid"); } // now let's upload the new media in create-mode if ($mode == "create") { $media = $_FILES['media']; - $data = save_media_to_database("photo", $media, $type, $album, trim($allow_cid), trim($deny_cid), trim($allow_gid), trim($deny_gid), $desc, Photo::DEFAULT, $visibility); + $data = save_media_to_database("photo", $media, $type, $album, trim($allow_cid), trim($deny_cid), trim($allow_gid), trim($deny_gid), $desc, Photo::DEFAULT, $visibility, null, $uid); // return success of updating or error message if (!is_null($data)) { @@ -3238,7 +3123,7 @@ function api_fr_photo_create_update($type) $result = false; if (count($updated_fields) > 0) { $nothingtodo = false; - $result = Photo::update($updated_fields, ['uid' => BaseApi::getCurrentUserID(), 'resource-id' => $photo_id, 'album' => $album]); + $result = Photo::update($updated_fields, ['uid' => $uid, 'resource-id' => $photo_id, 'album' => $album]); } else { $nothingtodo = true; } @@ -3246,7 +3131,7 @@ function api_fr_photo_create_update($type) if (!empty($_FILES['media'])) { $nothingtodo = false; $media = $_FILES['media']; - $data = save_media_to_database("photo", $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, Photo::DEFAULT, $visibility, $photo_id); + $data = save_media_to_database("photo", $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, Photo::DEFAULT, $visibility, $photo_id, $uid); if (!is_null($data)) { return DI::apiResponse()->formatData("photo_update", $type, $data); } @@ -3311,6 +3196,7 @@ function api_fr_photo_detail($type) function api_account_update_profile_image($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // input params $profile_id = $_REQUEST['profile_id'] ?? 0; @@ -3322,7 +3208,7 @@ function api_account_update_profile_image($type) // check if specified profile id is valid if ($profile_id != 0) { - $profile = DBA::selectFirst('profile', ['is-default'], ['uid' => BaseApi::getCurrentUserID(), 'id' => $profile_id]); + $profile = DBA::selectFirst('profile', ['is-default'], ['uid' => $uid, 'id' => $profile_id]); // error message if specified profile id is not in database if (!DBA::isResult($profile)) { throw new BadRequestException("profile_id not available"); @@ -3340,7 +3226,7 @@ function api_account_update_profile_image($type) $media = $_FILES['media']; } // save new profile image - $data = save_media_to_database("profileimage", $media, $type, DI::l10n()->t(Photo::PROFILE_PHOTOS), "", "", "", "", "", Photo::USER_AVATAR); + $data = save_media_to_database("profileimage", $media, $type, DI::l10n()->t(Photo::PROFILE_PHOTOS), "", "", "", "", "", Photo::USER_AVATAR, false, null, $uid); // get filetype if (is_array($media['type'])) { @@ -3358,18 +3244,18 @@ function api_account_update_profile_image($type) // change specified profile or all profiles to the new resource-id if ($is_default_profile) { - $condition = ["`profile` AND `resource-id` != ? AND `uid` = ?", $data['photo']['id'], BaseApi::getCurrentUserID()]; + $condition = ["`profile` AND `resource-id` != ? AND `uid` = ?", $data['photo']['id'], $uid]; Photo::update(['profile' => false, 'photo-type' => Photo::DEFAULT], $condition); } else { $fields = ['photo' => DI::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $fileext, 'thumb' => DI::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $fileext]; - DBA::update('profile', $fields, ['id' => $_REQUEST['profile'], 'uid' => BaseApi::getCurrentUserID()]); + DBA::update('profile', $fields, ['id' => $_REQUEST['profile'], 'uid' => $uid]); } - Contact::updateSelfFromUserID(BaseApi::getCurrentUserID(), true); + Contact::updateSelfFromUserID($uid, true); // Update global directory in background - Profile::publishUpdate(BaseApi::getCurrentUserID()); + Profile::publishUpdate($uid); // output for client if ($data) { @@ -3402,25 +3288,24 @@ api_register_func('api/account/update_profile_image', 'api_account_update_profil function api_account_update_profile($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); - $local_user = BaseApi::getCurrentUserID(); - - $api_user = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $api_user = DI::twitterUser()->createFromUserId($uid)->toArray(); if (!empty($_POST['name'])) { - DBA::update('profile', ['name' => $_POST['name']], ['uid' => $local_user]); - DBA::update('user', ['username' => $_POST['name']], ['uid' => $local_user]); - Contact::update(['name' => $_POST['name']], ['uid' => $local_user, 'self' => 1]); + DBA::update('profile', ['name' => $_POST['name']], ['uid' => $uid]); + DBA::update('user', ['username' => $_POST['name']], ['uid' => $uid]); + Contact::update(['name' => $_POST['name']], ['uid' => $uid, 'self' => 1]); Contact::update(['name' => $_POST['name']], ['id' => $api_user['id']]); } if (isset($_POST['description'])) { - DBA::update('profile', ['about' => $_POST['description']], ['uid' => $local_user]); - Contact::update(['about' => $_POST['description']], ['uid' => $local_user, 'self' => 1]); + DBA::update('profile', ['about' => $_POST['description']], ['uid' => $uid]); + Contact::update(['about' => $_POST['description']], ['uid' => $uid, 'self' => 1]); Contact::update(['about' => $_POST['description']], ['id' => $api_user['id']]); } - Profile::publishUpdate($local_user); + Profile::publishUpdate($uid); return api_account_verify_credentials($type); } @@ -3431,10 +3316,11 @@ api_register_func('api/account/update_profile', 'api_account_update_profile', tr /** * * @param string $acl_string + * @param int $uid * @return bool * @throws Exception */ -function check_acl_input($acl_string) +function check_acl_input($acl_string, $uid) { if (empty($acl_string)) { return false; @@ -3450,7 +3336,7 @@ function check_acl_input($acl_string) foreach ($cid_array as $cid) { $cid = str_replace("<", "", $cid); $cid = str_replace(">", "", $cid); - $condition = ['id' => $cid, 'uid' => BaseApi::getCurrentUserID()]; + $condition = ['id' => $cid, 'uid' => $uid]; $contact_not_found |= !DBA::exists('contact', $condition); } return $contact_not_found; @@ -3469,6 +3355,7 @@ function check_acl_input($acl_string) * @param integer $phototype * @param boolean $visibility * @param string $photo_id + * @param int $uid * @return array * @throws BadRequestException * @throws ForbiddenException @@ -3477,7 +3364,7 @@ function check_acl_input($acl_string) * @throws NotFoundException * @throws UnauthorizedException */ -function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, $phototype = 0, $visibility = false, $photo_id = null) +function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, $phototype, $visibility, $photo_id, $uid) { $visitor = 0; $src = ""; @@ -3552,13 +3439,13 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ // upload normal image (scales 0, 1, 2) logger::info("photo upload: starting new photo upload"); - $r = Photo::store($Image, BaseApi::getCurrentUserID(), $visitor, $resource_id, $filename, $album, 0, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, $uid, $visitor, $resource_id, $filename, $album, 0, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: image upload with scale 0 (original size) failed"); } if ($width > 640 || $height > 640) { $Image->scaleDown(640); - $r = Photo::store($Image, BaseApi::getCurrentUserID(), $visitor, $resource_id, $filename, $album, 1, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, $uid, $visitor, $resource_id, $filename, $album, 1, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: image upload with scale 1 (640x640) failed"); } @@ -3566,7 +3453,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 320 || $height > 320) { $Image->scaleDown(320); - $r = Photo::store($Image, BaseApi::getCurrentUserID(), $visitor, $resource_id, $filename, $album, 2, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, $uid, $visitor, $resource_id, $filename, $album, 2, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: image upload with scale 2 (320x320) failed"); } @@ -3578,7 +3465,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 300 || $height > 300) { $Image->scaleDown(300); - $r = Photo::store($Image, BaseApi::getCurrentUserID(), $visitor, $resource_id, $filename, $album, 4, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, $uid, $visitor, $resource_id, $filename, $album, 4, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: profile image upload with scale 4 (300x300) failed"); } @@ -3586,7 +3473,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 80 || $height > 80) { $Image->scaleDown(80); - $r = Photo::store($Image, BaseApi::getCurrentUserID(), $visitor, $resource_id, $filename, $album, 5, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, $uid, $visitor, $resource_id, $filename, $album, 5, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: profile image upload with scale 5 (80x80) failed"); } @@ -3594,7 +3481,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 48 || $height > 48) { $Image->scaleDown(48); - $r = Photo::store($Image, BaseApi::getCurrentUserID(), $visitor, $resource_id, $filename, $album, 6, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, $uid, $visitor, $resource_id, $filename, $album, 6, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: profile image upload with scale 6 (48x48) failed"); } @@ -3606,7 +3493,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if (!empty($r)) { // create entry in 'item'-table on new uploads to enable users to comment/like/dislike the photo if ($photo_id == null && $mediatype == "photo") { - post_photo_item($resource_id, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility); + post_photo_item($resource_id, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility, $uid); } // on success return image data in json/xml format (like /api/friendica/photo does when no scale is given) return prepare_photo_data($type, false, $resource_id); @@ -3624,17 +3511,18 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ * @param string $deny_gid * @param string $filetype * @param boolean $visibility + * @param int $uid * @throws InternalServerErrorException */ -function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility = false) +function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility, $uid) { // get data about the api authenticated user - $uri = Item::newURI(intval(BaseApi::getCurrentUserID())); - $owner_record = DBA::selectFirst('contact', [], ['uid' => BaseApi::getCurrentUserID(), 'self' => true]); + $uri = Item::newURI(intval($uid)); + $owner_record = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]); $arr = []; $arr['guid'] = System::createUUID(); - $arr['uid'] = intval(BaseApi::getCurrentUserID()); + $arr['uid'] = intval($uid); $arr['uri'] = $uri; $arr['type'] = 'photo'; $arr['wall'] = 1; @@ -3686,8 +3574,7 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f function prepare_photo_data($type, $scale, $photo_id) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $uid = BaseApi::getCurrentUserID(); $scale_sql = ($scale === false ? "" : sprintf("AND scale=%d", intval($scale))); $data_sql = ($scale === false ? "" : "data, "); @@ -3701,7 +3588,7 @@ function prepare_photo_data($type, $scale, $photo_id) FROM `photo` WHERE `uid` = ? AND `resource-id` = ? $scale_sql GROUP BY `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`, `type`, `height`, `width`, `datasize`, `profile`, `allow_cid`, `deny_cid`, `allow_gid`, `deny_gid`", - BaseApi::getCurrentUserID(), + $uid, $photo_id )); @@ -3744,7 +3631,7 @@ function prepare_photo_data($type, $scale, $photo_id) } // retrieve item element for getting activities (like, dislike etc.) related to photo - $condition = ['uid' => BaseApi::getCurrentUserID(), 'resource-id' => $photo_id]; + $condition = ['uid' => $uid, 'resource-id' => $photo_id]; $item = Post::selectFirst(['id', 'uid', 'uri', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition); if (!DBA::isResult($item)) { throw new NotFoundException('Photo-related item not found.'); @@ -3754,9 +3641,9 @@ function prepare_photo_data($type, $scale, $photo_id) // retrieve comments on photo $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)", - $item['parent'], BaseApi::getCurrentUserID(), GRAVITY_PARENT, GRAVITY_COMMENT]; + $item['parent'], $uid, GRAVITY_PARENT, GRAVITY_COMMENT]; - $statuses = Post::selectForUser(BaseApi::getCurrentUserID(), [], $condition); + $statuses = Post::selectForUser($uid, [], $condition); // prepare output of comments $commentData = api_format_items(Post::toArray($statuses), $type); @@ -3909,11 +3796,10 @@ function api_clean_plain_items($text) function api_friendica_group_show($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); $gid = $_REQUEST['gid'] ?? 0; - $uid = $user_info['uid']; // get data of the specified group id or all groups if not specified if ($gid != 0) { @@ -3937,13 +3823,13 @@ function api_friendica_group_show($type) $user_element = "users"; $k = 0; foreach ($members as $member) { - $user = DI::twitterUser()->createFromContactId($member['contact-id'], BaseApi::getCurrentUserID())->toArray(); + $user = DI::twitterUser()->createFromContactId($member['contact-id'], $uid)->toArray(); $users[$k++.":user"] = $user; } } else { $user_element = "user"; foreach ($members as $member) { - $user = DI::twitterUser()->createFromContactId($member['contact-id'], BaseApi::getCurrentUserID())->toArray(); + $user = DI::twitterUser()->createFromContactId($member['contact-id'], $uid)->toArray(); $users[] = $user; } } @@ -3970,11 +3856,10 @@ api_register_func('api/friendica/group_show', 'api_friendica_group_show', true); function api_lists_destroy($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); $gid = $_REQUEST['list_id'] ?? 0; - $uid = $user_info['uid']; // error if no gid specified if ($gid == 0) { @@ -3993,7 +3878,7 @@ function api_lists_destroy($type) 'name' => $group['name'], 'id' => intval($gid), 'id_str' => (string) $gid, - 'user' => $user_info + 'user' => DI::twitterUser()->createFromUserId($uid)->toArray() ]; return DI::apiResponse()->formatData("lists", $type, ['lists' => $list]); @@ -4071,11 +3956,10 @@ function group_create($name, $uid, $users = []) function api_friendica_group_create($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); $name = $_REQUEST['name'] ?? ''; - $uid = $user_info['uid']; $json = json_decode($_POST['json'], true); $users = $json['user']; @@ -4102,11 +3986,10 @@ api_register_func('api/friendica/group_create', 'api_friendica_group_create', tr function api_lists_create($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); $name = $_REQUEST['name'] ?? ''; - $uid = $user_info['uid']; $success = group_create($name, $uid); if ($success['success']) { @@ -4114,7 +3997,7 @@ function api_lists_create($type) 'name' => $success['name'], 'id' => intval($success['gid']), 'id_str' => (string) $success['gid'], - 'user' => $user_info + 'user' => DI::twitterUser()->createFromUserId($uid)->toArray() ]; return DI::apiResponse()->formatData("lists", $type, ['lists' => $grp]); @@ -4138,10 +4021,9 @@ api_register_func('api/lists/create', 'api_lists_create', true, API_METHOD_POST) function api_friendica_group_update($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); - $uid = $user_info['uid']; $gid = $_REQUEST['gid'] ?? 0; $name = $_REQUEST['name'] ?? ''; $json = json_decode($_POST['json'], true); @@ -4208,12 +4090,11 @@ api_register_func('api/friendica/group_update', 'api_friendica_group_update', tr function api_lists_update($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); $gid = $_REQUEST['list_id'] ?? 0; $name = $_REQUEST['name'] ?? ''; - $uid = $user_info['uid']; // error if no gid specified if ($gid == 0) { @@ -4232,7 +4113,7 @@ function api_lists_update($type) 'name' => $name, 'id' => intval($gid), 'id_str' => (string) $gid, - 'user' => $user_info + 'user' => DI::twitterUser()->createFromUserId($uid)->toArray() ]; return DI::apiResponse()->formatData("lists", $type, ['lists' => $list]); @@ -4257,8 +4138,7 @@ api_register_func('api/lists/update', 'api_lists_update', true, API_METHOD_POST) function api_friendica_notification_seen($type) { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $uid = BaseApi::getCurrentUserID(); if (DI::args()->getArgc() !== 4) { throw new BadRequestException('Invalid argument count'); @@ -4268,7 +4148,7 @@ function api_friendica_notification_seen($type) try { $Notify = DI::notify()->selectOneById($id); - if ($Notify->uid !== BaseApi::getCurrentUserID()) { + if ($Notify->uid !== $uid) { throw new NotFoundException(); } @@ -4280,7 +4160,7 @@ function api_friendica_notification_seen($type) DI::notify()->save($Notify); if ($Notify->otype === Notification\ObjectType::ITEM) { - $item = Post::selectFirstForUser(BaseApi::getCurrentUserID(), [], ['id' => $Notify->iid, 'uid' => BaseApi::getCurrentUserID()]); + $item = Post::selectFirstForUser($uid, [], ['id' => $Notify->iid, 'uid' => $uid]); if (DBA::isResult($item)) { // we found the item, return it to the user $ret = api_format_items([$item], $type); @@ -4318,11 +4198,11 @@ api_register_func('api/friendica/notification/seen', 'api_friendica_notification function api_friendica_direct_messages_search($type, $box = "") { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); // params - $user_info = DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray(); + $user_info = DI::twitterUser()->createFromUserId($uid)->toArray(); $searchstring = $_REQUEST['searchstring'] ?? ''; - $uid = $user_info['uid']; // error if no searchstring specified if ($searchstring == "") { @@ -4349,9 +4229,9 @@ function api_friendica_direct_messages_search($type, $box = "") foreach ($r as $item) { if ($box == "inbox" || $item['from-url'] != $profile_url) { $recipient = $user_info; - $sender = DI::twitterUser()->createFromContactId($item['contact-id'], BaseApi::getCurrentUserID())->toArray(); + $sender = DI::twitterUser()->createFromContactId($item['contact-id'], $uid)->toArray(); } elseif ($box == "sentbox" || $item['from-url'] == $profile_url) { - $recipient = DI::twitterUser()->createFromContactId($item['contact-id'], BaseApi::getCurrentUserID())->toArray(); + $recipient = DI::twitterUser()->createFromContactId($item['contact-id'], $uid)->toArray(); $sender = $user_info; } diff --git a/src/Module/Api/ApiResponse.php b/src/Module/Api/ApiResponse.php index 9521e2270..8d4812659 100644 --- a/src/Module/Api/ApiResponse.php +++ b/src/Module/Api/ApiResponse.php @@ -4,6 +4,7 @@ namespace Friendica\Module\Api; use Friendica\App\Arguments; use Friendica\Core\L10n; +use Friendica\Module\BaseApi; use Friendica\Util\Arrays; use Friendica\Util\HTTPInputData; use Friendica\Util\XML; @@ -108,14 +109,16 @@ class ApiResponse * @param string $root_element Name of the root element * @param string $type Return type (atom, rss, xml, json) * @param array $data JSON style array + * @param int $cid ID of the contact for RSS * * @return array|string (string|array) XML data or JSON data */ - public function formatData(string $root_element, string $type, array $data) + public function formatData(string $root_element, string $type, array $data, int $cid = 0) { switch ($type) { - case 'atom': case 'rss': + $data = BaseApi::addRSSValues($data, $cid); + case 'atom': case 'xml': return $this->createXML($data, $root_element); case 'json': diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index eaefdd0bd..e3e980286 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -314,4 +314,29 @@ class BaseApi extends BaseModule return $cid; } + + /** + * Set values for RSS template + * + * @param array $arr Array to be passed to template + * @param int $cid Contact ID of template + * @return array + */ + public static function addRSSValues(array $arr, int $cid) + { + $user_info = DI::twitterUser()->createFromContactId($cid)->toArray(); + + $arr['$user'] = $user_info; + $arr['$rss'] = [ + 'alternate' => $user_info['url'], + 'self' => DI::baseUrl() . '/' . DI::args()->getQueryString(), + 'base' => DI::baseUrl(), + 'updated' => DateTimeFormat::utc(null, DateTimeFormat::API), + 'atom_updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), + 'language' => $user_info['lang'], + 'logo' => DI::baseUrl() . '/images/friendica-32.png', + ]; + + return $arr; + } } diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 51b568ed5..f654499ef 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -555,6 +555,7 @@ class ApiTest extends FixtureTest */ public function testApiRssExtra() { + /* $user_info = ['url' => 'user_url', 'lang' => 'en']; $result = api_rss_extra([], $user_info); self::assertEquals($user_info, $result['$user']); @@ -565,6 +566,7 @@ class ApiTest extends FixtureTest self::assertArrayHasKey('atom_updated', $result['$rss']); self::assertArrayHasKey('language', $result['$rss']); self::assertArrayHasKey('logo', $result['$rss']); + */ } /** @@ -574,6 +576,7 @@ class ApiTest extends FixtureTest */ public function testApiRssExtraWithoutUserInfo() { + /* $result = api_rss_extra([], null); self::assertIsArray($result['$user']); self::assertArrayHasKey('alternate', $result['$rss']); @@ -583,6 +586,7 @@ class ApiTest extends FixtureTest self::assertArrayHasKey('atom_updated', $result['$rss']); self::assertArrayHasKey('language', $result['$rss']); self::assertArrayHasKey('logo', $result['$rss']); + */ } /** @@ -2232,7 +2236,7 @@ class ApiTest extends FixtureTest 'plink' => '', ] ]; - $result = api_format_items($items); + $result = api_format_items($items, ['id' => 0], true); foreach ($result as $status) { self::assertStatus($status); } @@ -2255,7 +2259,7 @@ class ApiTest extends FixtureTest 'plink' => '', ] ]; - $result = api_format_items($items, 'xml'); + $result = api_format_items($items, ['id' => 0], true, 'xml'); foreach ($result as $status) { self::assertStatus($status); } @@ -3025,7 +3029,7 @@ class ApiTest extends FixtureTest */ public function testCheckAclInput() { - $result = check_acl_input(''); + $result = check_acl_input('', BaseApi::getCurrentUserID()); // Where does this result come from? self::assertEquals(1, $result); } @@ -3037,7 +3041,7 @@ class ApiTest extends FixtureTest */ public function testCheckAclInputWithEmptyAclString() { - $result = check_acl_input(' '); + $result = check_acl_input(' ', BaseApi::getCurrentUserID()); self::assertFalse($result); } From f04b07b20aab343196fde3049e3c10cdfefaa657 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 20 Nov 2021 23:38:52 +0000 Subject: [PATCH 04/24] Removed some more API functions --- include/api.php | 195 ++++++++++++--------------------------- tests/legacy/ApiTest.php | 9 +- 2 files changed, 66 insertions(+), 138 deletions(-) diff --git a/include/api.php b/include/api.php index 9a619ce52..d1bd8a9ab 100644 --- a/include/api.php +++ b/include/api.php @@ -898,9 +898,13 @@ function api_search($type) $statuses = $statuses ?: Post::selectForUser($uid, [], $condition, $params); - $data['status'] = api_format_items(Post::toArray($statuses), $type); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); - bindComments($data['status']); + $data['status'] = $ret; return DI::apiResponse()->formatData('statuses', $type, $data); } @@ -967,15 +971,13 @@ function api_statuses_home_timeline($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $items = Post::toArray($statuses); - - $ret = api_format_items($items, $type); - - // Set all posts from the query above to seen + $ret = []; $idarray = []; - foreach ($items as $item) { - $idarray[] = intval($item["id"]); + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + $idarray[] = intval($status['id']); } + DBA::close($statuses); if (!empty($idarray)) { $unseen = Post::exists(['unseen' => true, 'id' => $idarray]); @@ -984,8 +986,6 @@ function api_statuses_home_timeline($type) } } - bindComments($ret); - return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } @@ -1034,8 +1034,6 @@ function api_statuses_public_timeline($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - - $r = Post::toArray($statuses); } else { $condition = ["`gravity` IN (?, ?) AND `id` > ? AND `private` = ? AND `wall` AND `origin` AND NOT `author-hidden`", GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC]; @@ -1051,13 +1049,13 @@ function api_statuses_public_timeline($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - - $r = Post::toArray($statuses); } - $ret = api_format_items($r, $type); - - bindComments($ret); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } @@ -1099,11 +1097,13 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::toArray(Post::selectForUser($uid, Item::DISPLAY_FIELDLIST, $condition, $params)); + $statuses = Post::selectForUser($uid, Item::DISPLAY_FIELDLIST, $condition, $params); - $ret = api_format_items($statuses, $type); - - bindComments($ret); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } @@ -1173,7 +1173,11 @@ function api_statuses_show($type) throw new BadRequestException(sprintf("There is no status or conversation with the id %d.", $id)); } - $ret = api_format_items(Post::toArray($statuses), $type); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); if ($conversation) { $data = ['status' => $ret]; @@ -1252,7 +1256,11 @@ function api_conversation_show($type) throw new BadRequestException("There is no status with id $id."); } - $ret = api_format_items(Post::toArray($statuses), $type); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); $data = ['status' => $ret]; return DI::apiResponse()->formatData("statuses", $type, $data); @@ -1438,7 +1446,11 @@ function api_statuses_mentions($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $ret = api_format_items(Post::toArray($statuses), $type); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } @@ -1498,9 +1510,11 @@ function api_statuses_user_timeline($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $ret = api_format_items(Post::toArray($statuses), $type); - - bindComments($ret); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } @@ -1617,9 +1631,11 @@ function api_favorites($type) $statuses = Post::selectForUser($uid, [], $condition, $params); - $ret = api_format_items(Post::toArray($statuses), $type); - - bindComments($ret); + $ret = []; + while ($status = DBA::fetch($statuses)) { + $ret[] = api_format_item($status, $type); + } + DBA::close($statuses); return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); } @@ -2104,27 +2120,6 @@ function api_format_items_activities($item, $type = "json") return $activities; } -/** - * format items to be returned by api - * - * @param array $items array of items - * @param string $type Return type (atom, rss, xml, json) - * @return array - * @throws BadRequestException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - */ -function api_format_items($items, $type = "json") -{ - $ret = []; - foreach ($items as $item) { - $ret[] = api_format_item($item, $type); - } - - return $ret; -} - /** * @param array $item Item record * @param string $type Return format (atom, rss, xml, json) @@ -2178,7 +2173,8 @@ function api_format_item($item, $type = "json") 'external_url' => DI::baseUrl() . "/display/" . $item['guid'], 'friendica_activities' => api_format_items_activities($item, $type), 'friendica_title' => $item['title'], - 'friendica_html' => BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::EXTERNAL) + 'friendica_html' => BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::EXTERNAL), + 'friendica_comments' => Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]) ]; if (count($converted["attachments"]) > 0) { @@ -2415,7 +2411,11 @@ function api_lists_statuses($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $items = api_format_items(Post::toArray($statuses), $type); + $items = []; + while ($status = DBA::fetch($statuses)) { + $items[] = api_format_item($status, $type); + } + DBA::close($statuses); return DI::apiResponse()->formatData("statuses", $type, ['status' => $items], Contact::createSelfFromUserId($uid)); } @@ -3646,7 +3646,12 @@ function prepare_photo_data($type, $scale, $photo_id) $statuses = Post::selectForUser($uid, [], $condition); // prepare output of comments - $commentData = api_format_items(Post::toArray($statuses), $type); + $commentData = []; + while ($status = DBA::fetch($statuses)) { + $commentData[] = api_format_item($status, $type); + } + DBA::close($statuses); + $comments = []; if ($type == "xml") { $k = 0; @@ -4163,7 +4168,7 @@ function api_friendica_notification_seen($type) $item = Post::selectFirstForUser($uid, [], ['id' => $Notify->iid, 'uid' => $uid]); if (DBA::isResult($item)) { // we found the item, return it to the user - $ret = api_format_items([$item], $type); + $ret = [api_format_item($item, $type)]; $data = ['status' => $ret]; return DI::apiResponse()->formatData('status', $type, $data); } @@ -4247,81 +4252,3 @@ function api_friendica_direct_messages_search($type, $box = "") /// @TODO move to top of file or somewhere better api_register_func('api/friendica/direct_messages_search', 'api_friendica_direct_messages_search', true); - -/* - * Number of comments - * - * Bind comment numbers(friendica_comments: Int) on each statuses page of *_timeline / favorites / search - * - * @param object $data [Status, Status] - * - * @return void - */ -function bindComments(&$data) -{ - if (count($data) == 0) { - return; - } - - $ids = []; - $comments = []; - foreach ($data as $item) { - $ids[] = $item['id']; - } - - $idStr = DBA::escape(implode(', ', $ids)); - $sql = "SELECT `parent`, COUNT(*) as comments FROM `post-user-view` WHERE `parent` IN ($idStr) AND `deleted` = ? AND `gravity`= ? GROUP BY `parent`"; - $items = DBA::p($sql, 0, GRAVITY_COMMENT); - $itemsData = DBA::toArray($items); - - foreach ($itemsData as $item) { - $comments[$item['parent']] = $item['comments']; - } - - foreach ($data as $idx => $item) { - $id = $item['id']; - $data[$idx]['friendica_comments'] = isset($comments[$id]) ? $comments[$id] : 0; - } -} - -/* -@TODO Maybe open to implement? -To.Do: - [pagename] => api/1.1/statuses/lookup.json - [id] => 605138389168451584 - [include_cards] => true - [cards_platform] => Android-12 - [include_entities] => true - [include_my_retweet] => 1 - [include_rts] => 1 - [include_reply_count] => true - [include_descendent_reply_count] => true -(?) - - -Not implemented by now: -statuses/retweets_of_me -friendships/create -friendships/destroy -friendships/exists -friendships/show -account/update_location -account/update_profile_background_image -blocks/create -blocks/destroy -friendica/profile/update -friendica/profile/create -friendica/profile/delete - -Not implemented in status.net: -statuses/retweeted_to_me -statuses/retweeted_by_me -direct_messages/destroy -account/end_session -account/update_delivery_device -notifications/follow -notifications/leave -blocks/exists -blocks/blocking -lists -*/ diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index f654499ef..a64ffbf91 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -2236,8 +2236,8 @@ class ApiTest extends FixtureTest 'plink' => '', ] ]; - $result = api_format_items($items, ['id' => 0], true); - foreach ($result as $status) { + foreach ($items as $item) { + $status = api_format_item($item); self::assertStatus($status); } } @@ -2259,8 +2259,9 @@ class ApiTest extends FixtureTest 'plink' => '', ] ]; - $result = api_format_items($items, ['id' => 0], true, 'xml'); - foreach ($result as $status) { + + foreach ($items as $item) { + $status = api_format_item($item, 'xml'); self::assertStatus($status); } } From 0f09b4f636384dcca0961720adb44037a1136bc8 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 20 Nov 2021 23:47:03 +0000 Subject: [PATCH 05/24] Fixing (some) tests --- src/Module/BaseApi.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index e3e980286..c097a5826 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -324,6 +324,10 @@ class BaseApi extends BaseModule */ public static function addRSSValues(array $arr, int $cid) { + if (empty($cid)) { + return $arr; + } + $user_info = DI::twitterUser()->createFromContactId($cid)->toArray(); $arr['$user'] = $user_info; From c732027b04f674d186ee8652280313f25eedbd9a Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 00:14:38 +0000 Subject: [PATCH 06/24] Tests ... --- src/Factory/Api/Twitter/User.php | 4 ++++ tests/legacy/ApiTest.php | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/Factory/Api/Twitter/User.php b/src/Factory/Api/Twitter/User.php index e545bd78c..c502e3830 100644 --- a/src/Factory/Api/Twitter/User.php +++ b/src/Factory/Api/Twitter/User.php @@ -48,6 +48,10 @@ class User extends BaseFactory $userContact = []; } + if (empty($publicContact)) { + return null; + } + $apcontact = APContact::getByURL($publicContact['url'], false); return new \Friendica\Object\Api\Twitter\User($publicContact, $apcontact, $userContact, $skip_status, $include_user_entities); diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index a64ffbf91..8e5743fac 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -2234,6 +2234,7 @@ class ApiTest extends FixtureTest 'author-network' => Protocol::DFRN, 'author-link' => 'http://localhost/profile/othercontact', 'plink' => '', + 'uid' => $this->selfUser['id'], ] ]; foreach ($items as $item) { @@ -2257,6 +2258,7 @@ class ApiTest extends FixtureTest 'author-network' => Protocol::DFRN, 'author-link' => 'http://localhost/profile/othercontact', 'plink' => '', + 'uid' => $this->selfUser['id'], ] ]; From ae99144f576cf349a0d6718d09af511df3576fb2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 00:15:36 +0000 Subject: [PATCH 07/24] Tests again --- src/Module/BaseApi.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index c097a5826..1d24e53b9 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -329,6 +329,9 @@ class BaseApi extends BaseModule } $user_info = DI::twitterUser()->createFromContactId($cid)->toArray(); + if (empty($user_info)) { + return $arr; + } $arr['$user'] = $user_info; $arr['$rss'] = [ From 2c6afe65a79767286e4412372d1f0070fa50b711 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 00:26:35 +0000 Subject: [PATCH 08/24] Use correct function to fetch the self user --- include/api.php | 20 ++++++++++---------- src/Factory/Api/Twitter/User.php | 4 ---- src/Module/BaseApi.php | 3 --- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/include/api.php b/include/api.php index d1bd8a9ab..d097c8d66 100644 --- a/include/api.php +++ b/include/api.php @@ -986,7 +986,7 @@ function api_statuses_home_timeline($type) } } - return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); } @@ -1057,7 +1057,7 @@ function api_statuses_public_timeline($type) } DBA::close($statuses); - return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1105,7 +1105,7 @@ function api_statuses_networkpublic_timeline($type) } DBA::close($statuses); - return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1452,7 +1452,7 @@ function api_statuses_mentions($type) } DBA::close($statuses); - return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1516,7 +1516,7 @@ function api_statuses_user_timeline($type) } DBA::close($statuses); - return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1583,7 +1583,7 @@ function api_favorites_create_destroy($type) $ret = api_format_item($item, $type); - return DI::apiResponse()->formatData("status", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("status", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -1637,7 +1637,7 @@ function api_favorites($type) } DBA::close($statuses); - return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -2417,7 +2417,7 @@ function api_lists_statuses($type) } DBA::close($statuses); - return DI::apiResponse()->formatData("statuses", $type, ['status' => $items], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("statuses", $type, ['status' => $items], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -2653,7 +2653,7 @@ function api_direct_messages_new($type) $ret = ["error" => $id]; } - return DI::apiResponse()->formatData("direct-messages", $type, ['direct_message' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("direct-messages", $type, ['direct_message' => $ret], Contact::getPublicIdByUserId($uid)); } /// @TODO move to top of file or somewhere better @@ -2896,7 +2896,7 @@ function api_direct_messages_box($type, $box, $verbose) } } - return DI::apiResponse()->formatData("direct-messages", $type, ['direct_message' => $ret], Contact::createSelfFromUserId($uid)); + return DI::apiResponse()->formatData("direct-messages", $type, ['direct_message' => $ret], Contact::getPublicIdByUserId($uid)); } /** diff --git a/src/Factory/Api/Twitter/User.php b/src/Factory/Api/Twitter/User.php index c502e3830..e545bd78c 100644 --- a/src/Factory/Api/Twitter/User.php +++ b/src/Factory/Api/Twitter/User.php @@ -48,10 +48,6 @@ class User extends BaseFactory $userContact = []; } - if (empty($publicContact)) { - return null; - } - $apcontact = APContact::getByURL($publicContact['url'], false); return new \Friendica\Object\Api\Twitter\User($publicContact, $apcontact, $userContact, $skip_status, $include_user_entities); diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index 1d24e53b9..c097a5826 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -329,9 +329,6 @@ class BaseApi extends BaseModule } $user_info = DI::twitterUser()->createFromContactId($cid)->toArray(); - if (empty($user_info)) { - return $arr; - } $arr['$user'] = $user_info; $arr['$rss'] = [ From 8954c19896559c8f1256b36c09ffc297a0306ad1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 00:36:02 +0000 Subject: [PATCH 09/24] Fix some tests --- tests/legacy/ApiTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 8e5743fac..9dfa84efe 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -2233,6 +2233,7 @@ class ApiTest extends FixtureTest 'author-id' => 43, 'author-network' => Protocol::DFRN, 'author-link' => 'http://localhost/profile/othercontact', + 'owner-id' => 43, 'plink' => '', 'uid' => $this->selfUser['id'], ] @@ -2257,6 +2258,7 @@ class ApiTest extends FixtureTest 'author-id' => 43, 'author-network' => Protocol::DFRN, 'author-link' => 'http://localhost/profile/othercontact', + 'owner-id' => 43, 'plink' => '', 'uid' => $this->selfUser['id'], ] From 7e49ee8fb5fd26d405eac4e4d168497422c6c151 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 00:44:26 +0000 Subject: [PATCH 10/24] Hopefully fix some tests --- include/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/api.php b/include/api.php index d097c8d66..ef0e0bfbd 100644 --- a/include/api.php +++ b/include/api.php @@ -3725,7 +3725,7 @@ function api_in_reply_to($item) $in_reply_to['user_id_str'] = null; $in_reply_to['screen_name'] = null; - if (($item['thr-parent'] != $item['uri']) && ($item['gravity'] != GRAVITY_PARENT)) { + if (!empty($item['thr-parent']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] != GRAVITY_PARENT)) { $parent = Post::selectFirst(['id'], ['uid' => $item['uid'], 'uri' => $item['thr-parent']]); if (DBA::isResult($parent)) { $in_reply_to['status_id'] = intval($parent['id']); From ff9786f3c37ea4dc0a95f74ded3b762589568c18 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 05:16:21 +0000 Subject: [PATCH 11/24] Fixing tests --- tests/legacy/ApiTest.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 9dfa84efe..593d48975 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -2568,9 +2568,9 @@ class ApiTest extends FixtureTest public function testApiDirectMessagesNewWithScreenName() { $this->app->setLoggedInUserNickname($this->selfUser['nick']); - $_POST['text'] = 'message_text'; - $_POST['screen_name'] = $this->friendUser['nick']; - $result = api_direct_messages_new('json'); + $_POST['text'] = 'message_text'; + $_POST['user_id'] = $this->friendUser['id']; + $result = api_direct_messages_new('json'); self::assertStringContainsString('message_text', $result['direct_message']['text']); self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']); self::assertEquals(1, $result['direct_message']['friendica_seen']); @@ -2584,10 +2584,10 @@ class ApiTest extends FixtureTest public function testApiDirectMessagesNewWithTitle() { $this->app->setLoggedInUserNickname($this->selfUser['nick']); - $_POST['text'] = 'message_text'; - $_POST['screen_name'] = $this->friendUser['nick']; - $_REQUEST['title'] = 'message_title'; - $result = api_direct_messages_new('json'); + $_POST['text'] = 'message_text'; + $_POST['user_id'] = $this->friendUser['id']; + $_REQUEST['title'] = 'message_title'; + $result = api_direct_messages_new('json'); self::assertStringContainsString('message_text', $result['direct_message']['text']); self::assertStringContainsString('message_title', $result['direct_message']['text']); self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']); @@ -2602,9 +2602,9 @@ class ApiTest extends FixtureTest public function testApiDirectMessagesNewWithRss() { $this->app->setLoggedInUserNickname($this->selfUser['nick']); - $_POST['text'] = 'message_text'; - $_POST['screen_name'] = $this->friendUser['nick']; - $result = api_direct_messages_new('rss'); + $_POST['text'] = 'message_text'; + $_POST['user_id'] = $this->friendUser['id']; + $result = api_direct_messages_new('rss'); self::assertXml($result, 'direct-messages'); } From 1acaa61446feb01139fa0a8f776e711014998994 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 05:40:48 +0000 Subject: [PATCH 12/24] Use user contact --- include/api.php | 6 ++++-- tests/legacy/ApiTest.php | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/api.php b/include/api.php index ef0e0bfbd..5d6fb2add 100644 --- a/include/api.php +++ b/include/api.php @@ -2633,7 +2633,7 @@ function api_direct_messages_new($type) $replyto = ''; if (!empty($_REQUEST['replyto'])) { - $mail = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uid' => $uid, 'id' => $_REQUEST['replyto']]); + $mail = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uid' => $uid, 'id' => $_REQUEST['replyto']]); $replyto = $mail['parent-uri']; $sub = $mail['title']; } else { @@ -2644,7 +2644,9 @@ function api_direct_messages_new($type) } } - $id = Mail::send($cid, $_POST['text'], $sub, $replyto); + $cdata = Contact::getPublicAndUserContactID($cid, $uid); + + $id = Mail::send($cdata['user'], $_POST['text'], $sub, $replyto); if ($id > -1) { $mail = DBA::selectFirst('mail', [], ['id' => $id]); diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 593d48975..cfe748977 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -2236,6 +2236,7 @@ class ApiTest extends FixtureTest 'owner-id' => 43, 'plink' => '', 'uid' => $this->selfUser['id'], + 'uri-id' => 1, ] ]; foreach ($items as $item) { @@ -2261,6 +2262,7 @@ class ApiTest extends FixtureTest 'owner-id' => 43, 'plink' => '', 'uid' => $this->selfUser['id'], + 'uri-id' => 1, ] ]; From c54d3ddf18d826dfec9ce26c04227cfdc5b3aab2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 05:55:16 +0000 Subject: [PATCH 13/24] Test return value --- tests/legacy/ApiTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index cfe748977..b9768350c 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1770,7 +1770,7 @@ class ApiTest extends FixtureTest { $_REQUEST['page'] = -2; $result = api_statuses_user_timeline('json'); - self::assertNotEmpty($result['status']); + self::assertNotEmpty($result['status'], print_r($result['status'], true)); foreach ($result['status'] as $status) { self::assertStatus($status); } @@ -2228,6 +2228,7 @@ class ApiTest extends FixtureTest 'item_network' => 'item_network', 'source' => 'web', 'coord' => '5 7', + 'title' => '', 'body' => '', 'verb' => '', 'author-id' => 43, @@ -2254,6 +2255,7 @@ class ApiTest extends FixtureTest $items = [ [ 'coord' => '5 7', + 'title' => '', 'body' => '', 'verb' => '', 'author-id' => 43, From 52a3563de462e09626d9c67caf932b88802c4319 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 06:10:19 +0000 Subject: [PATCH 14/24] Added test data --- tests/legacy/ApiTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index b9768350c..f6e38dc08 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1770,7 +1770,7 @@ class ApiTest extends FixtureTest { $_REQUEST['page'] = -2; $result = api_statuses_user_timeline('json'); - self::assertNotEmpty($result['status'], print_r($result['status'], true)); + self::assertNotEmpty($result['status']); foreach ($result['status'] as $status) { self::assertStatus($status); } @@ -2238,6 +2238,7 @@ class ApiTest extends FixtureTest 'plink' => '', 'uid' => $this->selfUser['id'], 'uri-id' => 1, + 'created' => '', ] ]; foreach ($items as $item) { @@ -2265,6 +2266,7 @@ class ApiTest extends FixtureTest 'plink' => '', 'uid' => $this->selfUser['id'], 'uri-id' => 1, + 'created' => '', ] ]; From 16e69cbffa27835ae865056dd3bc29cefcdb9751 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 06:14:38 +0000 Subject: [PATCH 15/24] Adding more test data --- tests/legacy/ApiTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index f6e38dc08..37988707f 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -2237,8 +2237,10 @@ class ApiTest extends FixtureTest 'owner-id' => 43, 'plink' => '', 'uid' => $this->selfUser['id'], + 'id' => 1, 'uri-id' => 1, 'created' => '', + 'app' => '', ] ]; foreach ($items as $item) { @@ -2265,8 +2267,10 @@ class ApiTest extends FixtureTest 'owner-id' => 43, 'plink' => '', 'uid' => $this->selfUser['id'], + 'id' => 1, 'uri-id' => 1, 'created' => '', + 'app' => '', ] ]; From f385be04dc746096a3877860dd2e30c8d3720d53 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 06:19:35 +0000 Subject: [PATCH 16/24] More test data --- tests/legacy/ApiTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 37988707f..d6097ec02 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -10,6 +10,7 @@ use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Protocol; use Friendica\DI; +use Friendica\Model\Item; use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; @@ -2238,9 +2239,12 @@ class ApiTest extends FixtureTest 'plink' => '', 'uid' => $this->selfUser['id'], 'id' => 1, + 'parent' => 1, 'uri-id' => 1, 'created' => '', 'app' => '', + 'starred' => false, + 'private' => Item::PRIVATE, ] ]; foreach ($items as $item) { @@ -2268,9 +2272,12 @@ class ApiTest extends FixtureTest 'plink' => '', 'uid' => $this->selfUser['id'], 'id' => 1, + 'parent' => 1, 'uri-id' => 1, 'created' => '', 'app' => '', + 'starred' => false, + 'private' => Item::PRIVATE, ] ]; From cf2ada64da4229d53c5b8c1c41812a2daf7ca46f Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 06:31:04 +0000 Subject: [PATCH 17/24] Adding test data --- tests/datasets/api.fixture.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index 6743dbf11..9e0174c02 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -303,6 +303,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, ], @@ -317,6 +318,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, ], @@ -331,6 +333,7 @@ return [ 'causer-id' => 43, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, ], @@ -345,6 +348,7 @@ return [ 'causer-id' => 44, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, ], @@ -359,6 +363,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, ], @@ -373,6 +378,7 @@ return [ 'causer-id' => 44, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, ], @@ -392,6 +398,7 @@ return [ 'parent-uri-id' => 1, 'thr-parent-id' => 1, 'private' => Item::PUBLIC, + 'global' => true, 'gravity' => GRAVITY_PARENT, 'network' => Protocol::DFRN, 'wall' => 1, @@ -413,6 +420,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 1, @@ -433,6 +441,7 @@ return [ 'causer-id' => 43, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 1, @@ -453,6 +462,7 @@ return [ 'causer-id' => 44, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 1, @@ -473,6 +483,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 1, @@ -493,6 +504,7 @@ return [ 'causer-id' => 44, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 1, @@ -513,6 +525,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 0, @@ -533,6 +546,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 0, @@ -553,6 +567,7 @@ return [ 'causer-id' => 43, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 0, @@ -573,6 +588,7 @@ return [ 'causer-id' => 44, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 0, @@ -593,6 +609,7 @@ return [ 'causer-id' => 42, 'vid' => 8, 'private' => Item::PUBLIC, + 'global' => true, 'visible' => 1, 'deleted' => 0, 'wall' => 0, @@ -611,6 +628,7 @@ return [ 'parent-uri-id' => 6, 'thr-parent-id' => 6, 'private' => Item::PUBLIC, + 'global' => true, 'gravity' => GRAVITY_PARENT, 'network' => Protocol::DFRN, 'origin' => 0, From 8bc748758d19fa2bf78f2384dc4b168da49f4a88 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 06:37:09 +0000 Subject: [PATCH 18/24] Fetch data for test --- tests/legacy/ApiTest.php | 49 +++------------------------------------- 1 file changed, 3 insertions(+), 46 deletions(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index d6097ec02..e64c1e7ff 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -10,7 +10,7 @@ use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Protocol; use Friendica\DI; -use Friendica\Model\Item; +use Friendica\Model\Post; use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; @@ -2224,29 +2224,7 @@ class ApiTest extends FixtureTest */ public function testApiFormatItems() { - $items = [ - [ - 'item_network' => 'item_network', - 'source' => 'web', - 'coord' => '5 7', - 'title' => '', - 'body' => '', - 'verb' => '', - 'author-id' => 43, - 'author-network' => Protocol::DFRN, - 'author-link' => 'http://localhost/profile/othercontact', - 'owner-id' => 43, - 'plink' => '', - 'uid' => $this->selfUser['id'], - 'id' => 1, - 'parent' => 1, - 'uri-id' => 1, - 'created' => '', - 'app' => '', - 'starred' => false, - 'private' => Item::PRIVATE, - ] - ]; + $items = Post::selectToArray([], ['uid' => 42]); foreach ($items as $item) { $status = api_format_item($item); self::assertStatus($status); @@ -2259,28 +2237,7 @@ class ApiTest extends FixtureTest */ public function testApiFormatItemsWithXml() { - $items = [ - [ - 'coord' => '5 7', - 'title' => '', - 'body' => '', - 'verb' => '', - 'author-id' => 43, - 'author-network' => Protocol::DFRN, - 'author-link' => 'http://localhost/profile/othercontact', - 'owner-id' => 43, - 'plink' => '', - 'uid' => $this->selfUser['id'], - 'id' => 1, - 'parent' => 1, - 'uri-id' => 1, - 'created' => '', - 'app' => '', - 'starred' => false, - 'private' => Item::PRIVATE, - ] - ]; - + $items = Post::selectToArray([], ['uid' => 42]); foreach ($items as $item) { $status = api_format_item($item, 'xml'); self::assertStatus($status); From 1e4e0914fff2622cb633ed3f6651ab4daaa0841e Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 07:10:10 +0000 Subject: [PATCH 19/24] Added parameter for test --- tests/legacy/ApiTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index e64c1e7ff..f984c48bf 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1752,10 +1752,12 @@ class ApiTest extends FixtureTest */ public function testApiStatusesUserTimeline() { + $_REQUEST['user_id'] = 42; $_REQUEST['max_id'] = 10; $_REQUEST['exclude_replies'] = true; $_REQUEST['conversation_id'] = 1; - $result = api_statuses_user_timeline('json'); + + $result = api_statuses_user_timeline('json'); self::assertNotEmpty($result['status']); foreach ($result['status'] as $status) { self::assertStatus($status); @@ -1769,8 +1771,10 @@ class ApiTest extends FixtureTest */ public function testApiStatusesUserTimelineWithNegativePage() { - $_REQUEST['page'] = -2; - $result = api_statuses_user_timeline('json'); + $_REQUEST['user_id'] = 42; + $_REQUEST['page'] = -2; + + $result = api_statuses_user_timeline('json'); self::assertNotEmpty($result['status']); foreach ($result['status'] as $status) { self::assertStatus($status); From bfddf14b6b71aa12440327bc030cdd96e9eaddaa Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 07:18:40 +0000 Subject: [PATCH 20/24] Persuading the tests --- tests/legacy/ApiTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index f984c48bf..cc89dc0f8 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1755,7 +1755,7 @@ class ApiTest extends FixtureTest $_REQUEST['user_id'] = 42; $_REQUEST['max_id'] = 10; $_REQUEST['exclude_replies'] = true; - $_REQUEST['conversation_id'] = 1; + $_REQUEST['conversation_id'] = 7; $result = api_statuses_user_timeline('json'); self::assertNotEmpty($result['status']); From 0e5f876680eeff4859da41f85cef7f38aa3a233e Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 09:55:42 +0000 Subject: [PATCH 21/24] function moved --- src/Module/Api/ApiResponse.php | 45 +++++++++++++++++++++++++++++----- src/Module/BaseApi.php | 29 ---------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/Module/Api/ApiResponse.php b/src/Module/Api/ApiResponse.php index 8d4812659..b2d17f7fb 100644 --- a/src/Module/Api/ApiResponse.php +++ b/src/Module/Api/ApiResponse.php @@ -3,12 +3,14 @@ namespace Friendica\Module\Api; use Friendica\App\Arguments; +use Friendica\App\BaseURL; use Friendica\Core\L10n; -use Friendica\Module\BaseApi; use Friendica\Util\Arrays; +use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPInputData; use Friendica\Util\XML; use Psr\Log\LoggerInterface; +use Friendica\Factory\Api\Twitter\User as TwitterUser; /** * This class is used to format and return API responses @@ -27,11 +29,13 @@ class ApiResponse * @param Arguments $args * @param LoggerInterface $logger */ - public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger) + public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger, BaseURL $baseurl, TwitterUser $twitteruser) { - $this->l10n = $l10n; - $this->args = $args; - $this->logger = $logger; + $this->l10n = $l10n; + $this->args = $args; + $this->logger = $logger; + $this->baseurl = $baseurl; + $this->twitterUser = $twitteruser; } /** @@ -103,6 +107,35 @@ class ApiResponse return XML::fromArray($data3, $xml, false, $namespaces); } + /** + * Set values for RSS template + * + * @param array $arr Array to be passed to template + * @param int $cid Contact ID of template + * @return array + */ + private function addRSSValues(array $arr, int $cid) + { + if (empty($cid)) { + return $arr; + } + + $user_info = $this->twitterUser->createFromContactId($cid)->toArray(); + + $arr['$user'] = $user_info; + $arr['$rss'] = [ + 'alternate' => $user_info['url'], + 'self' => $this->baseurl . '/' . $this->args->getQueryString(), + 'base' => $this->baseurl, + 'updated' => DateTimeFormat::utc(null, DateTimeFormat::API), + 'atom_updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), + 'language' => $user_info['lang'], + 'logo' => $this->baseurl . '/images/friendica-32.png', + ]; + + return $arr; + } + /** * Formats the data according to the data type * @@ -117,7 +150,7 @@ class ApiResponse { switch ($type) { case 'rss': - $data = BaseApi::addRSSValues($data, $cid); + $data = $this->addRSSValues($data, $cid); case 'atom': case 'xml': return $this->createXML($data, $root_element); diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index c097a5826..eaefdd0bd 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -314,33 +314,4 @@ class BaseApi extends BaseModule return $cid; } - - /** - * Set values for RSS template - * - * @param array $arr Array to be passed to template - * @param int $cid Contact ID of template - * @return array - */ - public static function addRSSValues(array $arr, int $cid) - { - if (empty($cid)) { - return $arr; - } - - $user_info = DI::twitterUser()->createFromContactId($cid)->toArray(); - - $arr['$user'] = $user_info; - $arr['$rss'] = [ - 'alternate' => $user_info['url'], - 'self' => DI::baseUrl() . '/' . DI::args()->getQueryString(), - 'base' => DI::baseUrl(), - 'updated' => DateTimeFormat::utc(null, DateTimeFormat::API), - 'atom_updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), - 'language' => $user_info['lang'], - 'logo' => DI::baseUrl() . '/images/friendica-32.png', - ]; - - return $arr; - } } From 8abf6c9dd0bf3a2f4af463f8191900931395b097 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 10:20:03 +0000 Subject: [PATCH 22/24] Possibly fixing tests --- tests/src/Module/Api/ApiResponseTest.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/src/Module/Api/ApiResponseTest.php b/tests/src/Module/Api/ApiResponseTest.php index b6f582621..69fb799e1 100644 --- a/tests/src/Module/Api/ApiResponseTest.php +++ b/tests/src/Module/Api/ApiResponseTest.php @@ -22,8 +22,10 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); - $response = new ApiResponseDouble($l10n, $args, new NullLogger()); + $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'json'); self::assertEquals('{"error":"error_message","code":"200 OK","request":""}', ApiResponseDouble::getOutput()); @@ -34,8 +36,10 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); - $response = new ApiResponseDouble($l10n, $args, new NullLogger()); + $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'xml'); self::assertEquals('' . "\n" . @@ -54,8 +58,10 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); - $response = new ApiResponseDouble($l10n, $args, new NullLogger()); + $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'rss'); self::assertEquals( @@ -75,8 +81,10 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); - $response = new ApiResponseDouble($l10n, $args, new NullLogger()); + $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'atom'); self::assertEquals( @@ -99,8 +107,10 @@ class ApiResponseTest extends MockedTest }); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); - $response = new ApiResponseDouble($l10n, $args, new NullLogger()); + $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->unsupported(); self::assertEquals('{"error":"API endpoint %s %s is not implemented","error_description":"The API endpoint is currently not implemented but might be in the future."}', ApiResponseDouble::getOutput()); From b4cb5631ad7dea270b84da29a5c0b0975ff39e2a Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Nov 2021 10:23:54 +0000 Subject: [PATCH 23/24] Fixing tests again --- tests/src/Module/Api/ApiResponseTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/src/Module/Api/ApiResponseTest.php b/tests/src/Module/Api/ApiResponseTest.php index 69fb799e1..707638ac2 100644 --- a/tests/src/Module/Api/ApiResponseTest.php +++ b/tests/src/Module/Api/ApiResponseTest.php @@ -22,7 +22,7 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(BaseURL::class); + $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); @@ -36,7 +36,7 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(BaseURL::class); + $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); @@ -58,7 +58,7 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(BaseURL::class); + $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); @@ -81,7 +81,7 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(BaseURL::class); + $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); @@ -107,7 +107,7 @@ class ApiResponseTest extends MockedTest }); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(BaseURL::class); + $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); From 8135c49613f8d9f4e962fdb8e7859349be9e0161 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 21 Nov 2021 11:45:50 +0100 Subject: [PATCH 24/24] Fix ApiResponseTest --- tests/src/Module/Api/ApiResponseTest.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/src/Module/Api/ApiResponseTest.php b/tests/src/Module/Api/ApiResponseTest.php index 707638ac2..524c9ebd1 100644 --- a/tests/src/Module/Api/ApiResponseTest.php +++ b/tests/src/Module/Api/ApiResponseTest.php @@ -3,7 +3,9 @@ namespace Friendica\Test\src\Module\Api; use Friendica\App\Arguments; +use Friendica\App\BaseURL; use Friendica\Core\L10n; +use Friendica\Factory\Api\Twitter\User; use Friendica\Test\MockedTest; use Friendica\Test\Util\ApiResponseDouble; use Psr\Log\NullLogger; @@ -22,8 +24,8 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); - $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'json'); @@ -36,8 +38,8 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); - $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'xml'); @@ -58,8 +60,8 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); - $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'rss'); @@ -81,8 +83,8 @@ class ApiResponseTest extends MockedTest $l10n = \Mockery::mock(L10n::class); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); - $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->error(200, 'OK', 'error_message', 'atom'); @@ -107,8 +109,8 @@ class ApiResponseTest extends MockedTest }); $args = \Mockery::mock(Arguments::class); $args->shouldReceive('getQueryString')->andReturn(''); - $baseUrl = \Mockery::mock(Friendica\App\BaseURL::class); - $twitterUser = \Mockery::mock(Friendica\Factory\Api\Twitter\User::class); + $baseUrl = \Mockery::mock(BaseURL::class); + $twitterUser = \Mockery::mock(User::class); $response = new ApiResponseDouble($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->unsupported();