]> git.mxchange.org Git - friendica.git/commitdiff
The "share" handling is improved
authorMichael <heluecht@pirati.ca>
Tue, 25 Oct 2022 06:37:23 +0000 (06:37 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 25 Oct 2022 06:37:23 +0000 (06:37 +0000)
14 files changed:
mod/display.php
mod/share.php
src/Content/Item.php
src/Content/Text/BBCode.php
src/Factory/Api/Mastodon/Status.php
src/Factory/Api/Twitter/Status.php
src/Model/Contact.php
src/Model/Item.php
src/Model/Post/Media.php
src/Model/Tag.php
src/Protocol/ActivityPub/Transmitter.php
src/Protocol/Diaspora.php
src/Protocol/Feed.php
src/Worker/Delivery.php

index 1a024c76853bccad3e24053bfe26348197f1901d..fd508fcfc217cc08e2673e0e9d76abb285793d5d 100644 (file)
@@ -123,11 +123,9 @@ function display_init(App $a)
 
 function display_fetchauthor($item)
 {
-       if (Diaspora::isReshare($item['body'], true)) {
-               $shared = Item::getShareArray($item);
-               if (!empty($shared['profile'])) {
-                       $contact = Contact::getByURLForUser($shared['profile'], Session::getLocalUser());
-               }
+       $shared = Item::getShareArray($item);
+       if (empty($shared['comment']) && !empty($shared['guid']) && !empty($shared['profile'])) {
+               $contact = Contact::getByURLForUser($shared['profile'], Session::getLocalUser());
        }
 
        if (empty($contact)) {
index 1ebce5291f6d5cd9aca61de6784337b3eed2e1bf..237da9553a96d6ff91f5947c8ef504f4dce73af1 100644 (file)
@@ -42,7 +42,7 @@ function share_init(App $a) {
                System::exit();
        }
 
-       $shared = BBCode::fetchShareAttributes($item['body']);
+       $shared = Item::getShareArray($item);
        if (empty($shared['comment']) && (!empty($shared['message_id']) || !empty($shared['link']))) {
                $content = '[share]' . ($shared['message_id'] ?: $shared['link']) . '[/share]';
        } else {
index 521c01546866dafb675ed952d528f18964ac29fe..2f780ec4e67c382115f30cf23227734d42398ac7 100644 (file)
@@ -677,10 +677,10 @@ class Item
                        $shared_content .= '[h3]' . $item['title'] . "[/h3]\n";
                }
 
-               $shared = BBCode::fetchShareAttributes($item['body']);
+               $shared = ItemModel::getShareArray($item);
 
                // If it is a reshared post then reformat it to avoid display problems with two share elements
-               if (Diaspora::isReshare($item['body'], false)) {
+               if (!empty($shared)) {
                        if (!empty($shared['guid']) && ($encaspulated_share = self::createSharedPostByGuid($shared['guid'], 0, '', $add_media))) {
                                $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
                        }
index d03161df9f9e8a8c0e49579a0e846654f096555d..7394f5fd7b0d435b85648c50e998253f02fc2193 100644 (file)
@@ -1023,6 +1023,7 @@ class BBCode
        {
                DI::profiler()->startRecording('rendering');
                if (preg_match('~(.*?)\[share](.*)\[/share]~ism', $text, $matches)) {
+                       DI::profiler()->stopRecording();
                        return [
                                'author'     => '',
                                'profile'    => '',
@@ -1093,12 +1094,7 @@ class BBCode
         */
        public static function removeSharedData(string $body): string
        {
-               return BBCode::convertShare(
-                       $body,
-                       function (array $attributes) {
-                               return '';
-                       }
-               );
+               return preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
        }
 
        /**
index 21ecee0f95608853d6654d29784cc36f49ff9dbb..4e1f790f500e109a20ec91100ea275ca364d0c5e 100644 (file)
@@ -155,7 +155,7 @@ class Status extends BaseFactory
                        $poll = null;
                }
 
-               $shared = BBCode::fetchShareAttributes($item['body']);
+               $shared = Item::getShareArray($item);
                if (!empty($shared['guid'])) {
                        $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
 
index a3ced8e58c91fae250349b8aa30d8134dcaea8bc..ac03172e89fe3031698baedd267537693345765d 100644 (file)
@@ -178,7 +178,7 @@ class Status extends BaseFactory
 
                $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid);
 
-               $shared = BBCode::fetchShareAttributes($item['body']);
+               $shared = Item::getShareArray($item);
                if (!empty($shared['guid'])) {
                        $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
 
index d8a5b387fd6b11f247daa1dc77ace124ef283b4a..143b3fcd4d1041d87da011b1c49028bb80a42e30 100644 (file)
@@ -97,11 +97,11 @@ class Contact
         * Relationship types
         * @{
         */
-       const NOTHING  = 0;
-       const FOLLOWER = 1;
-       const SHARING  = 2;
-       const FRIEND   = 3;
-       const SELF     = 4;
+       const NOTHING  = 0; // There is no relationship between the contact and the user
+       const FOLLOWER = 1; // The contact is following this user
+       const SHARING  = 2; // The contact shares their content with this user
+       const FRIEND   = 3; // There is a mutual relationship between the contact and the user
+       const SELF     = 4; // This is the user theirself
        /**
         * @}
         */
index e53d64d5e1985ccf81eeb22d875dbc1852d3367d..563356313aeafc3ecf3cc965143066c42285a321 100644 (file)
@@ -113,7 +113,7 @@ class Item
                        'private', 'title', 'body', 'raw-body', 'location', 'coord', 'app',
                        'inform', 'deleted', 'extid', 'post-type', 'post-reason', 'gravity',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
-                       'author-id', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
+                       'author-id', 'author-addr', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
                        'signed_text', 'network', 'wall', 'contact-id', 'plink', 'origin',
                        'thr-parent-id', 'parent-uri-id', 'quote-uri', 'quote-uri-id', 'postopts', 'pubmail',
                        'event-created', 'event-edited', 'event-start', 'event-finish',
@@ -2950,7 +2950,7 @@ class Item
                $item['mentions'] = $tags['mentions'];
 
                $body = $item['body'] ?? '';
-               $shared = BBCode::fetchShareAttributes($body);
+               $shared = self::getShareArray($item);
                if (!empty($shared['guid'])) {
                        $shared_item = Post::selectFirst(['uri-id', 'guid', 'plink', 'has-media'], ['guid' => $shared['guid'], 'uid' => [$item['uid'], 0]]);
                }
@@ -3043,7 +3043,7 @@ class Item
                        $s = self::addVisualAttachments($shared_attachments, $item, $s, true);
                        $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $shared_attachments, $body, $s, true, []);
                        $s = self::addNonVisualAttachments($shared_attachments, $item, $s, true);
-                       $body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
+                       $body = BBCode::removeSharedData($body);
                }
 
                $s = self::addVisualAttachments($attachments, $item, $s, false);
@@ -3601,7 +3601,29 @@ class Item
         */
        public static function getShareArray(array $item): array
        {
-               return BBCode::fetchShareAttributes($item['body']);
+               $attributes = BBCode::fetchShareAttributes($item['body'] ?? '');
+               if (!empty($attributes)) {
+                       return $attributes;
+               }
+
+               if (!empty($item['quote-uri-id'])) {
+                       $shared = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'uri', 'body'], ['uri-id' => $item['quote-uri-id']]);
+                       if (!empty($shared)) {
+                               return [
+                                       'author'     => $shared['author-name'],
+                                       'profile'    => $shared['author-link'],
+                                       'avatar'     => $shared['author-avatar'],
+                                       'link'       => $shared['plink'],
+                                       'posted'     => $shared['created'],
+                                       'guid'       => $shared['guid'],
+                                       'message_id' => $shared['uri'],
+                                       'comment'    => $item['body'],
+                                       'shared'     => $shared['body'],
+                               ];                              
+                       }
+               }
+
+               return [];
        }
 
        /**
index 6d0707743eed1876d09a2f9bf9b76b0e92dfe9ee..8d86bc757cba596d8688ca5c93809edc688f6bad 100644 (file)
@@ -406,7 +406,7 @@ class Media
                // Only remove the shared data from "real" reshares
                $shared = BBCode::fetchShareAttributes($body);
                if (!empty($shared['guid'])) {
-                       $unshared_body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
+                       $unshared_body = BBCode::removeSharedData($body);
                }
 
                $attachments = [];
@@ -489,7 +489,7 @@ class Media
                $shared = BBCode::fetchShareAttributes($body);
                if (!empty($shared['guid'])) {
                        // Don't look at the shared content
-                       $body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
+                       $body = BBCode::removeSharedData($body);
                }
 
                // Remove all hashtags and mentions
@@ -522,7 +522,7 @@ class Media
        public static function insertFromAttachmentData(int $uriid, string $body)
        {
                // Don't look at the shared content
-               $body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
+               $body = BBCode::removeSharedData($body);
 
                $data = BBCode::getAttachmentData($body);
                if (empty($data))  {
index 435dfda7cbf2711c3775e83321a4e474b92c177f..6611245a0c69ff3268ddb2740ceeef39eb2b58e8 100644 (file)
@@ -311,10 +311,8 @@ class Tag
                // Only remove the shared data from "real" reshares
                $shared = BBCode::fetchShareAttributes($body);
                if (!empty($shared['guid'])) {
-                       if (preg_match("/\s*\[share .*?\](.*?)\[\/share\]\s*/ism",  $body, $matches)) {
-                               $share_body = $matches[1];
-                       }
-                       $body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
+                       $share_body = $shared['shared'];
+                       $body = BBCode::removeSharedData($body);
                }
 
                foreach (self::getFromBody($body, $tags) as $tag) {
index 8b07d12e7d2a1c9fca5a6d7834ab183845a099ea..cc7774b23a9f1ff67e3febec29159f18cad940d5 100644 (file)
@@ -1664,17 +1664,18 @@ class Transmitter
                                $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
                        }
 
-                       $body = BBCode::setMentionsToNicknames($body);
+                       $body   = BBCode::setMentionsToNicknames($body);
+                       $shared = BBCode::fetchShareAttributes($body);
 
                        if (!empty($item['quote-uri']) && Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
                                $real_quote = true;
-                               if (Diaspora::isReshare($body, false)) {
+                               if (!empty($shared['link'])) {
                                        $body = BBCode::replaceSharedData($body);
                                } elseif (strpos($body, $item['quote-uri']) === false) {
                                        $body .= "\n♲ " . $item['quote-uri'];
                                }
                                $data['quoteUrl'] = $item['quote-uri'];
-                       } elseif (!empty($item['quote-uri']) && !Diaspora::isReshare($body, false)) {
+                       } elseif (!empty($item['quote-uri']) && empty($shared)) {
                                $body .= "\n" . DI::contentItem()->createSharedPostByUriId($item['quote-uri-id'], $item['uid'], true);
                                $item['body'] = Item::improveSharedDataInBody($item, true);
                        }
@@ -1691,8 +1692,10 @@ class Transmitter
 
                        if ($real_quote) {
                                $shared = BBCode::fetchShareAttributes($richbody);
-                               if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) {
+                               if (!empty($shared['link'])) {
                                        $richbody = BBCode::replaceSharedData($richbody);
+                               } elseif (strpos($richbody, $item['quote-uri']) === false) {
+                                       $richbody .= "\n♲ " . $item['quote-uri'];
                                }
                        }
 
index ee8edf0a04092455306d4119a921fd5c9770449f..b3108c8ff321abdf2c868cf2821f07925f767d96 100644 (file)
@@ -776,36 +776,6 @@ class Diaspora
                return '';
        }
 
-       /**
-        * get a handle (user@domain.tld) from a given contact id
-        *
-        * @param int $contact_id  The id in the contact table
-        * @param int $pcontact_id The id in the contact table (Used for the public contact)
-        *
-        * @return string the handle
-        * @throws \Exception
-        */
-       private static function handleFromContact(int $contact_id, int $pcontact_id = 0): string
-       {
-               $handle = '';
-
-               if ($pcontact_id != 0) {
-                       $contact = Contact::getById($pcontact_id, ['addr']);
-                       if (DBA::isResult($contact)) {
-                               $handle = $contact['addr'];
-                       }
-               }
-
-               if (empty($handle)) {
-                       $contact = Contact::getById($contact_id, ['addr']);
-                       if (DBA::isResult($contact)) {
-                               $handle = $contact['addr'];
-                       }
-               }
-
-               return strtolower($handle);
-       }
-
        /**
         * Get a contact id for a given handle
         *
@@ -3203,59 +3173,35 @@ class Diaspora
        /**
         * Checks a message body if it is a reshare
         *
-        * @param string $body     The message body that is to be check
-        * @param bool   $complete Should it be a complete check or a simple check?
+        * @param array $item The message body that is to be check
         *
-        * @return array|bool Reshare details or "false" if no reshare
+        * @return array Reshare details or "false" if no reshare
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function isReshare(string $body, bool $complete = true)
+       public static function isReshare(array $item): array
        {
-               $body = trim($body);
-
-               $reshared = Item::getShareArray(['body' => $body]);
+               $reshared = Item::getShareArray($item);
                if (empty($reshared)) {
-                       return false;
-               }
-
-               // Skip if it isn't a pure repeated messages
-               // Does it start with a share?
-               if (!empty($reshared['comment']) && $complete) {
-                       return false;
-               }
-
-               if (!empty($reshared['guid']) && $complete) {
-                       $condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
-                       $item = Post::selectFirst(['contact-id'], $condition);
-                       if (DBA::isResult($item)) {
-                               $ret = [];
-                               $ret['root_handle'] = self::handleFromContact($item['contact-id']);
-                               $ret['root_guid'] = $reshared['guid'];
-                               return $ret;
-                       } elseif ($complete) {
-                               // We are resharing something that isn't a DFRN or Diaspora post.
-                               // So we have to return "false" on "$complete" to not trigger a reshare.
-                               return false;
-                       }
-               } elseif (empty($reshared['guid']) && $complete) {
-                       return false;
+                       return [];
                }
 
-               $ret = [];
-
-               if (!empty($reshared['profile']) && ($cid = Contact::getIdForURL($reshared['profile']))) {
-                       $contact = DBA::selectFirst('contact', ['addr'], ['id' => $cid]);
-                       if (!empty($contact['addr'])) {
-                               $ret['root_handle'] = $contact['addr'];
-                       }
+               // Skip if it isn't a pure repeated messages or not a real reshare
+               if (!empty($reshared['comment']) || empty($reshared['guid'])) {
+                       return [];
                }
 
-               if (empty($ret) && !$complete) {
-                       return true;
+               $condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
+               $item = Post::selectFirst(['author-addr'], $condition);
+               if (DBA::isResult($item)) {
+                       return [
+                               'root_handle' => strtolower($item['author-addr']),
+                               'root_guid'   => $reshared['guid']
+                       ];
                }
 
-               return $ret;
+               // We are resharing something that isn't a DFRN or Diaspora post.
+               return [];
        }
 
        /**
@@ -3352,7 +3298,7 @@ class Diaspora
                $edited = DateTimeFormat::utc($item['edited'] ?? $item['created'], DateTimeFormat::ATOM);
 
                // Detect a share element and do a reshare
-               if (($item['private'] != Item::PRIVATE) && ($ret = self::isReshare($item['body']))) {
+               if (($item['private'] != Item::PRIVATE) && ($ret = self::isReshare($item))) {
                        $message = [
                                'author'                => $myaddr,
                                'guid'                  => $item['guid'],
@@ -3725,7 +3671,7 @@ class Diaspora
         */
        public static function sendRetraction(array $item, array $owner, array $contact, bool $public_batch = false, bool $relay = false): int
        {
-               $itemaddr = self::handleFromContact($item['contact-id'], $item['author-id']);
+               $itemaddr = strtolower($item['author-addr']);
 
                $msg_type = 'retraction';
 
index e5f205aa5a90712c7e439c8dd25fc4473b4a844a..7c6a68b60187907828d914fade202efe8054a41e 100644 (file)
@@ -438,7 +438,7 @@ class Feed
                                }
 
                                if (!empty($href)) {
-                                       $attachment = ['type' => Post\Media::UNKNOWN, 'url' => $href, 'mimetype' => $type, 'size' => $length];
+                                       $attachment = ['uri-id' => -1, 'type' => Post\Media::UNKNOWN, 'url' => $href, 'mimetype' => $type, 'size' => $length];
 
                                        $attachment = Post\Media::fetchAdditionalData($attachment);
 
index fa0121c8314fbd5e20d5aa18f36db44b93a0d9ab..f51f5f205bd10ef808d69fbbe29d1f070a9a4cc2 100644 (file)
@@ -274,7 +274,7 @@ class Delivery
        private static function deliverDFRN(string $cmd, array $contact, array $owner, array $items, array $target_item, bool $public_message, bool $top_level, bool $followup, int $server_protocol = null)
        {
                // Transmit Diaspora reshares via Diaspora if the Friendica contact support Diaspora
-               if (Diaspora::isReshare($target_item['body'] ?? '') && !empty(FContact::getByURL($contact['addr'], false))) {
+               if (Diaspora::isReshare($target_item ?? []) && !empty(FContact::getByURL($contact['addr'], false))) {
                        Logger::info('Reshare will be transmitted via Diaspora', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
                        self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
                        return;