]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Improved handling for undeterminded languages
[friendica.git] / src / Model / Item.php
index 5458514aa5b4bfde125dd74b4935a54d56fb3ca3..a672f5b174791aa7af0d61f6446bd45eb4188812 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2023, the Friendica project
+ * @copyright Copyright (C) 2010-2024, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -138,7 +138,7 @@ class Item
                'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type', 'post-reason',
                'private', 'pubmail', 'visible', 'starred',
                'unseen', 'deleted', 'origin', 'mention', 'global', 'network',
-               'title', 'content-warning', 'body', 'location', 'coord', 'app',
+               'title', 'content-warning', 'body', 'language', 'location', 'coord', 'app',
                'rendered-hash', 'rendered-html', 'object-type', 'object', 'target-type', 'target',
                'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
                'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'causer-id'
@@ -196,6 +196,10 @@ class Item
                        $previous = Post::selectFirst(['edited'], $condition);
                }
 
+               if (!empty($fields['body'])) {
+                       $fields['body'] = self::setHashtags($fields['body']);
+               }
+
                $rows = Post::update($fields, $condition);
                if (is_bool($rows)) {
                        return $rows;
@@ -334,7 +338,7 @@ class Item
                // locate item to be deleted
                $fields = [
                        'id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri-id', 'origin',
-                       'deleted', 'resource-id', 'event-id',
+                       'thr-parent-id', 'deleted', 'resource-id', 'event-id', 'vid', 'body',
                        'verb', 'object-type', 'object', 'target', 'contact-id', 'psid', 'gravity'
                ];
                $item = Post::selectFirst($fields, ['id' => $item_id]);
@@ -414,6 +418,10 @@ class Item
                DI::notify()->deleteForItem($item['uri-id']);
                DI::notification()->deleteForItem($item['uri-id']);
 
+               if (in_array($item['gravity'], [self::GRAVITY_ACTIVITY, self::GRAVITY_COMMENT])) {
+                       Post\Counts::update($item['thr-parent-id'], $item['parent-uri-id'], $item['vid'], $item['verb'], $item['body']);
+               }
+
                Logger::info('Item has been marked for deletion.', ['id' => $item_id]);
 
                return true;
@@ -1423,18 +1431,50 @@ class Item
                        Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$posted_item['uri-id'], (int)$posted_item['uid']);
                }
 
-               // Fill the cache with the rendered content.
-               if (in_array($posted_item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT]) && ($posted_item['uid'] == 0)) {
-                       self::updateDisplayCache($posted_item['uri-id']);
-               }
-
                if ($inserted) {
-                       Post\Engagement::storeFromItem($posted_item);
+                       // Fill the cache with the rendered content.
+                       if (in_array($posted_item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT])) {
+                               self::updateDisplayCache($posted_item['uri-id']);
+                       }
+
+                       if (in_array($posted_item['gravity'], [self::GRAVITY_ACTIVITY, self::GRAVITY_COMMENT])) {
+                               Post\Counts::update($posted_item['thr-parent-id'], $posted_item['parent-uri-id'], $posted_item['vid'], $posted_item['verb'], $posted_item['body']);
+                       }
+
+                       $engagement_uri_id = Post\Engagement::storeFromItem($posted_item);
+                       if ($engagement_uri_id) {
+                               self::reshareChannelPost($engagement_uri_id);
+                       }
                }
 
                return $post_user_id;
        }
 
+       private static function reshareChannelPost(int $uri_id)
+       {
+               $item = Post::selectFirst(['id', 'private', 'network', 'language', 'owner-id'], ['uri-id' => $uri_id, 'uid' => 0]);
+               if (empty($item['id'])) {
+                       return;
+               }
+
+               if (($item['private'] != self::PUBLIC) || !in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+                       return;
+               }
+
+               $engagement = DBA::selectFirst('post-engagement', ['searchtext', 'media-type'], ['uri-id' => $uri_id]);
+               if (empty($engagement['searchtext'])) {
+                       return;
+               }
+
+               $language = !empty($item['language']) ? array_key_first(json_decode($item['language'], true)) : '';
+               $tags     = array_column(Tag::getByURIId($uri_id, [Tag::HASHTAG]), 'name');
+
+               foreach (DI::userDefinedChannel()->getMatchingChannelUsers($engagement['searchtext'], $language, $tags, $engagement['media-type'], $item['owner-id']) as $uid) {
+                       Logger::debug('Reshare post', ['uid' => $uid, 'uri-id' => $uri_id, 'language' => $language, 'tags' => $tags, 'searchtext' => $engagement['searchtext'], 'media_type' => $engagement['media-type']]);
+                       self::performActivity($item['id'], 'announce', $uid);
+               }
+       }
+
        /**
         * Fetch the post reason for a given item array
         *
@@ -1541,7 +1581,25 @@ class Item
                        return;
                }
 
+               $languages = $item['language'] ? array_keys(json_decode($item['language'], true)) : [];
+               
                foreach (Tag::getUIDListByURIId($item['uri-id']) as $uid => $tags) {
+                       if (!empty($languages)) {
+                               $keep = false;
+                               $user_languages = User::getWantedLanguages($uid);
+                               foreach ($user_languages as $language) {
+                                       if (in_array($language, $languages)) {
+                                               $keep = true;
+                                       }
+                               }
+                               if ($keep) {
+                                       Logger::debug('Wanted languages found', ['uid' => $uid, 'user-languages' => $user_languages, 'item-languages' => $languages]);
+                               } else {
+                                       Logger::debug('No wanted languages found', ['uid' => $uid, 'user-languages' => $user_languages, 'item-languages' => $languages]);
+                                       continue;
+                               }
+                       }
+
                        $stored = self::storeForUserByUriId($item['uri-id'], $uid, ['post-reason' => self::PR_TAG]);
                        Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
                        foreach ($tags as $tag) {
@@ -2049,7 +2107,7 @@ class Item
                }
 
                if (empty($searchtext)) {
-                       return [];
+                       return ['un' => 1];
                }
 
                $ld = new Language(DI::l10n()->getDetectableLanguages());
@@ -2072,6 +2130,10 @@ class Item
                        }
                }
 
+               if (empty($result)) {
+                       return ['un' => 1];
+               }
+
                $result = self::compactLanguages($result);
 
                arsort($result);
@@ -2182,8 +2244,13 @@ class Item
                foreach (json_decode($item['language'], true) as $language => $reliability) {
                        $code = DI::l10n()->toISO6391($language);
 
-                       $native   = $iso639->nativeByCode1($code);
-                       $language = $iso639->languageByCode1($code);
+                       if ($code == 'un') {
+                               $native = $language = DI::l10n()->t('Undetermined');
+                       } else {
+                               $native   = $iso639->nativeByCode1($code);
+                               $language = $iso639->languageByCode1($code);
+                       }
+
                        if ($native != $language) {
                                $used_languages .= DI::l10n()->t('%s (%s - %s): %s', $native, $language, $code, number_format($reliability, 5)) . '\n';
                        } else {