Store the receivers of a post in the tags

This commit is contained in:
Michael 2022-02-19 13:31:49 +00:00
parent c03ff7833b
commit c867581530
7 changed files with 173 additions and 78 deletions

View file

@ -526,6 +526,8 @@ class Processor
self::storeFromBody($item);
self::storeTags($item['uri-id'], $activity['tags']);
self::storeReceivers($item['uri-id'], $activity['receiver_urls'] ?? []);
$item['location'] = $activity['location'];
if (!empty($activity['latitude']) && !empty($activity['longitude'])) {
@ -756,6 +758,22 @@ class Processor
}
}
public static function storeReceivers(int $uriid, array $receivers)
{
foreach (['as:to' => Tag::TO, 'as:cc' => Tag::CC, 'as:bto' => Tag::BTO, 'as:bcc' => Tag::BCC] as $element => $type) {
if (!empty($receivers[$element])) {
foreach ($receivers[$element] as $receiver) {
if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
$name = Receiver::PUBLIC_COLLECTION;
} else {
$name = trim(parse_url($receiver, PHP_URL_PATH), '/');
}
Tag::store($uriid, $type, $name, $receiver);
}
}
}
}
/**
* Creates an mail post
*

View file

@ -297,12 +297,18 @@ class Receiver
$reception_types[$data['uid']] = $data['type'] ?? self::TARGET_UNKNOWN;
}
$urls = self::getReceiverURL($activity);
// When it is a delivery to a personal inbox we add that user to the receivers
if (!empty($uid)) {
$additional = [$uid => $uid];
$receivers = array_replace($receivers, $additional);
if (empty($activity['thread-completion']) && (empty($reception_types[$uid]) || in_array($reception_types[$uid], [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL]))) {
$reception_types[$uid] = self::TARGET_BCC;
$owner = User::getOwnerDataById($uid);
if (!empty($owner['url'])) {
$urls['as:bcc'][] = $owner['url'];
}
}
}
@ -408,6 +414,12 @@ class Receiver
$object_data['object_type'] = $object_type;
}
foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
if (!empty($urls[$element])) {
$object_data['receiver_urls'][$element] = array_unique(array_merge($object_data['receiver_urls'][$element] ?? [], $urls[$element]));
}
}
$object_data['type'] = $type;
$object_data['actor'] = $actor;
$object_data['item_receiver'] = $receivers;
@ -679,6 +691,27 @@ class Receiver
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;
}
/**
* Fetch the receiver list from an activity array
*
@ -1508,7 +1541,8 @@ class Receiver
$reception_types[$data['uid']] = $data['type'] ?? 0;
}
$object_data['receiver'] = $receivers;
$object_data['receiver_urls'] = self::getReceiverURL($object);
$object_data['receiver'] = $receivers;
$object_data['reception_type'] = $reception_types;
$object_data['unlisted'] = in_array(-1, $object_data['receiver']);

View file

@ -424,7 +424,7 @@ class Transmitter
}
/**
* Returns an array with permissions of a given item array
* Returns an array with permissions of the thread parent of the given item array
*
* @param array $item
*
@ -432,34 +432,25 @@ class Transmitter
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
private static function fetchPermissionBlockFromConversation($item)
public static function fetchPermissionBlockFromThreadParent($item)
{
if (empty($item['thr-parent'])) {
if (empty($item['thr-parent-id'])) {
return [];
}
$condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
$conversation = DBA::selectFirst('conversation', ['source'], $condition);
if (!DBA::isResult($conversation)) {
$parent = Post::selectFirstPost(['author-link'], ['uri-id' => $item['thr-parent-id']]);
if (empty($parent)) {
return [];
}
$permissions = [
'to' => [],
'to' => [$parent['author-link']],
'cc' => [],
'bto' => [],
'bcc' => [],
];
$activity = json_decode($conversation['source'], true);
$actor = JsonLD::fetchElement($activity, 'actor', 'id');
if (!empty($actor)) {
$permissions['to'][] = $actor;
$profile = APContact::getByURL($actor);
} else {
$profile = [];
}
$parent_profile = APContact::getByURL($parent['author-link']);
$item_profile = APContact::getByURL($item['author-link']);
$exclude[] = $item['author-link'];
@ -468,26 +459,15 @@ class Transmitter
$exclude[] = $item['owner-link'];
}
foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
if (empty($activity[$element])) {
continue;
}
if (is_string($activity[$element])) {
$activity[$element] = [$activity[$element]];
}
foreach ($activity[$element] as $receiver) {
if (empty($receiver)) {
continue;
}
if (!empty($profile['followers']) && $receiver == $profile['followers'] && !empty($item_profile['followers'])) {
$permissions[$element][] = $item_profile['followers'];
} elseif (!in_array($receiver, $exclude)) {
$permissions[$element][] = $receiver;
}
$type = [Tag::TO => 'to', Tag::CC => 'cc', Tag::BTO => 'bto', Tag::BCC => 'bcc'];
foreach (Tag::getByURIId($item['thr-parent-id'], [Tag::TO, Tag::CC, Tag::BTO, Tag::BCC]) as $receiver) {
if (!empty($parent_profile['followers']) && $receiver['url'] == $parent_profile['followers'] && !empty($item_profile['followers'])) {
$permissions[$type[$receiver['type']]][] = $item_profile['followers'];
} elseif (!in_array($receiver['url'], $exclude)) {
$permissions[$type[$receiver['type']]][] = $receiver['url'];
}
}
return $permissions;
}
@ -573,7 +553,7 @@ class Transmitter
$data['cc'][] = $announce['actor']['url'];
}
$data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
$data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item));
// Check if the item is completely public or unlisted
if ($item['private'] == Item::PUBLIC) {
@ -721,6 +701,19 @@ class Transmitter
unset($receivers['bcc']);
}
foreach (['to' => Tag::TO, 'cc' => Tag::CC, 'bcc' => Tag::BCC] as $element => $type) {
if (!empty($receivers[$element])) {
foreach ($receivers[$element] as $receiver) {
if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
$name = Receiver::PUBLIC_COLLECTION;
} else {
$name = trim(parse_url($receiver, PHP_URL_PATH), '/');
}
Tag::store($item['uri-id'], $type, $name, $receiver);
}
}
}
return $receivers;
}