advancedcontentfilter: Add language values to filter fields (#10052, #10136)
[friendica-addons.git/.git] / twitter / twitter.php
index 7c8f1a3..953571f 100644 (file)
@@ -81,8 +81,8 @@ 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;
 use Friendica\Model\User;
 use Friendica\Protocol\Activity;
@@ -403,14 +403,14 @@ function twitter_hook_fork(App $a, array &$b)
                return;
        }
 
-       if ($post['app'] == 'Twitter') {
+       if (substr($post['app'], 0, 7) == 'Twitter') {
                $b['execute'] = false;
                return;
        }
 
        if (DI::pConfig()->get($post['uid'], 'twitter', 'import')) {
                // Don't fork if it isn't a reply to a twitter post
-               if (($post['parent'] != $post['id']) && !Item::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
+               if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
                        Logger::notice('No twitter parent found', ['item' => $post['id']]);
                        $b['execute'] = false;
                        return;
@@ -544,6 +544,8 @@ function twitter_post_hook(App $a, array &$b)
                return;
        }
 
+       $thr_parent = null;
+
        if ($b['parent'] != $b['id']) {
                Logger::debug('Got comment', ['item' => $b]);
 
@@ -556,28 +558,24 @@ function twitter_post_hook(App $a, array &$b)
                }
 
                $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
-               $orig_post = Item::selectFirst([], $condition);
-               if (!DBA::isResult($orig_post)) {
+               $thr_parent = Post::selectFirst(['uri', 'extid', 'author-link', 'author-nick', 'author-network'], $condition);
+               if (!DBA::isResult($thr_parent)) {
                        Logger::warning('No parent found', ['thr-parent' => $b["thr-parent"]]);
                        return;
-               } else {
-                       $iscomment = true;
                }
 
+               if ($thr_parent['author-network'] == Protocol::TWITTER) {
+                       $nickname = '@[url=' . $thr_parent['author-link'] . ']' . $thr_parent['author-nick'] . '[/url]';
+                       $nicknameplain = '@' . $thr_parent['author-nick'];
 
-               $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
-               $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
-               $nicknameplain = "@" . $nicknameplain;
-
-               Logger::info('Comparing', ['nickname' => $nickname, 'nicknameplain' => $nicknameplain, 'body' => $b["body"]]);
-               if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
-                       $b["body"] = $nickname . " " . $b["body"];
+                       Logger::info('Comparing', ['nickname' => $nickname, 'nicknameplain' => $nicknameplain, 'body' => $b["body"]]);
+                       if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
+                               $b["body"] = $nickname . " " . $b["body"];
+                       }
                }
 
-               Logger::debug('Parent found', ['parent' => $orig_post]);
+               Logger::debug('Parent found', ['parent' => $thr_parent]);
        } else {
-               $iscomment = false;
-
                if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
                        return;
                }
@@ -591,7 +589,7 @@ function twitter_post_hook(App $a, array &$b)
        }
 
        if (($b['verb'] == Activity::POST) && $b['deleted']) {
-               twitter_action($a, $b["uid"], twitter_get_id($orig_post["uri"]), "delete");
+               twitter_action($a, $b['uid'], twitter_get_id($thr_parent['uri']), 'delete');
        }
 
        if ($b['verb'] == Activity::LIKE) {
@@ -608,7 +606,7 @@ function twitter_post_hook(App $a, array &$b)
        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($orig_post["extid"]), "delete");
+                       twitter_action($a, $b['uid'], twitter_get_id($thr_parent['extid']), 'delete');
                } else {
                        twitter_retweet($b["uid"], twitter_get_id($b["thr-parent"]));
                }
@@ -667,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"];
 
@@ -675,6 +673,11 @@ function twitter_post_hook(App $a, array &$b)
                        $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
                }
 
