]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #7139 from annando/ap-mail
authorHypolite Petovan <hypolite@mrpetovan.com>
Tue, 14 May 2019 18:38:42 +0000 (14:38 -0400)
committerGitHub <noreply@github.com>
Tue, 14 May 2019 18:38:42 +0000 (14:38 -0400)
Sending direct messages via AP does work now - receiving is pending

src/Protocol/ActivityPub.php
src/Protocol/ActivityPub/Transmitter.php
src/Util/JsonLD.php
src/Worker/APDelivery.php
src/Worker/Notifier.php

index 22073a50287cfa42a2bd4edce552b8e69dd60503..fa63c44d574bcb9b8f2f6af54707f3cd721f6229 100644 (file)
@@ -44,8 +44,10 @@ class ActivityPub
                ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
                'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
                'diaspora' => 'https://diasporafoundation.org/ns/',
+               'litepub' => 'http://litepub.social/ns#',
                'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
-               'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
+               'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
+               'directMessage' => 'litepub:directMessage']];
        const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
        /**
         * Checks if the web request is done for the AP protocol
index 0d024d26fc097fac9c2700a10c759e4a3704cc8f..1b418a7a54207e70afb8a7c722f3234660f1060c 100644 (file)
@@ -631,6 +631,100 @@ class Transmitter
                return $inboxes;
        }
 
+       /**
+        * Creates an array in the structure of the item table for a given mail id
+        *
+        * @param integer $mail_id
+        *
+        * @return array
+        * @throws \Exception
+        */
+       public static function ItemArrayFromMail($mail_id)
+       {
+               $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
+
+               $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+
+               $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
+               $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
+               $mail['allow_gid'] = '';
+               $mail['deny_cid'] = '';
+               $mail['deny_gid'] = '';
+               $mail['private'] = true;
+               $mail['deleted'] = false;
+               $mail['edited'] = $mail['created'];
+               $mail['plink'] = $mail['uri'];
+               $mail['thr-parent'] = $reply['uri'];
+               $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
+
+               $mail['event-type'] = '';
+               $mail['attach'] = '';
+
+               $mail['parent'] = 0;
+
+               return $mail;
+       }
+
+       /**
+        * Creates an activity array for a given mail id
+        *
+        * @param integer $mail_id
+        * @param boolean $object_mode Is the activity item is used inside another object?
+        *
+        * @return array of activity
+        * @throws \Exception
+        */
+       public static function createActivityFromMail($mail_id, $object_mode = false)
+       {
+               $mail = self::ItemArrayFromMail($mail_id);
+               $object = self::createNote($mail);
+
+               $object['to'] = $object['cc'];
+               unset($object['cc']);
+
+               $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => 'test']];
+
+               if (!$object_mode) {
+                       $data = ['@context' => ActivityPub::CONTEXT];
+               } else {
+                       $data = [];
+               }
+
+               $data['id'] = $mail['uri'] . '#Create';
+               $data['type'] = 'Create';
+               $data['actor'] = $mail['author-link'];
+               $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
+               $data['instrument'] = self::getService();
+               $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
+
+               if (empty($data['to']) && !empty($data['cc'])) {
+                       $data['to'] = $data['cc'];
+               }
+
+               if (empty($data['to']) && !empty($data['bcc'])) {
+                       $data['to'] = $data['bcc'];
+               }
+
+               unset($data['cc']);
+               unset($data['bcc']);
+
+               $object['to'] = $data['to'];
+               unset($object['cc']);
+               unset($object['bcc']);
+
+               $data['directMessage'] = true;
+
+               $data['object'] = $object;
+
+               $owner = User::getOwnerDataById($mail['uid']);
+
+               if (!$object_mode && !empty($owner)) {
+                       return LDSignature::sign($data, $owner);
+               } else {
+                       return $data;
+               }
+       }
+
        /**
         * Returns the activity type of a given item
         *
index 9566c424adf77937cd9658bd5407c5b9e7068eba..69973f4febf7d95757189a7e36b8a3ffa4c04b10 100644 (file)
@@ -96,7 +96,8 @@ class JsonLD
                        'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
                        'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
                        'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
-                       'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id']];
+                       'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
+                       'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id']];
 
                // Preparation for adding possibly missing content to the context
                if (!empty($json['@context']) && is_string($json['@context'])) {
index 2048b97d02259261d5dc71eabdafd9fc115c7035..25c1dfb71929a7269166b61c035ad05b248b0282 100644 (file)
@@ -30,6 +30,10 @@ class APDelivery extends BaseObject
                $success = true;
 
                if ($cmd == Delivery::MAIL) {
+                       $data = ActivityPub\Transmitter::createActivityFromMail($target_id);
+                       if (!empty($data)) {
+                               $success = HTTPSignature::transmit($data, $inbox, $uid);
+                       }
                } elseif ($cmd == Delivery::SUGGESTION) {
                        $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $target_id);
                } elseif ($cmd == Delivery::RELOCATION) {
index 1ce4ff244e4075f45a2f4c31d9a12a94db226f06..77aa4d564a5ed5a7520a1dbe1ba68e7453b4aae3 100644 (file)
@@ -76,6 +76,14 @@ class Notifier
                        }
                        $uid = $message['uid'];
                        $recipients[] = $message['contact-id'];
+
+                       $mail = ActivityPub\Transmitter::ItemArrayFromMail($target_id);
+                       $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($mail, §uid, true);
+                       foreach ($inboxes as $inbox) {
+                               Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'id' => $target_id, 'inbox' => $inbox]);
+                               Worker::add(['priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true],
+                                       'APDelivery', $cmd, $target_id, $inbox, $uid);
+                       }
                } elseif ($cmd == Delivery::SUGGESTION) {
                        $suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $target_id]);
                        if (!DBA::isResult($suggest)) {
@@ -593,7 +601,7 @@ class Notifier
 
                $inboxes = ActivityPub\Transmitter::fetchTargetInboxesforUser(0);
                foreach ($inboxes as $inbox) {
-                       Logger::log('Account removal for user ' . $self_user_id . ' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
+                       Logger::info('Account removal via ActivityPub', ['uid' => $self_user_id, 'inbox' => $inbox]);
                        Worker::add(['priority' => PRIORITY_NEGLIGIBLE, 'created' => $created, 'dont_fork' => true],
                                'APDelivery', Delivery::REMOVAL, '', $inbox, $self_user_id);
                }
@@ -642,7 +650,7 @@ class Notifier
                ActivityPub\Transmitter::createCachedActivityFromItem($target_item['id'], true);
 
                foreach ($inboxes as $inbox) {
-                       Logger::log('Deliver ' . $target_item['id'] .' to ' . $inbox .' via ActivityPub', Logger::DEBUG);
+                       Logger::info('Delivery via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]);
 
                        Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true],
                                        'APDelivery', $cmd, $target_item['id'], $inbox, $uid);