+ /**
+ * Stores unhandled or unknown Activities as a file
+ *
+ * @param boolean $unknown "true" if the activity is unknown, "false" if it is unhandled
+ * @param string $type Activity type
+ * @param array $object_data Preprocessed array that is generated out of the received activity
+ * @param array $activity Array with activity data
+ * @param string $body The unprocessed body
+ * @param integer $uid User ID
+ * @param boolean $trust_source Do we trust the source?
+ * @param boolean $push Message had been pushed to our system
+ * @param array $signer The signer of the post
+ * @return void
+ */
+ private static function storeUnhandledActivity(bool $unknown, string $type, array $object_data, array $activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [])
+ {
+ if (!DI::config()->get('debug', 'ap_log_unknown')) {
+ return;
+ }
+
+ $file = ($unknown ? 'unknown-' : 'unhandled-') . str_replace(':', '-', $type) . '-';
+
+ if (!empty($object_data['object_type'])) {
+ $file .= str_replace(':', '-', $object_data['object_type']) . '-';
+ }
+
+ if (!empty($object_data['object_object_type'])) {
+ $file .= str_replace(':', '-', $object_data['object_object_type']) . '-';
+ }
+
+ $tempfile = tempnam(System::getTempPath(), $file);
+ file_put_contents($tempfile, json_encode(['activity' => $activity, 'body' => $body, 'uid' => $uid, 'trust_source' => $trust_source, 'push' => $push, 'signer' => $signer, 'object_data' => $object_data], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
+ Logger::notice('Unknown activity stored', ['type' => $type, 'object_type' => $object_data['object_type'], $object_data['object_object_type'] ?? '', 'file' => $tempfile]);
+ }
+
+ /**
+ * Fetch a user id from an activity array
+ *
+ * @param array $activity
+ * @param string $actor
+ *
+ * @return int user id
+ */
+ public static function getBestUserForActivity(array $activity)
+ {
+ $uid = 0;
+ $actor = JsonLD::fetchElement($activity, 'as:actor', '@id') ?? '';
+
+ $receivers = self::getReceivers($activity, $actor);
+ foreach ($receivers as $receiver) {
+ if ($receiver['type'] == self::TARGET_GLOBAL) {
+ return 0;
+ }
+ if (empty($uid) || ($receiver['type'] == self::TARGET_TO)) {
+ $uid = $receiver['uid'];
+ }
+ }
+
+ // When we haven't found any user yet, we just chose a user who most likely could have access to the content
+ if (empty($uid)) {
+ $contact = Contact::selectFirst(['uid'], ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND]]);
+ if (!empty($contact['uid'])) {
+ $uid = $contact['uid'];
+ }
+ }
+
+ return $uid;
+ }
+
+ public static function getReceiverURL($activity)
+ {
+ $urls = [];
+
+ foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
+ $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
+ if (empty($receiver_list)) {
+ continue;
+ }
+
+ foreach ($receiver_list as $receiver) {
+ if ($receiver == self::PUBLIC_COLLECTION) {
+ $receiver = ActivityPub::PUBLIC_COLLECTION;
+ }
+ $urls[$element][] = $receiver;
+ }
+ }
+
+ return $urls;
+ }
+