From 3f85fee7e37701a1e0eaba75ac180b8c653a81b6 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 13 Sep 2018 23:23:53 +0200 Subject: [PATCH 1/7] Add api_friendships_destroy() like mod/unfollow.php --- include/api.php | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/include/api.php b/include/api.php index 5510eddb4..d543c03db 100644 --- a/include/api.php +++ b/include/api.php @@ -3629,6 +3629,102 @@ function api_direct_messages_destroy($type) /// @TODO move to top of file or somewhere better api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE); +function api_friendships_destroy($type) +{ + $a = get_app(); + + logger("OrigUser: ".$a->user['uid'], LOGGER_DEBUG); + logger("ContactUser: ".$_REQUEST['user_id'], LOGGER_DEBUG); + if (api_user() === false) { + throw new ForbiddenException(); + } + $uid = local_user(); + + $contact_id = (x($_REQUEST, 'user_id') ? $_REQUEST['user_id'] : null); + + if ($contact_id == null) { + logger("No POST user_id", LOGGER_DEBUG); + throw new BadRequestException("no user_id specified"); + } + + $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0", intval($contact_id), 0); + + if(!DBA::isResult($contact)) { + logger("No contact by _id", LOGGER_DEBUG); + throw new BadRequestException("no contact found to given ID"); + } + + $url = $contact[0]["url"]; + logger("Contact Url: ".$contact[0]["url"], LOGGER_DEBUG); + + $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)", + $uid, Contact::SHARING, Contact::FRIEND, normalise_link($url), + normalise_link($url), $url]; + $contact = DBA::selectFirst('contact', [], $condition); + + if (!DBA::isResult($contact)) { + logger("No contact founded", LOGGER_DEBUG); + throw new BadRequestException("Not following Contact"); + } + + if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) { + logger("Not supported", LOGGER_DEBUG); + throw new BadRequestException("Not supported"); + } + + $dissolve = ($contact['rel'] == Contact::SHARING); + + $owner = User::getOwnerDataById($uid); + if ($owner) { + Contact::terminateFriendship($owner, $contact, $dissolve); + } + + // Sharing-only contacts get deleted as there no relationship any more + if ($dissolve) { + Contact::remove($contact['id']); + $return_path = 'contacts'; + } else { + DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]); + } + + /////////////////// + /* + $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0", intval($contact_id), 0); + + if(!DBA::isResult($contact)) { + throw new BadRequestException("no contact found to given ID"); + } + + logger("Contact Url: ".$contact[0]["url"], LOGGER_DEBUG); + + $contact_uid = Contact::getIdForUrl($contact[0]["url"], $a->user["uid"]); + if ($contact_uid == 0) { + logger("No UserURL founded", LOGGER_DEBUG); + throw new BadRequestException("No contact id found"); + } + logger("User found: ".$contact_uid, LOGGER_DEBUG); + + $contact_user = q("SELECT * FROM `user` WHERE uid = $contact_uid"); + if(!DBA::isResult($contact_user)) { + logger("No Contact to ContactId founded", LOGGER_DEBUG); + throw new BadRequestException("No Profile found"); + } + logger("Contact founded!", LOGGER_DEBUG); + + logger("Founded User: ".$contact_user[0][nick]." + ".$contact[0]["id"]); + + Contact::terminateFriendship($a->user, $contact_user[0]); + Contact::remove($contact_user[0]['uid']); + */ + + $answer = ['result' => 'ok', 'contact' => 'contact deleted']; + return api_format_data("friendships-destroy", $type, ['result' => $answer]); +} +api_register_func('api/friendships/destroy', 'api_friendships_destroy', true, API_METHOD_POST); + + + + /** * * @param string $type Return type (atom, rss, xml, json) From a89f9cf7d5df076dcd7db74be9650299c6bb8c03 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 13 Sep 2018 23:32:26 +0200 Subject: [PATCH 2/7] api_friendships_destroy cleanup --- include/api.php | 39 ++++----------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/include/api.php b/include/api.php index d543c03db..c8d4e71de 100644 --- a/include/api.php +++ b/include/api.php @@ -3633,8 +3633,6 @@ function api_friendships_destroy($type) { $a = get_app(); - logger("OrigUser: ".$a->user['uid'], LOGGER_DEBUG); - logger("ContactUser: ".$_REQUEST['user_id'], LOGGER_DEBUG); if (api_user() === false) { throw new ForbiddenException(); } @@ -3643,19 +3641,19 @@ function api_friendships_destroy($type) $contact_id = (x($_REQUEST, 'user_id') ? $_REQUEST['user_id'] : null); if ($contact_id == null) { - logger("No POST user_id", LOGGER_DEBUG); + logger("No given user_id", LOGGER_DEBUG); throw new BadRequestException("no user_id specified"); } + // Get Contact by given id $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0", intval($contact_id), 0); if(!DBA::isResult($contact)) { - logger("No contact by _id", LOGGER_DEBUG); + logger("No contact by id founded", LOGGER_DEBUG); throw new BadRequestException("no contact found to given ID"); } $url = $contact[0]["url"]; - logger("Contact Url: ".$contact[0]["url"], LOGGER_DEBUG); $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)", $uid, Contact::SHARING, Contact::FRIEND, normalise_link($url), @@ -3663,7 +3661,7 @@ function api_friendships_destroy($type) $contact = DBA::selectFirst('contact', [], $condition); if (!DBA::isResult($contact)) { - logger("No contact founded", LOGGER_DEBUG); + logger("Not following Contact", LOGGER_DEBUG); throw new BadRequestException("Not following Contact"); } @@ -3687,35 +3685,6 @@ function api_friendships_destroy($type) DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]); } - /////////////////// - /* - $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0", intval($contact_id), 0); - - if(!DBA::isResult($contact)) { - throw new BadRequestException("no contact found to given ID"); - } - - logger("Contact Url: ".$contact[0]["url"], LOGGER_DEBUG); - - $contact_uid = Contact::getIdForUrl($contact[0]["url"], $a->user["uid"]); - if ($contact_uid == 0) { - logger("No UserURL founded", LOGGER_DEBUG); - throw new BadRequestException("No contact id found"); - } - logger("User found: ".$contact_uid, LOGGER_DEBUG); - - $contact_user = q("SELECT * FROM `user` WHERE uid = $contact_uid"); - if(!DBA::isResult($contact_user)) { - logger("No Contact to ContactId founded", LOGGER_DEBUG); - throw new BadRequestException("No Profile found"); - } - logger("Contact founded!", LOGGER_DEBUG); - - logger("Founded User: ".$contact_user[0][nick]." + ".$contact[0]["id"]); - - Contact::terminateFriendship($a->user, $contact_user[0]); - Contact::remove($contact_user[0]['uid']); - */ $answer = ['result' => 'ok', 'contact' => 'contact deleted']; return api_format_data("friendships-destroy", $type, ['result' => $answer]); From 3eb539aefde3f0ef3d0d695c3b75deb4919188ec Mon Sep 17 00:00:00 2001 From: Jonny Tischbein Date: Fri, 14 Sep 2018 09:28:14 +0200 Subject: [PATCH 3/7] api_friendships_destroy idents, DBA::selectFirst, Excepions and LogMessages --- include/api.php | 58 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/include/api.php b/include/api.php index c8d4e71de..faa465b18 100644 --- a/include/api.php +++ b/include/api.php @@ -3631,29 +3631,30 @@ api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', function api_friendships_destroy($type) { - $a = get_app(); + $a = api_user(); - if (api_user() === false) { - throw new ForbiddenException(); - } - $uid = local_user(); - - $contact_id = (x($_REQUEST, 'user_id') ? $_REQUEST['user_id'] : null); - - if ($contact_id == null) { - logger("No given user_id", LOGGER_DEBUG); - throw new BadRequestException("no user_id specified"); - } - - // Get Contact by given id - $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0", intval($contact_id), 0); - - if(!DBA::isResult($contact)) { - logger("No contact by id founded", LOGGER_DEBUG); - throw new BadRequestException("no contact found to given ID"); + if ($a === false) { + throw new ForbiddenException(); } - $url = $contact[0]["url"]; + $uid = local_user(); + + $contact_id = defaults($_REQUEST, 'user_id'); + + if ($contact_id == null) { + logger("No user_id specified", LOGGER_DEBUG); + throw new 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("No contact found for ID" . $contact_id, LOGGER_DEBUG); + throw new NoFoundException("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, normalise_link($url), @@ -3662,12 +3663,12 @@ function api_friendships_destroy($type) if (!DBA::isResult($contact)) { logger("Not following Contact", LOGGER_DEBUG); - throw new BadRequestException("Not following Contact"); + throw new NoFoundException("Not following Contact"); } if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) { logger("Not supported", LOGGER_DEBUG); - throw new BadRequestException("Not supported"); + throw new ExpectationFailedException("Not supported"); } $dissolve = ($contact['rel'] == Contact::SHARING); @@ -3676,24 +3677,23 @@ function api_friendships_destroy($type) if ($owner) { Contact::terminateFriendship($owner, $contact, $dissolve); } + else { + logger("No owner found", LOGGER_DEBUG); + throw new Exception("Error Processing Request"); + } // Sharing-only contacts get deleted as there no relationship any more if ($dissolve) { Contact::remove($contact['id']); - $return_path = 'contacts'; } else { DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]); } - - $answer = ['result' => 'ok', 'contact' => 'contact deleted']; - return api_format_data("friendships-destroy", $type, ['result' => $answer]); + $answer = ['result' => 'ok', 'user_id' => $contact_id, 'contact' => 'contact deleted']; + return api_format_data("friendships-destroy", $type, ['result' => $answer]); } api_register_func('api/friendships/destroy', 'api_friendships_destroy', true, API_METHOD_POST); - - - /** * * @param string $type Return type (atom, rss, xml, json) From 2c3a58d44e858381f8324643d7727b71a8e5c2dc Mon Sep 17 00:00:00 2001 From: Jonny Tischbein Date: Fri, 14 Sep 2018 10:06:26 +0200 Subject: [PATCH 4/7] api_friendhips_destroy function header --- include/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/api.php b/include/api.php index faa465b18..db42d63c5 100644 --- a/include/api.php +++ b/include/api.php @@ -3679,7 +3679,7 @@ function api_friendships_destroy($type) } else { logger("No owner found", LOGGER_DEBUG); - throw new Exception("Error Processing Request"); + throw new NoFoundException("Error Processing Request"); } // Sharing-only contacts get deleted as there no relationship any more From b7e15e8b3856e8ffde5aae032da3e7af6e17a7a0 Mon Sep 17 00:00:00 2001 From: Jonny Tischbein Date: Fri, 14 Sep 2018 19:35:24 +0200 Subject: [PATCH 5/7] api_friendships_destroy finally function header + NotFoundException typo --- include/api.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/api.php b/include/api.php index db42d63c5..49aa4489b 100644 --- a/include/api.php +++ b/include/api.php @@ -3629,6 +3629,15 @@ function api_direct_messages_destroy($type) /// @TODO move to top of file or somewhere better api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE); +/** + * Unfollow Contact + * + * @brief unfollow contact + * + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string|array + * @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) { $a = api_user(); @@ -3651,7 +3660,7 @@ function api_friendships_destroy($type) if(!DBA::isResult($contact)) { logger("No contact found for ID" . $contact_id, LOGGER_DEBUG); - throw new NoFoundException("no contact found to given ID"); + throw new NotFoundException("no contact found to given ID"); } $url = $contact["url"]; @@ -3663,7 +3672,7 @@ function api_friendships_destroy($type) if (!DBA::isResult($contact)) { logger("Not following Contact", LOGGER_DEBUG); - throw new NoFoundException("Not following Contact"); + throw new NotFoundException("Not following Contact"); } if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) { @@ -3679,7 +3688,7 @@ function api_friendships_destroy($type) } else { logger("No owner found", LOGGER_DEBUG); - throw new NoFoundException("Error Processing Request"); + throw new NotFoundException("Error Processing Request"); } // Sharing-only contacts get deleted as there no relationship any more From a0942963c9f3881c61c05b4c6b9943f6ebf4d686 Mon Sep 17 00:00:00 2001 From: Jonny Tischbein Date: Sat, 15 Sep 2018 11:06:55 +0200 Subject: [PATCH 6/7] api_friendships_detroy uid - api_user instead of local_uer, empty check for --- include/api.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/include/api.php b/include/api.php index 49aa4489b..242f4bc3b 100644 --- a/include/api.php +++ b/include/api.php @@ -3640,17 +3640,15 @@ api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', */ function api_friendships_destroy($type) { - $a = api_user(); + $uid = api_user(); - if ($a === false) { + if ($uid === false) { throw new ForbiddenException(); } - $uid = local_user(); - $contact_id = defaults($_REQUEST, 'user_id'); - if ($contact_id == null) { + if (empty($contact_id)) { logger("No user_id specified", LOGGER_DEBUG); throw new BadRequestException("no user_id specified"); } From 9821f173a42e676507fbead7418c885c7118de04 Mon Sep 17 00:00:00 2001 From: Jonny Tischbein Date: Sun, 16 Sep 2018 19:36:25 +0200 Subject: [PATCH 7/7] fix response --- include/api.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/api.php b/include/api.php index 242f4bc3b..8588b2cf8 100644 --- a/include/api.php +++ b/include/api.php @@ -3696,8 +3696,14 @@ function api_friendships_destroy($type) DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]); } - $answer = ['result' => 'ok', 'user_id' => $contact_id, 'contact' => 'contact deleted']; - return api_format_data("friendships-destroy", $type, ['result' => $answer]); + // "uid" and "self" are only needed for some internal stuff, so remove it from here + unset($contact["uid"]); + unset($contact["self"]); + + // Set screen_name since Twidere requests it + $contact["screen_name"] = $contact["nick"]; + + return api_format_data("friendships-destroy", $type, ['user' => $contact]); } api_register_func('api/friendships/destroy', 'api_friendships_destroy', true, API_METHOD_POST);