]> git.mxchange.org Git - friendica.git/commitdiff
Issue 13535: Handle Firefish chat messages
authorMichael <heluecht@pirati.ca>
Sun, 15 Oct 2023 03:34:37 +0000 (03:34 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 15 Oct 2023 03:34:37 +0000 (03:34 +0000)
src/Protocol/ActivityPub/Processor.php
src/Protocol/ActivityPub/Receiver.php

index 081596bcdb5241aa7b4204dcca11a64d07fb5440..f8c2c4b82b075eccd929caedd4793f0145edd863 100644 (file)
@@ -1114,8 +1114,7 @@ class Processor
                                $item['contact-id'] = Contact::getIdForURL($activity['author']);
                        }
 
-                       if (!empty($activity['directmessage'])) {
-                               self::postMail($activity, $item);
+                       if (!empty($activity['directmessage']) && self::postMail($item)) {
                                continue;
                        }
 
@@ -1347,18 +1346,22 @@ class Processor
        /**
         * Creates an mail post
         *
-        * @param array $activity Activity data
-        * @param array $item     item array
+        * @param array $item item array
         * @return int|bool New mail table row id or false on error
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function postMail(array $activity, array $item)
+       private static function postMail(array $item): bool
        {
                if (($item['gravity'] != Item::GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
                        Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
                        return false;
                }
 
+               if (!Contact::isFollower($item['contact-id'], $item['uid']) && !Contact::isSharing($item['contact-id'], $item['uid'])) {
+                       Logger::info('Contact is not a sharer or follower, mail will be discarded.', ['item' => $item]);
+                       return false;
+               }
+
                Logger::info('Direct Message', $item);
 
                $msg = [];
index 7a07e1a7f2ab52f4e6739d06201c68ee8408ae47..4990c03cfd21d8be3abfc2b70bea839eeac5805c 100644 (file)
@@ -429,6 +429,10 @@ class Receiver
                                $object_data['directmessage'] = true;
                        } else {
                                $object_data['directmessage'] = JsonLD::fetchElement($activity, 'litepub:directMessage');
+
+                               if (!empty(JsonLD::fetchElement($activity['as:object'], 'misskey:_misskey_talk'))) {
+                                       $object_data = self::setChatData($object_data, $receivers);
+                               }
                        }
                } elseif (in_array($type, array_merge(self::ACTIVITY_TYPES, ['as:Announce', 'as:Follow'])) && in_array($object_type, self::CONTENT_TYPES)) {
                        // Create a mostly empty array out of the activity data (instead of the object).
@@ -507,6 +511,26 @@ class Receiver
                return $object_data;
        }
 
+       private static function setChatData(array $object_data, array $receivers): array
+       {
+               if (count($receivers) != 1) {
+                       return $object_data;
+               }
+
+               $user = User::getById(array_key_first($receivers), ['language']);
+               $l10n = DI::l10n()->withLang($user['language']);
+               $object_data['name'] = $l10n->t('Chat');
+
+               $mail = DBA::selectFirst('mail', ['uri'], ['uid' => array_key_first($receivers), 'title' => $object_data['name']], ['order' => ['id' => true]]);
+               if (!empty($mail['uri'])) {
+                       $object_data['reply-to-id'] = $mail['uri'];
+               }
+
+               $object_data['directmessage'] = true;
+               Logger::debug('Got Misskey Chat');
+               return $object_data;
+       }
+
        /**
         * Fetches the first user id from the receiver array
         *