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