]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
AP: Support "discoverable"
[friendica.git] / src / Protocol / ActivityPub.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Protocol;
23
24 use Friendica\Core\Protocol;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\APContact;
28 use Friendica\Model\User;
29 use Friendica\Util\HTTPSignature;
30 use Friendica\Util\JsonLD;
31
32 /**
33  * ActivityPub Protocol class
34  *
35  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
36  * https://www.w3.org/TR/activitypub/
37  * https://www.w3.org/TR/activitystreams-core/
38  * https://www.w3.org/TR/activitystreams-vocabulary/
39  *
40  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
41  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
42  *
43  * Digest: https://tools.ietf.org/html/rfc5843
44  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
45  *
46  * Mastodon implementation of supported activities:
47  * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
48  *
49  * Funkwhale:
50  * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
51  *
52  * To-do:
53  * - Polling the outboxes for missing content?
54  *
55  * Missing parts from DFRN:
56  * - Public Forum
57  * - Private Forum
58  * - Relocation
59  */
60 class ActivityPub
61 {
62         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
63         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
64                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
65                 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
66                 'diaspora' => 'https://diasporafoundation.org/ns/',
67                 'litepub' => 'http://litepub.social/ns#',
68                 'toot' => 'http://joinmastodon.org/ns#',
69                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
70                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
71                 'directMessage' => 'litepub:directMessage',
72                 'discoverable' => 'toot:discoverable']];
73         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application', 'Tombstone'];
74         /**
75          * Checks if the web request is done for the AP protocol
76          *
77          * @return bool is it AP?
78          */
79         public static function isRequest()
80         {
81                 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
82                         stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') ||
83                         stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
84         }
85
86         /**
87          * Fetches ActivityPub content from the given url
88          *
89          * @param string  $url content url
90          * @param integer $uid User ID for the signature
91          * @return array
92          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
93          */
94         public static function fetchContent(string $url, int $uid = 0)
95         {
96                 return HTTPSignature::fetch($url, $uid);
97         }
98
99         private static function getAccountType($apcontact)
100         {
101                 $accounttype = -1;
102
103                 switch($apcontact['type']) {
104                         case 'Person':
105                                 $accounttype = User::ACCOUNT_TYPE_PERSON;
106                                 break;
107                         case 'Organization':
108                                 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
109                                 break;
110                         case 'Service':
111                                 $accounttype = User::ACCOUNT_TYPE_NEWS;
112                                 break;
113                         case 'Group':
114                                 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
115                                 break;
116                         case 'Application':
117                                 $accounttype = User::ACCOUNT_TYPE_RELAY;
118                                 break;
119                         case 'Tombstone':
120                                 $accounttype = User::ACCOUNT_TYPE_DELETED;
121                                 break;
122                 }
123
124                 return $accounttype;
125         }
126
127         /**
128          * Fetches a profile from the given url into an array that is compatible to Probe::uri
129          *
130          * @param string  $url    profile url
131          * @param boolean $update Update the profile
132          * @return array
133          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
134          * @throws \ImagickException
135          */
136         public static function probeProfile($url, $update = true)
137         {
138                 $apcontact = APContact::getByURL($url, $update);
139                 if (empty($apcontact)) {
140                         return [];
141                 }
142
143                 $profile = ['network' => Protocol::ACTIVITYPUB];
144                 $profile['nick'] = $apcontact['nick'];
145                 $profile['name'] = $apcontact['name'];
146                 $profile['guid'] = $apcontact['uuid'];
147                 $profile['url'] = $apcontact['url'];
148                 $profile['addr'] = $apcontact['addr'];
149                 $profile['alias'] = $apcontact['alias'];
150                 $profile['following'] = $apcontact['following'];
151                 $profile['followers'] = $apcontact['followers'];
152                 $profile['inbox'] = $apcontact['inbox'];
153                 $profile['outbox'] = $apcontact['outbox'];
154                 $profile['sharedinbox'] = $apcontact['sharedinbox'];
155                 $profile['photo'] = $apcontact['photo'];
156                 $profile['header'] = $apcontact['header'];
157                 $profile['account-type'] = self::getAccountType($apcontact);
158                 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
159                 // $profile['keywords']
160                 // $profile['location']
161                 $profile['about'] = $apcontact['about'];
162                 $profile['batch'] = $apcontact['sharedinbox'];
163                 $profile['notify'] = $apcontact['inbox'];
164                 $profile['poll'] = $apcontact['outbox'];
165                 $profile['pubkey'] = $apcontact['pubkey'];
166                 $profile['subscribe'] = $apcontact['subscribe'];
167                 $profile['manually-approve'] = $apcontact['manually-approve'];
168                 $profile['baseurl'] = $apcontact['baseurl'];
169                 $profile['gsid'] = $apcontact['gsid'];
170
171                 if (!is_null($apcontact['discoverable'])) {
172                         $profile['hide'] = !$apcontact['discoverable'];
173                 }
174
175                 // Remove all "null" fields
176                 foreach ($profile as $field => $content) {
177                         if (is_null($content)) {
178                                 unset($profile[$field]);
179                         }
180                 }
181
182                 return $profile;
183         }
184
185         /**
186          * Fetches activities from the outbox of a given profile and processes it
187          *
188          * @param string  $url
189          * @param integer $uid User ID
190          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
191          */
192         public static function fetchOutbox($url, $uid)
193         {
194                 $data = self::fetchContent($url, $uid);
195                 if (empty($data)) {
196                         return;
197                 }
198
199                 if (!empty($data['orderedItems'])) {
200                         $items = $data['orderedItems'];
201                 } elseif (!empty($data['first']['orderedItems'])) {
202                         $items = $data['first']['orderedItems'];
203                 } elseif (!empty($data['first'])) {
204                         self::fetchOutbox($data['first'], $uid);
205                         return;
206                 } else {
207                         $items = [];
208                 }
209
210                 foreach ($items as $activity) {
211                         $ldactivity = JsonLD::compact($activity);
212                         ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
213                 }
214         }
215
216         /**
217          * Fetch items from AP endpoints
218          *
219          * @param string $url  Address of the endpoint
220          * @param integer $uid Optional user id
221          * @return array Endpoint items
222          */
223         public static function fetchItems(string $url, int $uid = 0)
224         {
225                 $data = self::fetchContent($url, $uid);
226                 if (empty($data)) {
227                         return [];
228                 }
229
230                 if (!empty($data['orderedItems'])) {
231                         $items = $data['orderedItems'];
232                 } elseif (!empty($data['first']['orderedItems'])) {
233                         $items = $data['first']['orderedItems'];
234                 } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) {
235                         return self::fetchItems($data['first'], $uid);
236                 } else {
237                         return [];
238                 }
239
240                 if (!empty($data['next']) && is_string($data['next'])) {
241                         $items = array_merge($items, self::fetchItems($data['next'], $uid));
242                 }
243
244                 return $items;
245         }
246
247         /**
248          * Checks if the given contact url does support ActivityPub
249          *
250          * @param string  $url    profile url
251          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
252          * @return boolean
253          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
254          * @throws \ImagickException
255          */
256         public static function isSupportedByContactUrl($url, $update = null)
257         {
258                 return !empty(APContact::getByURL($url, $update));
259         }
260 }