]> git.mxchange.org Git - friendica-addons.git/blobdiff - bluesky/bluesky.php
Bluesky: Support for transmitted languages
[friendica-addons.git] / bluesky / bluesky.php
index 252becc12c27eda871225473eb90feabcdb74782..82501594f90ed4eefe96a6a092e8c914d8028b36 100644 (file)
@@ -32,7 +32,6 @@ use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
-use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
@@ -43,6 +42,7 @@ use Friendica\Model\Photo;
 use Friendica\Model\Post;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
+use Friendica\Object\Image;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
@@ -50,6 +50,7 @@ use Friendica\Util\Strings;
 
 const BLUESKY_DEFAULT_POLL_INTERVAL = 10; // given in minutes
 const BLUESKY_HOST = 'https://bsky.app'; // Hard wired until Bluesky will run on multiple systems
+const BLUESKY_IMAGE_SIZE = [1000000, 500000, 100000, 50000];
 
 function bluesky_install()
 {
@@ -609,6 +610,13 @@ function bluesky_create_post(array $item, stdClass $root = null, stdClass $paren
                return;
        }
 
+       // Try to fetch the language from the post itself
+       if (!empty($item['language'])) {
+               $language = array_key_first(json_decode($item['language'], true));
+       } else {
+               $language = '';
+       }
+
        $did  = DI::pConfig()->get($uid, 'bluesky', 'did');
        $urls = bluesky_get_urls(Post\Media::removeFromBody($item['body']));
        $item['body'] = $urls['body'];
@@ -620,10 +628,14 @@ function bluesky_create_post(array $item, stdClass $root = null, stdClass $paren
 
                $record = [
                        'text'      => $facets['body'],
+                       '$type'     => 'app.bsky.feed.post',
                        'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
-                       '$type'     => 'app.bsky.feed.post'
                ];
 
+               if (!empty($language)) {
+                       $record['langs'] = [$language];
+               }
+
                if (!empty($facets['facets'])) {
                        $record['facets'] = $facets['facets'];
                }
@@ -634,6 +646,12 @@ function bluesky_create_post(array $item, stdClass $root = null, stdClass $paren
 
                if ($key == count($msg['parts']) - 1) {
                        $record = bluesky_add_embed($uid, $msg, $record);
+                       if (empty($record)) {
+                               if (Worker::getRetrial() < 3) {
+                                       Worker::defer();
+                               }
+                               return;
+                       }
                }
 
                $post = [
@@ -644,6 +662,9 @@ function bluesky_create_post(array $item, stdClass $root = null, stdClass $paren
 
                $parent = bluesky_xrpc_post($uid, 'com.atproto.repo.createRecord', $post);
                if (empty($parent)) {
+                       if ($part == 0) {
+                               Worker::defer();
+                       }
                        return;
                }
                Logger::debug('Posting done', ['return' => $parent]);
@@ -663,6 +684,7 @@ function bluesky_get_urls(string $body): array
        // Remove all hashtag and mention links
        $body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $body);
 
+       $body = BBCode::expandVideoLinks($body);
        $urls = [];
 
        // Search for pure links
@@ -746,12 +768,15 @@ function bluesky_add_embed(int $uid, array $msg, array $record): array
        if (($msg['type'] != 'link') && !empty($msg['images'])) {
                $images = [];
                foreach ($msg['images'] as $image) {
-                       $photo = Photo::selectFirst(['resource-id'], ['id' => $image['id']]);
-                       $photo = Photo::selectFirst([], ["`resource-id` = ? AND `scale` > ?", $photo['resource-id'], 0], ['order' => ['scale']]);
+                       if (count($images) == 4) {
+                               continue;
+                       }
+                       $photo = Photo::selectFirst([], ['id' => $image['id']]);
                        $blob = bluesky_upload_blob($uid, $photo);
-                       if (!empty($blob) && count($images) < 4) {
-                               $images[] = ['alt' => $image['description'] ?? '', 'image' => $blob];
+                       if (empty($blob)) {
+                               return [];
                        }
+                       $images[] = ['alt' => $image['description'] ?? '', 'image' => $blob];
                }
                if (!empty($images)) {
                        $record['embed'] = ['$type' => 'app.bsky.embed.images', 'images' => $images];
@@ -778,13 +803,29 @@ function bluesky_add_embed(int $uid, array $msg, array $record): array
 
 function bluesky_upload_blob(int $uid, array $photo): ?stdClass
 {
+       $retrial = Worker::getRetrial();
        $content = Photo::getImageForPhoto($photo);
+
+       $picture = new Image($content, $photo['type']);
+       $height  = $picture->getHeight();
+       $width   = $picture->getWidth();
+       $size    = strlen($content);
+
+       $picture    = Photo::resizeToFileSize($picture, BLUESKY_IMAGE_SIZE[$retrial]);
+       $new_height = $picture->getHeight();
+       $new_width  = $picture->getWidth();
+       $content    = $picture->asString();
+       $new_size   = strlen($content);
+
+       Logger::info('Uploading', ['uid' => $uid, 'retrial' => $retrial, 'height' => $new_height, 'width' => $new_width, 'size' => $new_size, 'orig-height' => $height, 'orig-width' => $width, 'orig-size' => $size]);
+
        $data = bluesky_post($uid, '/xrpc/com.atproto.repo.uploadBlob', $content, ['Content-type' => $photo['type'], 'Authorization' => ['Bearer ' . bluesky_get_token($uid)]]);
        if (empty($data)) {
+               Logger::info('Uploading failed', ['uid' => $uid, 'retrial' => $retrial, 'height' => $new_height, 'width' => $new_width, 'size' => $new_size, 'orig-height' => $height, 'orig-width' => $width, 'orig-size' => $size]);
                return null;
        }
 
-       Logger::debug('Uploaded blob', ['return' => $data]);
+       Logger::debug('Uploaded blob', ['return' => $data, 'uid' => $uid, 'retrial' => $retrial, 'height' => $new_height, 'width' => $new_width, 'size' => $new_size, 'orig-height' => $height, 'orig-width' => $width, 'orig-size' => $size]);
        return $data->blob;
 }
 
@@ -886,7 +927,7 @@ function bluesky_fetch_notifications(int $uid)
                                        $data = Item::insert($item);
                                        Logger::debug('Got like', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
                                } else {
-                                       Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $$item['thr-parent'], 'uri' => $uri]);
+                                       Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]);
                                }
                        break;
 
@@ -900,7 +941,7 @@ function bluesky_fetch_notifications(int $uid)
                                        $data = Item::insert($item);
                                        Logger::debug('Got repost', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
                                } else {
-                                       Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $$item['thr-parent'], 'uri' => $uri]);
+                                       Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]);
                                }
                        break;
 
