]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge remote-tracking branch 'upstream/2022.09-rc' into language
[friendica.git] / src / Model / Item.php
index c00bd06de4480364a7640854e96dc4dbf177010e..78bc8c64d18b26410117ba65d0546db74ebb32e8 100644 (file)
@@ -33,7 +33,6 @@ use Friendica\Model\Tag;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Model\Post;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
@@ -280,7 +279,7 @@ class Item
                        if ($item['uid'] == $uid) {
                                self::markForDeletionById($item['id'], PRIORITY_HIGH);
                        } elseif ($item['uid'] != 0) {
-                               Logger::notice('Wrong ownership. Not deleting item', ['id' => $item['id']]);
+                               Logger::warning('Wrong ownership. Not deleting item', ['id' => $item['id']]);
                        }
                }
                DBA::close($items);
@@ -581,7 +580,7 @@ class Item
                if (!empty($item['uid'])) {
                        $owner = User::getOwnerDataById($item['uid'], false);
                        if (!$owner) {
-                               Logger::notice('Missing item user owner data', ['uid' => $item['uid']]);
+                               Logger::warning('Missing item user owner data', ['uid' => $item['uid']]);
                                return false;
                        }
 
@@ -920,8 +919,6 @@ class Item
 
                $item['gravity'] = self::getGravity($item);
 
-               $item['language'] = self::getLanguage($item);
-
                $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
                        'photo' => $item['author-avatar'], 'network' => $item['network']];
                $item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, null, $default);
@@ -1109,6 +1106,8 @@ class Item
                // Check for hashtags in the body and repair or add hashtag links
                $item['body'] = self::setHashtags($item['body']);
 
+               $item['language'] = self::getLanguage($item);
+
                $notify_type = Delivery::POST;
 
                // Filling item related side tables
@@ -1649,13 +1648,13 @@ class Item
                $post = Post::selectFirst(['id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
                if (!empty($post['id'])) {
                        if (!empty($item['event-id'])) {
-                               $post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
-                               if (!empty($post['event-id'])) {
+                               $event_post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
+                               if (!empty($event_post['event-id'])) {
                                        $event = DBA::selectFirst('event', ['edited', 'start', 'finish', 'summary', 'desc', 'location', 'nofinish', 'adjust'], ['id' => $item['event-id']]);
                                        if (!empty($event)) {
                                                // We aren't using "Event::store" here, since we don't want to trigger any further action
-                                               $ret = DBA::update('event', $event, ['id' => $post['event-id']]);
-                                               Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $post['event-id'], 'ret' => $ret]);
+                                               $ret = DBA::update('event', $event, ['id' => $event_post['event-id']]);
+                                               Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $event_post['event-id'], 'ret' => $ret]);
                                        }
                                }
                        }
@@ -1870,6 +1869,8 @@ class Item
                        return '';
                }
 
+               $naked_body = self::getDominantLanguage($naked_body);
+
                $availableLanguages = DI::l10n()->getAvailableLanguages();
                // See https://github.com/friendica/friendica/issues/10511
                // Persian is manually added to language detection until a persian translation is provided for the interface, at
@@ -1885,6 +1886,33 @@ class Item
                return '';
        }
 
+       /**
+        * Check if latin or non latin are dominant in the body and only return the dominant one
+        *
+        * @param string $body
+        * @return string
+        */
+       private static function getDominantLanguage(string $body): string
+       {
+               $latin = '';
+               $non_latin = '';
+               for ($i = 0; $i < mb_strlen($body); $i++) { 
+                       $character = mb_substr($body, $i, 1);
+                       $ord = mb_ord($character);
+
+                       // We add the most common characters to both strings.
+                       if (($ord <= 64) || ($ord >= 91 && $ord <= 96) || ($ord >= 123 && $ord <= 191) || in_array($ord, [215, 247]) || ($ord >= 697 && $ord <= 735) || ($ord > 65535)) {
+                               $latin .= $character;
+                               $non_latin .= $character;
+                       } elseif ($ord < 768) {
+                               $latin .= $character;
+                       } else {
+                               $non_latin .= $character;
+                       }
+               }
+               return (mb_strlen($latin) > mb_strlen($non_latin)) ? $latin : $non_latin;
+       }
+
        public static function getLanguageMessage(array $item): string
        {
                $iso639 = new \Matriphe\ISO639\ISO639;
@@ -2516,7 +2544,7 @@ class Item
 
                $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
-                       Logger::notice('like: unknown item', ['id' => $item_id]);
+                       Logger::warning('Post had not been fetched', ['id' => $item_id]);
                        return false;
                }
 
@@ -2545,7 +2573,7 @@ class Item
                }
 
                // Retrieve the current logged in user's public contact
-               $author_id = Contact::getIdForURL($owner['url']);
+               $author_id = Contact::getPublicIdByUserId($uid);
                if (empty($author_id)) {
                        Logger::info('Empty public contact');
                        return false;
@@ -2582,7 +2610,7 @@ class Item
                                $activity = Activity::ANNOUNCE;
                                break;
                        default:
-                               Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
+                               Logger::warning('unknown verb', ['verb' => $verb, 'item' => $item_id]);
                                return false;
                }
 
@@ -2652,7 +2680,7 @@ class Item
                $new_item = [
                        'guid'          => System::createUUID(),
                        'uri'           => self::newURI(),
-                       'uid'           => $item['uid'],
+                       'uid'           => $uid,
                        'contact-id'    => $owner['id'],
                        'wall'          => $item['wall'],
                        'origin'        => 1,
@@ -2675,22 +2703,20 @@ class Item
                        'unseen'        => 1,
                ];
 
-               $signed = Diaspora::createLikeSignature($uid, $new_item);
-               if (!empty($signed)) {
-                       $new_item['diaspora_signed_text'] = json_encode($signed);
+               if (in_array($activity, [Activity::LIKE, Activity::DISLIKE])) {
+                       $signed = Diaspora::createLikeSignature($uid, $new_item);
+                       if (!empty($signed)) {
+                               $new_item['diaspora_signed_text'] = json_encode($signed);
+                       }
                }
 
-               $new_item_id = self::insert($new_item);
+               self::insert($new_item, true);
 
                // If the parent item isn't visible then set it to visible
+               // @todo Check if this is still needed
                if (!$item['visible']) {
                        self::update(['visible' => true], ['id' => $item['id']]);
                }
-
-               $new_item['id'] = $new_item_id;
-
-               Hook::callAll('post_local_end', $new_item);
-
                return true;
        }