]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Improved handling for undeterminded languages
[friendica.git] / src / Model / Item.php
index e54d5fb098e8a79f337814200cc728494f31282a..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
  *
@@ -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
         *
@@ -2067,7 +2107,7 @@ class Item
                }
 
                if (empty($searchtext)) {
-                       return [];
+                       return ['un' => 1];
                }
 
                $ld = new Language(DI::l10n()->getDetectableLanguages());
@@ -2090,6 +2130,10 @@ class Item
                        }
                }
 
+               if (empty($result)) {
+                       return ['un' => 1];
+               }
+
                $result = self::compactLanguages($result);
 
                arsort($result);
@@ -2200,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 {