]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #11452 from atjn/manifest-icons
[friendica.git] / src / Model / Item.php
index 0d53049bc16aa70307ee57262c171151731256e4..34f7e0d8b8959e61e80d4d99def20eb1461e5a23 100644 (file)
@@ -869,7 +869,7 @@ class Item
                $item["contact-id"] = self::contactId($item);
 
                if (!empty($item['direction']) && in_array($item['direction'], [Conversation::PUSH, Conversation::RELAY]) &&
-                       self::isTooOld($item)) {
+                       empty($item['origin']) &&self::isTooOld($item)) {
                        Logger::info('Item is too old', ['item' => $item]);
                        return 0;
                }
@@ -1076,6 +1076,13 @@ class Item
                        unset($item['causer-id']);
                }
 
+               if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+                       $content_warning = BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB);
+                       if (!empty($content_warning) && empty($item['content-warning'])) {
+                               $item['content-warning'] = $content_warning;
+                       }
+               }
+
                Post::insert($item['uri-id'], $item);
 
                if ($item['gravity'] == GRAVITY_PARENT) {
@@ -1236,8 +1243,11 @@ class Item
                        return;
                }
 
+               $self_contact = Contact::selectFirst(['id'], ['uid' => $item['uid'], 'self' => true]);
+               $self = !empty($self_contact) ? $self_contact['id'] : 0;
+
                $cid = Contact::getIdForURL($author['url'], $item['uid']);
-               if (empty($cid) || !Contact::isSharing($cid, $item['uid'])) {
+               if (empty($cid) || (!Contact::isSharing($cid, $item['uid']) && ($cid != $self))) {
                        Logger::info('The resharer is not a following contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid'], 'cid' => $cid]);
                        return;
                }
@@ -1408,7 +1418,7 @@ class Item
                $is_reshare = ($item['gravity'] == GRAVITY_ACTIVITY) && ($item['verb'] == Activity::ANNOUNCE);
 
                if ((($item['gravity'] == GRAVITY_PARENT) || $is_reshare) &&
-                       DI::pConfig()->get($uid, 'system', 'accept_only_sharer') === self::COMPLETION_NONE &&
+                       DI::pConfig()->get($uid, 'system', 'accept_only_sharer') == self::COMPLETION_NONE &&
                        !Contact::isSharingByURL($item['author-link'], $uid) &&
                        !Contact::isSharingByURL($item['owner-link'], $uid)) {
                        Logger::info('Contact is not a follower, thread will not be stored', ['author' => $item['author-link'], 'uid' => $uid]);
@@ -1471,7 +1481,7 @@ class Item
                }
 
                // When the post belongs to a a forum then all forum users are allowed to access it
-               foreach (Tag::getByURIId($uriid, [Tag::EXCLUSIVE_MENTION]) as $tag) {
+               foreach (Tag::getByURIId($uriid, [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $tag) {
                        if (DBA::exists('contact', ['uid' => $uid, 'nurl' => Strings::normaliseLink($tag['url']), 'contact-type' => Contact::TYPE_COMMUNITY])) {
                                $target_uid = User::getIdForURL($tag['url']);
                                if (!empty($target_uid)) {
@@ -1741,7 +1751,7 @@ class Item
                // which point it will be automatically available through `getAvailableLanguages()` and this should be removed.
                $availableLanguages['fa'] = 'fa';
 
-               $ld = new Language($availableLanguages);
+               $ld = new Language(array_keys($availableLanguages));
                $languages = $ld->detect($naked_body)->limit(0, 3)->close();
                if (is_array($languages)) {
                        return json_encode($languages);
@@ -1763,7 +1773,10 @@ class Item
        }
 
        /**
-        * Creates an unique guid out of a given uri
+        * Creates an unique guid out of a given uri.
+        * This function is used for messages outside the fediverse (Connector posts, feeds, Mails, ...)
+        * Posts that are created on this system are using System::createUUID.
+        * Received ActivityPub posts are using Processor::getGUIDByURL.
         *
         * @param string $uri uri of an item entry
         * @param string $host hostname for the GUID prefix
@@ -1775,19 +1788,14 @@ class Item
                // We have to avoid that different routines could accidentally create the same value
                $parsed = parse_url($uri);
 
-               // We use a hash of the hostname as prefix for the guid
-               $guid_prefix = hash("crc32", $host);
-
                // Remove the scheme to make sure that "https" and "http" doesn't make a difference
                unset($parsed["scheme"]);
 
                // Glue it together to be able to make a hash from it
                $host_id = implode("/", $parsed);
 
-               // We could use any hash algorithm since it isn't a security issue
-               $host_hash = hash("ripemd128", $host_id);
-
-               return $guid_prefix.$host_hash;
+               // Use a mixture of several hashes to provide some GUID like experience
+               return hash("crc32", $host) . '-'. hash('joaat', $host_id) . '-'. hash('fnv164', $host_id);
        }
 
        /**
@@ -3183,6 +3191,12 @@ class Item
         */
        public static function getPlink($item)
        {
+               if (!empty($item['plink']) && Network::isValidHttpUrl($item['plink'])) {
+                       $plink = $item['plink'];
+               } elseif (!empty($item['uri']) && Network::isValidHttpUrl($item['uri']) && !Network::isLocalLink($item['uri'])) {
+                       $plink = $item['uri'];
+               }
+
                if (local_user()) {
                        $ret = [
                                'href' => "display/" . $item['guid'],
@@ -3191,14 +3205,14 @@ class Item
                                'orig_title' => DI::l10n()->t('View on separate page'),
                        ];
 
-                       if (!empty($item['plink'])) {
-                               $ret['href'] = DI::baseUrl()->remove($item['plink']);
+                       if (!empty($plink)) {
+                               $ret['href'] = DI::baseUrl()->remove($plink);
                                $ret['title'] = DI::l10n()->t('Link to source');
                        }
-               } elseif (!empty($item['plink']) && ($item['private'] != self::PRIVATE)) {
+               } elseif (!empty($plink) && ($item['private'] != self::PRIVATE)) {
                        $ret = [
-                               'href' => $item['plink'],
-                               'orig' => $item['plink'],
+                               'href' => $plink,
+                               'orig' => $plink,
                                'title' => DI::l10n()->t('Link to source'),
                                'orig_title' => DI::l10n()->t('Link to source'),
                        ];