]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Log function
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 7295408d83c7242822d3b48590202dd2427fa4eb..d9535f0b62287182a87362437d26a17e3e18a5fd 100644 (file)
@@ -6,6 +6,7 @@ namespace Friendica\Protocol\ActivityPub;
 
 use Friendica\BaseObject;
 use Friendica\Database\DBA;
+use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\Util\HTTPSignature;
 use Friendica\Core\Protocol;
@@ -33,15 +34,7 @@ require_once 'include/api.php';
  * @brief ActivityPub Transmitter Protocol class
  *
  * To-Do:
- *
- * Missing object types:
- * - Event
- *
- * Complicated object types:
  * - Undo Announce
- *
- * General:
- * - Queueing unsucessful deliveries
  */
 class Transmitter
 {
@@ -289,9 +282,8 @@ class Transmitter
 
                        foreach ($activity[$element] as $receiver) {
                                if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
-                                       $receiver = $item_profile['followers'];
-                               }
-                               if (!in_array($receiver, $exclude)) {
+                                       $permissions[$element][] = $item_profile['followers'];
+                               } elseif (!in_array($receiver, $exclude)) {
                                        $permissions[$element][] = $receiver;
                                }
                        }
@@ -303,10 +295,11 @@ class Transmitter
         * Creates an array of permissions from an item thread
         *
         * @param array $item
+        * @param boolean $blindcopy
         *
         * @return array with permission data
         */
-       private static function createPermissionBlockForItem($item)
+       private static function createPermissionBlockForItem($item, $blindcopy)
        {
                // Will be activated in a later step
                // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
@@ -314,15 +307,15 @@ class Transmitter
                // For now only send to these contacts:
                $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
 
-               $data = ['to' => [], 'cc' => []];
-
-               $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
+               $data = ['to' => [], 'cc' => [], 'bcc' => []];
 
                $actor_profile = APContact::getByURL($item['author-link']);
 
                $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
 
                if (!$item['private']) {
+                       $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
+
                        $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
                        if (!empty($actor_profile['followers'])) {
                                $data['cc'][] = $actor_profile['followers'];
@@ -352,6 +345,8 @@ class Transmitter
                        foreach ($receiver_list as $receiver) {
                                $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => $networks]);
                                if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
+                                       // BCC is currently deactivated, due to Pleroma and Mastodon not reacting like expected
+                                       // $data['bcc'][] = $profile['url'];
                                        $data['cc'][] = $profile['url'];
                                }
                        }
@@ -386,6 +381,7 @@ class Transmitter
 
                $data['to'] = array_unique($data['to']);
                $data['cc'] = array_unique($data['cc']);
+               $data['bcc'] = array_unique($data['bcc']);
 
                if (($key = array_search($item['author-link'], $data['to'])) !== false) {
                        unset($data['to'][$key]);
@@ -395,13 +391,33 @@ class Transmitter
                        unset($data['cc'][$key]);
                }
 
+               if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
+                       unset($data['bcc'][$key]);
+               }
+
                foreach ($data['to'] as $to) {
                        if (($key = array_search($to, $data['cc'])) !== false) {
                                unset($data['cc'][$key]);
                        }
+
+                       if (($key = array_search($to, $data['bcc'])) !== false) {
+                               unset($data['bcc'][$key]);
+                       }
                }
 
-               return ['to' => array_values($data['to']), 'cc' => array_values($data['cc'])];
+               foreach ($data['cc'] as $cc) {
+                       if (($key = array_search($cc, $data['bcc'])) !== false) {
+                               unset($data['bcc'][$key]);
+                       }
+               }
+
+               $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
+
+               if (!$blindcopy) {
+                       unset($receivers['bcc']);
+               }
+
+               return $receivers;
        }
 
        /**
@@ -456,7 +472,7 @@ class Transmitter
         */
        public static function fetchTargetInboxes($item, $uid, $personal = false)
        {
-               $permissions = self::createPermissionBlockForItem($item);
+               $permissions = self::createPermissionBlockForItem($item, true);
                if (empty($permissions)) {
                        return [];
                }
@@ -474,13 +490,15 @@ class Transmitter
                                continue;
                        }
 
