advancedcontentfilter: Add language values to filter fields (#10052, #10136)
[friendica-addons.git/.git] / twitter / twitter.php
index df23e34..953571f 100644 (file)
@@ -81,7 +81,6 @@ use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
-use Friendica\Model\ItemContent;
 use Friendica\Model\ItemURI;
 use Friendica\Model\Post;
 use Friendica\Model\Tag;
@@ -666,7 +665,7 @@ function twitter_post_hook(App $a, array &$b)
 
                $b['body'] = twitter_update_mentions($b['body']);
 
-               $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, BBCode::TWITTER);
+               $msgarr = Plaintext::getPost($b, $max_char, true, BBCode::TWITTER);
                Logger::info('Got plaintext', ['id' => $b['id'], 'message' => $msgarr]);
                $msg = $msgarr["text"];
 
@@ -854,7 +853,7 @@ function twitter_expire(App $a)
        $r = Post::select(['id', 'guid'], ['deleted' => true, 'network' => Protocol::TWITTER]);
        while ($row = Post::fetch($r)) {
                Logger::info('[twitter] Delete expired item', ['id' => $row['id'], 'guid' => $row['guid'], 'callstack' => \Friendica\Core\System::callstack()]);
-               DBA::delete('item', ['id' => $row['id']]);
+               Item::markForDeletionById($row['id']);
        }
        DBA::close($r);
 
@@ -896,7 +895,7 @@ function twitter_prepare_body(App $a, array &$b)
                        }
                }
 
-               $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, BBCode::TWITTER);
+               $msgarr = Plaintext::getPost($item, $max_char, true, BBCode::TWITTER);
                $msg = $msgarr["text"];
 
                if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
@@ -911,6 +910,24 @@ function twitter_prepare_body(App $a, array &$b)
        }
 }
 
+function twitter_statuses_show(string $id, TwitterOAuth $twitterOAuth = null)
+{
+       if ($twitterOAuth === null) {
+               $ckey = DI::config()->get('twitter', 'consumerkey');
+               $csecret = DI::config()->get('twitter', 'consumersecret');
+
+               if (empty($ckey) || empty($csecret)) {
+                       return new stdClass();
+               }
+
+               $twitterOAuth = new TwitterOAuth($ckey, $csecret);
+       }
+
+       $parameters = ['trim_user' => false, 'tweet_mode' => 'extended', 'id' => $id, 'include_ext_alt_text' => true];
+
+       return $twitterOAuth->get('statuses/show', $parameters);
+}
+
 /**
  * Parse Twitter status URLs since Twitter removed OEmbed
  *
@@ -930,18 +947,7 @@ function twitter_parse_link(App $a, array &$b)
                return;
        }
 
-       $ckey = DI::config()->get('twitter', 'consumerkey');
-       $csecret = DI::config()->get('twitter', 'consumersecret');
-
-       if (empty($ckey) || empty($csecret)) {
-               return;
-       }
-
-       $connection = new TwitterOAuth($ckey, $csecret);
-
-       $parameters = ['trim_user' => false, 'tweet_mode' => 'extended', 'id' => $matches[1], 'include_ext_alt_text' => true];
-
-       $status = $connection->get('statuses/show', $parameters);
+       $status = twitter_statuses_show($matches[1]);
 
        if (empty($status->id)) {
                return;
@@ -1389,7 +1395,6 @@ function twitter_expand_entities($body, stdClass $status, $picture)
                $plain = str_replace($url->url, '', $plain);
 
                if ($url->url && $url->expanded_url && $url->display_url) {
-
                        // Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
                        if (!empty($status->quoted_status) && isset($status->quoted_status_id_str)
                                && substr($url->expanded_url, -strlen($status->quoted_status_id_str)) == $status->quoted_status_id_str
@@ -1407,23 +1412,19 @@ function twitter_expand_entities($body, stdClass $status, $picture)
 
                        $oembed_data = OEmbed::fetchURL($final_url);
 
-                       if (empty($oembed_data) || empty($oembed_data->type)) {
-                               continue;
-                       }
+                       $type = $oembed_data->type ?? '';
 
                        // Quickfix: Workaround for URL with '[' and ']' in it
                        if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
                                $expanded_url = $url->url;
                        }
 
-                       if ($oembed_data->type == 'video') {
+                       if ($type === 'video') {
                                $attachmentUrl = $expanded_url;
                                $replace = '';
-                       } elseif (($oembed_data->type == 'photo') && isset($oembed_data->url)) {
+                       } elseif ($type === 'photo' && !empty($oembed_data->url)) {
                                $replace = '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]';
-                       } elseif ($oembed_data->type != 'link') {
-                               $replace = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
-                       } else {
+                       } elseif ($type === 'link') {
                                $img_str = DI::httpRequest()->fetch($final_url, 4);
 
                                $tempfile = tempnam(get_temppath(), 'cache');
@@ -1444,6 +1445,8 @@ function twitter_expand_entities($body, stdClass $status, $picture)
                                        $attachmentUrl = $expanded_url;
                                        $replace = '';
                                }
+                       } else {
+                               $replace = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
                        }
 
                        $replacementList[$url->indices[0]] = [
@@ -1459,6 +1462,8 @@ function twitter_expand_entities($body, stdClass $status, $picture)
                $body = Strings::substringReplace($body, $parameters['replace'], $startIndex, $parameters['length']);
        }
 
+       $body = trim($body);
+
        // Footer will be taken care of with a share block in the case of a quote
        if (empty($status->quoted_status)) {
                $footer = '';
@@ -1475,7 +1480,7 @@ function twitter_expand_entities($body, stdClass $status, $picture)
                }
        }
 
-       return ['body' => $body, 'plain' => trim($plain), 'taglist' => $taglist];
+       return ['body' => trim($body), 'plain' => trim($plain), 'taglist' => $taglist];
 }
 
 /**
@@ -1526,8 +1531,10 @@ function twitter_media_entities($post, array &$postarray)
                                }
 
                                $postarray['object-type'] = Activity\ObjectType::IMAGE;
+                               $postarray['post-type'] = Item::PT_IMAGE;
                                break;
                        case 'video':
+                               $postarray['post-type'] = Item::PT_VIDEO;
                        case 'animated_gif':
                                if (!empty($medium->ext_alt_text)) {
                                        Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
@@ -1783,17 +1790,15 @@ function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection,
        $posts = [];
 
        while (!empty($post->in_reply_to_status_id_str)) {
-               $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str, "include_ext_alt_text" => true];
-
                try {
-                       $post = $connection->get('statuses/show', $parameters);
+                       $post = twitter_statuses_show($post->in_reply_to_status_id_str, $connection);
                } catch (TwitterOAuthException $e) {
                        Logger::warning('Error fetching parent post', ['uid' => $uid, 'post' => $post->id_str, 'message' => $e->getMessage()]);
                        break;
                }
 
                if (empty($post)) {
-                       Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters['id'], Logger::DEBUG);
+                       Logger::log("twitter_fetchparentposts: Can't fetch post " . $post->in_reply_to_status_id_str, Logger::DEBUG);
                        break;
                }