Merge pull request #990 from MrPetovan/task/share-block-guid
[friendica-addons.git/.git] / twitter / twitter.php
index 6e44759..f743d96 100644 (file)
@@ -491,24 +491,31 @@ 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);
+                       if ($connection->getLastHttpCode() != 200) {
+                               Logger::error('Unable to create favorite', ['result' => $result]);
+                       }
                        break;
-               case "unlike":
+               case 'unlike':
                        $result = $connection->post('favorites/destroy', $post);
+                       if ($connection->getLastHttpCode() != 200) {
+                               Logger::error('Unable to destroy favorite', ['result' => $result]);
+                       }
                        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)
@@ -904,13 +911,12 @@ function twitter_do_mirrorpost(App $a, $uid, $post)
                        return [];
                }
 
-               $datarray['body'] = "\n" . share_header(
+               $datarray['body'] = "\n" . BBCode::getShareOpeningTag(
                        $item['author-name'],
                        $item['author-link'],
                        $item['author-avatar'],
-                       '',
-                       $item['created'],
-                       $item['plink']
+                       $item['plink'],
+                       $item['created']
                );
 
                $datarray['body'] .= $item['body'] . '[/share]';
@@ -1047,33 +1053,39 @@ 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;
+               $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;
+               }
 
-       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()]);
        }
 
-       Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]);
-
        return $relation;
 }
 
+/**
+ * @param $data
+ * @return array
+ */
 function twitter_user_to_contact($data)
 {
        if (empty($data->id_str)) {
-               return -1;
+               return [];
        }
 
        $baseurl = 'https://twitter.com';
@@ -1627,13 +1639,12 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
                } else {
                        $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true, $uriid);
                        if (!empty($quoted['body'])) {
-                               $postarray['body'] .= "\n" . share_header(
+                               $postarray['body'] .= "\n" . BBCode::getShareOpeningTag(
                                                $quoted['author-name'],
                                                $quoted['author-link'],
                                                $quoted['author-avatar'],
-                                               "",
-                                               $quoted['created'],
-                                               $quoted['plink']
+                                               $quoted['plink'],
+                                               $quoted['created']
                                        );
 
                                $postarray['body'] .= $quoted['body'] . '[/share]';
@@ -1915,7 +1926,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;
                }