]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Transmitter.php
bc127d4086db89e1c686ad4a7bb7988810c086bd
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub/Transmitter.php
4  */
5 namespace Friendica\Protocol\ActivityPub;
6
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
9 use Friendica\Core\Logger;
10 use Friendica\Core\System;
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\Term;
18 use Friendica\Model\User;
19 use Friendica\Util\DateTimeFormat;
20 use Friendica\Content\Text\BBCode;
21 use Friendica\Util\JsonLD;
22 use Friendica\Util\LDSignature;
23 use Friendica\Model\Profile;
24 use Friendica\Core\Config;
25 use Friendica\Object\Image;
26 use Friendica\Protocol\ActivityPub;
27 use Friendica\Protocol\Diaspora;
28 use Friendica\Core\Cache;
29 use Friendica\Util\Map;
30 use Friendica\Util\Network;
31
32 require_once 'include/api.php';
33
34 /**
35  * @brief ActivityPub Transmitter Protocol class
36  *
37  * To-Do:
38  * - Undo Announce
39  */
40 class Transmitter
41 {
42         /**
43          * collects the lost of followers of the given owner
44          *
45          * @param array   $owner Owner array
46          * @param integer $page  Page number
47          *
48          * @return array of owners
49          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
50          */
51         public static function getFollowers($owner, $page = null)
52         {
53                 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
54                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
55                 $count = DBA::count('contact', $condition);
56
57                 $data = ['@context' => ActivityPub::CONTEXT];
58                 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
59                 $data['type'] = 'OrderedCollection';
60                 $data['totalItems'] = $count;
61
62                 // When we hide our friends we will only show the pure number but don't allow more.
63                 $profile = Profile::getByUID($owner['uid']);
64                 if (!empty($profile['hide-friends'])) {
65                         return $data;
66                 }
67
68                 if (empty($page)) {
69                         $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
70                 } else {
71                         $list = [];
72
73                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
74                         while ($contact = DBA::fetch($contacts)) {
75                                 $list[] = $contact['url'];
76                         }
77
78                         if (!empty($list)) {
79                                 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
80                         }
81
82                         $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
83
84                         $data['orderedItems'] = $list;
85                 }
86
87                 return $data;
88         }
89
90         /**
91          * Create list of following contacts
92          *
93          * @param array   $owner Owner array
94          * @param integer $page  Page numbe
95          *
96          * @return array of following contacts
97          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
98          */
99         public static function getFollowing($owner, $page = null)
100         {
101                 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
102                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
103                 $count = DBA::count('contact', $condition);
104
105                 $data = ['@context' => ActivityPub::CONTEXT];
106                 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
107                 $data['type'] = 'OrderedCollection';
108                 $data['totalItems'] = $count;
109
110                 // When we hide our friends we will only show the pure number but don't allow more.
111                 $profile = Profile::getByUID($owner['uid']);
112                 if (!empty($profile['hide-friends'])) {
113                         return $data;
114                 }
115
116                 if (empty($page)) {
117                         $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
118                 } else {
119                         $list = [];
120
121                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
122                         while ($contact = DBA::fetch($contacts)) {
123                                 $list[] = $contact['url'];
124                         }
125
126                         if (!empty($list)) {
127                                 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
128                         }
129
130                         $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
131
132                         $data['orderedItems'] = $list;
133                 }
134
135                 return $data;
136         }
137
138         /**
139          * Public posts for the given owner
140          *
141          * @param array   $owner Owner array
142          * @param integer $page  Page numbe
143          *
144          * @return array of posts
145          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
146          * @throws \ImagickException
147          */
148         public static function getOutbox($owner, $page = null)
149         {
150                 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
151
152                 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
153                         'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
154                         'deleted' => false, 'visible' => true];
155                 $count = DBA::count('item', $condition);
156
157                 $data = ['@context' => ActivityPub::CONTEXT];
158                 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
159                 $data['type'] = 'OrderedCollection';
160                 $data['totalItems'] = $count;
161
162                 if (empty($page)) {
163                         $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
164                 } else {
165                         $list = [];
166
167                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
168
169                         $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
170                         while ($item = Item::fetch($items)) {
171                                 $object = self::createObjectFromItemID($item['id']);
172                                 unset($object['@context']);
173                                 $list[] = $object;
174                         }
175
176                         if (!empty($list)) {
177                                 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
178                         }
179
180                         $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
181
182                         $data['orderedItems'] = $list;
183                 }
184
185                 return $data;
186         }
187
188         /**
189          * Return the ActivityPub profile of the given user
190          *
191          * @param integer $uid User ID
192          * @return array with profile data
193          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
194          */
195         public static function getProfile($uid)
196         {
197                 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
198                         'account_removed' => false, 'verified' => true];
199                 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
200                 $user = DBA::selectFirst('user', $fields, $condition);
201                 if (!DBA::isResult($user)) {
202                         return [];
203                 }
204
205                 $fields = ['locality', 'region', 'country-name'];
206                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
207                 if (!DBA::isResult($profile)) {
208                         return [];
209                 }
210
211                 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
212                 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
213                 if (!DBA::isResult($contact)) {
214                         return [];
215                 }
216
217                 // On old installations and never changed contacts this might not be filled
218                 if (empty($contact['avatar'])) {
219                         $contact['avatar'] = $contact['photo'];
220                 }
221
222                 $data = ['@context' => ActivityPub::CONTEXT];
223                 $data['id'] = $contact['url'];
224                 $data['diaspora:guid'] = $user['guid'];
225                 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
226                 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
227                 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
228                 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
229                 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
230                 $data['preferredUsername'] = $user['nickname'];
231                 $data['name'] = $contact['name'];
232                 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
233                         'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
234                 $data['summary'] = $contact['about'];
235                 $data['url'] = $contact['url'];
236                 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
237                 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
238                         'owner' => $contact['url'],
239                         'publicKeyPem' => $user['pubkey']];
240                 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
241                 $data['icon'] = ['type' => 'Image',
242                         'url' => $contact['avatar']];
243
244                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
245                 return $data;
246         }
247
248         /**
249          * Returns an array with permissions of a given item array
250          *
251          * @param array $item
252          *
253          * @return array with permissions
254          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255          * @throws \ImagickException
256          */
257         private static function fetchPermissionBlockFromConversation($item)
258         {
259                 if (empty($item['thr-parent'])) {
260                         return [];
261                 }
262
263                 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
264                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
265                 if (!DBA::isResult($conversation)) {
266                         return [];
267                 }
268
269                 $activity = json_decode($conversation['source'], true);
270
271                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
272                 $profile = APContact::getByURL($actor);
273
274                 $item_profile = APContact::getByURL($item['author-link']);
275                 $exclude[] = $item['author-link'];
276
277                 if ($item['gravity'] == GRAVITY_PARENT) {
278                         $exclude[] = $item['owner-link'];
279                 }
280
281                 $permissions['to'][] = $actor;
282
283                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
284                         if (empty($activity[$element])) {
285                                 continue;
286                         }
287                         if (is_string($activity[$element])) {
288                                 $activity[$element] = [$activity[$element]];
289                         }
290
291                         foreach ($activity[$element] as $receiver) {
292                                 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
293                                         $permissions[$element][] = $item_profile['followers'];
294                                 } elseif (!in_array($receiver, $exclude)) {
295                                         $permissions[$element][] = $receiver;
296                                 }
297                         }
298                 }
299                 return $permissions;
300         }
301
302         /**
303          * Creates an array of permissions from an item thread
304          *
305          * @param array   $item
306          * @param boolean $blindcopy
307          * @param boolean $last_id
308          *
309          * @return array with permission data
310          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
311          * @throws \ImagickException
312          */
313         private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0)
314         {
315                 if ($last_id == 0) {
316                         $last_id = $item['id'];
317                 }
318
319                 $always_bcc = false;
320
321                 // Check if we should always deliver our stuff via BCC
322                 if (!empty($item['uid'])) {
323                         $profile = Profile::getByUID($item['uid']);
324                         if (!empty($profile)) {
325                                 $always_bcc = $profile['hide-friends'];
326                         }
327                 }
328
329                 // Will be activated in a later step
330                 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
331
332                 // For now only send to these contacts:
333                 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
334
335                 $data = ['to' => [], 'cc' => [], 'bcc' => []];
336
337                 $actor_profile = APContact::getByURL($item['author-link']);
338
339                 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
340
341                 if (!$item['private']) {
342                         $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
343
344                         $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
345                         if (!empty($actor_profile['followers'])) {
346                                 $data['cc'][] = $actor_profile['followers'];
347                         }
348
349                         foreach ($terms as $term) {
350                                 $profile = APContact::getByURL($term['url'], false);
351                                 if (!empty($profile)) {
352                                         $data['to'][] = $profile['url'];
353                                 }
354                         }
355                 } else {
356                         $receiver_list = Item::enumeratePermissions($item);
357
358                         $mentioned = [];
359
360                         foreach ($terms as $term) {
361                                 $cid = Contact::getIdForURL($term['url'], $item['uid']);
362                                 if (!empty($cid) && in_array($cid, $receiver_list)) {
363                                         $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => $networks]);
364                                         if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
365                                                 $data['to'][] = $profile['url'];
366                                         }
367                                 }
368                         }
369
370                         foreach ($receiver_list as $receiver) {
371                                 $contact = DBA::selectFirst('contact', ['url', 'hidden'], ['id' => $receiver, 'network' => $networks]);
372                                 if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
373                                         if ($contact['hidden'] || $always_bcc) {
374                                                 $data['bcc'][] = $profile['url'];
375                                         } else {
376                                                 $data['cc'][] = $profile['url'];
377                                         }
378                                 }
379                         }
380                 }
381
382                 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
383                 while ($parent = Item::fetch($parents)) {
384                         // Don't include data from future posts
385                         if ($parent['id'] >= $last_id) {
386                                 continue;
387                         }
388
389                         $profile = APContact::getByURL($parent['author-link'], false);
390                         if (!empty($profile)) {
391                                 if ($parent['uri'] == $item['thr-parent']) {
392                                         $data['to'][] = $profile['url'];
393                                 } else {
394                                         $data['cc'][] = $profile['url'];
395                                 }
396                         }
397
398                         if ($item['gravity'] != GRAVITY_PARENT) {
399                                 continue;
400                         }
401
402                         $profile = APContact::getByURL($parent['owner-link'], false);
403                         if (!empty($profile)) {
404                                 $data['cc'][] = $profile['url'];
405                         }
406                 }
407                 DBA::close($parents);
408
409                 $data['to'] = array_unique($data['to']);
410                 $data['cc'] = array_unique($data['cc']);
411                 $data['bcc'] = array_unique($data['bcc']);
412
413                 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
414                         unset($data['to'][$key]);
415                 }
416
417                 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
418                         unset($data['cc'][$key]);
419                 }
420
421                 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
422                         unset($data['bcc'][$key]);
423                 }
424
425                 foreach ($data['to'] as $to) {
426                         if (($key = array_search($to, $data['cc'])) !== false) {
427                                 unset($data['cc'][$key]);
428                         }
429
430                         if (($key = array_search($to, $data['bcc'])) !== false) {
431                                 unset($data['bcc'][$key]);
432                         }
433                 }
434
435                 foreach ($data['cc'] as $cc) {
436                         if (($key = array_search($cc, $data['bcc'])) !== false) {
437                                 unset($data['bcc'][$key]);
438                         }
439                 }
440
441                 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
442
443                 if (!$blindcopy) {
444                         unset($receivers['bcc']);
445                 }
446
447                 return $receivers;
448         }
449
450         /**
451          * Fetches a list of inboxes of followers of a given user
452          *
453          * @param integer $uid      User ID
454          * @param boolean $personal fetch personal inboxes
455          *
456          * @return array of follower inboxes
457          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
458          * @throws \ImagickException
459          */
460         public static function fetchTargetInboxesforUser($uid, $personal = false)
461         {
462                 $inboxes = [];
463
464                 // Will be activated in a later step
465                 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
466
467                 // For now only send to these contacts:
468                 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
469
470                 $condition = ['uid' => $uid, 'network' => $networks, 'archive' => false, 'pending' => false];
471
472                 if (!empty($uid)) {
473                         $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
474                 }
475
476                 $contacts = DBA::select('contact', ['url'], $condition);
477                 while ($contact = DBA::fetch($contacts)) {
478                         if (Network::isUrlBlocked($contact['url'])) {
479                                 continue;
480                         }
481
482                         $profile = APContact::getByURL($contact['url'], false);
483                         if (!empty($profile)) {
484                                 if (empty($profile['sharedinbox']) || $personal) {
485                                         $target = $profile['inbox'];
486                                 } else {
487                                         $target = $profile['sharedinbox'];
488                                 }
489                                 $inboxes[$target] = $target;
490                         }
491                 }
492                 DBA::close($contacts);
493
494                 return $inboxes;
495         }
496
497         /**
498          * Fetches an array of inboxes for the given item and user
499          *
500          * @param array   $item
501          * @param integer $uid      User ID
502          * @param boolean $personal fetch personal inboxes
503          * @param integer $last_id Last item id for adding receivers
504          *
505          * @return array with inboxes
506          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
507          * @throws \ImagickException
508          */
509         public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0)
510         {
511                 $permissions = self::createPermissionBlockForItem($item, true, $last_id);
512                 if (empty($permissions)) {
513                         return [];
514                 }
515
516                 $inboxes = [];
517
518                 if ($item['gravity'] == GRAVITY_ACTIVITY) {
519                         $item_profile = APContact::getByURL($item['author-link'], false);
520                 } else {
521                         $item_profile = APContact::getByURL($item['owner-link'], false);
522                 }
523
524                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
525                         if (empty($permissions[$element])) {
526                                 continue;
527                         }
528
529                         $blindcopy = in_array($element, ['bto', 'bcc']);
530
531                         foreach ($permissions[$element] as $receiver) {
532                                 if (Network::isUrlBlocked($receiver)) {
533                                         continue;
534                                 }
535
536                                 if ($receiver == $item_profile['followers']) {
537                                         $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
538                                 } else {
539                                         $profile = APContact::getByURL($receiver, false);
540                                         if (!empty($profile)) {
541                                                 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
542                                                         $target = $profile['inbox'];
543                                                 } else {
544                                                         $target = $profile['sharedinbox'];
545                                                 }
546                                                 $inboxes[$target] = $target;
547                                         }
548                                 }
549                         }
550                 }
551
552                 return $inboxes;
553         }
554
555         /**
556          * Returns the activity type of a given item
557          *
558          * @param array $item
559          *
560          * @return string with activity type
561          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
562          * @throws \ImagickException
563          */
564         private static function getTypeOfItem($item)
565         {
566                 $reshared = false;
567
568                 // Only check for a reshare, if it is a real reshare and no quoted reshare
569                 if (strpos($item['body'], "[share") === 0) {
570                         $announce = api_share_as_retweet($item);
571                         $reshared = !empty($announce['plink']);
572                 }
573
574                 if ($reshared) {
575                         $type = 'Announce';
576                 } elseif ($item['verb'] == ACTIVITY_POST) {
577                         if ($item['created'] == $item['edited']) {
578                                 $type = 'Create';
579                         } else {
580                                 $type = 'Update';
581                         }
582                 } elseif ($item['verb'] == ACTIVITY_LIKE) {
583                         $type = 'Like';
584                 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
585                         $type = 'Dislike';
586                 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
587                         $type = 'Accept';
588                 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
589                         $type = 'Reject';
590                 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
591                         $type = 'TentativeAccept';
592                 } else {
593                         $type = '';
594                 }
595
596                 return $type;
597         }
598
599         /**
600          * Creates the activity or fetches it from the cache
601          *
602          * @param integer $item_id
603          * @param boolean $force Force new cache entry
604          *
605          * @return array with the activity
606          * @throws \Exception
607          */
608         public static function createCachedActivityFromItem($item_id, $force = false)
609         {
610                 $cachekey = 'APDelivery:createActivity:' . $item_id;
611
612                 if (!$force) {
613                         $data = Cache::get($cachekey);
614                         if (!is_null($data)) {
615                                 return $data;
616                         }
617                 }
618
619                 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
620
621                 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
622                 return $data;
623         }
624
625         /**
626          * Creates an activity array for a given item id
627          *
628          * @param integer $item_id
629          * @param boolean $object_mode Is the activity item is used inside another object?
630          *
631          * @return array of activity
632          * @throws \Exception
633          */
634         public static function createActivityFromItem($item_id, $object_mode = false)
635         {
636                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
637
638                 if (!DBA::isResult($item)) {
639                         return false;
640                 }
641
642                 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
643                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
644                 if (DBA::isResult($conversation)) {
645                         $data = json_decode($conversation['source']);
646                         if (!empty($data)) {
647                                 return $data;
648                         }
649                 }
650
651                 $type = self::getTypeOfItem($item);
652
653                 if (!$object_mode) {
654                         $data = ['@context' => ActivityPub::CONTEXT];
655
656                         if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
657                                 $type = 'Undo';
658                         } elseif ($item['deleted']) {
659                                 $type = 'Delete';
660                         }
661                 } else {
662                         $data = [];
663                 }
664
665                 $data['id'] = $item['uri'] . '#' . $type;
666                 $data['type'] = $type;
667                 $data['actor'] = $item['owner-link'];
668
669                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
670
671                 $data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()];
672
673                 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
674
675                 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
676                         $data['object'] = self::createNote($item);
677                 } elseif ($data['type'] == 'Announce') {
678                         $data['object'] = self::createAnnounce($item);
679                 } elseif ($data['type'] == 'Undo') {
680                         $data['object'] = self::createActivityFromItem($item_id, true);
681                 } else {
682                         $data['diaspora:guid'] = $item['guid'];
683                         if (!empty($item['signed_text'])) {
684                                 $data['diaspora:like'] = $item['signed_text'];
685                         }
686                         $data['object'] = $item['thr-parent'];
687                 }
688
689                 if (!empty($item['contact-uid'])) {
690                         $uid = $item['contact-uid'];
691                 } else {
692                         $uid = $item['uid'];
693                 }
694
695                 $owner = User::getOwnerDataById($uid);
696
697                 if (!$object_mode && !empty($owner)) {
698                         return LDSignature::sign($data, $owner);
699                 } else {
700                         return $data;
701                 }
702
703                 /// @todo Create "conversation" entry
704         }
705
706         /**
707          * Creates an object array for a given item id
708          *
709          * @param integer $item_id
710          *
711          * @return array with the object data
712          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
713          * @throws \ImagickException
714          */
715         public static function createObjectFromItemID($item_id)
716         {
717                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
718
719                 if (!DBA::isResult($item)) {
720                         return false;
721                 }
722
723                 $data = ['@context' => ActivityPub::CONTEXT];
724                 $data = array_merge($data, self::createNote($item));
725
726                 return $data;
727         }
728
729         /**
730          * Creates a location entry for a given item array
731          *
732          * @param array $item
733          *
734          * @return array with location array
735          */
736         private static function createLocation($item)
737         {
738                 $location = ['type' => 'Place'];
739
740                 if (!empty($item['location'])) {
741                         $location['name'] = $item['location'];
742                 }
743
744                 $coord = [];
745
746                 if (empty($item['coord'])) {
747                         $coord = Map::getCoordinates($item['location']);
748                 } else {
749                         $coords = explode(' ', $item['coord']);
750                         if (count($coords) == 2) {
751                                 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
752                         }
753                 }
754
755                 if (!empty($coord['lat']) && !empty($coord['lon'])) {
756                         $location['latitude'] = $coord['lat'];
757                         $location['longitude'] = $coord['lon'];
758                 }
759
760                 return $location;
761         }
762
763         /**
764          * Returns a tag array for a given item array
765          *
766          * @param array $item
767          *
768          * @return array of tags
769          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
770          */
771         private static function createTagList($item)
772         {
773                 $tags = [];
774
775                 $terms = Term::tagArrayFromItemId($item['id']);
776                 foreach ($terms as $term) {
777                         if ($term['type'] == TERM_HASHTAG) {
778                                 $url = System::baseUrl() . '/search?tag=' . urlencode($term['term']);
779                                 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
780                         } elseif ($term['type'] == TERM_MENTION) {
781                                 $contact = Contact::getDetailsByURL($term['url']);
782                                 if (!empty($contact['addr'])) {
783                                         $mention = '@' . $contact['addr'];
784                                 } else {
785                                         $mention = '@' . $term['url'];
786                                 }
787
788                                 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
789                         }
790                 }
791                 return $tags;
792         }
793
794         /**
795          * Adds attachment data to the JSON document
796          *
797          * @param array  $item Data of the item that is to be posted
798          * @param string $type Object type
799          *
800          * @return array with attachment data
801          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
802          */
803         private static function createAttachmentList($item, $type)
804         {
805                 $attachments = [];
806
807                 $arr = explode('[/attach],', $item['attach']);
808                 if (count($arr)) {
809                         foreach ($arr as $r) {
810                                 $matches = false;
811                                 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
812                                 if ($cnt) {
813                                         $attributes = ['type' => 'Document',
814                                                         'mediaType' => $matches[3],
815                                                         'url' => $matches[1],
816                                                         'name' => null];
817
818                                         if (trim($matches[4]) != '') {
819                                                 $attributes['name'] = trim($matches[4]);
820                                         }
821
822                                         $attachments[] = $attributes;
823                                 }
824                         }
825                 }
826
827                 if ($type != 'Note') {
828                         return $attachments;
829                 }
830
831                 // Simplify image codes
832                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
833
834                 // Grab all pictures and create attachments out of them
835                 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
836                         foreach ($pictures[1] as $picture) {
837                                 $imgdata = Image::getInfoFromURL($picture);
838                                 if ($imgdata) {
839                                         $attachments[] = ['type' => 'Document',
840                                                 'mediaType' => $imgdata['mime'],
841                                                 'url' => $picture,
842                                                 'name' => null];
843                                 }
844                         }
845                 }
846
847                 return $attachments;
848         }
849
850         /**
851          * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
852          *
853          * @param array $match Matching values for the callback
854          * @return string Replaced mention
855          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
856          */
857         private static function mentionCallback($match)
858         {
859                 if (empty($match[1])) {
860                         return;
861                 }
862
863                 $data = Contact::getDetailsByURL($match[1]);
864                 if (empty($data) || empty($data['nick'])) {
865                         return;
866                 }
867
868                 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
869         }
870
871         /**
872          * Remove image elements and replaces them with links to the image
873          *
874          * @param string $body
875          *
876          * @return string with replaced elements
877          */
878         private static function removePictures($body)
879         {
880                 // Simplify image codes
881                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
882
883                 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
884                 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
885
886                 return $body;
887         }
888
889         /**
890          * Fetches the "context" value for a givem item array from the "conversation" table
891          *
892          * @param array $item
893          *
894          * @return string with context url
895          * @throws \Exception
896          */
897         private static function fetchContextURLForItem($item)
898         {
899                 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
900                 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
901                         $context_uri = $conversation['conversation-href'];
902                 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
903                         $context_uri = $conversation['conversation-uri'];
904                 } else {
905                         $context_uri = $item['parent-uri'] . '#context';
906                 }
907                 return $context_uri;
908         }
909
910         /**
911          * Returns if the post contains sensitive content ("nsfw")
912          *
913          * @param integer $item_id
914          *
915          * @return boolean
916          * @throws \Exception
917          */
918         private static function isSensitive($item_id)
919         {
920                 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
921                 return DBA::exists('term', $condition);
922         }
923
924         /**
925          * Creates event data
926          *
927          * @param array $item
928          *
929          * @return array with the event data
930          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
931          */
932         public static function createEvent($item)
933         {
934                 $event = [];
935                 $event['name'] = $item['event-summary'];
936                 $event['content'] = BBCode::convert($item['event-desc'], false, 7);
937                 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
938
939                 if (!$item['event-nofinish']) {
940                         $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
941                 }
942
943                 if (!empty($item['event-location'])) {
944                         $item['location'] = $item['event-location'];
945                         $event['location'] = self::createLocation($item);
946                 }
947
948                 return $event;
949         }
950
951         /**
952          * Creates a note/article object array
953          *
954          * @param array $item
955          *
956          * @return array with the object data
957          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
958          * @throws \ImagickException
959          */
960         public static function createNote($item)
961         {
962                 if ($item['event-type'] == 'event') {
963                         $type = 'Event';
964                 } elseif (!empty($item['title'])) {
965                         $type = 'Article';
966                 } else {
967                         $type = 'Note';
968                 }
969
970                 if ($item['deleted']) {
971                         $type = 'Tombstone';
972                 }
973
974                 $data = [];
975                 $data['id'] = $item['uri'];
976                 $data['type'] = $type;
977
978                 if ($item['deleted']) {
979                         return $data;
980                 }
981
982                 $data['summary'] = null; // Ignore by now
983
984                 if ($item['uri'] != $item['thr-parent']) {
985                         $data['inReplyTo'] = $item['thr-parent'];
986                 } else {
987                         $data['inReplyTo'] = null;
988                 }
989
990                 $data['diaspora:guid'] = $item['guid'];
991                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
992
993                 if ($item['created'] != $item['edited']) {
994                         $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
995                 }
996
997                 $data['url'] = $item['plink'];
998                 $data['attributedTo'] = $item['author-link'];
999                 $data['sensitive'] = self::isSensitive($item['id']);
1000                 $data['context'] = self::fetchContextURLForItem($item);
1001
1002                 if (!empty($item['title'])) {
1003                         $data['name'] = BBCode::toPlaintext($item['title'], false);
1004                 }
1005
1006                 $body = $item['body'];
1007
1008                 if ($type == 'Note') {
1009                         $body = self::removePictures($body);
1010                 }
1011
1012                 if ($type == 'Event') {
1013                         $data = array_merge($data, self::createEvent($item));
1014                 } else {
1015                         $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1016                         $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1017
1018                         $data['content'] = BBCode::convert($body, false, 7);
1019                 }
1020
1021                 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1022
1023                 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1024                         $data['diaspora:comment'] = $item['signed_text'];
1025                 }
1026
1027                 $data['attachment'] = self::createAttachmentList($item, $type);
1028                 $data['tag'] = self::createTagList($item);
1029
1030                 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1031                         $data['location'] = self::createLocation($item);
1032                 }
1033
1034                 if (!empty($item['app'])) {
1035                         $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1036                 }
1037
1038                 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
1039
1040                 return $data;
1041         }
1042
1043         /**
1044          * Creates an announce object entry
1045          *
1046          * @param array $item
1047          *
1048          * @return string with announced object url
1049          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1050          * @throws \ImagickException
1051          */
1052         public static function createAnnounce($item)
1053         {
1054                 $announce = api_share_as_retweet($item);
1055                 if (empty($announce['plink'])) {
1056                         return self::createNote($item);
1057                 }
1058
1059                 // Fetch the original id of the object
1060                 $activity = ActivityPub::fetchContent($announce['plink'], $item['uid']);
1061                 if (!empty($activity)) {
1062                         $ldactivity = JsonLD::compact($activity);
1063                         $id = JsonLD::fetchElement($ldactivity, '@id');
1064                         if (!empty($id)) {
1065                                 return $id;
1066                         }
1067                 }
1068
1069                 return self::createNote($item);
1070         }
1071
1072         /**
1073          * Creates an activity id for a given contact id
1074          *
1075          * @param integer $cid Contact ID of target
1076          *
1077          * @return bool|string activity id
1078          */
1079         public static function activityIDFromContact($cid)
1080         {
1081                 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1082                 if (!DBA::isResult($contact)) {
1083                         return false;
1084                 }
1085
1086                 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1087                 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1088                 return System::baseUrl() . '/activity/' . $uuid;
1089         }
1090
1091         /**
1092          * Transmits a contact suggestion to a given inbox
1093          *
1094          * @param integer $uid           User ID
1095          * @param string  $inbox         Target inbox
1096          * @param integer $suggestion_id Suggestion ID
1097          *
1098          * @return boolean was the transmission successful?
1099          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1100          */
1101         public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1102         {
1103                 $owner = User::getOwnerDataById($uid);
1104
1105                 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
1106
1107                 $data = ['@context' => ActivityPub::CONTEXT,
1108                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1109                         'type' => 'Announce',
1110                         'actor' => $owner['url'],
1111                         'object' => $suggestion['url'],
1112                         'content' => $suggestion['note'],
1113                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1114                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1115                         'cc' => []];
1116
1117                 $signed = LDSignature::sign($data, $owner);
1118
1119                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1120                 return HTTPSignature::transmit($signed, $inbox, $uid);
1121         }
1122
1123         /**
1124          * Transmits a profile relocation to a given inbox
1125          *
1126          * @param integer $uid   User ID
1127          * @param string  $inbox Target inbox
1128          *
1129          * @return boolean was the transmission successful?
1130          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1131          */
1132         public static function sendProfileRelocation($uid, $inbox)
1133         {
1134                 $owner = User::getOwnerDataById($uid);
1135
1136                 $data = ['@context' => ActivityPub::CONTEXT,
1137                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1138                         'type' => 'dfrn:relocate',
1139                         'actor' => $owner['url'],
1140                         'object' => $owner['url'],
1141                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1142                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1143                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1144                         'cc' => []];
1145
1146                 $signed = LDSignature::sign($data, $owner);
1147
1148                 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1149                 return HTTPSignature::transmit($signed, $inbox, $uid);
1150         }
1151
1152         /**
1153          * Transmits a profile deletion to a given inbox
1154          *
1155          * @param integer $uid   User ID
1156          * @param string  $inbox Target inbox
1157          *
1158          * @return boolean was the transmission successful?
1159          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1160          */
1161         public static function sendProfileDeletion($uid, $inbox)
1162         {
1163                 $owner = User::getOwnerDataById($uid);
1164
1165                 $data = ['@context' => ActivityPub::CONTEXT,
1166                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1167                         'type' => 'Delete',
1168                         'actor' => $owner['url'],
1169                         'object' => $owner['url'],
1170                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1171                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1172                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1173                         'cc' => []];
1174
1175                 $signed = LDSignature::sign($data, $owner);
1176
1177                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1178                 return HTTPSignature::transmit($signed, $inbox, $uid);
1179         }
1180
1181         /**
1182          * Transmits a profile change to a given inbox
1183          *
1184          * @param integer $uid   User ID
1185          * @param string  $inbox Target inbox
1186          *
1187          * @return boolean was the transmission successful?
1188          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1189          * @throws \ImagickException
1190          */
1191         public static function sendProfileUpdate($uid, $inbox)
1192         {
1193                 $owner = User::getOwnerDataById($uid);
1194                 $profile = APContact::getByURL($owner['url']);
1195
1196                 $data = ['@context' => ActivityPub::CONTEXT,
1197                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1198                         'type' => 'Update',
1199                         'actor' => $owner['url'],
1200                         'object' => self::getProfile($uid),
1201                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1202                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1203                         'to' => [$profile['followers']],
1204                         'cc' => []];
1205
1206                 $signed = LDSignature::sign($data, $owner);
1207
1208                 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1209                 return HTTPSignature::transmit($signed, $inbox, $uid);
1210         }
1211
1212         /**
1213          * Transmits a given activity to a target
1214          *
1215          * @param string  $activity Type name
1216          * @param string  $target   Target profile
1217          * @param integer $uid      User ID
1218          * @return bool
1219          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1220          * @throws \ImagickException
1221          * @throws \Exception
1222          */
1223         public static function sendActivity($activity, $target, $uid, $id = '')
1224         {
1225                 $profile = APContact::getByURL($target);
1226
1227                 $owner = User::getOwnerDataById($uid);
1228
1229                 if (empty($id)) {
1230                         $id = System::baseUrl() . '/activity/' . System::createGUID();
1231                 }
1232
1233                 $data = ['@context' => ActivityPub::CONTEXT,
1234                         'id' => $id,
1235                         'type' => $activity,
1236                         'actor' => $owner['url'],
1237                         'object' => $profile['url'],
1238                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1239                         'to' => [$profile['url']]];
1240
1241                 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1242
1243                 $signed = LDSignature::sign($data, $owner);
1244                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1245         }
1246
1247         /**
1248          * Transmit a message that the contact request had been accepted
1249          *
1250          * @param string  $target Target profile
1251          * @param         $id
1252          * @param integer $uid    User ID
1253          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1254          * @throws \ImagickException
1255          */
1256         public static function sendContactAccept($target, $id, $uid)
1257         {
1258                 $profile = APContact::getByURL($target);
1259
1260                 $owner = User::getOwnerDataById($uid);
1261                 $data = ['@context' => ActivityPub::CONTEXT,
1262                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1263                         'type' => 'Accept',
1264                         'actor' => $owner['url'],
1265                         'object' => ['id' => $id, 'type' => 'Follow',
1266                                 'actor' => $profile['url'],
1267                                 'object' => $owner['url']],
1268                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1269                         'to' => [$profile['url']]];
1270
1271                 Logger::log('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1272
1273                 $signed = LDSignature::sign($data, $owner);
1274                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1275         }
1276
1277         /**
1278          * Reject a contact request or terminates the contact relation
1279          *
1280          * @param string  $target Target profile
1281          * @param         $id
1282          * @param integer $uid    User ID
1283          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1284          * @throws \ImagickException
1285          */
1286         public static function sendContactReject($target, $id, $uid)
1287         {
1288                 $profile = APContact::getByURL($target);
1289
1290                 $owner = User::getOwnerDataById($uid);
1291                 $data = ['@context' => ActivityPub::CONTEXT,
1292                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1293                         'type' => 'Reject',
1294                         'actor' => $owner['url'],
1295                         'object' => ['id' => $id, 'type' => 'Follow',
1296                                 'actor' => $profile['url'],
1297                                 'object' => $owner['url']],
1298                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1299                         'to' => [$profile['url']]];
1300
1301                 Logger::log('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1302
1303                 $signed = LDSignature::sign($data, $owner);
1304                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1305         }
1306
1307         /**
1308          * Transmits a message that we don't want to follow this contact anymore
1309          *
1310          * @param string  $target Target profile
1311          * @param integer $uid    User ID
1312          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1313          * @throws \ImagickException
1314          * @throws \Exception
1315          */
1316         public static function sendContactUndo($target, $cid, $uid)
1317         {
1318                 $profile = APContact::getByURL($target);
1319
1320                 $object_id = self::activityIDFromContact($cid);
1321                 if (empty($object_id)) {
1322                         return;
1323                 }
1324
1325                 $id = System::baseUrl() . '/activity/' . System::createGUID();
1326
1327                 $owner = User::getOwnerDataById($uid);
1328                 $data = ['@context' => ActivityPub::CONTEXT,
1329                         'id' => $id,
1330                         'type' => 'Undo',
1331                         'actor' => $owner['url'],
1332                         'object' => ['id' => $object_id, 'type' => 'Follow',
1333                                 'actor' => $owner['url'],
1334                                 'object' => $profile['url']],
1335                         'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1336                         'to' => [$profile['url']]];
1337
1338                 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1339
1340                 $signed = LDSignature::sign($data, $owner);
1341                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1342         }
1343 }