]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Receiver.php
Moved documentation
[friendica.git] / src / Protocol / ActivityPub / Receiver.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub.php
4  */
5 namespace Friendica\Protocol\ActivityPub;
6
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
27 use Friendica\Protocol\ActivityPub;
28
29 /**
30  * @brief ActivityPub Receiver Protocol class
31  *
32  * To-Do:
33  * - Update (Image, Video, Article, Note)
34  * - Event
35  * - Undo Announce
36  *
37  * Check what this is meant to do:
38  * - Add
39  * - Block
40  * - Flag
41  * - Remove
42  * - Undo Block
43  * - Undo Accept (Problem: This could invert a contact accept or an event accept)
44  *
45  * General:
46  * - Possibly using the LD-JSON parser
47  */
48 class Receiver
49 {
50         /**
51          * @brief Checks if the web request is done for the AP protocol
52          *
53          * @return is it AP?
54          */
55         public static function isRequest()
56         {
57                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
58                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
59         }
60
61         /**
62          * @brief 
63          *
64          * @param $body
65          * @param $header
66          * @param integer $uid User ID
67          */
68         public static function processInbox($body, $header, $uid)
69         {
70                 $http_signer = HTTPSignature::getSigner($body, $header);
71                 if (empty($http_signer)) {
72                         logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
73                         return;
74                 } else {
75                         logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
76                 }
77
78                 $activity = json_decode($body, true);
79
80                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
81                 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
82
83                 if (empty($activity)) {
84                         logger('Invalid body.', LOGGER_DEBUG);
85                         return;
86                 }
87
88                 if (LDSignature::isSigned($activity)) {
89                         $ld_signer = LDSignature::getSigner($activity);
90                         if (empty($ld_signer)) {
91                                 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
92                         }
93                         if (!empty($ld_signer && ($actor == $http_signer))) {
94                                 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
95                                 $trust_source = true;
96                         } elseif (!empty($ld_signer)) {
97                                 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
98                                 $trust_source = true;
99                         } elseif ($actor == $http_signer) {
100                                 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
101                                 $trust_source = true;
102                         } else {
103                                 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
104                                 $trust_source = false;
105                         }
106                 } elseif ($actor == $http_signer) {
107                         logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
108                         $trust_source = true;
109                 } else {
110                         logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
111                         $trust_source = false;
112                 }
113
114                 self::processActivity($activity, $body, $uid, $trust_source);
115         }
116
117         /**
118          * @brief 
119          *
120          * @param array $activity
121          * @param integer $uid User ID
122          * @param $trust_source
123          *
124          * @return 
125          */
126         private static function prepareObjectData($activity, $uid, &$trust_source)
127         {
128                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
129                 if (empty($actor)) {
130                         logger('Empty actor', LOGGER_DEBUG);
131                         return [];
132                 }
133
134                 // Fetch all receivers from to, cc, bto and bcc
135                 $receivers = self::getReceivers($activity, $actor);
136
137                 // When it is a delivery to a personal inbox we add that user to the receivers
138                 if (!empty($uid)) {
139                         $owner = User::getOwnerDataById($uid);
140                         $additional = ['uid:' . $uid => $uid];
141                         $receivers = array_merge($receivers, $additional);
142                 }
143
144                 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
145
146                 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
147                 if (empty($object_id)) {
148                         logger('No object found', LOGGER_DEBUG);
149                         return [];
150                 }
151
152                 // Fetch the content only on activities where this matters
153                 if (in_array($activity['type'], ['Create', 'Announce'])) {
154                         $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
155                         if (empty($object_data)) {
156                                 logger("Object data couldn't be processed", LOGGER_DEBUG);
157                                 return [];
158                         }
159                         // We had been able to retrieve the object data - so we can trust the source
160                         $trust_source = true;
161                 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
162                         // Create a mostly empty array out of the activity data (instead of the object).
163                         // This way we later don't have to check for the existence of ech individual array element.
164                         $object_data = self::processObject($activity);
165                         $object_data['name'] = $activity['type'];
166                         $object_data['author'] = $activity['actor'];
167                         $object_data['object'] = $object_id;
168                         $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
169                 } else {
170                         $object_data = [];
171                         $object_data['id'] = $activity['id'];
172                         $object_data['object'] = $activity['object'];
173                         $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
174                 }
175
176                 $object_data = self::addActivityFields($object_data, $activity);
177
178                 $object_data['type'] = $activity['type'];
179                 $object_data['owner'] = $actor;
180                 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
181
182                 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
183
184                 return $object_data;
185         }
186
187         /**
188          * @brief 
189          *
190          * @param array $activity
191          * @param $body
192          * @param integer $uid User ID
193          * @param $trust_source
194          */
195         public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
196         {
197                 if (empty($activity['type'])) {
198                         logger('Empty type', LOGGER_DEBUG);
199                         return;
200                 }
201
202                 if (empty($activity['object'])) {
203                         logger('Empty object', LOGGER_DEBUG);
204                         return;
205                 }
206
207                 if (empty($activity['actor'])) {
208                         logger('Empty actor', LOGGER_DEBUG);
209                         return;
210
211                 }
212
213                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
214                 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
215                 if (empty($object_data)) {
216                         logger('No object data found', LOGGER_DEBUG);
217                         return;
218                 }
219
220                 if (!$trust_source) {
221                         logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
222                 }
223
224                 switch ($activity['type']) {
225                         case 'Create':
226                         case 'Announce':
227                                 ActivityPub\Processor::createItem($object_data, $body);
228                                 break;
229
230                         case 'Like':
231                                 ActivityPub\Processor::likeItem($object_data, $body);
232                                 break;
233
234                         case 'Dislike':
235                                 ActivityPub\Processor::dislikeItem($object_data, $body);
236                                 break;
237
238                         case 'Update':
239                                 if (in_array($object_data['object_type'], ActivityPub::CONTENT_TYPES)) {
240                                         /// @todo
241                                 } elseif (in_array($object_data['object_type'], ActivityPub::ACCOUNT_TYPES)) {
242                                         ActivityPub\Processor::updatePerson($object_data, $body);
243                                 }
244                                 break;
245
246                         case 'Delete':
247                                 if ($object_data['object_type'] == 'Tombstone') {
248                                         ActivityPub\Processor::deleteItem($object_data, $body);
249                                 } elseif (in_array($object_data['object_type'], ActivityPub::ACCOUNT_TYPES)) {
250                                         ActivityPub\Processor::deletePerson($object_data, $body);
251                                 }
252                                 break;
253
254                         case 'Follow':
255                                 ActivityPub\Processor::followUser($object_data);
256                                 break;
257
258                         case 'Accept':
259                                 if ($object_data['object_type'] == 'Follow') {
260                                         ActivityPub\Processor::acceptFollowUser($object_data);
261                                 }
262                                 break;
263
264                         case 'Reject':
265                                 if ($object_data['object_type'] == 'Follow') {
266                                         ActivityPub\Processor::rejectFollowUser($object_data);
267                                 }
268                                 break;
269
270                         case 'Undo':
271                                 if ($object_data['object_type'] == 'Follow') {
272                                         ActivityPub\Processor::undoFollowUser($object_data);
273                                 } elseif (in_array($object_data['object_type'], ActivityPub::ACTIVITY_TYPES)) {
274                                         ActivityPub\Processor::undoActivity($object_data);
275                                 }
276                                 break;
277
278                         default:
279                                 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
280                                 break;
281                 }
282         }
283
284         /**
285          * @brief 
286          *
287          * @param array $activity
288          * @param $actor
289          *
290          * @return 
291          */
292         private static function getReceivers($activity, $actor)
293         {
294                 $receivers = [];
295
296                 // When it is an answer, we inherite the receivers from the parent
297                 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
298                 if (!empty($replyto)) {
299                         $parents = Item::select(['uid'], ['uri' => $replyto]);
300                         while ($parent = Item::fetch($parents)) {
301                                 $receivers['uid:' . $parent['uid']] = $parent['uid'];
302                         }
303                 }
304
305                 if (!empty($actor)) {
306                         $profile = APContact::getByURL($actor);
307                         $followers = defaults($profile, 'followers', '');
308
309                         logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
310                 } else {
311                         logger('Empty actor', LOGGER_DEBUG);
312                         $followers = '';
313                 }
314
315                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
316                         if (empty($activity[$element])) {
317                                 continue;
318                         }
319
320                         // The receiver can be an array or a string
321                         if (is_string($activity[$element])) {
322                                 $activity[$element] = [$activity[$element]];
323                         }
324
325                         foreach ($activity[$element] as $receiver) {
326                                 if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
327                                         $receivers['uid:0'] = 0;
328                                 }
329
330                                 if (($receiver == ActivityPub::PUBLIC_COLLECTION) && !empty($actor)) {
331                                         // This will most likely catch all OStatus connections to Mastodon
332                                         $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
333                                                 , 'archive' => false, 'pending' => false];
334                                         $contacts = DBA::select('contact', ['uid'], $condition);
335                                         while ($contact = DBA::fetch($contacts)) {
336                                                 if ($contact['uid'] != 0) {
337                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
338                                                 }
339                                         }
340                                         DBA::close($contacts);
341                                 }
342
343                                 if (in_array($receiver, [$followers, ActivityPub::PUBLIC_COLLECTION]) && !empty($actor)) {
344                                         $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
345                                                 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
346                                         $contacts = DBA::select('contact', ['uid'], $condition);
347                                         while ($contact = DBA::fetch($contacts)) {
348                                                 if ($contact['uid'] != 0) {
349                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
350                                                 }
351                                         }
352                                         DBA::close($contacts);
353                                         continue;
354                                 }
355
356                                 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
357                                 $contact = DBA::selectFirst('contact', ['uid'], $condition);
358                                 if (!DBA::isResult($contact)) {
359                                         continue;
360                                 }
361                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
362                         }
363                 }
364
365                 self::switchContacts($receivers, $actor);
366
367                 return $receivers;
368         }
369
370         /**
371          * @brief 
372          *
373          * @param $cid
374          * @param integer $uid User ID
375          * @param $url
376          */
377         private static function switchContact($cid, $uid, $url)
378         {
379                 $profile = ActivityPub::probeProfile($url);
380                 if (empty($profile)) {
381                         return;
382                 }
383
384                 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
385
386                 $photo = $profile['photo'];
387                 unset($profile['photo']);
388                 unset($profile['baseurl']);
389
390                 $profile['nurl'] = normalise_link($profile['url']);
391                 DBA::update('contact', $profile, ['id' => $cid]);
392
393                 Contact::updateAvatar($photo, $uid, $cid);
394         }
395
396         /**
397          * @brief 
398          *
399          * @param $receivers
400          * @param $actor
401          */
402         private static function switchContacts($receivers, $actor)
403         {
404                 if (empty($actor)) {
405                         return;
406                 }
407
408                 foreach ($receivers as $receiver) {
409                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
410                         if (DBA::isResult($contact)) {
411                                 self::switchContact($contact['id'], $receiver, $actor);
412                         }
413
414                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
415                         if (DBA::isResult($contact)) {
416                                 self::switchContact($contact['id'], $receiver, $actor);
417                         }
418                 }
419         }
420
421         /**
422          * @brief 
423          *
424          * @param $object_data
425          * @param array $activity
426          *
427          * @return 
428          */
429         private static function addActivityFields($object_data, $activity)
430         {
431                 if (!empty($activity['published']) && empty($object_data['published'])) {
432                         $object_data['published'] = $activity['published'];
433                 }
434
435                 if (!empty($activity['updated']) && empty($object_data['updated'])) {
436                         $object_data['updated'] = $activity['updated'];
437                 }
438
439                 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
440                         $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
441                 }
442
443                 if (!empty($activity['instrument'])) {
444                         $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
445                 }
446                 return $object_data;
447         }
448
449         /**
450          * @brief 
451          *
452          * @param $object_id
453          * @param $object
454          * @param $trust_source
455          *
456          * @return 
457          */
458         private static function fetchObject($object_id, $object = [], $trust_source = false)
459         {
460                 if (!$trust_source || is_string($object)) {
461                         $data = ActivityPub::fetchContent($object_id);
462                         if (empty($data)) {
463                                 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
464                                 $data = $object_id;
465                         } else {
466                                 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
467                         }
468                 } else {
469                         logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
470                         $data = $object;
471                 }
472
473                 if (is_string($data)) {
474                         $item = Item::selectFirst([], ['uri' => $data]);
475                         if (!DBA::isResult($item)) {
476                                 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
477                                 return false;
478                         }
479                         logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
480                         $data = ActivityPub\Transmitter::createNote($item);
481                 }
482
483                 if (empty($data['type'])) {
484                         logger('Empty type', LOGGER_DEBUG);
485                         return false;
486                 }
487
488                 if (in_array($data['type'], ActivityPub::CONTENT_TYPES)) {
489                         return self::processObject($data);
490                 }
491
492                 if ($data['type'] == 'Announce') {
493                         if (empty($data['object'])) {
494                                 return false;
495                         }
496                         return self::fetchObject($data['object']);
497                 }
498
499                 logger('Unhandled object type: ' . $data['type'], LOGGER_DEBUG);
500         }
501
502         /**
503          * @brief 
504          *
505          * @param $object
506          *
507          * @return 
508          */
509         private static function processObject($object)
510         {
511                 if (empty($object['id'])) {
512                         return false;
513                 }
514
515                 $object_data = [];
516                 $object_data['object_type'] = $object['type'];
517                 $object_data['id'] = $object['id'];
518
519                 if (!empty($object['inReplyTo'])) {
520                         $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
521                 } else {
522                         $object_data['reply-to-id'] = $object_data['id'];
523                 }
524
525                 $object_data['published'] = defaults($object, 'published', null);
526                 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
527
528                 if (empty($object_data['published']) && !empty($object_data['updated'])) {
529                         $object_data['published'] = $object_data['updated'];
530                 }
531
532                 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
533                 if (empty($actor)) {
534                         $actor = defaults($object, 'actor', null);
535                 }
536
537                 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
538                 $object_data['owner'] = $object_data['author'] = $actor;
539                 $object_data['context'] = defaults($object, 'context', null);
540                 $object_data['conversation'] = defaults($object, 'conversation', null);
541                 $object_data['sensitive'] = defaults($object, 'sensitive', null);
542                 $object_data['name'] = defaults($object, 'title', null);
543                 $object_data['name'] = defaults($object, 'name', $object_data['name']);
544                 $object_data['summary'] = defaults($object, 'summary', null);
545                 $object_data['content'] = defaults($object, 'content', null);
546                 $object_data['source'] = defaults($object, 'source', null);
547                 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
548                 $object_data['attachments'] = defaults($object, 'attachment', null);
549                 $object_data['tags'] = defaults($object, 'tag', null);
550                 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
551                 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
552                 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
553
554                 // Common object data:
555
556                 // Unhandled
557                 // @context, type, actor, signature, mediaType, duration, replies, icon
558
559                 // Also missing: (Defined in the standard, but currently unused)
560                 // audience, preview, endTime, startTime, generator, image
561
562                 // Data in Notes:
563
564                 // Unhandled
565                 // contentMap, announcement_count, announcements, context_id, likes, like_count
566                 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
567
568                 // Data in video:
569
570                 // To-Do?
571                 // category, licence, language, commentsEnabled
572
573                 // Unhandled
574                 // views, waitTranscoding, state, support, subtitleLanguage
575                 // likes, dislikes, shares, comments
576
577                 return $object_data;
578         }
579 }