@@ -942,12 +983,33 @@ function bluesky_fetch_feed(int $uid, string $feed)
                return;
        }
 
+       $feeddata = bluesky_xrpc_get($uid, 'app.bsky.feed.getFeedGenerator', ['feed' => $feed]);
+       if (!empty($feeddata)) {
+               $feedurl  = $feeddata->view->uri;
+               $feedname = $feeddata->view->displayName;
+       } else {
+               $feedurl  = $feed;
+               $feedname = $feed;
+       }
+
        foreach (array_reverse($data->feed) as $entry) {
-               if (!Relay::isWantedLanguage($entry->post->record->text)) {
+               $contact   = bluesky_get_contact($entry->post->author, 0, $uid);
+               $languages = $entry->post->record->langs ?? [];
+
+               if (!Relay::isWantedLanguage($entry->post->record->text, 0, $contact['id'] ?? 0, $languages)) {
                        Logger::debug('Unwanted language detected', ['text' => $entry->post->record->text]);
                        continue;
                }
-               bluesky_process_post($entry->post, $uid, Item::PR_TAG, 0);
+               $id = bluesky_process_post($entry->post, $uid, Item::PR_TAG, 0);
+               if (!empty($id)) {
+                       $post = Post::selectFirst(['uri-id'], ['id' => $id]);
+                       if (!empty($post['uri-id'])) {
+                               $stored = Post\Category::storeFileByURIId($post['uri-id'], $uid, Post\Category::SUBCRIPTION, $feedname, $feedurl);
+                               Logger::debug('Stored tag subscription for user', ['uri-id' => $post['uri-id'], 'uid' => $uid, 'name' => $feedname, 'url' => $feedurl, 'stored' => $stored]);
+                       } else {
+                               Logger::notice('Post not found', ['id' => $id, 'entry' => $entry]);
+                       }
+               }
                if (!empty($entry->reason)) {
                        bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid);
                }
@@ -958,8 +1020,12 @@ function bluesky_process_post(stdClass $post, int $uid, int $post_reason, $level
 {
        $uri = bluesky_get_uri($post);
 
-       if (Post::exists(['uri' => $uri, 'uid' => $uid]) || Post::exists(['extid' => $uri, 'uid' => $uid])) {
-               return 0;
+       if ($id = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $uid])) {
+               return $id['id'];       
+       }
+
+       if ($id = Post::selectFirst(['id'], ['extid' => $uri, 'uid' => $uid])) {
+               return $id['id'];       
        }
 
        Logger::debug('Importing post', ['uid' => $uid, 'indexedAt' => $post->indexedAt, 'uri' => $post->uri, 'cid' => $post->cid, 'root' => $post->record->reply->root ?? '']);
@@ -1001,6 +1067,7 @@ function bluesky_get_header(stdClass $post, string $uri, int $uid, int $fetch_ui
                'author-link'   => $contact['url'],
                'author-avatar' => $contact['avatar'],
                'plink'         => $contact['alias'] . '/post/' . $parts->rkey,
+               'source'        => json_encode($post),
        ];
 
        $item['uri-id']       = ItemURI::getIdByURI($uri);
@@ -1041,12 +1108,13 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui
 
        $item['body']    = bluesky_get_text($record);
        $item['created'] = DateTimeFormat::utc($record->createdAt, DateTimeFormat::MYSQL);
+       $item['transmitted-languages'] = $record->langs ?? [];
        return $item;
 }
 
 function bluesky_get_text(stdClass $record): string
 {
-       $text = $record->text;
+       $text = $record->text ?? '';
 
        if (empty($record->facets)) {
                return $text;
@@ -1139,11 +1207,10 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le
                                                        $shared = bluesky_add_media($single, $shared, $fetch_uid, $level);
                                                }
                                        }
-                                       $id = Item::insert($shared);
-                                       $shared = Post::selectFirst(['uri-id'], ['id' => $id]);
+                                       Item::insert($shared);
                                }
                        }
