Merge pull request #1178 from MrPetovan/task/10739-twitter-block

[twitter] Add support for block and unblock hooks
This commit is contained in:
Michael Vogel 2021-10-02 23:40:14 +02:00 committed by GitHub
commit 020a1245fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -107,6 +107,8 @@ function twitter_install()
Hook::register('support_follow' , __FILE__, 'twitter_support_follow'); Hook::register('support_follow' , __FILE__, 'twitter_support_follow');
Hook::register('follow' , __FILE__, 'twitter_follow'); Hook::register('follow' , __FILE__, 'twitter_follow');
Hook::register('unfollow' , __FILE__, 'twitter_unfollow'); Hook::register('unfollow' , __FILE__, 'twitter_unfollow');
Hook::register('block' , __FILE__, 'twitter_block');
Hook::register('unblock' , __FILE__, 'twitter_unblock');
Hook::register('expire' , __FILE__, 'twitter_expire'); Hook::register('expire' , __FILE__, 'twitter_expire');
Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body'); Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification'); Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
@ -157,19 +159,7 @@ function twitter_follow(App $a, array &$contact)
$uid = $a->getLoggedInUserId(); $uid = $a->getLoggedInUserId();
$ckey = DI::config()->get('twitter', 'consumerkey'); twitter_api_contact('friendships/create', ['network' => Protocol::TWITTER, 'nick' => $nickname], $uid);
$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)) {
$contact = false;
return;
}
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
$connection->post('friendships/create', ['screen_name' => $nickname]);
$user = twitter_fetchuser($nickname); $user = twitter_fetchuser($nickname);
@ -184,12 +174,24 @@ function twitter_follow(App $a, array &$contact)
function twitter_unfollow(App $a, array &$hook_data) function twitter_unfollow(App $a, array &$hook_data)
{ {
$contact = $hook_data['contact']; $hook_data['result'] = twitter_api_contact('friendship/destroy', $hook_data['contact'], $hook_data['uid']);
if ($contact['network'] !== Protocol::TWITTER) { }
return;
}
$uid = $a->getLoggedInUserId(); function twitter_block(App $a, array &$hook_data)
{
$hook_data['result'] = twitter_api_contact('blocks/create', $hook_data['contact'], $hook_data['uid']);
}
function twitter_unblock(App $a, array &$hook_data)
{
$hook_data['result'] = twitter_api_contact('blocks/destroy', $hook_data['contact'], $hook_data['uid']);
}
function twitter_api_contact(string $apiPath, array $contact, int $uid): ?bool
{
if ($contact['network'] !== Protocol::TWITTER) {
return null;
}
$ckey = DI::config()->get('twitter', 'consumerkey'); $ckey = DI::config()->get('twitter', 'consumerkey');
$csecret = DI::config()->get('twitter', 'consumersecret'); $csecret = DI::config()->get('twitter', 'consumersecret');
@ -198,17 +200,17 @@ function twitter_unfollow(App $a, array &$hook_data)
// If the addon is not configured (general or for this user) quit here // If the addon is not configured (general or for this user) quit here
if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) { if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
return; return null;
} }
try { try {
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
$result = $connection->post('friendships/destroy', ['screen_name' => $contact['nick']]); $result = $connection->post($apiPath, ['screen_name' => $contact['nick']]);
Logger::info('[twitter] API call "friendship/destroy" successful', ['result' => $result]); Logger::info('[twitter] API call successful', ['apiPath' => $apiPath, 'result' => $result]);
$hook_data['result'] = true; return true;
} catch (Exception $e) { } catch (Exception $e) {
Logger::notice('[twitter] API call "friendships/destroy" failed', ['uid' => $uid, 'url' => $contact['url'], 'exception' => $e]); Logger::notice('[twitter] API call failed', ['apiPath' => $apiPath, 'uid' => $uid, 'url' => $contact['url'], 'exception' => $e]);
$hook_data['result'] = false; return false;
} }
} }
@ -520,7 +522,7 @@ function twitter_probe_detect(App $a, array &$hookData)
} }
} }
function twitter_action(App $a, $uid, $pid, $action) function twitter_api_post(string $apiPath, string $pid, int $uid)
{ {
if (empty($pid)) { if (empty($pid)) {
return; return;
@ -535,34 +537,21 @@ function twitter_action(App $a, $uid, $pid, $action)
$post = ['id' => $pid]; $post = ['id' => $pid];
Logger::debug('before action', ['action' => $action, 'pid' => $pid, 'data' => $post]); Logger::debug('before action', ['action' => $apiPath, 'pid' => $pid, 'data' => $post]);
$result = [];
try { try {
switch ($action) { $result = $connection->post($apiPath, $post);
case 'delete': if ($connection->getLastHttpCode() != 200) {
// To-Do: $result = $connection->post('statuses/destroy', $post); Logger::warning('[twitter] API call unsuccessful', ['apiPath' => $apiPath, 'post' => $post, 'result' => $result]);
break;
case 'like':
$result = $connection->post('favorites/create', $post);
if ($connection->getLastHttpCode() != 200) {
Logger::warning('Unable to create favorite', ['result' => $result]);
}
break;
case 'unlike':
$result = $connection->post('favorites/destroy', $post);
if ($connection->getLastHttpCode() != 200) {
Logger::warning('Unable to destroy favorite', ['result' => $result]);
}
break;
default:
Logger::warning('Unhandled action', ['action' => $action]);
} }
} catch (TwitterOAuthException $twitterOAuthException) { } catch (TwitterOAuthException $twitterOAuthException) {
Logger::warning('Unable to communicate with twitter', ['action' => $action, 'data' => $post, 'code' => $twitterOAuthException->getCode(), 'exception' => $twitterOAuthException]); Logger::warning('Unable to communicate with twitter', ['apiPath' => $apiPath, 'data' => $post, 'code' => $twitterOAuthException->getCode(), 'exception' => $twitterOAuthException]);
$result = false;
} }
Logger::info('after action', ['action' => $action, 'result' => $result]); Logger::info('after action', ['action' => $apiPath, 'result' => $result]);
return $result;
} }
function twitter_get_id(string $uri) function twitter_get_id(string $uri)
@ -634,16 +623,13 @@ function twitter_post_hook(App $a, array &$b)
} }
if (($b['verb'] == Activity::POST) && $b['deleted']) { if (($b['verb'] == Activity::POST) && $b['deleted']) {
twitter_action($a, $b['uid'], twitter_get_id($thr_parent['uri']), 'delete'); twitter_api_post('statuses/destroy', twitter_get_id($thr_parent['uri']), $b['uid']);
} }
if ($b['verb'] == Activity::LIKE) { if ($b['verb'] == Activity::LIKE) {
Logger::info('Like', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]); Logger::info('Like', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]);
if ($b['deleted']) {
twitter_action($a, $b["uid"], twitter_get_id($b["thr-parent"]), "unlike"); twitter_api_post($b['deleted'] ? 'favorite/destroy' : 'favorite/create', twitter_get_id($b["thr-parent"]), $b["uid"]);
} else {
twitter_action($a, $b["uid"], twitter_get_id($b["thr-parent"]), "like");
}
return; return;
} }
@ -651,7 +637,7 @@ function twitter_post_hook(App $a, array &$b)
if ($b['verb'] == Activity::ANNOUNCE) { if ($b['verb'] == Activity::ANNOUNCE) {
Logger::info('Retweet', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]); Logger::info('Retweet', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]);
if ($b['deleted']) { if ($b['deleted']) {
twitter_action($a, $b['uid'], twitter_get_id($thr_parent['extid']), 'delete'); twitter_api_post('statuses/destroy', twitter_get_id($thr_parent['extid']), $b['uid']);
} else { } else {
twitter_retweet($b["uid"], twitter_get_id($b["thr-parent"])); twitter_retweet($b["uid"], twitter_get_id($b["thr-parent"]));
} }
@ -779,8 +765,7 @@ function twitter_post_hook(App $a, array &$b)
$post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']); $post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
} }
$url = 'statuses/update'; $result = $connection->post('statuses/update', $post);
$result = $connection->post($url, $post);
Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]); Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
if (!empty($result->source)) { if (!empty($result->source)) {
@ -2155,13 +2140,7 @@ function twitter_retweet(int $uid, int $id, int $item_id = 0)
{ {
Logger::info('Retweeting', ['user' => $uid, 'id' => $id]); Logger::info('Retweeting', ['user' => $uid, 'id' => $id]);
$ckey = DI::config()->get('twitter', 'consumerkey'); $result = twitter_api_post('statuses/retweet', $id, $uid);
$csecret = DI::config()->get('twitter', 'consumersecret');
$otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
$osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
$result = $connection->post('statuses/retweet/' . $id);
Logger::info('Retweeted', ['user' => $uid, 'id' => $id, 'result' => $result]); Logger::info('Retweeted', ['user' => $uid, 'id' => $id, 'result' => $result]);