]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Initialize $permissions variable in ActivityPub\Transmitter::fetchPermissionBlockFrom...
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 6c78db302c4dedb975a54e2cb47756c2dfed7966..9b5d05213888918aff1ccb3c3970080292b3f9bc 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Protocol\ActivityPub;
 
 use Friendica\Content\Feature;
 use Friendica\Content\Text\BBCode;
-use Friendica\Core\Cache\Duration;
+use Friendica\Core\Cache\Enum\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
@@ -115,7 +115,7 @@ class Transmitter
                $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
                $success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id);
                if ($success) {
-                       DBA::update('contact', ['rel' => Contact::FRIEND], ['id' => $contact['id']]);
+                       Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
                }
 
                return $success;
@@ -137,7 +137,7 @@ class Transmitter
 
                $success = self::sendContactUndo($url, $contact['id'], 0);
                if ($success || $force) {
-                       DBA::update('contact', ['rel' => Contact::NOTHING], ['id' => $contact['id']]);
+                       Contact::update(['rel' => Contact::NOTHING], ['id' => $contact['id']]);
                }
 
                return $success;
@@ -166,21 +166,17 @@ class Transmitter
                        'pending' => false,
                        'blocked' => false,
                ];
-               $condition = DBA::buildCondition($parameters);
 
-               $sql = "SELECT COUNT(*) as `count`
-                       FROM `contact`
-                       JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
-                       " . $condition;
+               $condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
 
-               $contacts = DBA::fetchFirst($sql, ...$parameters);
+               $total = DBA::count('contact', $condition);
 
                $modulePath = '/' . $module . '/';
 
                $data = ['@context' => ActivityPub::CONTEXT];
                $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
                $data['type'] = 'OrderedCollection';
-               $data['totalItems'] = $contacts['count'];
+               $data['totalItems'] = $total;
 
                // When we hide our friends we will only show the pure number but don't allow more.
                $profile = Profile::getByUID($owner['uid']);
@@ -194,16 +190,7 @@ class Transmitter
                        $data['type'] = 'OrderedCollectionPage';
                        $list = [];
 
-                       $sql = "SELECT `contact`.`url`
-                               FROM `contact`
-                               JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
-                               " . $condition . "
-                               LIMIT ?, ?";
-
-                       $parameters[] = ($page - 1) * 100;
-                       $parameters[] = 100;
-
-                       $contacts = DBA::p($sql, ...$parameters);
+                       $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
                        while ($contact = DBA::fetch($contacts)) {
                                $list[] = $contact['url'];
                        }
@@ -242,7 +229,7 @@ class Transmitter
                                $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']);
                                if (!empty($permissionSets)) {
                                        $condition = ['psid' => array_merge($permissionSets->column('id'),
-                                                       [DI::permissionSet()->getIdFromACL($owner['uid'], '', '', '', '')])];
+                                                       [DI::permissionSet()->selectPublicForUser($owner['uid'])])];
                                }
                        }
                }
@@ -352,14 +339,25 @@ class Transmitter
                        $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
                }
 
+               if (!empty($owner['xmpp']) || !empty($owner['matrix'])) {
+                       $data['vcard:hasInstantMessage'] = [];
+
+                       if (!empty($owner['xmpp'])) {
+                               $data['vcard:hasInstantMessage'][] = 'xmpp:' . $owner['xmpp'];
+                       }
+                       if (!empty($owner['matrix'])) {
+                               $data['vcard:hasInstantMessage'][] = 'matrix:' . $owner['matrix'];
+                       }
+               }
+
                $data['url'] = $owner['url'];
                $data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
-               $data['discoverable'] = $owner['net-publish'];
+               $data['discoverable'] = (bool)$owner['net-publish'];
                $data['publicKey'] = ['id' => $owner['url'] . '#main-key',
                        'owner' => $owner['url'],
                        'publicKeyPem' => $owner['pubkey']];
                $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
-               $data['icon'] = ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($owner['id'], '', $owner['updated'])];
+               $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
 
                $resourceid = Photo::ridFromURI($owner['photo']);
                if (!empty($resourceid)) {
@@ -381,6 +379,20 @@ class Transmitter
                        }
                }
 
