]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Replace q() with DBA methods, fix code style
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index e1da961534dadc9a7b7a503c8c84cb0723742f3e..e70e6184776ccd64b2ca76a5fb626020c442e83e 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;
@@ -26,6 +27,7 @@ use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
 use Friendica\Core\Cache;
 use Friendica\Util\Map;
+use Friendica\Util\Network;
 
 require_once 'include/api.php';
 
@@ -33,11 +35,6 @@ require_once 'include/api.php';
  * @brief ActivityPub Transmitter Protocol class
  *
  * To-Do:
- *
- * Missing object types:
- * - Event
- *
- * Complicated object types:
  * - Undo Announce
  */
 class Transmitter
@@ -287,8 +284,7 @@ class Transmitter
                        foreach ($activity[$element] as $receiver) {
                                if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
                                        $permissions[$element][] = $item_profile['followers'];
-                               }
-                               if (!in_array($receiver, $exclude)) {
+                               } elseif (!in_array($receiver, $exclude)) {
                                        $permissions[$element][] = $receiver;
                                }
                        }
@@ -314,13 +310,13 @@ class Transmitter
 
                $data = ['to' => [], 'cc' => [], 'bcc' => []];
 
-               $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
-
                $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'];
@@ -451,6 +447,10 @@ class Transmitter
 
                $contacts = DBA::select('contact', ['url'], $condition);
                while ($contact = DBA::fetch($contacts)) {
+                       if (Network::isUrlBlocked($contact['url'])) {
+                               continue;
+                       }
+
                        $profile = APContact::getByURL($contact['url'], false);
                        if (!empty($profile)) {
                                if (empty($profile['sharedinbox']) || $personal) {
@@ -498,6 +498,10 @@ class Transmitter
                        $blindcopy = in_array($element, ['bto', 'bcc']);
 
                        foreach ($permissions[$element] as $receiver) {
+                               if (Network::isUrlBlocked($receiver)) {
+                                       continue;
+                               }
+
                                if ($receiver == $item_profile['followers']) {
                                        $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
                                } else {
@@ -555,15 +559,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);
@@ -629,10 +637,13 @@ 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'];
                }
 
-               $owner = User::getOwnerDataById($item['uid']);
+               $owner = User::getOwnerDataById($item['contact-uid']);
 
                if (!$object_mode) {
                        return LDSignature::sign($data, $owner);
@@ -853,6 +864,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
         *
@@ -862,7 +899,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';
@@ -910,10 +949,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'])) {
@@ -923,7 +967,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);
                }
 
@@ -965,11 +1009,10 @@ class Transmitter
        public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
        {
                $owner = User::getOwnerDataById($uid);
-               $profile = APContact::getByURL($owner['url']);
 
                $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
 
-               $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+               $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => System::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => 'Announce',
                        'actor' => $owner['url'],
@@ -981,7 +1024,35 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
+               Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+               return HTTPSignature::transmit($signed, $inbox, $uid);
+       }
+
+       /**
+        * Transmits a profile relocation to a given inbox
+        *
+        * @param integer $uid User ID
+        * @param string $inbox Target inbox
+        *
+        * @return boolean was the transmission successful?
+        */
+       public static function sendProfileRelocation($uid, $inbox)
+       {
+               $owner = User::getOwnerDataById($uid);
+
+               $data = ['@context' => ActivityPub::CONTEXT,
+                       'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+                       'type' => 'dfrn:relocate',
+                       'actor' => $owner['url'],
+                       'object' => $owner['url'],
+                       'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'to' => [ActivityPub::PUBLIC_COLLECTION],
+                       'cc' => []];
+
+               $signed = LDSignature::sign($data, $owner);
+
+               Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
                return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
@@ -996,9 +1067,8 @@ class Transmitter
        public static function sendProfileDeletion($uid, $inbox)
        {
                $owner = User::getOwnerDataById($uid);
-               $profile = APContact::getByURL($owner['url']);
 
-               $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+               $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => System::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => 'Delete',
                        'actor' => $owner['url'],
@@ -1010,7 +1080,7 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
+               Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
                return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
@@ -1027,7 +1097,7 @@ class Transmitter
                $owner = User::getOwnerDataById($uid);
                $profile = APContact::getByURL($owner['url']);
 
-               $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+               $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => System::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => 'Update',
                        'actor' => $owner['url'],
@@ -1039,7 +1109,7 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
+               Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
                return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
@@ -1056,7 +1126,7 @@ class Transmitter
 
                $owner = User::getOwnerDataById($uid);
 
-               $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+               $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => System::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => $activity,
                        'actor' => $owner['url'],
@@ -1064,7 +1134,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);
@@ -1082,7 +1152,7 @@ class Transmitter
                $profile = APContact::getByURL($target);
 
                $owner = User::getOwnerDataById($uid);
-               $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+               $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => System::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => 'Accept',
                        'actor' => $owner['url'],
@@ -1092,7 +1162,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);
@@ -1110,7 +1180,7 @@ class Transmitter
                $profile = APContact::getByURL($target);
 
                $owner = User::getOwnerDataById($uid);
-               $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+               $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => System::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => 'Reject',
                        'actor' => $owner['url'],
@@ -1120,7 +1190,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);
@@ -1139,7 +1209,7 @@ class Transmitter
                $id = System::baseUrl() . '/activity/' . System::createGUID();
 
                $owner = User::getOwnerDataById($uid);
-               $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+               $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => $id,
                        'type' => 'Undo',
                        'actor' => $owner['url'],
@@ -1149,7 +1219,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);