+               // Add the link to the body if the type isn't a photo or there are more than 4 images in the post
+               if (!empty($msgarr['url']) && (($msgarr['type'] != 'photo') || empty($msgarr['images']) || (count($msgarr['images']) > 4))) {
+                       $msg .= "\n" . $msgarr['url'];
+               }
+
                if (empty($msg)) {
                        Logger::notice('Empty message', ['id' => $b['id']]);
                        return;
@@ -727,8 +730,8 @@ function twitter_post_hook(App $a, array &$b)
 
                $post['status'] = $msg;
 
-               if ($iscomment) {
-                       $post["in_reply_to_status_id"] = twitter_get_id($orig_post["uri"]);
+               if ($thr_parent) {
+                       $post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
                }
 
                $url = 'statuses/update';
@@ -742,7 +745,7 @@ function twitter_post_hook(App $a, array &$b)
                if (!empty($result->errors)) {
                        Logger::error('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
                        Worker::defer();
-               } elseif ($iscomment) {
+               } elseif ($thr_parent) {
                        Logger::notice('Post send, updating extid', ['id' => $b['id'], 'extid' => $result->id_str]);
                        Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
                }
@@ -847,10 +850,10 @@ function twitter_expire(App $a)
 
        Logger::notice('Start deleting expired posts');
 
-       $r = Item::select(['id', 'guid'], ['deleted' => true, 'network' => Protocol::TWITTER]);
-       while ($row = DBA::fetch($r)) {
+       $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);
 
@@ -881,7 +884,7 @@ function twitter_prepare_body(App $a, array &$b)
                $item["plink"] = DI::baseUrl()->get() . "/display/" . $item["guid"];
 
                $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
-               $orig_post = Item::selectFirst(['author-link'], $condition);
+               $orig_post = Post::selectFirst(['author-link'], $condition);
                if (DBA::isResult($orig_post)) {
                        $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
                        $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
@@ -892,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")) {
@@ -907,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
  *
@@ -926,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;
@@ -999,11 +1009,8 @@ function twitter_parse_link(App $a, array &$b)
  */
 function twitter_do_mirrorpost(App $a, $uid, $post)
 {
-       $datarray['api_source'] = true;
-       $datarray['profile_uid'] = $uid;
-       $datarray['extid'] = Protocol::TWITTER . ':' . $post->id;
-       $datarray['protocol'] = Conversation::PARCEL_TWITTER;
-       $datarray['source'] = json_encode($post);
+       $datarray['uid'] = $uid;
+       $datarray['extid'] = 'twitter::' . $post->id;
        $datarray['title'] = '';
 
        if (!empty($post->retweeted_status)) {
@@ -1033,7 +1040,7 @@ function twitter_do_mirrorpost(App $a, $uid, $post)
                $datarray['body'] = $item['body'];
        }
 
-       $datarray['source'] = $item['app'];
+       $datarray['app'] = $item['app'];
        $datarray['verb'] = $item['verb'];
 
        if (isset($item['location'])) {
@@ -1061,11 +1068,16 @@ function twitter_fetchtimeline(App $a, $uid)
                $application_name = DI::baseUrl()->getHostname();
        }
 
-       require_once 'mod/item.php';
-       require_once 'mod/share.php';
-
        $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
 
+       // Ensure to have the own contact
+       try {
+               twitter_fetch_own_contact($a, $uid);
+       } catch (TwitterOAuthException $e) {
+               Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
+               return;
+       }
+
        $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
 
        $first_time = ($lastid == "");
@@ -1102,20 +1114,17 @@ function twitter_fetchtimeline(App $a, $uid)
                        }
 
                        if (!stristr($post->source, $application_name)) {
-                               $_SESSION["authenticated"] = true;
-                               $_SESSION["uid"] = $uid;
-
                                Logger::info('Preparing mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
 
-                               $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
+                               $mirrorpost = twitter_do_mirrorpost($a, $uid, $post);
 
-                               if (empty($_REQUEST['body'])) {
+                               if (empty($mirrorpost['body'])) {
                                        continue;
                                }
 
-                               Logger::info('Posting mirror post ', ['twitter-id' => $post->id_str, 'uid' => $uid]);
+                               Logger::info('Posting mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
 
-                               item_post($a);
+                               Post\Delayed::add($mirrorpost['extid'], $mirrorpost, PRIORITY_MEDIUM, true);
                        }
                }
        }
@@ -1232,12 +1241,16 @@ function twitter_fetch_contact($uid, $data, $create_user)
        if (!empty($cid)) {
                DBA::update('contact', $fields, ['id' => $cid]);
                Contact::updateAvatar($cid, $avatar);
+       } else {
+               Logger::warning('No contact found', ['fields' => $fields]);
        }
 
        $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
-       if (!DBA::isResult($contact) && !$create_user) {
+       if (!DBA::isResult($contact) && empty($cid)) {
                Logger::warning('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
                return 0;
+       } elseif (!$create_user) {
+               return $cid;
        }
 
        if (!DBA::isResult($contact)) {
@@ -1382,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
@@ -1400,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');
@@ -1437,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]] = [
@@ -1452,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 = '';
@@ -1468,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];
 }
 
 /**
@@ -1519,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]);
@@ -1577,13 +1591,14 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
        $postarray['uri'] = "twitter::" . $post->id_str;
        $postarray['protocol'] = Conversation::PARCEL_TWITTER;
        $postarray['source'] = json_encode($post);
+       $postarray['direction'] = Conversation::PULL;
 
        if (empty($uriid)) {
                $uriid = $postarray['uri-id'] = ItemURI::insert(['uri' => $postarray['uri']]);
        }
 
        // Don't import our own comments
-       if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
+       if (Post::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
                Logger::info('Item found', ['extid' => $postarray['uri']]);
                return [];
        }
@@ -1593,9 +1608,9 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
        if ($post->in_reply_to_status_id_str != "") {
                $thr_parent = "twitter::" . $post->in_reply_to_status_id_str;
 
-               $item = Item::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
+               $item = Post::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
                if (!DBA::isResult($item)) {
-                       $item = Item::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid]);
+                       $item = Post::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid]);
                }
 
                if (DBA::isResult($item)) {
@@ -1717,6 +1732,7 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
                        $postarray['thr-parent'] = $retweet['uri'];
                } else {
                        $retweet['source'] = $postarray['source'];
+                       $retweet['direction'] = $postarray['direction'];
                        $retweet['private'] = $postarray['private'];
                        $retweet['allow_cid'] = $postarray['allow_cid'];
                        $retweet['contact-id'] = $postarray['contact-id'];
@@ -1774,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;
                }
 
@@ -1793,7 +1807,7 @@ function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection,
                        break;
                }
 
-               if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
+               if (Post::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
                        break;
                }
 
@@ -1934,8 +1948,8 @@ function twitter_fetchhometimeline(App $a, $uid)
 
                        if (empty($postarray['thr-parent'])) {
                                $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
-                               if (DBA::isResult($contact)) {
-                                       $notify = Item::isRemoteSelf($contact, $postarray);
+                               if (DBA::isResult($contact) && Item::isRemoteSelf($contact, $postarray)) {
+                                       $notify = PRIORITY_MEDIUM;
                                }
                        }