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