]> git.mxchange.org Git - friendica-addons.git/commitdiff
Twitter: Code for aending media to Twitter has been optimised
authorMichael <heluecht@pirati.ca>
Mon, 2 Apr 2018 05:42:33 +0000 (05:42 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 2 Apr 2018 05:42:33 +0000 (05:42 +0000)
twitter/twitter.php

index 29a1a631adff61b2cf22332898e64cdb18e24219..72a9682be624504bc3d60c860045db9970140c11 100644 (file)
@@ -549,8 +549,14 @@ function twitter_post_hook(App $a, &$b)
                        $image = $msgarr["image"];
                }
 
+               if (empty($msg)) {
+                       return;
+               }
+
                // and now tweet it :-)
-               if (strlen($msg) && ($image != "")) {
+               $post = [];
+
+               if (!empty($image)) {
                        try {
                                $img_str = Network::fetchUrl($image);
 
@@ -561,30 +567,7 @@ function twitter_post_hook(App $a, &$b)
 
                                unlink($tempfile);
 
-                               $post = ['status' => $msg, 'media_ids' => $media->media_id_string];
-
-                               if ($iscomment) {
-                                       $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
-                               }
-
-                               $result = $connection->post('statuses/update', $post);
-
-                               logger('twitter_post_with_media send, result: ' . print_r($result, true), LOGGER_DEBUG);
-
-                               if ($result->source) {
-                                       Config::set("twitter", "application_name", strip_tags($result->source));
-                               }
-
-                               if ($result->errors || $result->error) {
-                                       logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
-
-                                       // Workaround: Remove the picture link so that the post can be reposted without it
-                                       $msg .= " " . $image;
-                                       $image = "";
-                               } elseif ($iscomment) {
-                                       logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
-                                       Item::update(['extid' => "twitter::" . $result->id_str, 'body' => $result->text], ['id' => $b['id']]);
-                               }
+                               $post['media_ids'] = $media->media_id_string;
                        } catch (Exception $e) {
                                logger('Exception when trying to send to Twitter: ' . $e->getMessage());
 
@@ -594,50 +577,35 @@ function twitter_post_hook(App $a, &$b)
                        }
                }
 
-               if (strlen($msg) && ($image == "")) {
-// -----------------
-                       $max_char = 280;
-                       $msgarr = BBCode::toPlaintext($b, $max_char, true, 8);
-                       $msg = $msgarr["text"];
+               $post['status'] = $msg;
 
-                       if (($msg == "") && isset($msgarr["title"])) {
-                               $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
-                       }
+               if ($iscomment) {
+                       $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
+               }
 
-                       if (isset($msgarr["url"])) {
-                               $msg .= "\n" . $msgarr["url"];
-                       }
-// -----------------
-                       $url = 'statuses/update';
-                       $post = ['status' => $msg, 'weighted_character_count' => 'true'];
+               $url = 'statuses/update';
+               $result = $connection->post($url, $post);
+               logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
 
-                       if ($iscomment) {
-                               $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
-                       }
+               if ($result->source) {
+                       Config::set("twitter", "application_name", strip_tags($result->source));
+               }
 
-                       $result = $connection->post($url, $post);
-                       logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
+               if ($result->errors) {
+                       logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
 
-                       if ($result->source) {
-                               Config::set("twitter", "application_name", strip_tags($result->source));
+                       $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
+                       if (count($r)) {
+                               $a->contact = $r[0]["id"];
                        }
 
-                       if ($result->errors) {
-                               logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
-
-                               $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
-                               if (count($r)) {
-                                       $a->contact = $r[0]["id"];
-                               }
-
-                               $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
+                       $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
 
-                               Queue::add($a->contact, NETWORK_TWITTER, $s);
-                               notice(L10n::t('Twitter post failed. Queued for retry.') . EOL);
-                       } elseif ($iscomment) {
-                               logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
-                               Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
-                       }
+                       Queue::add($a->contact, NETWORK_TWITTER, $s);
+                       notice(L10n::t('Twitter post failed. Queued for retry.') . EOL);
+               } elseif ($iscomment) {
+                       logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
+                       Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
                }
        }
 }