]> git.mxchange.org Git - friendica-addons.git/blobdiff - bluesky/bluesky.php
Langfilter: Use two letter code for the language / Bluesky: Remove callstack
[friendica-addons.git] / bluesky / bluesky.php
index c96658721f8737d020e7c38f8ff35897cf473b48..2d497e8c2742b1af04769f86cb7975e72b12050e 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;
@@ -931,7 +930,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;
 
@@ -973,12 +972,30 @@ 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)) {
                        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);
                }
@@ -989,8 +1006,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 ?? '']);
@@ -1032,6 +1053,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);
@@ -1077,7 +1099,7 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui
 
 function bluesky_get_text(stdClass $record): string
 {
-       $text = $record->text;
+       $text = $record->text ?? '';
 
        if (empty($record->facets)) {
                return $text;
@@ -1170,11 +1192,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;
@@ -1191,17 +1212,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;
 
@@ -1215,7 +1234,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;
@@ -1316,7 +1335,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]);