From 9cc675233d1525f677c8c05d14765172fb7acbfd Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 27 Nov 2021 18:30:41 -0500 Subject: [PATCH 1/6] Add a second parameter to BaseApi::getRequest to enable API tests --- src/Module/Api/Friendica/Activity.php | 2 +- src/Module/Api/Friendica/DirectMessages/Setseen.php | 2 +- src/Module/Api/Friendica/Events/Index.php | 2 +- src/Module/Api/Friendica/Group/Delete.php | 2 +- src/Module/Api/Friendica/Photo/Delete.php | 2 +- src/Module/Api/Friendica/Photoalbum/Delete.php | 2 +- src/Module/Api/Friendica/Photoalbum/Update.php | 2 +- src/Module/Api/Mastodon/Accounts/Followers.php | 2 +- src/Module/Api/Mastodon/Accounts/Following.php | 2 +- src/Module/Api/Mastodon/Accounts/Note.php | 2 +- src/Module/Api/Mastodon/Accounts/Relationships.php | 2 +- src/Module/Api/Mastodon/Accounts/Search.php | 2 +- src/Module/Api/Mastodon/Accounts/Statuses.php | 2 +- src/Module/Api/Mastodon/Apps.php | 2 +- src/Module/Api/Mastodon/Blocks.php | 2 +- src/Module/Api/Mastodon/Bookmarks.php | 2 +- src/Module/Api/Mastodon/Conversations.php | 2 +- src/Module/Api/Mastodon/Directory.php | 2 +- src/Module/Api/Mastodon/Favourited.php | 2 +- src/Module/Api/Mastodon/FollowRequests.php | 2 +- src/Module/Api/Mastodon/Lists.php | 2 +- src/Module/Api/Mastodon/Lists/Accounts.php | 2 +- src/Module/Api/Mastodon/Mutes.php | 2 +- src/Module/Api/Mastodon/Notifications.php | 2 +- src/Module/Api/Mastodon/PushSubscription.php | 2 +- src/Module/Api/Mastodon/ScheduledStatuses.php | 2 +- src/Module/Api/Mastodon/Search.php | 2 +- src/Module/Api/Mastodon/Statuses.php | 2 +- src/Module/Api/Mastodon/Statuses/Context.php | 2 +- src/Module/Api/Mastodon/Suggestions.php | 2 +- src/Module/Api/Mastodon/Timelines/Direct.php | 2 +- src/Module/Api/Mastodon/Timelines/Home.php | 2 +- src/Module/Api/Mastodon/Timelines/ListTimeline.php | 2 +- src/Module/Api/Mastodon/Timelines/PublicTimeline.php | 2 +- src/Module/Api/Mastodon/Timelines/Tag.php | 2 +- src/Module/Api/Mastodon/Trends.php | 2 +- src/Module/BaseApi.php | 8 ++++++-- src/Module/OAuth/Authorize.php | 2 +- src/Module/OAuth/Revoke.php | 2 +- src/Module/OAuth/Token.php | 2 +- 40 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/Module/Api/Friendica/Activity.php b/src/Module/Api/Friendica/Activity.php index aaab6417fb..070dc452ca 100644 --- a/src/Module/Api/Friendica/Activity.php +++ b/src/Module/Api/Friendica/Activity.php @@ -47,7 +47,7 @@ class Activity extends BaseApi $request = self::getRequest([ 'id' => 0, // Id of the post - ]); + ], $request); $res = Item::performActivity($request['id'], $this->parameters['verb'], $uid); diff --git a/src/Module/Api/Friendica/DirectMessages/Setseen.php b/src/Module/Api/Friendica/DirectMessages/Setseen.php index d64b4ee15d..a6a4875b83 100644 --- a/src/Module/Api/Friendica/DirectMessages/Setseen.php +++ b/src/Module/Api/Friendica/DirectMessages/Setseen.php @@ -37,7 +37,7 @@ class Setseen extends BaseApi $request = self::getRequest([ 'id' => 0, // Id of the direct message - ]); + ], $request); // return error if id is zero if (empty($request['id'])) { diff --git a/src/Module/Api/Friendica/Events/Index.php b/src/Module/Api/Friendica/Events/Index.php index 86f79578d1..6e3daa7706 100644 --- a/src/Module/Api/Friendica/Events/Index.php +++ b/src/Module/Api/Friendica/Events/Index.php @@ -41,7 +41,7 @@ class Index extends BaseApi $request = self::getRequest([ 'since_id' => 0, 'count' => 0, - ]); + ], $request); $condition = ["`id` > ? AND `uid` = ?", $request['since_id'], $uid]; $params = ['limit' => $request['count']]; diff --git a/src/Module/Api/Friendica/Group/Delete.php b/src/Module/Api/Friendica/Group/Delete.php index 56b3445c0d..ef38d93646 100644 --- a/src/Module/Api/Friendica/Group/Delete.php +++ b/src/Module/Api/Friendica/Group/Delete.php @@ -40,7 +40,7 @@ class Delete extends BaseApi $request = self::getRequest([ 'gid' => 0, 'name' => '' - ]); + ], $request); // params diff --git a/src/Module/Api/Friendica/Photo/Delete.php b/src/Module/Api/Friendica/Photo/Delete.php index 2edf398aab..1857de5c71 100644 --- a/src/Module/Api/Friendica/Photo/Delete.php +++ b/src/Module/Api/Friendica/Photo/Delete.php @@ -40,7 +40,7 @@ class Delete extends BaseApi $request = self::getRequest([ 'photo_id' => null, // Photo id - ]); + ], $request); // do several checks on input parameters // we do not allow calls without photo id diff --git a/src/Module/Api/Friendica/Photoalbum/Delete.php b/src/Module/Api/Friendica/Photoalbum/Delete.php index c7592a5eaf..2d71e35812 100644 --- a/src/Module/Api/Friendica/Photoalbum/Delete.php +++ b/src/Module/Api/Friendica/Photoalbum/Delete.php @@ -41,7 +41,7 @@ class Delete extends BaseApi $request = self::getRequest([ 'album' => '', // Album name - ]); + ], $request); // we do not allow calls without album string if (empty($request['album'])) { diff --git a/src/Module/Api/Friendica/Photoalbum/Update.php b/src/Module/Api/Friendica/Photoalbum/Update.php index 87513c5ad4..2c1e5e8786 100644 --- a/src/Module/Api/Friendica/Photoalbum/Update.php +++ b/src/Module/Api/Friendica/Photoalbum/Update.php @@ -40,7 +40,7 @@ class Update extends BaseApi $request = self::getRequest([ 'album' => '', // Current album name 'album_new' => '', // New album name - ]); + ], $request); // we do not allow calls without album string if (empty($request['album'])) { diff --git a/src/Module/Api/Mastodon/Accounts/Followers.php b/src/Module/Api/Mastodon/Accounts/Followers.php index 08c56b3d44..ba24625c47 100644 --- a/src/Module/Api/Mastodon/Accounts/Followers.php +++ b/src/Module/Api/Mastodon/Accounts/Followers.php @@ -53,7 +53,7 @@ class Followers extends BaseApi 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id 'limit' => 40, // Maximum number of results to return. Defaults to 40. - ]); + ], $request); $params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Accounts/Following.php b/src/Module/Api/Mastodon/Accounts/Following.php index ded1e6a4b6..a80af6ea99 100644 --- a/src/Module/Api/Mastodon/Accounts/Following.php +++ b/src/Module/Api/Mastodon/Accounts/Following.php @@ -53,7 +53,7 @@ class Following extends BaseApi 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id 'limit' => 40, // Maximum number of results to return. Defaults to 40. - ]); + ], $request); $params = ['order' => ['cid' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Accounts/Note.php b/src/Module/Api/Mastodon/Accounts/Note.php index cb0c181350..7135cb930e 100644 --- a/src/Module/Api/Mastodon/Accounts/Note.php +++ b/src/Module/Api/Mastodon/Accounts/Note.php @@ -43,7 +43,7 @@ class Note extends BaseApi $request = self::getRequest([ 'comment' => '', - ]); + ], $request); $cdata = Contact::getPublicAndUserContactID($this->parameters['id'], $uid); if (empty($cdata['user'])) { diff --git a/src/Module/Api/Mastodon/Accounts/Relationships.php b/src/Module/Api/Mastodon/Accounts/Relationships.php index c3c8864468..b56fd5a946 100644 --- a/src/Module/Api/Mastodon/Accounts/Relationships.php +++ b/src/Module/Api/Mastodon/Accounts/Relationships.php @@ -41,7 +41,7 @@ class Relationships extends BaseApi $request = self::getRequest([ 'id' => [], - ]); + ], $request); if (empty($request['id'])) { DI::mstdnError()->UnprocessableEntity(); diff --git a/src/Module/Api/Mastodon/Accounts/Search.php b/src/Module/Api/Mastodon/Accounts/Search.php index b7b963e11f..53569b79ac 100644 --- a/src/Module/Api/Mastodon/Accounts/Search.php +++ b/src/Module/Api/Mastodon/Accounts/Search.php @@ -47,7 +47,7 @@ class Search extends BaseApi 'limit' => 40, // Maximum number of results. Defaults to 40. 'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address. 'following' => false, // Only who the user is following. Defaults to false. - ]); + ], $request); $accounts = []; diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index 9027c68a4f..609bcdb202 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -63,7 +63,7 @@ class Statuses extends BaseApi 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'exclude_reblogs' => false, // Undocumented parameter 'tagged' => false, // Undocumented parameter - ]); + ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Apps.php b/src/Module/Api/Mastodon/Apps.php index 7b0b103c4c..5811e38733 100644 --- a/src/Module/Api/Mastodon/Apps.php +++ b/src/Module/Api/Mastodon/Apps.php @@ -42,7 +42,7 @@ class Apps extends BaseApi 'redirect_uris' => '', 'scopes' => 'read', 'website' => '', - ]); + ], $request); // Workaround for AndStatus, see issue https://github.com/andstatus/andstatus/issues/538 $postdata = Network::postdata(); diff --git a/src/Module/Api/Mastodon/Blocks.php b/src/Module/Api/Mastodon/Blocks.php index fe08f875dd..0a1e9506ff 100644 --- a/src/Module/Api/Mastodon/Blocks.php +++ b/src/Module/Api/Mastodon/Blocks.php @@ -53,7 +53,7 @@ class Blocks extends BaseApi 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id 'limit' => 40, // Maximum number of results. Defaults to 40. - ]); + ], $request); $params = ['order' => ['cid' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Bookmarks.php b/src/Module/Api/Mastodon/Bookmarks.php index e5fcaeeaf5..d56ad5a883 100644 --- a/src/Module/Api/Mastodon/Bookmarks.php +++ b/src/Module/Api/Mastodon/Bookmarks.php @@ -47,7 +47,7 @@ class Bookmarks extends BaseApi 'since_id' => 0, // Return results newer than id 'min_id' => 0, // Return results immediately newer than id 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. - ]); + ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Conversations.php b/src/Module/Api/Mastodon/Conversations.php index fc32c76ccc..5ff51deb14 100644 --- a/src/Module/Api/Mastodon/Conversations.php +++ b/src/Module/Api/Mastodon/Conversations.php @@ -59,7 +59,7 @@ class Conversations extends BaseApi 'max_id' => 0, // Return results older than this ID. Use HTTP Link header to paginate. 'since_id' => 0, // Return results newer than this ID. Use HTTP Link header to paginate. 'min_id' => 0, // Return results immediately newer than this ID. Use HTTP Link header to paginate. - ]); + ], $request); $params = ['order' => ['id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Directory.php b/src/Module/Api/Mastodon/Directory.php index 8ee6ac0360..740d5d92ae 100644 --- a/src/Module/Api/Mastodon/Directory.php +++ b/src/Module/Api/Mastodon/Directory.php @@ -46,7 +46,7 @@ class Directory extends BaseApi 'limit' => 40, // How many accounts to load. Default 40. 'order' => 'active', // active to sort by most recently posted statuses (default) or new to sort by most recently created profiles. 'local' => false, // Only return local accounts. - ]); + ], $request); Logger::info('directory', ['offset' => $request['offset'], 'limit' => $request['limit'], 'order' => $request['order'], 'local' => $request['local']]); diff --git a/src/Module/Api/Mastodon/Favourited.php b/src/Module/Api/Mastodon/Favourited.php index 3d3e6aad1e..c1ac2a89cc 100644 --- a/src/Module/Api/Mastodon/Favourited.php +++ b/src/Module/Api/Mastodon/Favourited.php @@ -47,7 +47,7 @@ class Favourited extends BaseApi 'min_id' => 0, // Return results immediately newer than id 'max_id' => 0, // Return results older than id 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. - ]); + ], $request); $params = ['order' => ['thr-parent-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php index dbdab7e51b..739c53c491 100644 --- a/src/Module/Api/Mastodon/FollowRequests.php +++ b/src/Module/Api/Mastodon/FollowRequests.php @@ -91,7 +91,7 @@ class FollowRequests extends BaseApi 'min_id' => 0, 'max_id' => 0, 'limit' => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header. - ]); + ], $request); $introductions = DI::intro()->selectForUser($uid, $request['min_id'], $request['max_id'], $request['limit']); diff --git a/src/Module/Api/Mastodon/Lists.php b/src/Module/Api/Mastodon/Lists.php index ec9ad86a34..53a9ac0128 100644 --- a/src/Module/Api/Mastodon/Lists.php +++ b/src/Module/Api/Mastodon/Lists.php @@ -58,7 +58,7 @@ class Lists extends BaseApi $request = self::getRequest([ 'title' => '', - ]); + ], $request); if (empty($request['title'])) { DI::mstdnError()->UnprocessableEntity(); diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index be87dbf9d1..151da9cc62 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -66,7 +66,7 @@ class Accounts extends BaseApi 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id 'limit' => 40, // Maximum number of results. Defaults to 40. Max 40. Set to 0 in order to get all accounts without pagination. - ]); + ], $request); $params = ['order' => ['contact-id' => true]]; diff --git a/src/Module/Api/Mastodon/Mutes.php b/src/Module/Api/Mastodon/Mutes.php index 515e04848c..9d612a455f 100644 --- a/src/Module/Api/Mastodon/Mutes.php +++ b/src/Module/Api/Mastodon/Mutes.php @@ -53,7 +53,7 @@ class Mutes extends BaseApi 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id 'limit' => 40, // Maximum number of results. Defaults to 40. - ]); + ], $request); $params = ['order' => ['cid' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Notifications.php b/src/Module/Api/Mastodon/Notifications.php index 664c996c97..f57c0268e3 100644 --- a/src/Module/Api/Mastodon/Notifications.php +++ b/src/Module/Api/Mastodon/Notifications.php @@ -64,7 +64,7 @@ class Notifications extends BaseApi 'account_id' => 0, // Return only notifications received from this account 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'count' => 0, // Unknown parameter - ]); + ], $request); $params = ['order' => ['id' => true]]; diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index 91e79b82db..b4b152e5c5 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -42,7 +42,7 @@ class PushSubscription extends BaseApi $request = self::getRequest([ 'subscription' => [], 'data' => [], - ]); + ], $request); $subscription = [ 'application-id' => $application['id'], diff --git a/src/Module/Api/Mastodon/ScheduledStatuses.php b/src/Module/Api/Mastodon/ScheduledStatuses.php index e79d1e153f..644aea874c 100644 --- a/src/Module/Api/Mastodon/ScheduledStatuses.php +++ b/src/Module/Api/Mastodon/ScheduledStatuses.php @@ -76,7 +76,7 @@ class ScheduledStatuses extends BaseApi 'max_id' => 0, // Return results older than ID 'since_id' => 0, // Return results newer than ID 'min_id' => 0, // Return results immediately newer than ID - ]); + ], $request); $params = ['order' => ['id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 026d71f04b..1730db68c3 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -56,7 +56,7 @@ class Search extends BaseApi 'limit' => 20, // Maximum number of results to load, per type. Defaults to 20. Max 40. 'offset' => 0, // Offset in search results. Used for pagination. Defaults to 0. 'following' => false, // Only include accounts that the user is following. Defaults to false. - ]); + ], $request); if (empty($request['q'])) { DI::mstdnError()->UnprocessableEntity(); diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index 21bb972850..54e85d2a6d 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -56,7 +56,7 @@ class Statuses extends BaseApi 'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct". 'scheduled_at' => '', // ISO 8601 Datetime at which to schedule a status. Providing this paramter will cause ScheduledStatus to be returned instead of Status. Must be at least 5 minutes in the future. 'language' => '', // ISO 639 language code for this status. - ]); + ], $request); $owner = User::getOwnerDataById($uid); diff --git a/src/Module/Api/Mastodon/Statuses/Context.php b/src/Module/Api/Mastodon/Statuses/Context.php index 674be5f22d..4193b8fa92 100644 --- a/src/Module/Api/Mastodon/Statuses/Context.php +++ b/src/Module/Api/Mastodon/Statuses/Context.php @@ -45,7 +45,7 @@ class Context extends BaseApi $request = self::getRequest([ 'limit' => 40, // Maximum number of results to return. Defaults to 40. - ]); + ], $request); $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Suggestions.php b/src/Module/Api/Mastodon/Suggestions.php index bda78becf1..d8d89070a0 100644 --- a/src/Module/Api/Mastodon/Suggestions.php +++ b/src/Module/Api/Mastodon/Suggestions.php @@ -41,7 +41,7 @@ class Suggestions extends BaseApi $request = self::getRequest([ 'limit' => 40, // Maximum number of results to return. Defaults to 40. - ]); + ], $request); $suggestions = Contact\Relation::getSuggestions($uid, 0, $request['limit']); diff --git a/src/Module/Api/Mastodon/Timelines/Direct.php b/src/Module/Api/Mastodon/Timelines/Direct.php index ef2a9bb1a3..f6b55a093d 100644 --- a/src/Module/Api/Mastodon/Timelines/Direct.php +++ b/src/Module/Api/Mastodon/Timelines/Direct.php @@ -45,7 +45,7 @@ class Direct extends BaseApi 'since_id' => 0, // Return results newer than id 'min_id' => 0, // Return results immediately newer than id 'limit' => 20, // Maximum number of results to return. Defaults to 20. - ]); + ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php index 2727745f03..f717ef119d 100644 --- a/src/Module/Api/Mastodon/Timelines/Home.php +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -51,7 +51,7 @@ class Home extends BaseApi 'only_media' => false, // Show only statuses with media attached? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false. 'exclude_replies' => false, // Don't show comments - ]); + ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Timelines/ListTimeline.php b/src/Module/Api/Mastodon/Timelines/ListTimeline.php index 1fe3f3b47f..a1ecd61f6f 100644 --- a/src/Module/Api/Mastodon/Timelines/ListTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/ListTimeline.php @@ -55,7 +55,7 @@ class ListTimeline extends BaseApi 'local' => false, // Show only local statuses? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false. 'exclude_replies' => false, // Don't show comments - ]); + ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 91894f5c6c..9d3b7f8365 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -53,7 +53,7 @@ class PublicTimeline extends BaseApi 'limit' => 20, // Maximum number of results to return. Defaults to 20. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'exclude_replies' => false, // Don't show comments - ]); + ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php index 701d656e43..d6915a68fd 100644 --- a/src/Module/Api/Mastodon/Timelines/Tag.php +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -63,7 +63,7 @@ class Tag extends BaseApi 'limit' => 20, // Maximum number of results to return. Defaults to 20. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'exclude_replies' => false, // Don't show comments - ]); + ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; diff --git a/src/Module/Api/Mastodon/Trends.php b/src/Module/Api/Mastodon/Trends.php index c9659f7d24..4e99b2c743 100644 --- a/src/Module/Api/Mastodon/Trends.php +++ b/src/Module/Api/Mastodon/Trends.php @@ -38,7 +38,7 @@ class Trends extends BaseApi { $request = self::getRequest([ 'limit' => 20, // Maximum number of results to return. Defaults to 10. - ]); + ], $request); $trending = []; $tags = Tag::getGlobalTrendingHashtags(24, 20); diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index 009809376e..5c94bc7727 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -108,12 +108,16 @@ class BaseApi extends BaseModule /** * Processes data from GET requests and sets defaults * + * @param array $defaults Associative array of expected request keys and their default typed value. A null + * value will remove the request key from the resulting value array. + * @param array|null $request Custom REQUEST array, superglobal instead * @return array request data + * @throws \Exception */ - public static function getRequest(array $defaults) + public static function getRequest(array $defaults, array $request = null): array { $httpinput = HTTPInputData::process(); - $input = array_merge($httpinput['variables'], $httpinput['files'], $_REQUEST); + $input = array_merge($httpinput['variables'], $httpinput['files'], $request ?? $_REQUEST); self::$request = $input; self::$boundaries = []; diff --git a/src/Module/OAuth/Authorize.php b/src/Module/OAuth/Authorize.php index 851e0f4611..973f31a0b1 100644 --- a/src/Module/OAuth/Authorize.php +++ b/src/Module/OAuth/Authorize.php @@ -47,7 +47,7 @@ class Authorize extends BaseApi 'redirect_uri' => '', // Set a URI to redirect the user to. If this parameter is set to "urn:ietf:wg:oauth:2.0:oob" then the authorization code will be shown instead. Must match one of the redirect URIs declared during app registration. 'scope' => 'read', // List of requested OAuth scopes, separated by spaces (or by pluses, if using query parameters). Must be a subset of scopes declared during app registration. If not provided, defaults to "read". 'state' => '', - ]); + ], $request); if ($request['response_type'] != 'code') { Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]); diff --git a/src/Module/OAuth/Revoke.php b/src/Module/OAuth/Revoke.php index 6543969bfb..536d603c79 100644 --- a/src/Module/OAuth/Revoke.php +++ b/src/Module/OAuth/Revoke.php @@ -38,7 +38,7 @@ class Revoke extends BaseApi 'client_id' => '', // Client ID, obtained during app registration 'client_secret' => '', // Client secret, obtained during app registration 'token' => '', // The previously obtained token, to be invalidated - ]); + ], $request); $condition = ['client_id' => $request['client_id'], 'client_secret' => $request['client_secret'], 'access_token' => $request['token']]; $token = DBA::selectFirst('application-view', ['id'], $condition); diff --git a/src/Module/OAuth/Token.php b/src/Module/OAuth/Token.php index 20ba22490c..8bbb272c31 100644 --- a/src/Module/OAuth/Token.php +++ b/src/Module/OAuth/Token.php @@ -43,7 +43,7 @@ class Token extends BaseApi 'scope' => 'read', // List of requested OAuth scopes, separated by spaces. Must be a subset of scopes declared during app registration. If not provided, defaults to "read". 'code' => '', // A user authorization code, obtained via /oauth/authorize 'grant_type' => '', // Set equal to "authorization_code" if code is provided in order to gain user-level access. Otherwise, set equal to "client_credentials" to obtain app-level access only. - ]); + ], $request); // AndStatus transmits the client data in the AUTHORIZATION header field, see https://github.com/andstatus/andstatus/issues/530 $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? ''; From b15d3a2523f3e147f476807b3324c6bbc8f4cdad Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Nov 2021 09:42:05 +0000 Subject: [PATCH 2/6] API: Next bunch of functions transformed --- include/api.php | 255 +----------------- src/Factory/Api/Twitter/Status.php | 31 ++- src/Module/Api/Twitter/Favorites/Create.php | 51 ++++ src/Module/Api/Twitter/Favorites/Destroy.php | 51 ++++ .../Api/Twitter/Friendships/Destroy.php | 86 ++++++ .../Api/Twitter/Media/Metadata/Create.php | 72 +++++ src/Module/Api/Twitter/Media/Upload.php | 70 +++++ src/Module/Api/Twitter/Statuses/Destroy.php | 8 +- src/Object/Api/Twitter/Status.php | 4 +- static/routes.config.php | 22 +- tests/legacy/ApiTest.php | 29 +- 11 files changed, 387 insertions(+), 292 deletions(-) create mode 100644 src/Module/Api/Twitter/Favorites/Create.php create mode 100644 src/Module/Api/Twitter/Favorites/Destroy.php create mode 100644 src/Module/Api/Twitter/Friendships/Destroy.php create mode 100644 src/Module/Api/Twitter/Media/Metadata/Create.php create mode 100644 src/Module/Api/Twitter/Media/Upload.php diff --git a/include/api.php b/include/api.php index b291b6e002..06d3607ab1 100644 --- a/include/api.php +++ b/include/api.php @@ -38,7 +38,6 @@ use Friendica\Model\Mail; use Friendica\Model\Photo; use Friendica\Model\Post; use Friendica\Model\Profile; -use Friendica\Model\User; use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; use Friendica\Network\HTTPException\BadRequestException; @@ -50,11 +49,9 @@ use Friendica\Network\HTTPException\UnauthorizedException; use Friendica\Object\Image; use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; -use Friendica\Util\Network; use Friendica\Util\Strings; require_once __DIR__ . '/../mod/item.php'; -require_once __DIR__ . '/../mod/wall_upload.php'; $API = []; @@ -681,7 +678,7 @@ function api_statuses_mediap($type) } $txt = HTML::toBBCode($txt); - $picture = wall_upload_post($a, false); + $picture = Photo::upload($uid, $_FILES['media']); // now that we have the img url in bbcode we can add it to the status and insert the wall item. $_REQUEST['body'] = $txt . "\n\n" . '[url=' . $picture["albumpage"] . '][img]' . $picture["preview"] . "[/img][/url]"; @@ -690,11 +687,10 @@ function api_statuses_mediap($type) $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); // output the post that we just posted. - $status_info = DI::twitterStatus()->createFromItemId($item_id, $include_entities)->toArray(); + $status_info = DI::twitterStatus()->createFromItemId($item_id, $uid, $include_entities)->toArray(); return DI::apiResponse()->formatData('statuses', $type, ['status' => $status_info]); } -/// @TODO move this to top of file or somewhere better! api_register_func('api/statuses/mediap', 'api_statuses_mediap', true); /** @@ -805,7 +801,7 @@ function api_statuses_update($type) $ids = explode(',', $_REQUEST['media_ids']); } elseif (!empty($_FILES['media'])) { // upload the image if we have one - $picture = wall_upload_post($a, false); + $picture = Photo::upload($uid, $_FILES['media']); if (is_array($picture)) { $ids[] = $picture['id']; } @@ -874,108 +870,13 @@ function api_statuses_update($type) $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); // output the post that we just posted. - $status_info = DI::twitterStatus()->createFromItemId($item_id, $include_entities)->toArray(); + $status_info = DI::twitterStatus()->createFromItemId($item_id, $uid, $include_entities)->toArray(); return DI::apiResponse()->formatData('statuses', $type, ['status' => $status_info]); } api_register_func('api/statuses/update', 'api_statuses_update', true); api_register_func('api/statuses/update_with_media', 'api_statuses_update', true); -/** - * Uploads an image to Friendica. - * - * @return array - * @throws BadRequestException - * @throws ForbiddenException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - * @see https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload - */ -function api_media_upload() -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - - if (empty($_FILES['media'])) { - // Output error - throw new BadRequestException("No media."); - } - - $media = wall_upload_post(DI::app(), false); - if (!$media) { - // Output error - throw new InternalServerErrorException(); - } - - $returndata = []; - $returndata["media_id"] = $media["id"]; - $returndata["media_id_string"] = (string)$media["id"]; - $returndata["size"] = $media["size"]; - $returndata["image"] = ["w" => $media["width"], - "h" => $media["height"], - "image_type" => $media["type"], - "friendica_preview_url" => $media["preview"]]; - - Logger::info('Media uploaded', ['return' => $returndata]); - - return ["media" => $returndata]; -} - -api_register_func('api/media/upload', 'api_media_upload', true); - -/** - * Updates media meta data (picture descriptions) - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws BadRequestException - * @throws ForbiddenException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws TooManyRequestsException - * @throws UnauthorizedException - * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update - * - * @todo Compare the corresponding Twitter function for correct return values - */ -function api_media_metadata_create($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - $postdata = Network::postdata(); - - if (empty($postdata)) { - throw new BadRequestException("No post data"); - } - - $data = json_decode($postdata, true); - if (empty($data)) { - throw new BadRequestException("Invalid post data"); - } - - if (empty($data['media_id']) || empty($data['alt_text'])) { - throw new BadRequestException("Missing post data values"); - } - - if (empty($data['alt_text']['text'])) { - throw new BadRequestException("No alt text."); - } - - Logger::info('Updating metadata', ['media_id' => $data['media_id']]); - - $condition = ['id' => $data['media_id'], 'uid' => $uid]; - $photo = DBA::selectFirst('photo', ['resource-id'], $condition); - if (!DBA::isResult($photo)) { - throw new BadRequestException("Metadata not found."); - } - - DBA::update('photo', ['desc' => $data['alt_text']['text']], ['resource-id' => $photo['resource-id']]); -} - -api_register_func('api/media/metadata/create', 'api_media_metadata_create', true); - /** * Repeats a status. * @@ -1049,81 +950,12 @@ function api_statuses_repeat($type) $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); // output the post that we just posted. - $status_info = DI::twitterStatus()->createFromItemId($item_id, $include_entities)->toArray(); + $status_info = DI::twitterStatus()->createFromItemId($item_id, $uid, $include_entities)->toArray(); return DI::apiResponse()->formatData('statuses', $type, ['status' => $status_info]); } api_register_func('api/statuses/retweet', 'api_statuses_repeat', true); -/** - * Star/unstar an item. - * param: id : id of the item - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws BadRequestException - * @throws ForbiddenException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - * @see https://web.archive.org/web/20131019055350/https://dev.twitter.com/docs/api/1/post/favorites/create/%3Aid - */ -function api_favorites_create_destroy($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // for versioned api. - /// @TODO We need a better global soluton - $action_argv_id = 2; - if (count(DI::args()->getArgv()) > 1 && DI::args()->getArgv()[1] == "1.1") { - $action_argv_id = 3; - } - - if (DI::args()->getArgc() <= $action_argv_id) { - throw new BadRequestException("Invalid request."); - } - $action = str_replace("." . $type, "", DI::args()->getArgv()[$action_argv_id]); - if (DI::args()->getArgc() == $action_argv_id + 2) { - $itemid = intval(DI::args()->getArgv()[$action_argv_id + 1] ?? 0); - } else { - $itemid = intval($_REQUEST['id'] ?? 0); - } - - $item = Post::selectFirstForUser($uid, [], ['id' => $itemid, 'uid' => $uid]); - - if (!DBA::isResult($item)) { - throw new BadRequestException("Invalid item."); - } - - switch ($action) { - case "create": - $item['starred'] = 1; - break; - case "destroy": - $item['starred'] = 0; - break; - default: - throw new BadRequestException("Invalid action ".$action); - } - - $r = Item::update(['starred' => $item['starred']], ['id' => $itemid]); - - if ($r === false) { - throw new InternalServerErrorException("DB error"); - } - - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - - $ret = DI::twitterStatus()->createFromUriId($item['uri-id'], $item['uid'], $include_entities)->toArray(); - - return DI::apiResponse()->formatData("status", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid)); -} - -api_register_func('api/favorites/create', 'api_favorites_create_destroy', true); -api_register_func('api/favorites/destroy', 'api_favorites_create_destroy', true); - /** * Returns all lists the user subscribes to. * @@ -1312,83 +1144,6 @@ function api_direct_messages_destroy($type) api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true); -/** - * Unfollow Contact - * - * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string|array - * @throws HTTPException\BadRequestException - * @throws HTTPException\ExpectationFailedException - * @throws HTTPException\ForbiddenException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\NotFoundException - * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy.html - */ -function api_friendships_destroy($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - $owner = User::getOwnerDataById($uid); - if (!$owner) { - Logger::notice(BaseApi::LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]); - throw new HTTPException\NotFoundException('Error Processing Request'); - } - - $contact_id = $_REQUEST['user_id'] ?? 0; - - if (empty($contact_id)) { - Logger::notice(BaseApi::LOG_PREFIX . 'No user_id specified', ['module' => 'api', 'action' => 'friendships_destroy']); - throw new HTTPException\BadRequestException('no user_id specified'); - } - - // Get Contact by given id - $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => 0, 'self' => false]); - - if(!DBA::isResult($contact)) { - Logger::notice(BaseApi::LOG_PREFIX . 'No public contact found for ID {contact}', ['module' => 'api', 'action' => 'friendships_destroy', 'contact' => $contact_id]); - throw new HTTPException\NotFoundException('no contact found to given ID'); - } - - $url = $contact['url']; - - $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)", - $uid, Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url), - Strings::normaliseLink($url), $url]; - $contact = DBA::selectFirst('contact', [], $condition); - - if (!DBA::isResult($contact)) { - Logger::notice(BaseApi::LOG_PREFIX . 'Not following contact', ['module' => 'api', 'action' => 'friendships_destroy']); - throw new HTTPException\NotFoundException('Not following Contact'); - } - - try { - $result = Contact::terminateFriendship($owner, $contact); - - if ($result === null) { - Logger::notice(BaseApi::LOG_PREFIX . 'Not supported for {network}', ['module' => 'api', 'action' => 'friendships_destroy', 'network' => $contact['network']]); - throw new HTTPException\ExpectationFailedException('Unfollowing is currently not supported by this contact\'s network.'); - } - - if ($result === false) { - throw new HTTPException\ServiceUnavailableException('Unable to unfollow this contact, please retry in a few minutes or contact your administrator.'); - } - } catch (Exception $e) { - Logger::error(BaseApi::LOG_PREFIX . $e->getMessage(), ['owner' => $owner, 'contact' => $contact]); - throw new HTTPException\InternalServerErrorException('Unable to unfollow this contact, please contact your administrator'); - } - - // "uid" is only needed for some internal stuff, so remove it from here - unset($contact['uid']); - - // Set screen_name since Twidere requests it - $contact['screen_name'] = $contact['nick']; - - return DI::apiResponse()->formatData('friendships-destroy', $type, ['user' => $contact]); -} - -api_register_func('api/friendships/destroy', 'api_friendships_destroy', true); - /** * * @param string $type Return type (atom, rss, xml, json) diff --git a/src/Factory/Api/Twitter/Status.php b/src/Factory/Api/Twitter/Status.php index 921f878e74..2db1cf1355 100644 --- a/src/Factory/Api/Twitter/Status.php +++ b/src/Factory/Api/Twitter/Status.php @@ -74,7 +74,7 @@ class Status extends BaseFactory * @throws HTTPException\InternalServerErrorException * @throws ImagickException|HTTPException\NotFoundException */ - public function createFromItemId(int $id, $include_entities = false): \Friendica\Object\Api\Twitter\Status + public function createFromItemId(int $id, int $uid, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status { $fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network', 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord']; @@ -82,7 +82,7 @@ class Status extends BaseFactory if (!$item) { throw new HTTPException\NotFoundException('Item with ID ' . $id . ' not found.'); } - return $this->createFromArray($item, $include_entities); + return $this->createFromArray($item, $uid, $include_entities); } /** @@ -101,7 +101,7 @@ class Status extends BaseFactory if (!$item) { throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.')); } - return $this->createFromArray($item, $include_entities); + return $this->createFromArray($item, $uid, $include_entities); } /** @@ -112,10 +112,10 @@ class Status extends BaseFactory * @throws HTTPException\InternalServerErrorException * @throws ImagickException|HTTPException\NotFoundException */ - private function createFromArray(array $item, $include_entities): \Friendica\Object\Api\Twitter\Status + private function createFromArray(array $item, int $uid, bool $include_entities): \Friendica\Object\Api\Twitter\Status { - $author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid'], true); - $owner = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid'], true); + $author = $this->twitterUser->createFromContactId($item['author-id'], $uid, true); + $owner = $this->twitterUser->createFromContactId($item['owner-id'], $uid, true); $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]); @@ -135,6 +135,15 @@ class Status extends BaseFactory } } + $liked = Post::exists([ + 'thr-parent-id' => $item['uri-id'], + 'uid' => $uid, + 'origin' => true, + 'gravity' => GRAVITY_ACTIVITY, + 'vid' => Verb::getID(Activity::LIKE), + 'deleted' => false + ]); + if ($include_entities) { $hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text); $medias = $this->media->createFromUriId($item['uri-id'], $text); @@ -144,7 +153,7 @@ class Status extends BaseFactory $attachments = $this->attachment->createFromUriId($item['uri-id'], $text); } - $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $item['uid']); + $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid); $shared = BBCode::fetchShareAttributes($item['body']); if (!empty($shared['guid'])) { @@ -163,11 +172,11 @@ class Status extends BaseFactory } if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) { - $retweeted = $this->createFromUriId($item['thr-parent-id'], $item['uid'])->toArray(); - $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $item['uid']]]); + $retweeted = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray(); + $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]); $item['title'] = $retweeted_item['title'] ?? $item['title']; $item['body'] = $retweeted_item['body'] ?? $item['body']; - $author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid'], true); + $author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $uid, true); } else { $retweeted = []; } @@ -181,6 +190,6 @@ class Status extends BaseFactory $entities = []; } - return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments); + return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments, $liked); } } diff --git a/src/Module/Api/Twitter/Favorites/Create.php b/src/Module/Api/Twitter/Favorites/Create.php new file mode 100644 index 0000000000..74fb722377 --- /dev/null +++ b/src/Module/Api/Twitter/Favorites/Create.php @@ -0,0 +1,51 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Favorites; + +use Friendica\DI; +use Friendica\Model\Item; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException\BadRequestException; + +/** + * @see https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-favorites-create + */ +class Create extends BaseApi +{ + protected function rawContent(array $request = []) + { + self::checkAllowedScope(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + + $id = $request['id'] ?? 0; + + if (empty($id)) { + throw new BadRequestException('Item id not specified'); + } + + Item::performActivity($id, 'like', $uid); + + $status_info = DI::twitterStatus()->createFromItemId($id, $uid)->toArray(); + + DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Twitter/Favorites/Destroy.php b/src/Module/Api/Twitter/Favorites/Destroy.php new file mode 100644 index 0000000000..6c797d8b50 --- /dev/null +++ b/src/Module/Api/Twitter/Favorites/Destroy.php @@ -0,0 +1,51 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Favorites; + +use Friendica\DI; +use Friendica\Model\Item; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException\BadRequestException; + +/** + * @see https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-favorites-destroy + */ +class Destroy extends BaseApi +{ + protected function rawContent(array $request = []) + { + self::checkAllowedScope(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + + $id = $request['id'] ?? 0; + + if (empty($id)) { + throw new BadRequestException('Item id not specified'); + } + + Item::performActivity($id, 'unlike', $uid); + + $status_info = DI::twitterStatus()->createFromItemId($id, $uid)->toArray(); + + DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Twitter/Friendships/Destroy.php b/src/Module/Api/Twitter/Friendships/Destroy.php new file mode 100644 index 0000000000..ef8ad71e8e --- /dev/null +++ b/src/Module/Api/Twitter/Friendships/Destroy.php @@ -0,0 +1,86 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Friendships; + +use Exception; +use Friendica\Core\Logger; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Model\User; +use Friendica\Module\Api\Twitter\ContactEndpoint; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * Unfollow Contact + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy.html + */ +class Destroy extends ContactEndpoint +{ + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + $owner = User::getOwnerDataById($uid); + if (!$owner) { + Logger::notice(BaseApi::LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]); + throw new HTTPException\NotFoundException('Error Processing Request'); + } + + $contact_id = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, 0); + + if (empty($contact_id)) { + Logger::notice(BaseApi::LOG_PREFIX . 'No user_id specified', ['module' => 'api', 'action' => 'friendships_destroy']); + throw new HTTPException\BadRequestException('no user_id specified'); + } + + // Get Contact by given id + $cdata = Contact::getPublicAndUserContactID($contact_id, $uid); + if (!empty($cdata['user'])) { + Logger::notice(BaseApi::LOG_PREFIX . 'Not following contact', ['module' => 'api', 'action' => 'friendships_destroy']); + throw new HTTPException\NotFoundException('Not following Contact'); + } + + $contact = Contact::getById($cdata['user']); + $user = $this->twitterUser->createFromContactId($contact_id, $uid, true)->toArray(); + + try { + $result = Contact::terminateFriendship($owner, $contact); + + if ($result === null) { + Logger::notice(BaseApi::LOG_PREFIX . 'Not supported for {network}', ['module' => 'api', 'action' => 'friendships_destroy', 'network' => $contact['network']]); + throw new HTTPException\ExpectationFailedException('Unfollowing is currently not supported by this contact\'s network.'); + } + + if ($result === false) { + throw new HTTPException\ServiceUnavailableException('Unable to unfollow this contact, please retry in a few minutes or contact your administrator.'); + } + } catch (Exception $e) { + Logger::error(BaseApi::LOG_PREFIX . $e->getMessage(), ['owner' => $owner, 'contact' => $contact]); + throw new HTTPException\InternalServerErrorException('Unable to unfollow this contact, please contact your administrator'); + } + + DI::apiResponse()->exit('friendships', ['user' => $user], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Twitter/Media/Metadata/Create.php b/src/Module/Api/Twitter/Media/Metadata/Create.php new file mode 100644 index 0000000000..d9dc77c3d2 --- /dev/null +++ b/src/Module/Api/Twitter/Media/Metadata/Create.php @@ -0,0 +1,72 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Media\Metadata; + +use Friendica\Core\Logger; +use Friendica\Model\Photo; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException\BadRequestException; +use Friendica\Util\Network; + +/** + * Updates media meta data (picture descriptions) + * + * @see https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-metadata-create + */ +class Create extends BaseApi +{ + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + $postdata = Network::postdata(); + + if (empty($postdata)) { + throw new BadRequestException('No post data'); + } + + $data = json_decode($postdata, true); + if (empty($data)) { + throw new BadRequestException('Invalid post data'); + } + + if (empty($data['media_id']) || empty($data['alt_text'])) { + throw new BadRequestException('Missing post data values'); + } + + if (empty($data['alt_text']['text'])) { + throw new BadRequestException('No alt text.'); + } + + Logger::info('Updating metadata', ['media_id' => $data['media_id']]); + + $condition = ['id' => $data['media_id'], 'uid' => $uid]; + + $photo = Photo::selectFirst(['resource-id'], $condition); + if (empty($photo['resource-id'])) { + throw new BadRequestException('Metadata not found.'); + } + + Photo::update(['desc' => $data['alt_text']['text']], ['resource-id' => $photo['resource-id']]); + } +} diff --git a/src/Module/Api/Twitter/Media/Upload.php b/src/Module/Api/Twitter/Media/Upload.php new file mode 100644 index 0000000000..48538a3313 --- /dev/null +++ b/src/Module/Api/Twitter/Media/Upload.php @@ -0,0 +1,70 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Media; + +use Friendica\Core\Logger; +use Friendica\DI; +use Friendica\Model\Photo; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException\BadRequestException; +use Friendica\Network\HTTPException\InternalServerErrorException; + +/** + * Uploads an image to Friendica. + * + * @see https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload + */ +class Upload extends BaseApi +{ + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + if (empty($_FILES['media'])) { + // Output error + throw new BadRequestException("No media."); + } + + $media = Photo::upload($uid, $_FILES['media']); + if (!$media) { + // Output error + throw new InternalServerErrorException(); + } + + $returndata = []; + + $returndata["media_id"] = $media["id"]; + $returndata["media_id_string"] = (string)$media["id"]; + $returndata["size"] = $media["size"]; + $returndata["image"] = [ + "w" => $media["width"], + "h" => $media["height"], + "image_type" => $media["type"], + "friendica_preview_url" => $media["preview"] + ]; + + Logger::info('Media uploaded', ['return' => $returndata]); + + DI::apiResponse()->exit('media', ['media' => $returndata], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Twitter/Statuses/Destroy.php b/src/Module/Api/Twitter/Statuses/Destroy.php index ae493eb2a1..5a4bc920ed 100644 --- a/src/Module/Api/Twitter/Statuses/Destroy.php +++ b/src/Module/Api/Twitter/Statuses/Destroy.php @@ -40,16 +40,16 @@ class Destroy extends BaseApi $uid = BaseApi::getCurrentUserID(); if (empty($this->parameters['id'])) { - $id = intval($_REQUEST['id'] ?? 0); + $id = intval($request['id'] ?? 0); } else { $id = (int)$this->parameters['id']; } - logger::notice('API: api_statuses_destroy: ' . $id); + $this->logger->notice('API: api_statuses_destroy: ' . $id); - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); + $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); - $ret = DI::twitterStatus()->createFromItemId($$id, $uid, $include_entities)->toArray(); + $ret = DI::twitterStatus()->createFromItemId($id, $uid, $include_entities)->toArray(); Item::deleteForUser(['id' => $id], $uid); diff --git a/src/Object/Api/Twitter/Status.php b/src/Object/Api/Twitter/Status.php index 9d168eba31..ed52deedb8 100644 --- a/src/Object/Api/Twitter/Status.php +++ b/src/Object/Api/Twitter/Status.php @@ -99,7 +99,7 @@ class Status extends BaseDataTransferObject * @param array $item * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function __construct(string $text, array $item, User $author, User $owner, array $retweeted, array $quoted, array $geo, array $friendica_activities, array $entities, array $attachments, int $friendica_comments) + public function __construct(string $text, array $item, User $author, User $owner, array $retweeted, array $quoted, array $geo, array $friendica_activities, array $entities, array $attachments, int $friendica_comments, bool $liked) { $this->id = (int)$item['id']; $this->id_str = (string)$item['id']; @@ -127,7 +127,7 @@ class Status extends BaseDataTransferObject $this->retweeted_status = $retweeted; $this->quoted_status = $quoted; $this->external_url = $item['plink']; - $this->favorited = (bool)$item['starred']; + $this->favorited = $liked; $this->friendica_comments = $friendica_comments; $this->source = $item['app']; $this->geo = $geo; diff --git a/static/routes.config.php b/static/routes.config.php index fec2f8964f..af3e995b85 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -54,21 +54,21 @@ $apiRoutes = [ '/direct_messages' => [ '/all[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/conversation[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]], + '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/new[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/sent[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], ], '/direct_messages[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET, R::POST]], '/externalprofile/show[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Users\Show::class, [R::GET ]], - '/favorites/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/favorites/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]], + '/favorites/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Favorites\Create::class, [ R::POST]], + '/favorites/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Favorites\Destroy::class, [ R::POST]], '/favorites[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Favorites::class, [R::GET ]], '/followers/ids[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Followers\Ids::class, [R::GET ]], '/followers/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Followers\Lists::class, [R::GET ]], '/friends/ids[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friends\Ids::class, [R::GET ]], '/friends/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friends\Lists::class, [R::GET ]], - '/friendships/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/friendships/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friendships\Destroy::class, [ R::POST]], '/friendships/incoming[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friendships\Incoming::class, [R::GET ]], '/friendica' => [ @@ -81,14 +81,14 @@ $apiRoutes = [ '/events[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Events\Index::class, [R::GET ]], '/group_show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/group_create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/group_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Delete::class, [R::DELETE, R::POST]], + '/group_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Delete::class, [ R::POST]], '/group_update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/profile/show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Profile\Show::class, [R::GET ]], - '/photoalbum/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Delete::class, [R::DELETE, R::POST]], + '/photoalbum/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Delete::class, [ R::POST]], '/photoalbum/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Update::class, [ R::POST]], '/photos/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/photo/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/photo/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Delete::class, [R::DELETE, R::POST]], + '/photo/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Delete::class, [ R::POST]], '/photo/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/photo[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], ], @@ -99,7 +99,7 @@ $apiRoutes = [ '/lists' => [ '/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]], + '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/ownerships[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/statuses[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Statuses::class, [R::GET ]], @@ -107,8 +107,8 @@ $apiRoutes = [ '/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], ], - '/media/upload[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/media/metadata/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/media/upload[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Media\Upload::class, [ R::POST]], + '/media/metadata/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Media\Metadata\Create::class, [ R::POST]], '/saved_searches/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\SavedSearches::class, [R::GET ]], '/search/tweets[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Search\Tweets::class, [R::GET ]], '/search[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Search\Tweets::class, [R::GET ]], @@ -118,7 +118,7 @@ $apiRoutes = [ '/statusnet/version[.{extension:json|xml|rss|atom}]' => [Module\Api\GNUSocial\GNUSocial\Version::class, [R::GET ]], '/statuses' => [ - '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]], + '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/followers[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Followers\Lists::class, [R::GET ]], '/friends[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friends\Lists::class, [R::GET ]], '/friends_timeline[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Statuses\HomeTimeline::class, [R::GET ]], diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 541a938c88..df583a2078 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -12,6 +12,7 @@ use Friendica\Core\Protocol; use Friendica\DI; use Friendica\Model\Post; use Friendica\Module\Api\ApiResponse; +use Friendica\Module\Api\Twitter\Media\Upload; use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; use Friendica\Security\BasicAuth; @@ -990,18 +991,18 @@ class ApiTest extends FixtureTest } /** - * Test the api_media_upload() function. + * Test the \Friendica\Module\Api\Twitter\Media\Upload module. * @runInSeparateProcess * @preserveGlobalState disabled */ public function testApiMediaUpload() { $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - api_media_upload(); + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); } /** - * Test the api_media_upload() function without an authenticated user. + * Test the \Friendica\Module\Api\Twitter\Media\Upload module without an authenticated user. * * @return void */ @@ -1010,11 +1011,11 @@ class ApiTest extends FixtureTest $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); BasicAuth::setCurrentUserID(); $_SESSION['authenticated'] = false; - api_media_upload(); + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); } /** - * Test the api_media_upload() function with an invalid uploaded media. + * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an invalid uploaded media. * * @return void */ @@ -1027,11 +1028,11 @@ class ApiTest extends FixtureTest 'tmp_name' => 'tmp_name' ] ]; - api_media_upload(); + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); } /** - * Test the api_media_upload() function with an valid uploaded media. + * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an valid uploaded media. * * @return void */ @@ -1048,14 +1049,14 @@ class ApiTest extends FixtureTest 'type' => 'image/png' ] ]; - $app = DI::app(); - DI::args()->setArgc(2); - $result = api_media_upload(); - self::assertEquals('image/png', $result['media']['image']['image_type']); - self::assertEquals(1, $result['media']['image']['w']); - self::assertEquals(1, $result['media']['image']['h']); - self::assertNotEmpty($result['media']['image']['friendica_preview_url']); + $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); + $media = json_decode($response->getBody(), true); + + self::assertEquals('image/png', $media['image']['image_type']); + self::assertEquals(1, $media['image']['w']); + self::assertEquals(1, $media['image']['h']); + self::assertNotEmpty($media['image']['friendica_preview_url']); } /** From 7c8542f25a28a73c9e5ea7af9af9cfd36f78d079 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Nov 2021 09:45:42 +0000 Subject: [PATCH 3/6] Tests deactivated --- tests/legacy/ApiTest.php | 46 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index df583a2078..a80b587d1e 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1107,9 +1107,9 @@ class ApiTest extends FixtureTest */ public function testApiFavoritesCreateDestroy() { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - DI::args()->setArgv(['api', '1.1', 'favorites', 'create']); - api_favorites_create_destroy('json'); + // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); + // DI::args()->setArgv(['api', '1.1', 'favorites', 'create']); + // api_favorites_create_destroy('json'); } /** @@ -1119,9 +1119,9 @@ class ApiTest extends FixtureTest */ public function testApiFavoritesCreateDestroyWithInvalidId() { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']); - api_favorites_create_destroy('json'); + // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); + // DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']); + // api_favorites_create_destroy('json'); } /** @@ -1131,10 +1131,10 @@ class ApiTest extends FixtureTest */ public function testApiFavoritesCreateDestroyWithInvalidAction() { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']); - $_REQUEST['id'] = 1; - api_favorites_create_destroy('json'); + // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); + // DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']); + // $_REQUEST['id'] = 1; + // api_favorites_create_destroy('json'); } /** @@ -1144,10 +1144,10 @@ class ApiTest extends FixtureTest */ public function testApiFavoritesCreateDestroyWithCreateAction() { - DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']); - $_REQUEST['id'] = 3; - $result = api_favorites_create_destroy('json'); - self::assertStatus($result['status']); + // DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']); + // $_REQUEST['id'] = 3; + // $result = api_favorites_create_destroy('json'); + // self::assertStatus($result['status']); } /** @@ -1157,10 +1157,10 @@ class ApiTest extends FixtureTest */ public function testApiFavoritesCreateDestroyWithCreateActionAndRss() { - DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']); - $_REQUEST['id'] = 3; - $result = api_favorites_create_destroy('rss'); - self::assertXml($result, 'status'); + // DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']); + // $_REQUEST['id'] = 3; + // $result = api_favorites_create_destroy('rss'); + // self::assertXml($result, 'status'); } /** @@ -1170,10 +1170,10 @@ class ApiTest extends FixtureTest */ public function testApiFavoritesCreateDestroyWithDestroyAction() { - DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']); - $_REQUEST['id'] = 3; - $result = api_favorites_create_destroy('json'); - self::assertStatus($result['status']); + // DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']); + // $_REQUEST['id'] = 3; + // $result = api_favorites_create_destroy('json'); + // self::assertStatus($result['status']); } /** @@ -1183,11 +1183,13 @@ class ApiTest extends FixtureTest */ public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser() { + /* $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']); BasicAuth::setCurrentUserID(); $_SESSION['authenticated'] = false; api_favorites_create_destroy('json'); + */ } From e381ca6ba044d911e8ca75be9a9ff56d26740e9e Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Nov 2021 12:55:13 +0000 Subject: [PATCH 4/6] Added group update --- include/api.php | 68 ----------------- src/Module/Api/Friendica/Group/Update.php | 89 +++++++++++++++++++++++ static/routes.config.php | 2 +- 3 files changed, 90 insertions(+), 69 deletions(-) create mode 100644 src/Module/Api/Friendica/Group/Update.php diff --git a/include/api.php b/include/api.php index 06d3607ab1..3373ffe48d 100644 --- a/include/api.php +++ b/include/api.php @@ -1795,74 +1795,6 @@ function api_lists_create($type) api_register_func('api/lists/create', 'api_lists_create', true); -/** - * Update the specified group with the posted array of contacts. - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws BadRequestException - * @throws ForbiddenException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - */ -function api_friendica_group_update($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // params - $gid = $_REQUEST['gid'] ?? 0; - $name = $_REQUEST['name'] ?? ''; - $json = json_decode($_POST['json'], true); - $users = $json['user']; - - // error if no name specified - if ($name == "") { - throw new BadRequestException('group name not specified'); - } - - // error if no gid specified - if ($gid == "") { - throw new BadRequestException('gid not specified'); - } - - // remove members - $members = Contact\Group::getById($gid); - foreach ($members as $member) { - $cid = $member['id']; - foreach ($users as $user) { - $found = ($user['cid'] == $cid ? true : false); - } - if (!isset($found) || !$found) { - $gid = Group::getIdByName($uid, $name); - Group::removeMember($gid, $cid); - } - } - - // add members - $erroraddinguser = false; - $errorusers = []; - foreach ($users as $user) { - $cid = $user['cid']; - - if (DBA::exists('contact', ['id' => $cid, 'uid' => $uid])) { - Group::addMember($gid, $cid); - } else { - $erroraddinguser = true; - $errorusers[] = $cid; - } - } - - // return success message incl. missing users in array - $status = ($erroraddinguser ? "missing user" : "ok"); - $success = ['success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers]; - return DI::apiResponse()->formatData("group_update", $type, ['result' => $success]); -} - -api_register_func('api/friendica/group_update', 'api_friendica_group_update', true); - /** * Update information about a group. * diff --git a/src/Module/Api/Friendica/Group/Update.php b/src/Module/Api/Friendica/Group/Update.php new file mode 100644 index 0000000000..c8d353e2b8 --- /dev/null +++ b/src/Module/Api/Friendica/Group/Update.php @@ -0,0 +1,89 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Group; + +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException\BadRequestException; + +/** + * API endpoint: /api/friendica/group_update + */ +class Update extends BaseApi +{ + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + // params + $gid = $request['gid'] ?? 0; + $name = $request['name'] ?? ''; + $json = json_decode($_POST['json'], true); + $users = $json['user']; + + // error if no name specified + if (!$name) { + throw new BadRequestException('group name not specified'); + } + + // error if no gid specified + if (!$gid) { + throw new BadRequestException('gid not specified'); + } + + // remove members + $members = Contact\Group::getById($gid); + foreach ($members as $member) { + $cid = $member['id']; + foreach ($users as $user) { + $found = $user['cid'] == $cid; + } + if (!isset($found) || !$found) { + $gid = Group::getIdByName($uid, $name); + Group::removeMember($gid, $cid); + } + } + + // add members + $erroraddinguser = false; + $errorusers = []; + foreach ($users as $user) { + $cid = $user['cid']; + + if (DBA::exists('contact', ['id' => $cid, 'uid' => $uid])) { + Group::addMember($gid, $cid); + } else { + $erroraddinguser = true; + $errorusers[] = $cid; + } + } + + // return success message incl. missing users in array + $status = ($erroraddinguser ? 'missing user' : 'ok'); + $success = ['success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers]; + DI::apiResponse()->exit('group_update', ['$result' => $success], $parameters['extension'] ?? null); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index af3e995b85..c0d327b5c8 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -82,7 +82,7 @@ $apiRoutes = [ '/group_show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/group_create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/group_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Delete::class, [ R::POST]], - '/group_update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/group_update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Update::class, [ R::POST]], '/profile/show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Profile\Show::class, [R::GET ]], '/photoalbum/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Delete::class, [ R::POST]], '/photoalbum/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Update::class, [ R::POST]], From 14ec87e68c5a2442c02fced100ac23ebb6000bdd Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Nov 2021 13:41:49 +0000 Subject: [PATCH 5/6] Deprecated upload function relocates to a replacement --- include/api.php | 48 +--------------------------------------- tests/legacy/ApiTest.php | 10 +++++---- 2 files changed, 7 insertions(+), 51 deletions(-) diff --git a/include/api.php b/include/api.php index 3373ffe48d..4f7040ab4b 100644 --- a/include/api.php +++ b/include/api.php @@ -646,53 +646,6 @@ function group_create($name, $uid, $users = []) * TWITTER API */ -/** - * Deprecated function to upload media. - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws BadRequestException - * @throws ForbiddenException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - */ -function api_statuses_mediap($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - $a = DI::app(); - - $_REQUEST['profile_uid'] = $uid; - $_REQUEST['api_source'] = true; - $txt = $_REQUEST['status'] ?? ''; - - if ((strpos($txt, '<') !== false) || (strpos($txt, '>') !== false)) { - $txt = HTML::toBBCodeVideo($txt); - $config = HTMLPurifier_Config::createDefault(); - $config->set('Cache.DefinitionImpl', null); - $purifier = new HTMLPurifier($config); - $txt = $purifier->purify($txt); - } - $txt = HTML::toBBCode($txt); - - $picture = Photo::upload($uid, $_FILES['media']); - - // now that we have the img url in bbcode we can add it to the status and insert the wall item. - $_REQUEST['body'] = $txt . "\n\n" . '[url=' . $picture["albumpage"] . '][img]' . $picture["preview"] . "[/img][/url]"; - $item_id = item_post($a); - - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - - // output the post that we just posted. - $status_info = DI::twitterStatus()->createFromItemId($item_id, $uid, $include_entities)->toArray(); - return DI::apiResponse()->formatData('statuses', $type, ['status' => $status_info]); -} - -api_register_func('api/statuses/mediap', 'api_statuses_mediap', true); - /** * Updates the user’s current status. * @@ -876,6 +829,7 @@ function api_statuses_update($type) api_register_func('api/statuses/update', 'api_statuses_update', true); api_register_func('api/statuses/update_with_media', 'api_statuses_update', true); +api_register_func('api/statuses/mediap', 'api_statuses_update', true); /** * Repeats a status. diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index a80b587d1e..d78a61df9d 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -875,6 +875,7 @@ class ApiTest extends FixtureTest */ public function testApiStatusesMediap() { + /* DI::args()->setArgc(2); $_FILES = [ @@ -892,6 +893,7 @@ class ApiTest extends FixtureTest $result = api_statuses_mediap('json'); self::assertStatus($result['status']); + */ } /** @@ -901,10 +903,10 @@ class ApiTest extends FixtureTest */ public function testApiStatusesMediapWithoutAuthenticatedUser() { - $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); - BasicAuth::setCurrentUserID(); - $_SESSION['authenticated'] = false; - api_statuses_mediap('json'); + // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); + // BasicAuth::setCurrentUserID(); + // $_SESSION['authenticated'] = false; + // api_statuses_mediap('json'); } /** From 313d74598dd7c85887866dfe32f0a075c1b5e08e Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 27 Nov 2021 19:03:01 +0100 Subject: [PATCH 6/6] Fix tests --- tests/src/Module/Api/Friendica/NotificationTest.php | 2 -- tests/src/Module/Api/Friendica/Photo/DeleteTest.php | 5 +++-- tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php | 5 +++-- tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php | 7 ++++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/src/Module/Api/Friendica/NotificationTest.php b/tests/src/Module/Api/Friendica/NotificationTest.php index ef93de3ef1..b5e2a41665 100644 --- a/tests/src/Module/Api/Friendica/NotificationTest.php +++ b/tests/src/Module/Api/Friendica/NotificationTest.php @@ -69,8 +69,6 @@ XML; $notification = new Notification(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'xml']); $response = $notification->run(); - print_r($response->getHeaders()); - self::assertXmlStringEqualsXmlString($assertXml, (string)$response->getBody()); self::assertEquals(['Content-type' => ['text/xml'], ICanCreateResponses::X_HEADER => ['xml']], $response->getHeaders()); } diff --git a/tests/src/Module/Api/Friendica/Photo/DeleteTest.php b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php index 42e3f77c04..108802c93a 100644 --- a/tests/src/Module/Api/Friendica/Photo/DeleteTest.php +++ b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php @@ -21,6 +21,7 @@ namespace Friendica\Test\src\Module\Api\Friendica\Photo; +use Friendica\App\Router; use Friendica\DI; use Friendica\Module\Api\Friendica\Photo\Delete; use Friendica\Network\HTTPException\BadRequestException; @@ -31,7 +32,7 @@ class DeleteTest extends ApiTest public function testEmpty() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(); + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(); } public function testWithoutAuthenticatedUser() @@ -42,7 +43,7 @@ class DeleteTest extends ApiTest public function testWrong() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(['photo_id' => 1]); + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(['photo_id' => 1]); } public function testWithCorrectPhotoId() diff --git a/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php b/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php index 118257c55b..5aa9b73ff5 100644 --- a/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php +++ b/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php @@ -21,6 +21,7 @@ namespace Friendica\Test\src\Module\Api\Friendica\Photoalbum; +use Friendica\App\Router; use Friendica\DI; use Friendica\Module\Api\Friendica\Photoalbum\Delete; use Friendica\Network\HTTPException\BadRequestException; @@ -31,14 +32,14 @@ class DeleteTest extends ApiTest public function testEmpty() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(); + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(); } public function testWrong() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(['album' => 'album_name']); + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(['album' => 'album_name']); } public function testValid() diff --git a/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php b/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php index 464845745f..7a90012bb9 100644 --- a/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php +++ b/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php @@ -21,6 +21,7 @@ namespace Friendica\Test\src\Module\Api\Friendica\Photoalbum; +use Friendica\App\Router; use Friendica\DI; use Friendica\Module\Api\Friendica\Photoalbum\Update; use Friendica\Network\HTTPException\BadRequestException; @@ -31,19 +32,19 @@ class UpdateTest extends ApiTest public function testEmpty() { $this->expectException(BadRequestException::class); - (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(); + (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(); } public function testTooFewArgs() { $this->expectException(BadRequestException::class); - (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(['album' => 'album_name']); + (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(['album' => 'album_name']); } public function testWrongUpdate() { $this->expectException(BadRequestException::class); - (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(['album' => 'album_name', 'album_new' => 'album_name']); + (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(['album' => 'album_name', 'album_new' => 'album_name']); } public function testWithoutAuthenticatedUser()