+ private static function fetchPermissionBlockFromConversation($item)
+ {
+ if (empty($item['thr-parent'])) {
+ return [];
+ }
+
+ $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
+ $conversation = DBA::selectFirst('conversation', ['source'], $condition);
+ if (!DBA::isResult($conversation)) {
+ return [];
+ }
+
+ $activity = json_decode($conversation['source'], true);
+
+ $actor = JsonLD::fetchElement($activity, 'actor', 'id');
+ $profile = ActivityPub::fetchprofile($actor);
+
+ $item_profile = ActivityPub::fetchprofile($item['owner-link']);
+
+ $permissions = [];
+
+ $elements = ['to', 'cc', 'bto', 'bcc'];
+ foreach ($elements as $element) {
+ if (empty($activity[$element])) {
+ continue;
+ }
+ if (is_string($activity[$element])) {
+ $activity[$element] = [$activity[$element]];
+ }
+ foreach ($activity[$element] as $receiver) {
+ if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
+ $receiver = $item_profile['followers'];
+ }
+ if ($receiver != $item['owner-link']) {
+ $permissions[$element][] = $receiver;
+ }
+ }
+ }
+ return $permissions;
+ }
+
+ public static function createPermissionBlockForItem($item)
+ {
+ $data = ['to' => [], 'cc' => []];
+
+ $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
+
+ $actor_profile = ActivityPub::fetchprofile($item['author-link']);
+
+ $terms = Term::tagArrayFromItemId($item['id']);
+
+ $contacts[$item['author-link']] = $item['author-link'];
+
+ if (!$item['private']) {
+ $data['to'][] = self::PUBLIC;
+ if (!empty($actor_profile['followers'])) {
+ $data['cc'][] = $actor_profile['followers'];
+ }
+
+ foreach ($terms as $term) {
+ if ($term['type'] != TERM_MENTION) {
+ continue;
+ }
+ $profile = self::fetchprofile($term['url']);
+ if (!empty($profile) && empty($contacts[$profile['url']])) {
+ $data['cc'][] = $profile['url'];
+ $contacts[$profile['url']] = $profile['url'];
+ }
+ }
+ } else {
+ $receiver_list = Item::enumeratePermissions($item);
+
+ $mentioned = [];
+
+ foreach ($terms as $term) {
+ if ($term['type'] != TERM_MENTION) {
+ continue;
+ }
+ $cid = Contact::getIdForURL($term['url'], $item['uid']);
+ if (!empty($cid) && in_array($cid, $receiver_list)) {
+ $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
+ $data['to'][] = $contact['url'];
+ $contacts[$contact['url']] = $contact['url'];
+ }
+ }
+
+ foreach ($receiver_list as $receiver) {
+ $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
+ if (empty($contacts[$contact['url']])) {
+ $data['cc'][] = $contact['url'];
+ $contacts[$contact['url']] = $contact['url'];
+ }
+ }
+ }
+
+ $parents = Item::select(['author-link', 'owner-link'], ['parent' => $item['parent']]);
+ while ($parent = Item::fetch($parents)) {
+ $profile = self::fetchprofile($parent['author-link']);
+ if (!empty($profile) && empty($contacts[$profile['url']])) {
+ $data['cc'][] = $profile['url'];
+ $contacts[$profile['url']] = $profile['url'];
+ }
+
+ $profile = self::fetchprofile($parent['owner-link']);
+ if (!empty($profile) && empty($contacts[$profile['url']])) {
+ $data['cc'][] = $profile['url'];
+ $contacts[$profile['url']] = $profile['url'];
+ }
+ }
+ DBA::close($parents);
+
+ if (empty($data['to'])) {
+ $data['to'] = $data['cc'];
+ $data['cc'] = [];
+ }
+
+ return $data;
+ }
+
+ public static function fetchTargetInboxes($item, $uid)
+ {
+ $permissions = self::createPermissionBlockForItem($item);
+ if (empty($permissions)) {
+ return [];
+ }
+
+ $inboxes = [];
+
+ $item_profile = ActivityPub::fetchprofile($item['owner-link']);
+
+ $elements = ['to', 'cc', 'bto', 'bcc'];
+ foreach ($elements as $element) {
+ if (empty($permissions[$element])) {
+ continue;
+ }
+ foreach ($permissions[$element] as $receiver) {
+ if ($receiver == $item_profile['followers']) {
+ $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
+ 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB]);
+ while ($contact = DBA::fetch($contacts)) {
+ $contact = defaults($contact, 'batch', $contact['notify']);
+ $inboxes[$contact] = $contact;
+ }
+ DBA::close($contacts);
+ } else {
+ $profile = self::fetchprofile($receiver);
+ if (!empty($profile)) {
+ $target = defaults($profile, 'sharedinbox', $profile['inbox']);
+ $inboxes[$target] = $target;
+ }
+ }
+ }
+ }
+
+ if (!empty($item_profile['sharedinbox'])) {
+ unset($inboxes[$item_profile['sharedinbox']]);
+ }
+
+ if (!empty($item_profile['inbox'])) {
+ unset($inboxes[$item_profile['inbox']]);
+ }
+
+ return $inboxes;
+ }
+