+                       $blindcopy = in_array($element, ['bto', 'bcc']);
+
                        foreach ($permissions[$element] as $receiver) {
                                if ($receiver == $item_profile['followers']) {
                                        $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
                                } else {
                                        $profile = APContact::getByURL($receiver, false);
                                        if (!empty($profile)) {
-                                               if (empty($profile['sharedinbox']) || $personal) {
+                                               if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
                                                        $target = $profile['inbox'];
                                                } else {
                                                        $target = $profile['sharedinbox'];
@@ -532,15 +550,19 @@ class Transmitter
         * Creates the activity or fetches it from the cache
         *
         * @param integer $item_id
+        * @param boolean $force   Force new cache entry
         *
         * @return array with the activity
         */
-       public static function createCachedActivityFromItem($item_id)
+       public static function createCachedActivityFromItem($item_id, $force = false)
        {
                $cachekey = 'APDelivery:createActivity:' . $item_id;
-               $data = Cache::get($cachekey);
-               if (!is_null($data)) {
-                       return $data;
+
+               if (!$force) {
+                       $data = Cache::get($cachekey);
+                       if (!is_null($data)) {
+                               return $data;
+                       }
                }
 
                $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
@@ -596,7 +618,7 @@ class Transmitter
 
                $data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()];
 
-               $data = array_merge($data, self::createPermissionBlockForItem($item));
+               $data = array_merge($data, self::createPermissionBlockForItem($item, false));
 
                if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
                        $data['object'] = self::createNote($item);
@@ -606,6 +628,9 @@ class Transmitter
                        $data['object'] = self::createActivityFromItem($item_id, true);
                } else {
                        $data['diaspora:guid'] = $item['guid'];
+                       if (!empty($item['signed_text'])) {
+                               $data['diaspora:like'] = $item['signed_text'];
+                       }
                        $data['object'] = $item['thr-parent'];
                }
 
@@ -830,6 +855,32 @@ class Transmitter
                return DBA::exists('term', $condition);
        }
 
+       /**
+        * Creates event data
+        *
+        * @param array $item
+        *
+        * @return array with the event data
+        */
+       public static function createEvent($item)
+       {
+               $event = [];
+               $event['name'] = $item['event-summary'];
+               $event['content'] = BBCode::convert($item['event-desc'], false, 7);
+               $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
+
+               if (!$item['event-nofinish']) {
+                       $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
+               }
+
+               if (!empty($item['event-location'])) {
+                       $item['location'] = $item['event-location'];
+                       $event['location'] = self::createLocation($item);
+               }
+
+               return $event;
+       }
+
        /**
         * Creates a note/article object array
         *
@@ -839,7 +890,9 @@ class Transmitter
         */
        public static function createNote($item)
        {
-               if (!empty($item['title'])) {
+               if ($item['event-type'] == 'event') {
+                       $type = 'Event';
+               } elseif (!empty($item['title'])) {
                        $type = 'Article';
                } else {
                        $type = 'Note';
@@ -887,10 +940,15 @@ class Transmitter
                        $body = self::removePictures($body);
                }
 
-               $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
-               $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
+               if ($type == 'Event') {
+                       $data = array_merge($data, self::createEvent($item));
+               } else {
+                       $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
+                       $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
+
+                       $data['content'] = BBCode::convert($body, false, 7);
+               }
 
-               $data['content'] = BBCode::convert($body, false, 7);
                $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
 
                if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
@@ -900,7 +958,7 @@ class Transmitter
                $data['attachment'] = self::createAttachmentList($item, $type);
                $data['tag'] = self::createTagList($item);
 
-               if (!empty($item['coord']) || !empty($item['location'])) {
+               if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
                        $data['location'] = self::createLocation($item);
                }
 
@@ -908,7 +966,7 @@ class Transmitter
                        $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
                }
 
-               $data = array_merge($data, self::createPermissionBlockForItem($item));
+               $data = array_merge($data, self::createPermissionBlockForItem($item, false));
 
                return $data;
        }
@@ -936,6 +994,8 @@ class Transmitter
         * @param integer $uid User ID
         * @param string $inbox Target inbox
         * @param integer $suggestion_id Suggestion ID
+        *
+        * @return boolean was the transmission successful?
         */
        public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
        {
@@ -956,8 +1016,8 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
-               HTTPSignature::transmit($signed, $inbox, $uid);
+               Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
+               return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
        /**
@@ -965,6 +1025,8 @@ class Transmitter
         *
         * @param integer $uid User ID
         * @param string $inbox Target inbox
+        *
+        * @return boolean was the transmission successful?
         */
        public static function sendProfileDeletion($uid, $inbox)
        {
@@ -983,8 +1045,8 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
-               HTTPSignature::transmit($signed, $inbox, $uid);
+               Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
+               return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
        /**
@@ -992,6 +1054,8 @@ class Transmitter
         *
         * @param integer $uid User ID
         * @param string $inbox Target inbox
+        *
+        * @return boolean was the transmission successful?
         */
        public static function sendProfileUpdate($uid, $inbox)
        {
@@ -1010,8 +1074,8 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
-               HTTPSignature::transmit($signed, $inbox, $uid);
+               Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
+               return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
        /**
@@ -1035,7 +1099,7 @@ class Transmitter
                        'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
                        'to' => $profile['url']];
 
-               logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
+               Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
 
                $signed = LDSignature::sign($data, $owner);
                HTTPSignature::transmit($signed, $profile['inbox'], $uid);
@@ -1063,7 +1127,7 @@ class Transmitter
                        'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
                        'to' => $profile['url']];
 
-               logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+               Logger::log('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
 
                $signed = LDSignature::sign($data, $owner);
                HTTPSignature::transmit($signed, $profile['inbox'], $uid);
@@ -1091,7 +1155,7 @@ class Transmitter
                        'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
                        'to' => $profile['url']];
 
-               logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+               Logger::log('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
 
                $signed = LDSignature::sign($data, $owner);
                HTTPSignature::transmit($signed, $profile['inbox'], $uid);
@@ -1120,7 +1184,7 @@ class Transmitter
                        'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
                        'to' => $profile['url']];
 
-               logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+               Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
 
                $signed = LDSignature::sign($data, $owner);
                HTTPSignature::transmit($signed, $profile['inbox'], $uid);