}
}
-function twitter_action(App $a, $uid, $pid, $action)
+function twitter_api_post(string $apiPath, string $pid, int $uid)
{
if (empty($pid)) {
return;
$post = ['id' => $pid];
- Logger::debug('before action', ['action' => $action, 'pid' => $pid, 'data' => $post]);
- $result = [];
+ Logger::debug('before action', ['action' => $apiPath, 'pid' => $pid, 'data' => $post]);
try {
- switch ($action) {
- case 'delete':
- // To-Do: $result = $connection->post('statuses/destroy', $post);
- 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]);
+ $result = $connection->post($apiPath, $post);
+ if ($connection->getLastHttpCode() != 200) {
+ Logger::warning('[twitter] API call unsuccessful', ['apiPath' => $apiPath, 'post' => $post, 'result' => $result]);
}
} 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)
}
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) {
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");
- } else {
- twitter_action($a, $b["uid"], twitter_get_id($b["thr-parent"]), "like");
- }
+
+ twitter_api_post($b['deleted'] ? 'favorite/destroy' : 'favorite/create', twitter_get_id($b["thr-parent"]), $b["uid"]);
return;
}
if ($b['verb'] == Activity::ANNOUNCE) {
Logger::info('Retweet', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]);
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 {
twitter_retweet($b["uid"], twitter_get_id($b["thr-parent"]));
}
$post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
}
- $url = 'statuses/update';
- $result = $connection->post($url, $post);
+ $result = $connection->post('statuses/update', $post);
Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
if (!empty($result->source)) {
{
Logger::info('Retweeting', ['user' => $uid, 'id' => $id]);
- $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');
-
- $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
- $result = $connection->post('statuses/retweet/' . $id);
+ $result = twitter_api_post('statuses/retweet', $id, $uid);
Logger::info('Retweeted', ['user' => $uid, 'id' => $id, 'result' => $result]);