From 5769d99478ca140e933263990c00cbf77d7f3f38 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 22 Sep 2021 23:01:09 -0400 Subject: [PATCH 1/4] [twitter] Add unfollow hook function --- twitter/twitter.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/twitter/twitter.php b/twitter/twitter.php index 45f8fe13..e0fa6f3a 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -105,6 +105,7 @@ function twitter_install() Hook::register('jot_networks' , __FILE__, 'twitter_jot_nets'); Hook::register('cron' , __FILE__, 'twitter_cron'); Hook::register('follow' , __FILE__, 'twitter_follow'); + Hook::register('unfollow' , __FILE__, 'twitter_unfollow'); Hook::register('expire' , __FILE__, 'twitter_expire'); Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body'); Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification'); @@ -173,6 +174,39 @@ function twitter_follow(App $a, array &$contact) } } +function twitter_unfollow(App $a, array &$hook_data) +{ + $contact = $hook_data['contact']; + Logger::info('Check if contact is twitter contact', ['url' => $contact["url"]]); + + if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) { + return; + } + + // contact seems to be a twitter contact, so continue + $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]); + $nickname = str_replace("@twitter.com", "", $nickname); + + $uid = $a->getLoggedInUserId(); + + $ckey = DI::config()->get('twitter', 'consumerkey'); + $csecret = DI::config()->get('twitter', 'consumersecret'); + $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken'); + $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret'); + + // If the addon is not configured (general or for this user) quit here + if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) { + return; + } + + try { + $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); + $connection->post('friendships/destroy', ['screen_name' => $nickname]); + } catch(Exception $e) { + Logger::notice('[twitter] API call "friendships/destroy" failed', ['uid' => $uid, 'url' => $contact['url'], 'exception' => $e]); + } +} + function twitter_jot_nets(App $a, array &$jotnets_fields) { if (!local_user()) { From b732cf89689712db2992d9e61cf8fed508bff138 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 26 Sep 2021 07:36:49 -0400 Subject: [PATCH 2/4] [twitter] Use existing contact fields instead of guessing in twitter_unfollow - Add `friendship/destroy` API call result logging --- twitter/twitter.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index e0fa6f3a..948de3ba 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -177,16 +177,10 @@ function twitter_follow(App $a, array &$contact) function twitter_unfollow(App $a, array &$hook_data) { $contact = $hook_data['contact']; - Logger::info('Check if contact is twitter contact', ['url' => $contact["url"]]); - - if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) { + if ($contact['netword'] !== Protocol::TWITTER) { return; } - // contact seems to be a twitter contact, so continue - $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]); - $nickname = str_replace("@twitter.com", "", $nickname); - $uid = $a->getLoggedInUserId(); $ckey = DI::config()->get('twitter', 'consumerkey'); @@ -201,7 +195,8 @@ function twitter_unfollow(App $a, array &$hook_data) try { $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); - $connection->post('friendships/destroy', ['screen_name' => $nickname]); + $result = $connection->post('friendships/destroy', ['screen_name' => $contact['nick']]); + Logger::info('[twitter] API call "friendship/destroy" successful', ['result' => $result]); } catch(Exception $e) { Logger::notice('[twitter] API call "friendships/destroy" failed', ['uid' => $uid, 'url' => $contact['url'], 'exception' => $e]); } From 250dafd2b67f9b8b59bcdfc6058ff68bdd2c330f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 26 Sep 2021 10:34:31 -0400 Subject: [PATCH 3/4] [twitter] Add data to the unfollow hook result key --- twitter/twitter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/twitter/twitter.php b/twitter/twitter.php index 948de3ba..f8da6cc4 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -197,8 +197,10 @@ function twitter_unfollow(App $a, array &$hook_data) $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); $result = $connection->post('friendships/destroy', ['screen_name' => $contact['nick']]); Logger::info('[twitter] API call "friendship/destroy" successful', ['result' => $result]); + $hook_data['result'] = true; } catch(Exception $e) { Logger::notice('[twitter] API call "friendships/destroy" failed', ['uid' => $uid, 'url' => $contact['url'], 'exception' => $e]); + $hook_data['result'] = false; } } From 31dd9f8e22dec96813e9d62f4e0525e40079948b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 2 Oct 2021 11:27:57 -0400 Subject: [PATCH 4/4] [twitter] Fix typo, improve formatting in twitter_unfollow() --- twitter/twitter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index f8da6cc4..02470f98 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -177,15 +177,15 @@ function twitter_follow(App $a, array &$contact) function twitter_unfollow(App $a, array &$hook_data) { $contact = $hook_data['contact']; - if ($contact['netword'] !== Protocol::TWITTER) { + if ($contact['network'] !== Protocol::TWITTER) { return; } $uid = $a->getLoggedInUserId(); - $ckey = DI::config()->get('twitter', 'consumerkey'); + $ckey = DI::config()->get('twitter', 'consumerkey'); $csecret = DI::config()->get('twitter', 'consumersecret'); - $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken'); + $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken'); $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret'); // If the addon is not configured (general or for this user) quit here @@ -195,10 +195,10 @@ function twitter_unfollow(App $a, array &$hook_data) try { $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); - $result = $connection->post('friendships/destroy', ['screen_name' => $contact['nick']]); + $result = $connection->post('friendships/destroy', ['screen_name' => $contact['nick']]); Logger::info('[twitter] API call "friendship/destroy" successful', ['result' => $result]); $hook_data['result'] = true; - } catch(Exception $e) { + } catch (Exception $e) { Logger::notice('[twitter] API call "friendships/destroy" failed', ['uid' => $uid, 'url' => $contact['url'], 'exception' => $e]); $hook_data['result'] = false; }