+               $custom_fields = [];
+
+               foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
+                       $custom_fields[] = [
+                               'type' => 'PropertyValue',
+                               'name' => $profile_field->label,
+                               'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value)
+                       ];
+               };
+
+               if (!empty($custom_fields)) {
+                       $data['attachment'] = $custom_fields;
+               }
+
                $data['generator'] = self::getService();
 
                // tags: https://kitty.town/@inmysocks/100656097926961126.json
@@ -425,6 +437,13 @@ class Transmitter
                        return [];
                }
 
+               $permissions = [
+                       'to' => [],
+                       'cc' => [],
+                       'bto' => [],
+                       'bcc' => [],
+               ];
+
                $activity = json_decode($conversation['source'], true);
 
                $actor = JsonLD::fetchElement($activity, 'actor', 'id');
@@ -866,6 +885,9 @@ class Transmitter
                }
 
                $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+               if (!DBA::isResult($reply)) {
+                       $reply = $mail;
+               }
 
                // Making the post more compatible for Mastodon by:
                // - Making it a note and not an article (no title)
@@ -1448,10 +1470,10 @@ class Transmitter
                $event = [];
                $event['name'] = $item['event-summary'];
                $event['content'] = BBCode::convertForUriId($item['uri-id'], $item['event-desc'], BBCode::ACTIVITYPUB);
-               $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
+               $event['startTime'] = DateTimeFormat::utc($item['event-start'], 'c');
 
                if (!$item['event-nofinish']) {
-                       $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
+                       $event['endTime'] = DateTimeFormat::utc($item['event-finish'], 'c');
                }
 
                if (!empty($item['event-location'])) {
@@ -1459,7 +1481,8 @@ class Transmitter
                        $event['location'] = self::createLocation($item);
                }
 
-               $event['dfrn:adjust'] = (bool)$item['event-adjust'];
+               // 2021.12: Backward compatibility value, all the events now "adjust" to the viewer timezone
+               $event['dfrn:adjust'] = true;
 
                return $event;
        }
@@ -1766,7 +1789,7 @@ class Transmitter
        {
                $owner = User::getOwnerDataById($uid);
 
-               $suggestion = DI::fsuggest()->getById($suggestion_id);
+               $suggestion = DI::fsuggest()->selectOneById($suggestion_id);
 
                $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
@@ -1780,7 +1803,7 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+               Logger::info('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
                return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
@@ -1809,7 +1832,7 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+               Logger::info('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
                return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
@@ -1848,7 +1871,7 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+               Logger::info('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
                return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
@@ -1880,7 +1903,7 @@ class Transmitter
 
                $signed = LDSignature::sign($data, $owner);
 
-               Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
+               Logger::info('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub');
                return HTTPSignature::transmit($signed, $inbox, $uid);
        }
 
@@ -1917,7 +1940,7 @@ class Transmitter
                        'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
-               Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
+               Logger::info('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid);
 
                $signed = LDSignature::sign($data, $owner);
                return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
@@ -1956,7 +1979,7 @@ class Transmitter
                $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
                        'author-id' => Contact::getPublicIdByUserId($uid)];
                if (Post::exists($condition)) {
-                       Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
+                       Logger::info('Follow for ' . $object . ' for user ' . $uid . ' does already exist.');
                        return false;
                }
 
@@ -1970,7 +1993,7 @@ class Transmitter
                        'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
-               Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
+               Logger::info('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid);
 
                $signed = LDSignature::sign($data, $owner);
                return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
@@ -2019,15 +2042,16 @@ class Transmitter
         * @param string  $target Target profile
         * @param         $id
         * @param integer $uid    User ID
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @return bool Operation success
+        * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function sendContactReject($target, $id, $uid)
+       public static function sendContactReject($target, $id, $uid): bool
        {
                $profile = APContact::getByURL($target);
                if (empty($profile['inbox'])) {
                        Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
-                       return;
+                       return false;
                }
 
                $owner = User::getOwnerDataById($uid);
@@ -2047,7 +2071,7 @@ class Transmitter
                Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
 
                $signed = LDSignature::sign($data, $owner);
-               HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+               return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
        }
 
        /**
@@ -2086,7 +2110,7 @@ class Transmitter
                        'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
-               Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
+               Logger::info('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id);
 
                $signed = LDSignature::sign($data, $owner);
                return HTTPSignature::transmit($signed, $profile['inbox'], $uid);