-                       if (!empty($shared)) {
+                       if (!empty($shared['uri-id'])) {
                                $item['quote-uri-id'] = $shared['uri-id'];
                        }
                        break;
@@ -1160,17 +1227,15 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le
                                                        $shared = bluesky_add_media($single, $shared, $fetch_uid, $level);
                                                }
                                        }
-
-                                       $id = Item::insert($shared);
-                                       $shared = Post::selectFirst(['uri-id'], ['id' => $id]);
+                                       Item::insert($shared);
                                }
                        }
-                       if (!empty($shared)) {
+                       if (!empty($shared['uri-id'])) {
                                $item['quote-uri-id'] = $shared['uri-id'];
                        }
 
                        if (!empty($embed->media)) {
-                               bluesky_add_media($embed->media, $item, $fetch_uid, $level);
+                               $item = bluesky_add_media($embed->media, $item, $fetch_uid, $level);
                        }
                        break;
 
@@ -1184,7 +1249,7 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le
 function bluesky_get_uri(stdClass $post): string
 {
        if (empty($post->cid)) {
-               Logger::info('Invalid URI', ['post' => $post, 'callstack' => System::callstack(10, 0, true)]);
+               Logger::info('Invalid URI', ['post' => $post]);
                return '';
        }
        return $post->uri . ':' . $post->cid;
@@ -1243,7 +1308,7 @@ function bluesky_fetch_missing_post(string $uri, int $uid, int $causer, int $lev
        if (++$level > 100) {
                Logger::info('Recursion level too deep', ['level' => $level, 'uid' => $uid, 'uri' => $uri, 'fallback' => $fallback]);
                // When the level is too deep we will fallback to the parent uri.
-               // Allthough the threading won't be correct, we at least had stored all posts and won't try again 
+               // Allthough the threading won't be correct, we at least had stored all posts and won't try again
                return $fallback;
        }
 
@@ -1256,8 +1321,8 @@ function bluesky_fetch_missing_post(string $uri, int $uid, int $causer, int $lev
                Logger::info('Thread was not fetched', ['level' => $level, 'uid' => $uid, 'uri' => $uri, 'fallback' => $fallback]);
                return $fallback;
        }
-       
-       Logger::debug('Reply count', ['replies' => $data->thread->post->replyCount, 'level' => $level, 'uid' => $uid, 'uri' => $uri]); 
+
+       Logger::debug('Reply count', ['level' => $level, 'uid' => $uid, 'uri' => $uri]);
 
        if ($causer != 0) {
                $cdata = Contact::getPublicAndUserContactID($causer, $uid);
@@ -1285,7 +1350,12 @@ function bluesky_fetch_post(string $uri, int $uid): string
 
 function bluesky_process_thread(stdClass $thread, int $uid, array $cdata, int $level): string
 {
+       if (empty($thread->post)) {
+               Logger::info('Invalid post', ['post' => $thread]);
+               return '';
+       }
        $uri = bluesky_get_uri($thread->post);
+
        $fetched_uri = bluesky_fetch_post($uri, $uid);
        if (empty($fetched_uri)) {
                Logger::debug('Process missing post', ['uri' => $uri]);
@@ -1556,4 +1626,4 @@ function bluesky_get(int $uid, string $url, string $accept_content = HttpClientA
        }
 
        return json_decode($curlResult->getBody());
-}
\ No newline at end of file
+}