From 9d2c8902d37e5f47add0051df3e527cabef7829f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 21 Jun 2020 16:14:08 -0400 Subject: [PATCH 1/3] [twitter] Improve logging for actions --- twitter/twitter.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index 5167e022..d4702379 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -491,24 +491,25 @@ function twitter_action(App $a, $uid, $pid, $action) $post = ['id' => $pid]; - Logger::log("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), Logger::DATA); + Logger::debug('before action', ['action' => $action, 'pid' => $pid, 'data' => $post]); switch ($action) { - case "delete": + case 'delete': // To-Do: $result = $connection->post('statuses/destroy', $post); $result = []; break; - case "like": + case 'like': $result = $connection->post('favorites/create', $post); break; - case "unlike": + case 'unlike': $result = $connection->post('favorites/destroy', $post); break; default: - Logger::log('Unhandled action ' . $action, Logger::DEBUG); + Logger::warning('Unhandled action', ['action' => $action]); $result = []; } - Logger::log("twitter_action '" . $action . "' send, result: " . print_r($result, true), Logger::DEBUG); + + Logger::info('after action', ['action' => $action, 'result' => $result]); } function twitter_post_hook(App $a, array &$b) From b07e4bd59c57e0796d7ac433c7584bceb7f46bd0 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 21 Jun 2020 16:32:37 -0400 Subject: [PATCH 2/3] [twitter] Add HTTP error code handling --- twitter/twitter.php | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index d4702379..ff773c0a 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -500,9 +500,15 @@ function twitter_action(App $a, $uid, $pid, $action) break; case 'like': $result = $connection->post('favorites/create', $post); + if ($connection->getLastHttpCode() != 200) { + Logger::error('Unable to create favorite', ['result' => $result]); + } break; case 'unlike': $result = $connection->post('favorites/destroy', $post); + if ($connection->getLastHttpCode() != 200) { + Logger::error('Unable to destroy favorite', ['result' => $result]); + } break; default: Logger::warning('Unhandled action', ['action' => $action]); @@ -1048,26 +1054,28 @@ function twitter_get_relation($uid, $target, $contact = []) try { $status = $connection->get('friendships/show', $parameters); - } catch (TwitterOAuthException $e) { - Logger::info('Error fetching friendship status', ['user' => $uid, 'target' => $target, 'message' => $e->getMessage()]); - return $relation; + if ($connection->getLastHttpCode() !== 200) { + throw new Exception($status->errors[0]->message ?? 'HTTP response code ' . $connection->getLastHttpCode(), $status->errors[0]->code ?? $connection->getLastHttpCode()); + } + + $following = $status->relationship->source->following; + $followed = $status->relationship->source->followed_by; + + if ($following && !$followed) { + $relation = Contact::SHARING; + } elseif (!$following && $followed) { + $relation = Contact::FOLLOWER; + } elseif ($following && $followed) { + $relation = Contact::FRIEND; + } elseif (!$following && !$followed) { + $relation = 0; + } + + Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]); + } catch (Throwable $e) { + Logger::error('Error fetching friendship status', ['user' => $uid, 'target' => $target, 'message' => $e->getMessage()]); } - $following = $status->relationship->source->following; - $followed = $status->relationship->source->followed_by; - - if ($following && !$followed) { - $relation = Contact::SHARING; - } elseif (!$following && $followed) { - $relation = Contact::FOLLOWER; - } elseif ($following && $followed) { - $relation = Contact::FRIEND; - } elseif (!$following && !$followed) { - $relation = 0; - } - - Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]); - return $relation; } From ef21c830370f89f4f51b3bdfb6c5f6071eefde9b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 21 Jun 2020 16:33:18 -0400 Subject: [PATCH 3/3] [twitter] Fix return value of twitter_user_to_contact() - Simplify check in twitter_fetch_own_contact() --- twitter/twitter.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index ff773c0a..15ad9b57 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1079,10 +1079,14 @@ function twitter_get_relation($uid, $target, $contact = []) return $relation; } +/** + * @param $data + * @return array + */ function twitter_user_to_contact($data) { if (empty($data->id_str)) { - return -1; + return []; } $baseurl = 'https://twitter.com'; @@ -1924,7 +1928,7 @@ function twitter_fetch_own_contact(App $a, $uid) // Fetching user data // get() may throw TwitterOAuthException, but we will catch it later $user = $connection->get('account/verify_credentials'); - if (empty($user) || empty($user->id_str)) { + if (empty($user->id_str)) { return false; }