Merge pull request #6577 from rabuzarus/20190129_-_jot_atachment_preview
[friendica.git/.git] / src / Protocol / ActivityPub / Processor.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub/Processor.php
4  */
5 namespace Friendica\Protocol\ActivityPub;
6
7 use Friendica\Database\DBA;
8 use Friendica\Content\Text\HTML;
9 use Friendica\Core\Config;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Protocol;
12 use Friendica\Model\Contact;
13 use Friendica\Model\APContact;
14 use Friendica\Model\Item;
15 use Friendica\Model\Event;
16 use Friendica\Model\User;
17 use Friendica\Protocol\ActivityPub;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\JsonLD;
20 use Friendica\Util\Strings;
21
22 /**
23  * ActivityPub Processor Protocol class
24  */
25 class Processor
26 {
27         /**
28          * Converts mentions from Pleroma into the Friendica format
29          *
30          * @param string $body
31          *
32          * @return string converted body
33          */
34         private static function convertMentions($body)
35         {
36                 $URLSearchString = "^\[\]";
37                 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
38
39                 return $body;
40         }
41
42         /**
43          * Replaces emojis in the body
44          *
45          * @param array $emojis
46          * @param string $body
47          *
48          * @return string with replaced emojis
49          */
50         public static function replaceEmojis($emojis, $body)
51         {
52                 foreach ($emojis as $emoji) {
53                         $replace = '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
54                         $body = str_replace($emoji['name'], $replace, $body);
55                 }
56                 return $body;
57         }
58
59         /**
60          * Constructs a string with tags for a given tag array
61          *
62          * @param array $tags
63          * @param boolean $sensitive
64          *
65          * @return string with tags
66          */
67         private static function constructTagList($tags, $sensitive)
68         {
69                 if (empty($tags)) {
70                         return '';
71                 }
72
73                 $tag_text = '';
74                 foreach ($tags as $tag) {
75                         if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag'])) {
76                                 if (!empty($tag_text)) {
77                                         $tag_text .= ',';
78                                 }
79
80                                 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
81                         }
82                 }
83
84                 /// @todo add nsfw for $sensitive
85
86                 return $tag_text;
87         }
88
89         /**
90          * Add attachment data to the item array
91          *
92          * @param array $attachments
93          * @param array $item
94          *
95          * @return array array
96          */
97         private static function constructAttachList($attachments, $item)
98         {
99                 if (empty($attachments)) {
100                         return $item;
101                 }
102
103                 foreach ($attachments as $attach) {
104                         $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
105                         if ($filetype == 'image') {
106                                 $item['body'] .= "\n[img]" . $attach['url'] . '[/img]';
107                         } else {
108                                 if (!empty($item["attach"])) {
109                                         $item["attach"] .= ',';
110                                 } else {
111                                         $item["attach"] = '';
112                                 }
113                                 if (!isset($attach['length'])) {
114                                         $attach['length'] = "0";
115                                 }
116                                 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
117                         }
118                 }
119
120                 return $item;
121         }
122
123         /**
124          * Updates a message
125          *
126          * @param array $activity Activity array
127          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
128          */
129         public static function updateItem($activity)
130         {
131                 $item = [];
132                 $item['changed'] = DateTimeFormat::utcNow();
133                 $item['edited'] = $activity['updated'];
134                 $item['title'] = HTML::toBBCode($activity['name']);
135                 $item['content-warning'] = HTML::toBBCode($activity['summary']);
136                 $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content']));
137                 $item['body'] = self::convertMentions($content);
138                 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
139
140                 Item::update($item, ['uri' => $activity['id']]);
141         }
142
143         /**
144          * Prepares data for a message
145          *
146          * @param array $activity Activity array
147          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
148          * @throws \ImagickException
149          */
150         public static function createItem($activity)
151         {
152                 $item = [];
153                 $item['verb'] = ACTIVITY_POST;
154                 $item['parent-uri'] = $activity['reply-to-id'];
155
156                 if ($activity['reply-to-id'] == $activity['id']) {
157                         $item['gravity'] = GRAVITY_PARENT;
158                         $item['object-type'] = ACTIVITY_OBJ_NOTE;
159                 } else {
160                         $item['gravity'] = GRAVITY_COMMENT;
161                         $item['object-type'] = ACTIVITY_OBJ_COMMENT;
162                 }
163
164                 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
165                         Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
166                         self::fetchMissingActivity($activity['reply-to-id'], $activity);
167                 }
168
169                 $item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', '');
170
171                 self::postItem($activity, $item);
172         }
173
174         /**
175          * Delete items
176          *
177          * @param array $activity
178          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
179          * @throws \ImagickException
180          */
181         public static function deleteItem($activity)
182         {
183                 $owner = Contact::getIdForURL($activity['actor']);
184
185                 Logger::log('Deleting item ' . $activity['object_id'] . ' from ' . $owner, Logger::DEBUG);
186                 Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]);
187         }
188
189         /**
190          * Prepare the item array for an activity
191          *
192          * @param array  $activity Activity array
193          * @param string $verb     Activity verb
194          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
195          * @throws \ImagickException
196          */
197         public static function createActivity($activity, $verb)
198         {
199                 $item = [];
200                 $item['verb'] = $verb;
201                 $item['parent-uri'] = $activity['object_id'];
202                 $item['gravity'] = GRAVITY_ACTIVITY;
203                 $item['object-type'] = ACTIVITY_OBJ_NOTE;
204
205                 $item['diaspora_signed_text'] = defaults($activity, 'diaspora:like', '');
206
207                 self::postItem($activity, $item);
208         }
209
210         /**
211          * Create an event
212          *
213          * @param array $activity Activity array
214          * @param array $item
215          * @throws \Exception
216          */
217         public static function createEvent($activity, $item)
218         {
219                 $event['summary']  = HTML::toBBCode($activity['name']);
220                 $event['desc']     = HTML::toBBCode($activity['content']);
221                 $event['start']    = $activity['start-time'];
222                 $event['finish']   = $activity['end-time'];
223                 $event['nofinish'] = empty($event['finish']);
224                 $event['location'] = $activity['location'];
225                 $event['adjust']   = true;
226                 $event['cid']      = $item['contact-id'];
227                 $event['uid']      = $item['uid'];
228                 $event['uri']      = $item['uri'];
229                 $event['edited']   = $item['edited'];
230                 $event['private']  = $item['private'];
231                 $event['guid']     = $item['guid'];
232                 $event['plink']    = $item['plink'];
233
234                 $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
235                 $ev = DBA::selectFirst('event', ['id'], $condition);
236                 if (DBA::isResult($ev)) {
237                         $event['id'] = $ev['id'];
238                 }
239
240                 $event_id = Event::store($event);
241                 Logger::log('Event '.$event_id.' was stored', Logger::DEBUG);
242         }
243
244         /**
245          * Creates an item post
246          *
247          * @param array $activity Activity data
248          * @param array $item     item array
249          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
250          * @throws \ImagickException
251          */
252         private static function postItem($activity, $item)
253         {
254                 /// @todo What to do with $activity['context']?
255
256                 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
257                         Logger::log('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', Logger::DEBUG);
258                         return;
259                 }
260
261                 $item['network'] = Protocol::ACTIVITYPUB;
262                 $item['private'] = !in_array(0, $activity['receiver']);
263                 $item['author-link'] = $activity['author'];
264                 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
265
266                 if (empty($activity['thread-completion'])) {
267                         $item['owner-link'] = $activity['actor'];
268                         $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
269                 } else {
270                         Logger::log('Ignoring actor because of thread completion.', Logger::DEBUG);
271                         $item['owner-link'] = $item['author-link'];
272                         $item['owner-id'] = $item['author-id'];
273                 }
274
275                 $item['uri'] = $activity['id'];
276
277                 if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
278                         $item_private = !in_array(0, $activity['item_receiver']);
279                         $parent = Item::selectFirst(['private'], ['uri' => $item['parent-uri']]);
280                         if (!DBA::isResult($parent)) {
281                                 return;
282                         }
283                         if ($item_private && !$parent['private']) {
284                                 Logger::log('Item ' . $item['uri'] . ' is private but the parent ' . $item['parent-uri'] . ' is not. So we drop it.');
285                                 return;
286                         }
287                 }
288
289                 $item['created'] = $activity['published'];
290                 $item['edited'] = $activity['updated'];
291                 $item['guid'] = $activity['diaspora:guid'];
292                 $item['title'] = HTML::toBBCode($activity['name']);
293                 $item['content-warning'] = HTML::toBBCode($activity['summary']);
294                 $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content']));
295                 $item['body'] = self::convertMentions($content);
296
297                 if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) {
298                         $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]';
299                 }
300
301                 $item['location'] = $activity['location'];
302
303                 if (!empty($item['latitude']) && !empty($item['longitude'])) {
304                         $item['coord'] = $item['latitude'] . ' ' . $item['longitude'];
305                 }
306
307                 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
308                 $item['app'] = $activity['generator'];
309                 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
310
311                 $item = self::constructAttachList($activity['attachments'], $item);
312
313                 if (!empty($activity['source'])) {
314                         $item['body'] = $activity['source'];
315                 }
316
317                 foreach ($activity['receiver'] as $receiver) {
318                         $item['uid'] = $receiver;
319                         $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
320
321                         if (($receiver != 0) && empty($item['contact-id'])) {
322                                 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
323                         }
324
325                         if ($activity['object_type'] == 'as:Event') {
326                                 self::createEvent($activity, $item);
327                         }
328
329                         $item_id = Item::insert($item);
330                         Logger::log('Storing for user ' . $item['uid'] . ': ' . $item_id);
331                 }
332
333                 if (!$item['private'] && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
334                         $author = APContact::getByURL($item['owner-link'], false);
335                         // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
336                         if ($author['type'] != 'Group') {
337                                 Logger::log('Send follow request for ' . $item['uri'] . ' to ' . $item['author-link'], Logger::DEBUG);
338                                 ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
339                         }
340                 }
341         }
342
343         /**
344          * Fetches missing posts
345          *
346          * @param $url
347          * @param $child
348          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
349          */
350         private static function fetchMissingActivity($url, $child)
351         {
352                 if (Config::get('system', 'ostatus_full_threads')) {
353                         return;
354                 }
355
356                 $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
357
358                 $object = ActivityPub::fetchContent($url, $uid);
359                 if (empty($object)) {
360                         Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
361                         return;
362                 }
363
364                 if (empty($object['id'])) {
365                         Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
366                         return;
367                 }
368
369                 $activity = [];
370                 $activity['@context'] = $object['@context'];
371                 unset($object['@context']);
372                 $activity['id'] = $object['id'];
373                 $activity['to'] = defaults($object, 'to', []);
374                 $activity['cc'] = defaults($object, 'cc', []);
375                 $activity['actor'] = $child['author'];
376                 $activity['object'] = $object;
377                 $activity['published'] = defaults($object, 'published', $child['published']);
378                 $activity['type'] = 'Create';
379
380                 $ldactivity = JsonLD::compact($activity);
381
382                 $ldactivity['thread-completion'] = true;
383
384                 ActivityPub\Receiver::processActivity($ldactivity);
385                 Logger::log('Activity ' . $url . ' had been fetched and processed.');
386         }
387
388         /**
389          * perform a "follow" request
390          *
391          * @param array $activity
392          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
393          * @throws \ImagickException
394          */
395         public static function followUser($activity)
396         {
397                 $uid = User::getIdForURL($activity['object_id']);
398                 if (empty($uid)) {
399                         return;
400                 }
401
402                 $owner = User::getOwnerDataById($uid);
403
404                 $cid = Contact::getIdForURL($activity['actor'], $uid);
405                 if (!empty($cid)) {
406                         self::switchContact($cid);
407                         DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
408                         $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
409                 } else {
410                         $contact = false;
411                 }
412
413                 $item = ['author-id' => Contact::getIdForURL($activity['actor']),
414                         'author-link' => $activity['actor']];
415
416                 // Ensure that the contact has got the right network type
417                 self::switchContact($item['author-id']);
418
419                 Contact::addRelationship($owner, $contact, $item);
420                 $cid = Contact::getIdForURL($activity['actor'], $uid);
421                 if (empty($cid)) {
422                         return;
423                 }
424
425                 if (empty($contact)) {
426                         DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
427                 }
428
429                 Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
430         }
431
432         /**
433          * Update the given profile
434          *
435          * @param array $activity
436          * @throws \Exception
437          */
438         public static function updatePerson($activity)
439         {
440                 if (empty($activity['object_id'])) {
441                         return;
442                 }
443
444                 Logger::log('Updating profile for ' . $activity['object_id'], Logger::DEBUG);
445                 APContact::getByURL($activity['object_id'], true);
446         }
447
448         /**
449          * Delete the given profile
450          *
451          * @param array $activity
452          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
453          */
454         public static function deletePerson($activity)
455         {
456                 if (empty($activity['object_id']) || empty($activity['actor'])) {
457                         Logger::log('Empty object id or actor.', Logger::DEBUG);
458                         return;
459                 }
460
461                 if ($activity['object_id'] != $activity['actor']) {
462                         Logger::log('Object id does not match actor.', Logger::DEBUG);
463                         return;
464                 }
465
466                 $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
467                 while ($contact = DBA::fetch($contacts)) {
468                         Contact::remove($contact['id']);
469                 }
470                 DBA::close($contacts);
471
472                 Logger::log('Deleted contact ' . $activity['object_id'], Logger::DEBUG);
473         }
474
475         /**
476          * Accept a follow request
477          *
478          * @param array $activity
479          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
480          * @throws \ImagickException
481          */
482         public static function acceptFollowUser($activity)
483         {
484                 $uid = User::getIdForURL($activity['object_actor']);
485                 if (empty($uid)) {
486                         return;
487                 }
488
489                 $cid = Contact::getIdForURL($activity['actor'], $uid);
490                 if (empty($cid)) {
491                         Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
492                         return;
493                 }
494
495                 self::switchContact($cid);
496
497                 $fields = ['pending' => false];
498
499                 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
500                 if ($contact['rel'] == Contact::FOLLOWER) {
501                         $fields['rel'] = Contact::FRIEND;
502                 }
503
504                 $condition = ['id' => $cid];
505                 DBA::update('contact', $fields, $condition);
506                 Logger::log('Accept contact request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
507         }
508
509         /**
510          * Reject a follow request
511          *
512          * @param array $activity
513          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
514          * @throws \ImagickException
515          */
516         public static function rejectFollowUser($activity)
517         {
518                 $uid = User::getIdForURL($activity['object_actor']);
519                 if (empty($uid)) {
520                         return;
521                 }
522
523                 $cid = Contact::getIdForURL($activity['actor'], $uid);
524                 if (empty($cid)) {
525                         Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
526                         return;
527                 }
528
529                 self::switchContact($cid);
530
531                 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
532                         Contact::remove($cid);
533                         Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', Logger::DEBUG);
534                 } else {
535                         Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', Logger::DEBUG);
536                 }
537         }
538
539         /**
540          * Undo activity like "like" or "dislike"
541          *
542          * @param array $activity
543          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
544          * @throws \ImagickException
545          */
546         public static function undoActivity($activity)
547         {
548                 if (empty($activity['object_id'])) {
549                         return;
550                 }
551
552                 if (empty($activity['object_actor'])) {
553                         return;
554                 }
555
556                 $author_id = Contact::getIdForURL($activity['object_actor']);
557                 if (empty($author_id)) {
558                         return;
559                 }
560
561                 Item::delete(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
562         }
563
564         /**
565          * Activity to remove a follower
566          *
567          * @param array $activity
568          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
569          * @throws \ImagickException
570          */
571         public static function undoFollowUser($activity)
572         {
573                 $uid = User::getIdForURL($activity['object_object']);
574                 if (empty($uid)) {
575                         return;
576                 }
577
578                 $owner = User::getOwnerDataById($uid);
579
580                 $cid = Contact::getIdForURL($activity['actor'], $uid);
581                 if (empty($cid)) {
582                         Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
583                         return;
584                 }
585
586                 self::switchContact($cid);
587
588                 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
589                 if (!DBA::isResult($contact)) {
590                         return;
591                 }
592
593                 Contact::removeFollower($owner, $contact);
594                 Logger::log('Undo following request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
595         }
596
597         /**
598          * Switches a contact to AP if needed
599          *
600          * @param integer $cid Contact ID
601          * @throws \Exception
602          */
603         private static function switchContact($cid)
604         {
605                 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
606                 if (!DBA::isResult($contact) || ($contact['network'] == Protocol::ACTIVITYPUB)) {
607                         return;
608                 }
609
610                 Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
611                 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
612         }
613 }