-
- private static function deliver(string $inbox)
- {
- $uri_ids = [];
- $success = true;
-
- $posts = Post\Delivery::selectForInbox($inbox);
- foreach ($posts as $post) {
- $uri_ids[] = $post['uri-id'];
- if ($success) {
- $success = self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], [], $post['uri-id']);
- }
- }
-
- return ['success' => $success, 'uri_ids' => $uri_ids];
- }
-
- private static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id)
- {
- if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
- $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => $uid]);
- $item_id = $item['id'] ?? 0;
- if (empty($receivers) && !empty($item)) {
- $parent = Post::selectFirst(Item::DELIVER_FIELDLIST, ['id' => $item['parent']]);
-
- $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid);
- $receivers = $inboxes[$inbox] ?? [];
-
- // When we haven't fetched the receiver list, it can be a personal inbox
- if (empty($receivers)) {
- $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true);
- $receivers = $inboxes[$inbox] ?? [];
- }
- }
- }
-
- $success = true;
-
- if ($cmd == Delivery::MAIL) {
- $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
- if (!empty($data)) {
- $success = HTTPSignature::transmit($data, $inbox, $uid);
- }
- } elseif ($cmd == Delivery::SUGGESTION) {
- $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
- } elseif ($cmd == Delivery::RELOCATION) {
- // @todo Implementation pending
- } elseif ($cmd == Delivery::POKE) {
- // Implementation not planned
- } elseif ($cmd == Delivery::REMOVAL) {
- $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
- } elseif ($cmd == Delivery::PROFILEUPDATE) {
- $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
- } else {
- $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
- if (!empty($data)) {
- $success = HTTPSignature::transmit($data, $inbox, $uid);
- if ($success && $uri_id) {
- Post\Delivery::remove($uri_id, $inbox);
- }
- }
- }
-
- self::setSuccess($receivers, $success);
-
- Logger::info('Delivered', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid, 'success' => $success]);
-
- return $success;
- }
-
- private static function setSuccess(array $receivers, bool $success)
- {
- $gsid = null;
-
- foreach ($receivers as $receiver) {
- $contact = Contact::getById($receiver);
- if (empty($contact)) {
- continue;
- }
-
- $gsid = $gsid ?: $contact['gsid'];
-
- if ($success) {
- Contact::unmarkForArchival($contact);
- } else {
- Contact::markForArchival($contact);
- }
- }
-
- if (!empty($gsid)) {
- GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);
- }
- }