diff --git a/twitter/twitter.php b/twitter/twitter.php index 9b02f44a..79f37e1f 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -621,19 +621,15 @@ function twitter_post_hook(App $a, array &$b) $msg = Plaintext::shorten($msgarr["title"], $max_char - 50); } - $image = ""; - - if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) { + if (($msgarr['url'] == $b['plink']) && !empty($msgarr['images']) && (count($msgarr['images']) <= 4)) { + $url_added = false; + } elseif (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) { $msg .= "\n" . $msgarr["url"]; $url_added = true; } else { $url_added = false; } - if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) { - $image = $msgarr["image"]; - } - if (empty($msg)) { return; } @@ -641,33 +637,43 @@ function twitter_post_hook(App $a, array &$b) // and now tweet it :-) $post = []; - if (!empty($image)) { + if (!empty($msgarr['images'])) { try { - $img_str = Network::fetchUrl($image); + $post['media_ids'] = ''; + $counter = 0; + foreach ($msgarr['images'] as $image) { + if (++$counter > 4) { + continue; + } - $tempfile = tempnam(get_temppath(), 'cache'); - file_put_contents($tempfile, $img_str); + $img_str = Network::fetchUrl($image['url']); - $media = $connection->upload('media/upload', ['media' => $tempfile]); + $tempfile = tempnam(get_temppath(), 'cache'); + file_put_contents($tempfile, $img_str); - unlink($tempfile); + $media = $connection->upload('media/upload', ['media' => $tempfile]); - if (isset($media->media_id_string)) { - $post['media_ids'] = $media->media_id_string; - //$details = $cb->account_verifyCredentials(); - } else { - throw new Exception('Failed upload of ' . $image); + unlink($tempfile); + + if (isset($media->media_id_string)) { + $post['media_ids'] .= $media->media_id_string . ','; + + if (!empty($image['description'])) { + $data = ['media_id' => $media->media_id_string, + 'alt_text' => ['text' => substr($image['description'], 0, 420)]]; + $ret = $cb->media_metadata_create($data); + Logger::info('Metadata create', ['data' => $data, 'return' => json_encode($ret)]); + } + } else { + throw new Exception('Failed upload of ' . $image['url']); + } + } + $post['media_ids'] = rtrim($post['media_ids'], ','); + if (empty($post['media_ids'])) { + unset($post['media_ids']); } } catch (Exception $e) { Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage()); - - // Workaround: Remove the picture link so that the post can be reposted without it - // When there is another url already added, a second url would be superfluous. - if (!$url_added) { - $msg .= "\n" . $image; - } - - $image = ""; } }