]> git.mxchange.org Git - friendica.git/blob - src/Module/ActivityPub/Whoami.php
Changes:
[friendica.git] / src / Module / ActivityPub / Whoami.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\ActivityPub;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\System;
26 use Friendica\DI;
27 use Friendica\Model\User;
28 use Friendica\Module\BaseApi;
29 use Friendica\Protocol\ActivityPub;
30
31 /**
32  * "who am i" endpoint for ActivityPub C2S
33  */
34 class Whoami extends BaseApi
35 {
36         /**
37          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38          */
39         protected function rawContent(array $request = [])
40         {
41                 $this->checkAllowedScope(self::SCOPE_READ);
42                 $uid = self::getCurrentUserID();
43
44                 $owner = User::getOwnerDataById($uid);
45
46                 $data = ['@context' => ActivityPub::CONTEXT];
47
48                 $data['id']                        = $owner['url'];
49                 $data['url']                       = $owner['url'];
50                 $data['type']                      = ActivityPub::ACCOUNT_TYPES[$owner['account-type']];
51                 $data['name']                      = $owner['name'];
52                 $data['preferredUsername']         = $owner['nick'];
53                 $data['alsoKnownAs']               = [];
54                 $data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
55                 $data['discoverable']              = (bool)$owner['net-publish'];
56                 $data['tag']                       = [];
57
58                 $data['icon'] = [
59                         'type' => 'Image',
60                         'url'  => User::getAvatarUrl($owner)
61                 ];
62
63                 if (!empty($owner['about'])) {
64                         $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
65                 }
66
67                 $custom_fields = [];
68
69                 foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
70                         $custom_fields[] = [
71                                 'type'  => 'PropertyValue',
72                                 'name'  => $profile_field->label,
73                                 'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value)
74                         ];
75                 };
76
77                 if (!empty($custom_fields)) {
78                         $data['attachment'] = $custom_fields;
79                 }
80
81                 $data['publicKey'] = [
82                         'id'           => $owner['url'] . '#main-key',
83                         'owner'        => $owner['url'],
84                         'publicKeyPem' => $owner['pubkey']
85                 ];
86
87                 $data['capabilities'] = [];
88                 $data['inbox']        = DI::baseUrl() . '/inbox/' . $owner['nick'];
89                 $data['outbox']       = DI::baseUrl() . '/outbox/' . $owner['nick'];
90                 $data['featured']     = DI::baseUrl() . '/featured/' . $owner['nick'];
91                 $data['followers']    = DI::baseUrl() . '/followers/' . $owner['nick'];
92                 $data['following']    = DI::baseUrl() . '/following/' . $owner['nick'];
93
94                 $data['endpoints'] = [
95                         'oauthAuthorizationEndpoint' => DI::baseUrl() . '/oauth/authorize',
96                         'oauthRegistrationEndpoint'  => DI::baseUrl() . '/api/v1/apps',
97                         'oauthTokenEndpoint'         => DI::baseUrl() . '/oauth/token',
98                         'sharedInbox'                => DI::baseUrl() . '/inbox',
99 //                      'uploadMedia'                => DI::baseUrl() . '/api/upload_media' // @todo Endpoint does not exist at the moment
100                 ];
101
102                 $data['generator'] = ActivityPub\Transmitter::getService();
103                 $this->jsonExit($data, 'application/activity+json');
104         }
105 }