]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Transmitter.php
7fc0f5d990957df5181ac929d606e45145b8c0cd
[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\Content\Feature;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Content\Text\Plaintext;
11 use Friendica\Core\Cache;
12 use Friendica\Core\Config;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Protocol;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\APContact;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Conversation;
20 use Friendica\Model\Item;
21 use Friendica\Model\Profile;
22 use Friendica\Model\Photo;
23 use Friendica\Model\Term;
24 use Friendica\Model\User;
25 use Friendica\Protocol\Activity;
26 use Friendica\Protocol\ActivityPub;
27 use Friendica\Util\DateTimeFormat;
28 use Friendica\Util\HTTPSignature;
29 use Friendica\Util\Images;
30 use Friendica\Util\JsonLD;
31 use Friendica\Util\LDSignature;
32 use Friendica\Util\Map;
33 use Friendica\Util\Network;
34 use Friendica\Util\XML;
35
36 require_once 'include/api.php';
37 require_once 'mod/share.php';
38
39 /**
40  * @brief ActivityPub Transmitter Protocol class
41  *
42  * To-Do:
43  * - Undo Announce
44  */
45 class Transmitter
46 {
47         /**
48          * collects the lost of followers of the given owner
49          *
50          * @param array   $owner Owner array
51          * @param integer $page  Page number
52          *
53          * @return array of owners
54          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
55          */
56         public static function getFollowers($owner, $page = null)
57         {
58                 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
59                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
60                 $count = DBA::count('contact', $condition);
61
62                 $data = ['@context' => ActivityPub::CONTEXT];
63                 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
64                 $data['type'] = 'OrderedCollection';
65                 $data['totalItems'] = $count;
66
67                 // When we hide our friends we will only show the pure number but don't allow more.
68                 $profile = Profile::getByUID($owner['uid']);
69                 if (!empty($profile['hide-friends'])) {
70                         return $data;
71                 }
72
73                 if (empty($page)) {
74                         $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
75                 } else {
76                         $data['type'] = 'OrderedCollectionPage';
77                         $list = [];
78
79                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
80                         while ($contact = DBA::fetch($contacts)) {
81                                 $list[] = $contact['url'];
82                         }
83
84                         if (!empty($list)) {
85                                 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
86                         }
87
88                         $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
89
90                         $data['orderedItems'] = $list;
91                 }
92
93                 return $data;
94         }
95
96         /**
97          * Create list of following contacts
98          *
99          * @param array   $owner Owner array
100          * @param integer $page  Page numbe
101          *
102          * @return array of following contacts
103          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
104          */
105         public static function getFollowing($owner, $page = null)
106         {
107                 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
108                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
109                 $count = DBA::count('contact', $condition);
110
111                 $data = ['@context' => ActivityPub::CONTEXT];
112                 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
113                 $data['type'] = 'OrderedCollection';
114                 $data['totalItems'] = $count;
115
116                 // When we hide our friends we will only show the pure number but don't allow more.
117                 $profile = Profile::getByUID($owner['uid']);
118                 if (!empty($profile['hide-friends'])) {
119                         return $data;
120                 }
121
122                 if (empty($page)) {
123                         $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
124                 } else {
125                         $data['type'] = 'OrderedCollectionPage';
126                         $list = [];
127
128                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
129                         while ($contact = DBA::fetch($contacts)) {
130                                 $list[] = $contact['url'];
131                         }
132
133                         if (!empty($list)) {
134                                 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
135                         }
136
137                         $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
138
139                         $data['orderedItems'] = $list;
140                 }
141
142                 return $data;
143         }
144
145         /**
146          * Public posts for the given owner
147          *
148          * @param array   $owner Owner array
149          * @param integer $page  Page numbe
150          *
151          * @return array of posts
152          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
153          * @throws \ImagickException
154          */
155         public static function getOutbox($owner, $page = null)
156         {
157                 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
158
159                 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
160                         'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
161                         'deleted' => false, 'visible' => true, 'moderated' => false];
162                 $count = DBA::count('item', $condition);
163
164                 $data = ['@context' => ActivityPub::CONTEXT];
165                 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
166                 $data['type'] = 'OrderedCollection';
167                 $data['totalItems'] = $count;
168
169                 if (empty($page)) {
170                         $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
171                 } else {
172                         $data['type'] = 'OrderedCollectionPage';
173                         $list = [];
174
175                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
176
177                         $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
178                         while ($item = Item::fetch($items)) {
179                                 $object = self::createObjectFromItemID($item['id']);
180                                 unset($object['@context']);
181                                 $list[] = $object;
182                         }
183
184                         if (!empty($list)) {
185                                 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
186                         }
187
188                         $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
189
190                         $data['orderedItems'] = $list;
191                 }
192
193                 return $data;
194         }
195
196         /**
197          * Return the service array containing information the used software and it's url
198          *
199          * @return array with service data
200          */
201         private static function getService()
202         {
203                 return ['type' => 'Service',
204                         'name' =>  FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
205                         'url' => BaseObject::getApp()->getBaseURL()];
206         }
207
208         /**
209          * Return the ActivityPub profile of the given user
210          *
211          * @param integer $uid User ID
212          * @return array with profile data
213          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
214          */
215         public static function getProfile($uid)
216         {
217                 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
218                         'account_removed' => false, 'verified' => true];
219                 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
220                 $user = DBA::selectFirst('user', $fields, $condition);
221                 if (!DBA::isResult($user)) {
222                         return [];
223                 }
224
225                 $fields = ['locality', 'region', 'country-name'];
226                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
227                 if (!DBA::isResult($profile)) {
228                         return [];
229                 }
230
231                 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
232                 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
233                 if (!DBA::isResult($contact)) {
234                         return [];
235                 }
236
237                 $data = ['@context' => ActivityPub::CONTEXT];
238                 $data['id'] = $contact['url'];
239                 $data['diaspora:guid'] = $user['guid'];
240                 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
241                 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
242                 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
243                 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
244                 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
245                 $data['preferredUsername'] = $user['nickname'];
246                 $data['name'] = $contact['name'];
247                 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
248                         'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
249                 $data['summary'] = $contact['about'];
250                 $data['url'] = $contact['url'];
251                 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
252                 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
253                         'owner' => $contact['url'],
254                         'publicKeyPem' => $user['pubkey']];
255                 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
256                 $data['icon'] = ['type' => 'Image',
257                         'url' => $contact['photo']];
258
259                 $data['generator'] = self::getService();
260
261                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
262                 return $data;
263         }
264
265         /**
266          * @param string $username
267          * @return array
268          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
269          */
270         public static function getDeletedUser($username)
271         {
272                 return [
273                         '@context' => ActivityPub::CONTEXT,
274                         'id' => System::baseUrl() . '/profile/' . $username,
275                         'type' => 'Tombstone',
276                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
277                         'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
278                         'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
279                 ];
280         }
281
282         /**
283          * Returns an array with permissions of a given item array
284          *
285          * @param array $item
286          *
287          * @return array with permissions
288          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
289          * @throws \ImagickException
290          */
291         private static function fetchPermissionBlockFromConversation($item)
292         {
293                 if (empty($item['thr-parent'])) {
294                         return [];
295                 }
296
297                 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
298                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
299                 if (!DBA::isResult($conversation)) {
300                         return [];
301                 }
302
303                 $activity = json_decode($conversation['source'], true);
304
305                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
306                 $profile = APContact::getByURL($actor);
307
308                 $item_profile = APContact::getByURL($item['author-link']);
309                 $exclude[] = $item['author-link'];
310
311                 if ($item['gravity'] == GRAVITY_PARENT) {
312                         $exclude[] = $item['owner-link'];
313                 }
314
315                 $permissions['to'][] = $actor;
316
317                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
318                         if (empty($activity[$element])) {
319                                 continue;
320                         }
321                         if (is_string($activity[$element])) {
322                                 $activity[$element] = [$activity[$element]];
323                         }
324
325                         foreach ($activity[$element] as $receiver) {
326                                 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
327                                         $permissions[$element][] = $item_profile['followers'];
328                                 } elseif (!in_array($receiver, $exclude)) {
329                                         $permissions[$element][] = $receiver;
330                                 }
331                         }
332                 }
333                 return $permissions;
334         }
335
336         /**
337          * Creates an array of permissions from an item thread
338          *
339          * @param array   $item       Item array
340          * @param boolean $blindcopy  addressing via "bcc" or "cc"?
341          * @param integer $last_id    Last item id for adding receivers
342          * @param boolean $forum_mode "true" means that we are sending content to a forum
343          *
344          * @return array with permission data
345          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
346          * @throws \ImagickException
347          */
348         private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
349         {
350                 if ($last_id == 0) {
351                         $last_id = $item['id'];
352                 }
353
354                 $always_bcc = false;
355
356                 // Check if we should always deliver our stuff via BCC
357                 if (!empty($item['uid'])) {
358                         $profile = Profile::getByUID($item['uid']);
359                         if (!empty($profile)) {
360                                 $always_bcc = $profile['hide-friends'];
361                         }
362                 }
363
364                 if (Config::get('debug', 'total_ap_delivery')) {
365                         // Will be activated in a later step
366                         $networks = Protocol::FEDERATED;
367                 } else {
368                         // For now only send to these contacts:
369                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
370                 }
371
372                 $data = ['to' => [], 'cc' => [], 'bcc' => []];
373
374                 if ($item['gravity'] == GRAVITY_PARENT) {
375                         $actor_profile = APContact::getByURL($item['owner-link']);
376                 } else {
377                         $actor_profile = APContact::getByURL($item['author-link']);
378                 }
379
380                 $terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
381
382                 if (!$item['private']) {
383                         $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
384
385                         $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
386
387                         foreach ($terms as $term) {
388                                 $profile = APContact::getByURL($term['url'], false);
389                                 if (!empty($profile)) {
390                                         $data['to'][] = $profile['url'];
391                                 }
392                         }
393                 } else {
394                         $receiver_list = Item::enumeratePermissions($item, true);
395
396                         foreach ($terms as $term) {
397                                 $cid = Contact::getIdForURL($term['url'], $item['uid']);
398                                 if (!empty($cid) && in_array($cid, $receiver_list)) {
399                                         $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
400                                         if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
401                                                 continue;
402                                         }
403
404                                         if (!empty($profile = APContact::getByURL($contact['url'], false))) {
405                                                 $data['to'][] = $profile['url'];
406                                         }
407                                 }
408                         }
409
410                         foreach ($receiver_list as $receiver) {
411                                 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
412                                 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
413                                         continue;
414                                 }
415
416                                 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
417                                         if ($contact['hidden'] || $always_bcc) {
418                                                 $data['bcc'][] = $profile['url'];
419                                         } else {
420                                                 $data['cc'][] = $profile['url'];
421                                         }
422                                 }
423                         }
424                 }
425
426                 if (!empty($item['parent'])) {
427                         $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
428                         while ($parent = Item::fetch($parents)) {
429                                 if ($parent['gravity'] == GRAVITY_PARENT) {
430                                         $profile = APContact::getByURL($parent['owner-link'], false);
431                                         if (!empty($profile)) {
432                                                 if ($item['gravity'] != GRAVITY_PARENT) {
433                                                         // Comments to forums are directed to the forum
434                                                         // But comments to forums aren't directed to the followers collection
435                                                         if ($profile['type'] == 'Group') {
436                                                                 $data['to'][] = $profile['url'];
437                                                         } else {
438                                                                 $data['cc'][] = $profile['url'];
439                                                                 if (!$item['private'] && !empty($actor_profile['followers'])) {
440                                                                         $data['cc'][] = $actor_profile['followers'];
441                                                                 }
442                                                         }
443                                                 } else {
444                                                         // Public thread parent post always are directed to the followers
445                                                         if (!$item['private'] && !$forum_mode) {
446                                                                 $data['cc'][] = $actor_profile['followers'];
447                                                         }
448                                                 }
449                                         }
450                                 }
451
452                                 // Don't include data from future posts
453                                 if ($parent['id'] >= $last_id) {
454                                         continue;
455                                 }
456
457                                 $profile = APContact::getByURL($parent['author-link'], false);
458                                 if (!empty($profile)) {
459                                         if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
460                                                 $data['to'][] = $profile['url'];
461                                         } else {
462                                                 $data['cc'][] = $profile['url'];
463                                         }
464                                 }
465                         }
466                         DBA::close($parents);
467                 }
468
469                 $data['to'] = array_unique($data['to']);
470                 $data['cc'] = array_unique($data['cc']);
471                 $data['bcc'] = array_unique($data['bcc']);
472
473                 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
474                         unset($data['to'][$key]);
475                 }
476
477                 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
478                         unset($data['cc'][$key]);
479                 }
480
481                 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
482                         unset($data['bcc'][$key]);
483                 }
484
485                 foreach ($data['to'] as $to) {
486                         if (($key = array_search($to, $data['cc'])) !== false) {
487                                 unset($data['cc'][$key]);
488                         }
489
490                         if (($key = array_search($to, $data['bcc'])) !== false) {
491                                 unset($data['bcc'][$key]);
492                         }
493                 }
494
495                 foreach ($data['cc'] as $cc) {
496                         if (($key = array_search($cc, $data['bcc'])) !== false) {
497                                 unset($data['bcc'][$key]);
498                         }
499                 }
500
501                 return ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
502         }
503
504         /**
505          * Check if an inbox is archived
506          *
507          * @param string $url Inbox url
508          *
509          * @return boolean "true" if inbox is archived
510          */
511         private static function archivedInbox($url)
512         {
513                 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
514         }
515
516         /**
517          * Fetches a list of inboxes of followers of a given user
518          *
519          * @param integer $uid      User ID
520          * @param boolean $personal fetch personal inboxes
521          *
522          * @return array of follower inboxes
523          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
524          * @throws \ImagickException
525          */
526         public static function fetchTargetInboxesforUser($uid, $personal = false)
527         {
528                 $inboxes = [];
529
530                 if (Config::get('debug', 'total_ap_delivery')) {
531                         // Will be activated in a later step
532                         $networks = Protocol::FEDERATED;
533                 } else {
534                         // For now only send to these contacts:
535                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
536                 }
537
538                 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
539
540                 if (!empty($uid)) {
541                         $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
542                 }
543
544                 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
545                 while ($contact = DBA::fetch($contacts)) {
546                         if (Contact::isLocal($contact['url'])) {
547                                 continue;
548                         }
549
550                         if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
551                                 continue;
552                         }
553
554                         if (Network::isUrlBlocked($contact['url'])) {
555                                 continue;
556                         }
557
558                         $profile = APContact::getByURL($contact['url'], false);
559                         if (!empty($profile)) {
560                                 if (empty($profile['sharedinbox']) || $personal) {
561                                         $target = $profile['inbox'];
562                                 } else {
563                                         $target = $profile['sharedinbox'];
564                                 }
565                                 if (!self::archivedInbox($target)) {
566                                         $inboxes[$target] = $target;
567                                 }
568                         }
569                 }
570                 DBA::close($contacts);
571
572                 return $inboxes;
573         }
574
575         /**
576          * Fetches an array of inboxes for the given item and user
577          *
578          * @param array   $item       Item array
579          * @param integer $uid        User ID
580          * @param boolean $personal   fetch personal inboxes
581          * @param integer $last_id    Last item id for adding receivers
582          * @param boolean $forum_mode "true" means that we are sending content to a forum
583          * @return array with inboxes
584          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
585          * @throws \ImagickException
586          */
587         public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
588         {
589                 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
590                 if (empty($permissions)) {
591                         return [];
592                 }
593
594                 $inboxes = [];
595
596                 if ($item['gravity'] == GRAVITY_ACTIVITY) {
597                         $item_profile = APContact::getByURL($item['author-link'], false);
598                 } else {
599                         $item_profile = APContact::getByURL($item['owner-link'], false);
600                 }
601
602                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
603                         if (empty($permissions[$element])) {
604                                 continue;
605                         }
606
607                         $blindcopy = in_array($element, ['bto', 'bcc']);
608
609                         foreach ($permissions[$element] as $receiver) {
610                                 if (Network::isUrlBlocked($receiver)) {
611                                         continue;
612                                 }
613
614                                 if ($receiver == $item_profile['followers']) {
615                                         $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
616                                 } else {
617                                         if (Contact::isLocal($receiver)) {
618                                                 continue;
619                                         }
620
621                                         $profile = APContact::getByURL($receiver, false);
622                                         if (!empty($profile)) {
623                                                 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
624                                                         $target = $profile['inbox'];
625                                                 } else {
626                                                         $target = $profile['sharedinbox'];
627                                                 }
628                                                 if (!self::archivedInbox($target)) {
629                                                         $inboxes[$target] = $target;
630                                                 }
631                                         }
632                                 }
633                         }
634                 }
635
636                 return $inboxes;
637         }
638
639         /**
640          * Creates an array in the structure of the item table for a given mail id
641          *
642          * @param integer $mail_id
643          *
644          * @return array
645          * @throws \Exception
646          */
647         public static function ItemArrayFromMail($mail_id)
648         {
649                 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
650                 if (!DBA::isResult($mail)) {
651                         return [];
652                 }
653
654                 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
655
656                 // Making the post more compatible for Mastodon by:
657                 // - Making it a note and not an article (no title)
658                 // - Moving the title into the "summary" field that is used as a "content warning"
659                 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
660                 $mail['title'] = '';
661
662                 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
663                 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
664                 $mail['allow_gid'] = '';
665                 $mail['deny_cid'] = '';
666                 $mail['deny_gid'] = '';
667                 $mail['private'] = true;
668                 $mail['deleted'] = false;
669                 $mail['edited'] = $mail['created'];
670                 $mail['plink'] = $mail['uri'];
671                 $mail['thr-parent'] = $reply['uri'];
672                 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
673
674                 $mail['event-type'] = '';
675                 $mail['attach'] = '';
676
677                 $mail['parent'] = 0;
678
679                 return $mail;
680         }
681
682         /**
683          * Creates an activity array for a given mail id
684          *
685          * @param integer $mail_id
686          * @param boolean $object_mode Is the activity item is used inside another object?
687          *
688          * @return array of activity
689          * @throws \Exception
690          */
691         public static function createActivityFromMail($mail_id, $object_mode = false)
692         {
693                 $mail = self::ItemArrayFromMail($mail_id);
694                 $object = self::createNote($mail);
695
696                 if (!empty($object['cc'])) {
697                         $object['to'] = array_merge($object['to'], $object['cc']);
698                         unset($object['cc']);
699                 }
700
701                 if (!empty($object['bcc'])) {
702                         $object['to'] = array_merge($object['to'], $object['bcc']);
703                         unset($object['bcc']);
704                 }
705
706                 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => 'test']];
707
708                 if (!$object_mode) {
709                         $data = ['@context' => ActivityPub::CONTEXT];
710                 } else {
711                         $data = [];
712                 }
713
714                 $data['id'] = $mail['uri'] . '#Create';
715                 $data['type'] = 'Create';
716                 $data['actor'] = $mail['author-link'];
717                 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
718                 $data['instrument'] = self::getService();
719                 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
720
721                 if (empty($data['to']) && !empty($data['cc'])) {
722                         $data['to'] = $data['cc'];
723                 }
724
725                 if (empty($data['to']) && !empty($data['bcc'])) {
726                         $data['to'] = $data['bcc'];
727                 }
728
729                 unset($data['cc']);
730                 unset($data['bcc']);
731
732                 $object['to'] = $data['to'];
733                 unset($object['cc']);
734                 unset($object['bcc']);
735
736                 $data['directMessage'] = true;
737
738                 $data['object'] = $object;
739
740                 $owner = User::getOwnerDataById($mail['uid']);
741
742                 if (!$object_mode && !empty($owner)) {
743                         return LDSignature::sign($data, $owner);
744                 } else {
745                         return $data;
746                 }
747         }
748
749         /**
750          * Returns the activity type of a given item
751          *
752          * @param array $item
753          *
754          * @return string with activity type
755          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
756          * @throws \ImagickException
757          */
758         private static function getTypeOfItem($item)
759         {
760                 $reshared = false;
761
762                 // Only check for a reshare, if it is a real reshare and no quoted reshare
763                 if (strpos($item['body'], "[share") === 0) {
764                         $announce = api_share_as_retweet($item);
765                         $reshared = !empty($announce['plink']);
766                 }
767
768                 if ($reshared) {
769                         $type = 'Announce';
770                 } elseif ($item['verb'] == Activity::POST) {
771                         if ($item['created'] == $item['edited']) {
772                                 $type = 'Create';
773                         } else {
774                                 $type = 'Update';
775                         }
776                 } elseif ($item['verb'] == Activity::LIKE) {
777                         $type = 'Like';
778                 } elseif ($item['verb'] == Activity::DISLIKE) {
779                         $type = 'Dislike';
780                 } elseif ($item['verb'] == Activity::ATTEND) {
781                         $type = 'Accept';
782                 } elseif ($item['verb'] == Activity::ATTENDNO) {
783                         $type = 'Reject';
784                 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
785                         $type = 'TentativeAccept';
786                 } elseif ($item['verb'] == Activity::FOLLOW) {
787                         $type = 'Follow';
788                 } elseif ($item['verb'] == Activity::TAG) {
789                         $type = 'Add';
790                 } else {
791                         $type = '';
792                 }
793
794                 return $type;
795         }
796
797         /**
798          * Creates the activity or fetches it from the cache
799          *
800          * @param integer $item_id
801          * @param boolean $force Force new cache entry
802          *
803          * @return array with the activity
804          * @throws \Exception
805          */
806         public static function createCachedActivityFromItem($item_id, $force = false)
807         {
808                 $cachekey = 'APDelivery:createActivity:' . $item_id;
809
810                 if (!$force) {
811                         $data = Cache::get($cachekey);
812                         if (!is_null($data)) {
813                                 return $data;
814                         }
815                 }
816
817                 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
818
819                 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
820                 return $data;
821         }
822
823         /**
824          * Creates an activity array for a given item id
825          *
826          * @param integer $item_id
827          * @param boolean $object_mode Is the activity item is used inside another object?
828          *
829          * @return array of activity
830          * @throws \Exception
831          */
832         public static function createActivityFromItem($item_id, $object_mode = false)
833         {
834                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
835
836                 if (!DBA::isResult($item)) {
837                         return false;
838                 }
839
840                 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
841                         $owner = User::getOwnerDataById($item['uid']);
842                         if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
843                                 $type = 'Announce';
844
845                                 // Disguise forum posts as reshares. Will later be converted to a real announce
846                                 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
847                                         $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
848                         }
849                 }
850
851                 if (empty($type)) {
852                         $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
853                         $conversation = DBA::selectFirst('conversation', ['source'], $condition);
854                         if (DBA::isResult($conversation)) {
855                                 $data = json_decode($conversation['source'], true);
856                                 if (!empty($data)) {
857                                         return $data;
858                                 }
859                         }
860
861                         $type = self::getTypeOfItem($item);
862                 }
863
864                 if (!$object_mode) {
865                         $data = ['@context' => ActivityPub::CONTEXT];
866
867                         if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
868                                 $type = 'Undo';
869                         } elseif ($item['deleted']) {
870                                 $type = 'Delete';
871                         }
872                 } else {
873                         $data = [];
874                 }
875
876                 $data['id'] = $item['uri'] . '#' . $type;
877                 $data['type'] = $type;
878
879                 if (Item::isForumPost($item) && ($type != 'Announce')) {
880                         $data['actor'] = $item['author-link'];
881                 } else {
882                         $data['actor'] = $item['owner-link'];
883                 }
884
885                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
886
887                 $data['instrument'] = self::getService();
888
889                 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
890
891                 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
892                         $data['object'] = self::createNote($item);
893                 } elseif ($data['type'] == 'Add') {
894                         $data = self::createAddTag($item, $data);
895                 } elseif ($data['type'] == 'Announce') {
896                         $data = self::createAnnounce($item, $data);
897                 } elseif ($data['type'] == 'Follow') {
898                         $data['object'] = $item['parent-uri'];
899                 } elseif ($data['type'] == 'Undo') {
900                         $data['object'] = self::createActivityFromItem($item_id, true);
901                 } else {
902                         $data['diaspora:guid'] = $item['guid'];
903                         if (!empty($item['signed_text'])) {
904                                 $data['diaspora:like'] = $item['signed_text'];
905                         }
906                         $data['object'] = $item['thr-parent'];
907                 }
908
909                 if (!empty($item['contact-uid'])) {
910                         $uid = $item['contact-uid'];
911                 } else {
912                         $uid = $item['uid'];
913                 }
914
915                 $owner = User::getOwnerDataById($uid);
916
917                 if (!$object_mode && !empty($owner)) {
918                         return LDSignature::sign($data, $owner);
919                 } else {
920                         return $data;
921                 }
922
923                 /// @todo Create "conversation" entry
924         }
925
926         /**
927          * Creates an object array for a given item id
928          *
929          * @param integer $item_id
930          *
931          * @return array with the object data
932          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
933          * @throws \ImagickException
934          */
935         public static function createObjectFromItemID($item_id)
936         {
937                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
938
939                 if (!DBA::isResult($item)) {
940                         return false;
941                 }
942
943                 $data = ['@context' => ActivityPub::CONTEXT];
944                 $data = array_merge($data, self::createNote($item));
945
946                 return $data;
947         }
948
949         /**
950          * Creates a location entry for a given item array
951          *
952          * @param array $item
953          *
954          * @return array with location array
955          */
956         private static function createLocation($item)
957         {
958                 $location = ['type' => 'Place'];
959
960                 if (!empty($item['location'])) {
961                         $location['name'] = $item['location'];
962                 }
963
964                 $coord = [];
965
966                 if (empty($item['coord'])) {
967                         $coord = Map::getCoordinates($item['location']);
968                 } else {
969                         $coords = explode(' ', $item['coord']);
970                         if (count($coords) == 2) {
971                                 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
972                         }
973                 }
974
975                 if (!empty($coord['lat']) && !empty($coord['lon'])) {
976                         $location['latitude'] = $coord['lat'];
977                         $location['longitude'] = $coord['lon'];
978                 }
979
980                 return $location;
981         }
982
983         /**
984          * Returns a tag array for a given item array
985          *
986          * @param array $item
987          *
988          * @return array of tags
989          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
990          */
991         private static function createTagList($item)
992         {
993                 $tags = [];
994
995                 $terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]);
996                 foreach ($terms as $term) {
997                         if ($term['type'] == Term::HASHTAG) {
998                                 $url = System::baseUrl() . '/search?tag=' . urlencode($term['term']);
999                                 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
1000                         } elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) {
1001                                 $contact = Contact::getDetailsByURL($term['url']);
1002                                 if (!empty($contact['addr'])) {
1003                                         $mention = '@' . $contact['addr'];
1004                                 } else {
1005                                         $mention = '@' . $term['url'];
1006                                 }
1007
1008                                 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1009                         }
1010                 }
1011                 return $tags;
1012         }
1013
1014         /**
1015          * Adds attachment data to the JSON document
1016          *
1017          * @param array  $item Data of the item that is to be posted
1018          * @param string $type Object type
1019          *
1020          * @return array with attachment data
1021          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1022          */
1023         private static function createAttachmentList($item, $type)
1024         {
1025                 $attachments = [];
1026
1027                 // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
1028                 // It will be reactivated, once this cleared.
1029                 /*
1030                 $attach_data = BBCode::getAttachmentData($item['body']);
1031                 if (!empty($attach_data['url'])) {
1032                         $attachment = ['type' => 'Page',
1033                                 'mediaType' => 'text/html',
1034                                 'url' => $attach_data['url']];
1035
1036                         if (!empty($attach_data['title'])) {
1037                                 $attachment['name'] = $attach_data['title'];
1038                         }
1039
1040                         if (!empty($attach_data['description'])) {
1041                                 $attachment['summary'] = $attach_data['description'];
1042                         }
1043
1044                         if (!empty($attach_data['image'])) {
1045                                 $imgdata = Images::getInfoFromURLCached($attach_data['image']);
1046                                 if ($imgdata) {
1047                                         $attachment['icon'] = ['type' => 'Image',
1048                                                 'mediaType' => $imgdata['mime'],
1049                                                 'width' => $imgdata[0],
1050                                                 'height' => $imgdata[1],
1051                                                 'url' => $attach_data['image']];
1052                                 }
1053                         }
1054
1055                         $attachments[] = $attachment;
1056                 }
1057                 */
1058                 $arr = explode('[/attach],', $item['attach']);
1059                 if (count($arr)) {
1060                         foreach ($arr as $r) {
1061                                 $matches = false;
1062                                 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1063                                 if ($cnt) {
1064                                         $attributes = ['type' => 'Document',
1065                                                         'mediaType' => $matches[3],
1066                                                         'url' => $matches[1],
1067                                                         'name' => null];
1068
1069                                         if (trim($matches[4]) != '') {
1070                                                 $attributes['name'] = trim($matches[4]);
1071                                         }
1072
1073                                         $attachments[] = $attributes;
1074                                 }
1075                         }
1076                 }
1077
1078                 if ($type != 'Note') {
1079                         return $attachments;
1080                 }
1081
1082                 // Simplify image codes
1083                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1084
1085                 // Grab all pictures without alternative descriptions and create attachments out of them
1086                 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1087                         foreach ($pictures[1] as $picture) {
1088                                 $imgdata = Images::getInfoFromURLCached($picture);
1089                                 if ($imgdata) {
1090                                         $attachments[] = ['type' => 'Document',
1091                                                 'mediaType' => $imgdata['mime'],
1092                                                 'url' => $picture,
1093                                                 'name' => null];
1094                                 }
1095                         }
1096                 }
1097
1098                 // Grab all pictures with alternative description and create attachments out of them
1099                 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1100                         foreach ($pictures as $picture) {
1101                                 $imgdata = Images::getInfoFromURLCached($picture[1]);
1102                                 if ($imgdata) {
1103                                         $attachments[] = ['type' => 'Document',
1104                                                 'mediaType' => $imgdata['mime'],
1105                                                 'url' => $picture[1],
1106                                                 'name' => $picture[2]];
1107                                 }
1108                         }
1109                 }
1110
1111                 return $attachments;
1112         }
1113
1114         /**
1115          * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
1116          *
1117          * @param array $match Matching values for the callback
1118          * @return string Replaced mention
1119          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1120          */
1121         private static function mentionCallback($match)
1122         {
1123                 if (empty($match[1])) {
1124                         return '';
1125                 }
1126
1127                 $data = Contact::getDetailsByURL($match[1]);
1128                 if (empty($data['nick'])) {
1129                         return $match[0];
1130                 }
1131
1132                 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1133         }
1134
1135         /**
1136          * Remove image elements since they are added as attachment
1137          *
1138          * @param string $body
1139          *
1140          * @return string with removed images
1141          */
1142         private static function removePictures($body)
1143         {
1144                 // Simplify image codes
1145                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1146                 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
1147
1148                 // Now remove local links
1149                 $body = preg_replace_callback(
1150                         '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1151                         function ($match) {
1152                                 // We remove the link when it is a link to a local photo page
1153                                 if (Photo::isLocalPage($match[1])) {
1154                                         return '';
1155                                 }
1156                                 // otherwise we just return the link
1157                                 return '[url]' . $match[1] . '[/url]';
1158                         },
1159                         $body
1160                 );
1161
1162                 // Remove all pictures
1163                 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $body);
1164
1165                 return $body;
1166         }
1167
1168         /**
1169          * Fetches the "context" value for a givem item array from the "conversation" table
1170          *
1171          * @param array $item
1172          *
1173          * @return string with context url
1174          * @throws \Exception
1175          */
1176         private static function fetchContextURLForItem($item)
1177         {
1178                 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1179                 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1180                         $context_uri = $conversation['conversation-href'];
1181                 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1182                         $context_uri = $conversation['conversation-uri'];
1183                 } else {
1184                         $context_uri = $item['parent-uri'] . '#context';
1185                 }
1186                 return $context_uri;
1187         }
1188
1189         /**
1190          * Returns if the post contains sensitive content ("nsfw")
1191          *
1192          * @param integer $item_id
1193          *
1194          * @return boolean
1195          * @throws \Exception
1196          */
1197         private static function isSensitive($item_id)
1198         {
1199                 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
1200                 return DBA::exists('term', $condition);
1201         }
1202
1203         /**
1204          * Creates event data
1205          *
1206          * @param array $item
1207          *
1208          * @return array with the event data
1209          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1210          */
1211         public static function createEvent($item)
1212         {
1213                 $event = [];
1214                 $event['name'] = $item['event-summary'];
1215                 $event['content'] = BBCode::convert($item['event-desc'], false, 9);
1216                 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1217
1218                 if (!$item['event-nofinish']) {
1219                         $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1220                 }
1221
1222                 if (!empty($item['event-location'])) {
1223                         $item['location'] = $item['event-location'];
1224                         $event['location'] = self::createLocation($item);
1225                 }
1226
1227                 return $event;
1228         }
1229
1230         /**
1231          * Creates a note/article object array
1232          *
1233          * @param array $item
1234          *
1235          * @return array with the object data
1236          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1237          * @throws \ImagickException
1238          */
1239         public static function createNote($item)
1240         {
1241                 if (empty($item)) {
1242                         return [];
1243                 }
1244
1245                 if ($item['event-type'] == 'event') {
1246                         $type = 'Event';
1247                 } elseif (!empty($item['title'])) {
1248                         $type = 'Article';
1249                 } else {
1250                         $type = 'Note';
1251                 }
1252
1253                 if ($item['deleted']) {
1254                         $type = 'Tombstone';
1255                 }
1256
1257                 $data = [];
1258                 $data['id'] = $item['uri'];
1259                 $data['type'] = $type;
1260
1261                 if ($item['deleted']) {
1262                         return $data;
1263                 }
1264
1265                 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1266
1267                 if ($item['uri'] != $item['thr-parent']) {
1268                         $data['inReplyTo'] = $item['thr-parent'];
1269                 } else {
1270                         $data['inReplyTo'] = null;
1271                 }
1272
1273                 $data['diaspora:guid'] = $item['guid'];
1274                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1275
1276                 if ($item['created'] != $item['edited']) {
1277                         $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1278                 }
1279
1280                 $data['url'] = $item['plink'];
1281                 $data['attributedTo'] = $item['author-link'];
1282                 $data['sensitive'] = self::isSensitive($item['id']);
1283                 $data['context'] = self::fetchContextURLForItem($item);
1284
1285                 if (!empty($item['title'])) {
1286                         $data['name'] = BBCode::toPlaintext($item['title'], false);
1287                 }
1288
1289                 $permission_block = self::createPermissionBlockForItem($item, false);
1290
1291                 $body = $item['body'];
1292
1293                 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1294                         $body = self::prependMentions($body, $permission_block);
1295                 }
1296
1297                 if ($type == 'Note') {
1298                         $body = self::removePictures($body);
1299                 } elseif (($type == 'Article') && empty($data['summary'])) {
1300                         $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1301                 }
1302
1303                 if ($type == 'Event') {
1304                         $data = array_merge($data, self::createEvent($item));
1305                 } else {
1306                         $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1307                         $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1308
1309                         $data['content'] = BBCode::convert($body, false, 9);
1310                 }
1311
1312                 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1313                 $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
1314                 $richbody = BBCode::removeAttachment($richbody);
1315
1316                 $data['contentMap']['text/html'] = BBCode::convert($richbody, false);
1317                 $data['contentMap']['text/markdown'] = BBCode::toMarkdown($item["body"]);
1318
1319                 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1320
1321                 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1322                         $data['diaspora:comment'] = $item['signed_text'];
1323                 }
1324
1325                 $data['attachment'] = self::createAttachmentList($item, $type);
1326                 $data['tag'] = self::createTagList($item);
1327
1328                 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1329                         $data['location'] = self::createLocation($item);
1330                 }
1331
1332                 if (!empty($item['app'])) {
1333                         $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1334                 }
1335
1336                 $data = array_merge($data, $permission_block);
1337
1338                 return $data;
1339         }
1340
1341         /**
1342          * Creates an an "add tag" entry
1343          *
1344          * @param array $item
1345          * @param array $data activity data
1346          *
1347          * @return array with activity data for adding tags
1348          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1349          * @throws \ImagickException
1350          */
1351         private static function createAddTag($item, $data)
1352         {
1353                 $object = XML::parseString($item['object'], false);
1354                 $target = XML::parseString($item["target"], false);
1355
1356                 $data['diaspora:guid'] = $item['guid'];
1357                 $data['actor'] = $item['author-link'];
1358                 $data['target'] = (string)$target->id;
1359                 $data['summary'] = BBCode::toPlaintext($item['body']);
1360                 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1361
1362                 return $data;
1363         }
1364
1365         /**
1366          * Creates an announce object entry
1367          *
1368          * @param array $item
1369          * @param array $data activity data
1370          *
1371          * @return array with activity data
1372          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1373          * @throws \ImagickException
1374          */
1375         private static function createAnnounce($item, $data)
1376         {
1377                 $orig_body = $item['body'];
1378                 $announce = api_share_as_retweet($item);
1379                 if (empty($announce['plink'])) {
1380                         $data['type'] = 'Create';
1381                         $data['object'] = self::createNote($item);
1382                         return $data;
1383                 }
1384
1385                 // Fetch the original id of the object
1386                 $activity = ActivityPub::fetchContent($announce['plink'], $item['uid']);
1387                 if (!empty($activity)) {
1388                         $ldactivity = JsonLD::compact($activity);
1389                         $id = JsonLD::fetchElement($ldactivity, '@id');
1390                         $type = str_replace('as:', '', JsonLD::fetchElement($ldactivity, '@type'));
1391                         if (!empty($id)) {
1392                                 if (empty($announce['share-pre-body'])) {
1393                                         // Pure announce, without a quote
1394                                         $data['type'] = 'Announce';
1395                                         $data['object'] = $id;
1396                                         return $data;
1397                                 }
1398
1399                                 // Quote
1400                                 $data['type'] = 'Create';
1401                                 $item['body'] = trim($announce['share-pre-body']) . "\n" . $id;
1402                                 $data['object'] = self::createNote($item);
1403
1404                                 /// @todo Finally descide how to implement this in AP. This is a possible way:
1405                                 $data['object']['attachment'][] = ['type' => $type, 'id' => $id];
1406
1407                                 $data['object']['source']['content'] = $orig_body;
1408                                 return $data;
1409                         }
1410                 }
1411
1412                 $item['body'] = $orig_body;
1413                 $data['type'] = 'Create';
1414                 $data['object'] = self::createNote($item);
1415                 return $data;
1416         }
1417
1418         /**
1419          * Creates an activity id for a given contact id
1420          *
1421          * @param integer $cid Contact ID of target
1422          *
1423          * @return bool|string activity id
1424          */
1425         public static function activityIDFromContact($cid)
1426         {
1427                 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1428                 if (!DBA::isResult($contact)) {
1429                         return false;
1430                 }
1431
1432                 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1433                 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1434                 return System::baseUrl() . '/activity/' . $uuid;
1435         }
1436
1437         /**
1438          * Transmits a contact suggestion to a given inbox
1439          *
1440          * @param integer $uid           User ID
1441          * @param string  $inbox         Target inbox
1442          * @param integer $suggestion_id Suggestion ID
1443          *
1444          * @return boolean was the transmission successful?
1445          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1446          */
1447         public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1448         {
1449                 $owner = User::getOwnerDataById($uid);
1450
1451                 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
1452
1453                 $data = ['@context' => ActivityPub::CONTEXT,
1454                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1455                         'type' => 'Announce',
1456                         'actor' => $owner['url'],
1457                         'object' => $suggestion['url'],
1458                         'content' => $suggestion['note'],
1459                         'instrument' => self::getService(),
1460                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1461                         'cc' => []];
1462
1463                 $signed = LDSignature::sign($data, $owner);
1464
1465                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1466                 return HTTPSignature::transmit($signed, $inbox, $uid);
1467         }
1468
1469         /**
1470          * Transmits a profile relocation to a given inbox
1471          *
1472          * @param integer $uid   User ID
1473          * @param string  $inbox Target inbox
1474          *
1475          * @return boolean was the transmission successful?
1476          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1477          */
1478         public static function sendProfileRelocation($uid, $inbox)
1479         {
1480                 $owner = User::getOwnerDataById($uid);
1481
1482                 $data = ['@context' => ActivityPub::CONTEXT,
1483                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1484                         'type' => 'dfrn:relocate',
1485                         'actor' => $owner['url'],
1486                         'object' => $owner['url'],
1487                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1488                         'instrument' => self::getService(),
1489                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1490                         'cc' => []];
1491
1492                 $signed = LDSignature::sign($data, $owner);
1493
1494                 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1495                 return HTTPSignature::transmit($signed, $inbox, $uid);
1496         }
1497
1498         /**
1499          * Transmits a profile deletion to a given inbox
1500          *
1501          * @param integer $uid   User ID
1502          * @param string  $inbox Target inbox
1503          *
1504          * @return boolean was the transmission successful?
1505          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1506          */
1507         public static function sendProfileDeletion($uid, $inbox)
1508         {
1509                 $owner = User::getOwnerDataById($uid);
1510
1511                 if (empty($owner)) {
1512                         Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1513                         return false;
1514                 }
1515
1516                 if (empty($owner['uprvkey'])) {
1517                         Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1518                         return false;
1519                 }
1520
1521                 $data = ['@context' => ActivityPub::CONTEXT,
1522                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1523                         'type' => 'Delete',
1524                         'actor' => $owner['url'],
1525                         'object' => $owner['url'],
1526                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1527                         'instrument' => self::getService(),
1528                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1529                         'cc' => []];
1530
1531                 $signed = LDSignature::sign($data, $owner);
1532
1533                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1534                 return HTTPSignature::transmit($signed, $inbox, $uid);
1535         }
1536
1537         /**
1538          * Transmits a profile change to a given inbox
1539          *
1540          * @param integer $uid   User ID
1541          * @param string  $inbox Target inbox
1542          *
1543          * @return boolean was the transmission successful?
1544          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1545          * @throws \ImagickException
1546          */
1547         public static function sendProfileUpdate($uid, $inbox)
1548         {
1549                 $owner = User::getOwnerDataById($uid);
1550                 $profile = APContact::getByURL($owner['url']);
1551
1552                 $data = ['@context' => ActivityPub::CONTEXT,
1553                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1554                         'type' => 'Update',
1555                         'actor' => $owner['url'],
1556                         'object' => self::getProfile($uid),
1557                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1558                         'instrument' => self::getService(),
1559                         'to' => [$profile['followers']],
1560                         'cc' => []];
1561
1562                 $signed = LDSignature::sign($data, $owner);
1563
1564                 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1565                 return HTTPSignature::transmit($signed, $inbox, $uid);
1566         }
1567
1568         /**
1569          * Transmits a given activity to a target
1570          *
1571          * @param string  $activity Type name
1572          * @param string  $target   Target profile
1573          * @param integer $uid      User ID
1574          * @return bool
1575          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1576          * @throws \ImagickException
1577          * @throws \Exception
1578          */
1579         public static function sendActivity($activity, $target, $uid, $id = '')
1580         {
1581                 $profile = APContact::getByURL($target);
1582                 if (empty($profile['inbox'])) {
1583                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1584                         return;
1585                 }
1586
1587                 $owner = User::getOwnerDataById($uid);
1588
1589                 if (empty($id)) {
1590                         $id = System::baseUrl() . '/activity/' . System::createGUID();
1591                 }
1592
1593                 $data = ['@context' => ActivityPub::CONTEXT,
1594                         'id' => $id,
1595                         'type' => $activity,
1596                         'actor' => $owner['url'],
1597                         'object' => $profile['url'],
1598                         'instrument' => self::getService(),
1599                         'to' => [$profile['url']]];
1600
1601                 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1602
1603                 $signed = LDSignature::sign($data, $owner);
1604                 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1605         }
1606
1607         /**
1608          * Transmits a "follow object" activity to a target
1609          * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1610          *
1611          * @param string  $object Object URL
1612          * @param string  $target Target profile
1613          * @param integer $uid    User ID
1614          * @return bool
1615          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1616          * @throws \ImagickException
1617          * @throws \Exception
1618          */
1619         public static function sendFollowObject($object, $target, $uid = 0)
1620         {
1621                 $profile = APContact::getByURL($target);
1622                 if (empty($profile['inbox'])) {
1623                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1624                         return;
1625                 }
1626
1627                 if (empty($uid)) {
1628                         // Fetch the list of administrators
1629                         $admin_mail = explode(',', str_replace(' ', '', Config::get('config', 'admin_email')));
1630
1631                         // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1632                         $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1633                         $first_user = DBA::selectFirst('user', ['uid'], $condition);
1634                         $uid = $first_user['uid'];
1635                 }
1636
1637                 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1638                         'author-id' => Contact::getPublicIdByUserId($uid)];
1639                 if (Item::exists($condition)) {
1640                         Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1641                         return false;
1642                 }
1643
1644                 $owner = User::getOwnerDataById($uid);
1645
1646                 $data = ['@context' => ActivityPub::CONTEXT,
1647                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1648                         'type' => 'Follow',
1649                         'actor' => $owner['url'],
1650                         'object' => $object,
1651                         'instrument' => self::getService(),
1652                         'to' => [$profile['url']]];
1653
1654                 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1655
1656                 $signed = LDSignature::sign($data, $owner);
1657                 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1658         }
1659
1660         /**
1661          * Transmit a message that the contact request had been accepted
1662          *
1663          * @param string  $target Target profile
1664          * @param         $id
1665          * @param integer $uid    User ID
1666          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1667          * @throws \ImagickException
1668          */
1669         public static function sendContactAccept($target, $id, $uid)
1670         {
1671                 $profile = APContact::getByURL($target);
1672                 if (empty($profile['inbox'])) {
1673                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1674                         return;
1675                 }
1676
1677                 $owner = User::getOwnerDataById($uid);
1678                 $data = ['@context' => ActivityPub::CONTEXT,
1679                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1680                         'type' => 'Accept',
1681                         'actor' => $owner['url'],
1682                         'object' => [
1683                                 'id' => (string)$id,
1684                                 'type' => 'Follow',
1685                                 'actor' => $profile['url'],
1686                                 'object' => $owner['url']
1687                         ],
1688                         'instrument' => self::getService(),
1689                         'to' => [$profile['url']]];
1690
1691                 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1692
1693                 $signed = LDSignature::sign($data, $owner);
1694                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1695         }
1696
1697         /**
1698          * Reject a contact request or terminates the contact relation
1699          *
1700          * @param string  $target Target profile
1701          * @param         $id
1702          * @param integer $uid    User ID
1703          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1704          * @throws \ImagickException
1705          */
1706         public static function sendContactReject($target, $id, $uid)
1707         {
1708                 $profile = APContact::getByURL($target);
1709                 if (empty($profile['inbox'])) {
1710                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1711                         return;
1712                 }
1713
1714                 $owner = User::getOwnerDataById($uid);
1715                 $data = ['@context' => ActivityPub::CONTEXT,
1716                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1717                         'type' => 'Reject',
1718                         'actor' => $owner['url'],
1719                         'object' => [
1720                                 'id' => (string)$id,
1721                                 'type' => 'Follow',
1722                                 'actor' => $profile['url'],
1723                                 'object' => $owner['url']
1724                         ],
1725                         'instrument' => self::getService(),
1726                         'to' => [$profile['url']]];
1727
1728                 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1729
1730                 $signed = LDSignature::sign($data, $owner);
1731                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1732         }
1733
1734         /**
1735          * Transmits a message that we don't want to follow this contact anymore
1736          *
1737          * @param string  $target Target profile
1738          * @param integer $uid    User ID
1739          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1740          * @throws \ImagickException
1741          * @throws \Exception
1742          */
1743         public static function sendContactUndo($target, $cid, $uid)
1744         {
1745                 $profile = APContact::getByURL($target);
1746                 if (empty($profile['inbox'])) {
1747                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1748                         return;
1749                 }
1750
1751                 $object_id = self::activityIDFromContact($cid);
1752                 if (empty($object_id)) {
1753                         return;
1754                 }
1755
1756                 $id = System::baseUrl() . '/activity/' . System::createGUID();
1757
1758                 $owner = User::getOwnerDataById($uid);
1759                 $data = ['@context' => ActivityPub::CONTEXT,
1760                         'id' => $id,
1761                         'type' => 'Undo',
1762                         'actor' => $owner['url'],
1763                         'object' => ['id' => $object_id, 'type' => 'Follow',
1764                                 'actor' => $owner['url'],
1765                                 'object' => $profile['url']],
1766                         'instrument' => self::getService(),
1767                         'to' => [$profile['url']]];
1768
1769                 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1770
1771                 $signed = LDSignature::sign($data, $owner);
1772                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1773         }
1774
1775         private static function prependMentions($body, array $permission_block)
1776         {
1777                 if (Config::get('system', 'disable_implicit_mentions')) {
1778                         return $body;
1779                 }
1780
1781                 $mentions = [];
1782
1783                 foreach ($permission_block['to'] as $profile_url) {
1784                         $profile = Contact::getDetailsByURL($profile_url);
1785                         if (!empty($profile['addr'])
1786                                 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1787                                 && !strstr($body, $profile['addr'])
1788                                 && !strstr($body, $profile_url)
1789                         ) {
1790                                 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1791                         }
1792                 }
1793
1794                 $mentions[] = $body;
1795
1796                 return implode(' ', $mentions);
1797         }
1798 }