+ if ($item['deleted']) {
+ $type = 'Tombstone';
+ }
+
+ $data = [];
+ $data['id'] = $item['uri'];
+ $data['type'] = $type;
+
+ if ($item['deleted']) {
+ return $data;
+ }
+
+ $data['summary'] = null; // Ignore by now
+
+ if ($item['uri'] != $item['thr-parent']) {
+ $data['inReplyTo'] = $item['thr-parent'];
+ } else {
+ $data['inReplyTo'] = null;
+ }
+
+ $data['diaspora:guid'] = $item['guid'];
+ $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
+
+ if ($item["created"] != $item["edited"]) {
+ $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
+ }
+
+ $data['url'] = $item['plink'];
+ $data['attributedTo'] = $item['author-link'];
+ $data['actor'] = $item['author-link'];
+ $data['sensitive'] = false; // - Query NSFW
+ $data['context'] = self::fetchContextURLForItem($item);
+
+ if (!empty($item['title'])) {
+ $data['name'] = BBCode::convert($item['title'], false, 7);
+ }
+
+ $data['content'] = BBCode::convert($item['body'], false, 7);
+ $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
+
+ if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
+ $data['diaspora:comment'] = $item['signed_text'];
+ }
+
+ $data['attachment'] = []; // @ToDo
+ $data['tag'] = self::createTagList($item);
+ $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
+
+ return $data;
+ }
+
+ /**
+ * @brief Transmits a profile deletion to a given inbox
+ *
+ * @param integer $uid User ID
+ * @param string $inbox Target inbox
+ */
+ public static function transmitProfileDeletion($uid, $inbox)
+ {
+ $owner = User::getOwnerDataById($uid);
+ $profile = APContact::getByURL($owner['url']);
+
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Delete',
+ 'actor' => $owner['url'],
+ 'object' => self::profile($uid),
+ 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+ 'to' => [self::PUBLIC_COLLECTION],
+ 'cc' => []];
+
+ $signed = LDSignature::sign($data, $owner);
+
+ logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
+ HTTPSignature::transmit($signed, $inbox, $uid);
+ }
+
+ /**
+ * @brief Transmits a profile change to a given inbox
+ *
+ * @param integer $uid User ID
+ * @param string $inbox Target inbox
+ */
+ public static function transmitProfileUpdate($uid, $inbox)
+ {
+ $owner = User::getOwnerDataById($uid);
+ $profile = APContact::getByURL($owner['url']);
+
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Update',
+ 'actor' => $owner['url'],
+ 'object' => self::profile($uid),
+ 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+ 'to' => [$profile['followers']],
+ 'cc' => []];
+
+ $signed = LDSignature::sign($data, $owner);
+
+ logger('Deliver profile update for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
+ HTTPSignature::transmit($signed, $inbox, $uid);
+ }
+
+ /**
+ * @brief Transmits a given activity to a target
+ *
+ * @param array $activity
+ * @param string $target Target profile
+ * @param integer $uid User ID
+ */
+ public static function transmitActivity($activity, $target, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $owner = User::getOwnerDataById($uid);
+
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => $activity,
+ 'actor' => $owner['url'],
+ 'object' => $profile['url'],
+ 'to' => $profile['url']];
+
+ logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * @brief Transmit a message that the contact request had been accepted
+ *
+ * @param string $target Target profile
+ * @param $id
+ * @param integer $uid User ID
+ */
+ public static function transmitContactAccept($target, $id, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $owner = User::getOwnerDataById($uid);
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Accept',
+ 'actor' => $owner['url'],
+ 'object' => ['id' => $id, 'type' => 'Follow',
+ 'actor' => $profile['url'],
+ 'object' => $owner['url']],
+ 'to' => $profile['url']];
+
+ logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * @brief
+ *
+ * @param string $target Target profile
+ * @param $id
+ * @param integer $uid User ID
+ */
+ public static function transmitContactReject($target, $id, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $owner = User::getOwnerDataById($uid);
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Reject',
+ 'actor' => $owner['url'],
+ 'object' => ['id' => $id, 'type' => 'Follow',
+ 'actor' => $profile['url'],
+ 'object' => $owner['url']],
+ 'to' => $profile['url']];
+
+ logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * @brief
+ *
+ * @param string $target Target profile
+ * @param integer $uid User ID
+ */
+ public static function transmitContactUndo($target, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $id = System::baseUrl() . '/activity/' . System::createGUID();
+
+ $owner = User::getOwnerDataById($uid);
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => $id,
+ 'type' => 'Undo',
+ 'actor' => $owner['url'],
+ 'object' => ['id' => $id, 'type' => 'Follow',
+ 'actor' => $owner['url'],
+ 'object' => $profile['url']],
+ 'to' => $profile['url']];
+
+ logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * Fetches ActivityPub content from the given url
+ *
+ * @param string $url content url
+ * @return array
+ */
+ public static function fetchContent($url)
+ {
+ $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
+ if (!$ret['success'] || empty($ret['body'])) {
+ return false;
+ }
+
+ return json_decode($ret['body'], true);
+ }
+
+ /**
+ * Fetches a profile from the given url into an array that is compatible to Probe::uri
+ *
+ * @param string $url profile url
+ * @return array
+ */
+ public static function probeProfile($url)
+ {
+ $apcontact = APContact::getByURL($url, true);
+ if (empty($apcontact)) {
+ return false;
+ }
+
+ $profile = ['network' => Protocol::ACTIVITYPUB];
+ $profile['nick'] = $apcontact['nick'];
+ $profile['name'] = $apcontact['name'];
+ $profile['guid'] = $apcontact['uuid'];
+ $profile['url'] = $apcontact['url'];
+ $profile['addr'] = $apcontact['addr'];
+ $profile['alias'] = $apcontact['alias'];
+ $profile['photo'] = $apcontact['photo'];
+ // $profile['community']
+ // $profile['keywords']
+ // $profile['location']
+ $profile['about'] = $apcontact['about'];
+ $profile['batch'] = $apcontact['sharedinbox'];
+ $profile['notify'] = $apcontact['inbox'];
+ $profile['poll'] = $apcontact['outbox'];
+ $profile['pubkey'] = $apcontact['pubkey'];
+ $profile['baseurl'] = $apcontact['baseurl'];
+
+ // Remove all "null" fields
+ foreach ($profile as $field => $content) {
+ if (is_null($content)) {
+ unset($profile[$field]);
+ }
+ }
+
+ return $profile;
+ }
+
+ /**
+ * @brief
+ *
+ * @param $body
+ * @param $header
+ * @param integer $uid User ID
+ */
+ public static function processInbox($body, $header, $uid)
+ {
+ $http_signer = HTTPSignature::getSigner($body, $header);
+ if (empty($http_signer)) {
+ logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
+ return;
+ } else {
+ logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
+ }
+
+ $activity = json_decode($body, true);
+
+ $actor = JsonLD::fetchElement($activity, 'actor', 'id');
+ logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
+
+ if (empty($activity)) {
+ logger('Invalid body.', LOGGER_DEBUG);