Merge pull request #13051 from annando/audience-followers

"audience" is set for forum posts / followers posts are directed to the followers collection
This commit is contained in:
Tobias Diekershoff 2023-04-28 07:42:40 +02:00 committed by GitHub
commit 9d770c0060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 38 deletions

View File

@ -392,21 +392,23 @@ class Group
/** /**
* Returns the combined list of contact ids from a group id list * Returns the combined list of contact ids from a group id list
* *
* @param int $uid User id * @param int $uid User id
* @param array $group_ids Groups ids * @param array $group_ids Groups ids
* @param boolean $check_dead Whether check "dead" records (?) * @param boolean $check_dead Whether check "dead" records (?)
* @param boolean $expand_followers Expand the list of followers
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function expand(int $uid, array $group_ids, bool $check_dead = false): array public static function expand(int $uid, array $group_ids, bool $check_dead = false, bool $expand_followers = true): array
{ {
if (!is_array($group_ids) || !count($group_ids)) { if (!is_array($group_ids) || !count($group_ids)) {
return []; return [];
} }
$return = []; $return = [];
$pubmail = false; $pubmail = false;
$networks = Protocol::SUPPORT_PRIVATE; $followers_collection = false;
$networks = Protocol::SUPPORT_PRIVATE;
$mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]); $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
if (DBA::isResult($mailacct)) { if (DBA::isResult($mailacct)) {
@ -419,20 +421,23 @@ class Group
$key = array_search(self::FOLLOWERS, $group_ids); $key = array_search(self::FOLLOWERS, $group_ids);
if ($key !== false) { if ($key !== false) {
$followers = Contact::selectToArray(['id'], [ if ($expand_followers) {
'uid' => $uid, $followers = Contact::selectToArray(['id'], [
'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'uid' => $uid,
'network' => $networks, 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON], 'network' => $networks,
'archive' => false, 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
'pending' => false, 'archive' => false,
'blocked' => false, 'pending' => false,
]); 'blocked' => false,
]);
foreach ($followers as $follower) { foreach ($followers as $follower) {
$return[] = $follower['id']; $return[] = $follower['id'];
}
} else {
$followers_collection = true;
} }
unset($group_ids[$key]); unset($group_ids[$key]);
} }
@ -465,6 +470,10 @@ class Group
$return = Contact::pruneUnavailable($return); $return = Contact::pruneUnavailable($return);
} }
if ($followers_collection) {
$return[] = -1;
}
return $return; return $return;
} }

View File

@ -2510,17 +2510,22 @@ class Item
/** /**
* Returns an array of contact-ids that are allowed to see this object * Returns an array of contact-ids that are allowed to see this object
* *
* @param array $obj Item array with at least uid, allow_cid, allow_gid, deny_cid and deny_gid * @param array $obj Item array with at least uid, allow_cid, allow_gid, deny_cid and deny_gid
* @param bool $check_dead Prunes unavailable contacts from the result * @param bool $check_dead Prunes unavailable contacts from the result
* @param bool $expand_followers Expand the list of followers
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function enumeratePermissions(array $obj, bool $check_dead = false): array public static function enumeratePermissions(array $obj, bool $check_dead = false, bool $expand_followers = true): array
{ {
$aclFormatter = DI::aclFormatter(); $aclFormatter = DI::aclFormatter();
if (!$expand_followers && (!empty($obj['deny_cid']) || !empty($obj['deny_gid']))) {
$expand_followers = true;
}
$allow_people = $aclFormatter->expand($obj['allow_cid']); $allow_people = $aclFormatter->expand($obj['allow_cid']);
$allow_groups = Group::expand($obj['uid'], $aclFormatter->expand($obj['allow_gid']), $check_dead); $allow_groups = Group::expand($obj['uid'], $aclFormatter->expand($obj['allow_gid']), $check_dead, $expand_followers);
$deny_people = $aclFormatter->expand($obj['deny_cid']); $deny_people = $aclFormatter->expand($obj['deny_cid']);
$deny_groups = Group::expand($obj['uid'], $aclFormatter->expand($obj['deny_gid']), $check_dead); $deny_groups = Group::expand($obj['uid'], $aclFormatter->expand($obj['deny_gid']), $check_dead);
$recipients = array_unique(array_merge($allow_people, $allow_groups)); $recipients = array_unique(array_merge($allow_people, $allow_groups));

View File

@ -557,15 +557,16 @@ class Transmitter
/** /**
* Creates an array of permissions from an item thread * Creates an array of permissions from an item thread
* *
* @param array $item Item array * @param array $item Item array
* @param boolean $blindcopy addressing via "bcc" or "cc"? * @param boolean $blindcopy addressing via "bcc" or "cc"?
* @param integer $last_id Last item id for adding receivers * @param boolean $expand_followers Expand the list of followers
* @param integer $last_id Last item id for adding receivers
* *
* @return array with permission data * @return array with permission data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
private static function createPermissionBlockForItem(array $item, bool $blindcopy, int $last_id = 0): array private static function createPermissionBlockForItem(array $item, bool $blindcopy, bool $expand_followers, int $last_id = 0): array
{ {
if ($last_id == 0) { if ($last_id == 0) {
$last_id = $item['id']; $last_id = $item['id'];
@ -607,7 +608,7 @@ class Transmitter
$networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS]; $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
} }
$data = ['to' => [], 'cc' => [], 'bcc' => []]; $data = ['to' => [], 'cc' => [], 'bcc' => [] , 'audience' => []];
if ($item['gravity'] == Item::GRAVITY_PARENT) { if ($item['gravity'] == Item::GRAVITY_PARENT) {
$actor_profile = APContact::getByURL($item['owner-link']); $actor_profile = APContact::getByURL($item['owner-link']);
@ -658,7 +659,8 @@ class Transmitter
if ($term['type'] == Tag::EXCLUSIVE_MENTION) { if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
$exclusive = true; $exclusive = true;
if (!empty($profile['followers']) && ($profile['type'] == 'Group')) { if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
$data['cc'][] = $profile['followers']; $data['cc'][] = $profile['followers'];
$data['audience'][] = $profile['url'];
} }
} elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) { } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
$mention = true; $mention = true;
@ -666,8 +668,11 @@ class Transmitter
$data['to'][] = $profile['url']; $data['to'][] = $profile['url'];
} }
} }
if (!$exclusive && ($item['private'] == Item::UNLISTED)) {
$data['to'][] = $actor_profile['followers'];
}
} else { } else {
$receiver_list = Item::enumeratePermissions($item, true); $receiver_list = Item::enumeratePermissions($item, true, $expand_followers);
foreach ($terms as $term) { foreach ($terms as $term) {
$cid = Contact::getIdForURL($term['url'], $item['uid']); $cid = Contact::getIdForURL($term['url'], $item['uid']);
@ -682,7 +687,8 @@ class Transmitter
if ($term['type'] == Tag::EXCLUSIVE_MENTION) { if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
$exclusive = true; $exclusive = true;
if (!empty($profile['followers']) && ($profile['type'] == 'Group')) { if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
$data['cc'][] = $profile['followers']; $data['cc'][] = $profile['followers'];
$data['audience'][] = $profile['url'];
} }
} elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) { } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
$mention = true; $mention = true;
@ -700,6 +706,11 @@ class Transmitter
$data['cc'][] = $follower; $data['cc'][] = $follower;
} elseif (!$exclusive) { } elseif (!$exclusive) {
foreach ($receiver_list as $receiver) { foreach ($receiver_list as $receiver) {
if ($receiver == -1) {
$data['to'][] = $actor_profile['followers'];
continue;
}
$contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]); $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) { if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
continue; continue;
@ -766,9 +777,10 @@ class Transmitter
DBA::close($parents); DBA::close($parents);
} }
$data['to'] = array_unique($data['to']); $data['to'] = array_unique($data['to']);
$data['cc'] = array_unique($data['cc']); $data['cc'] = array_unique($data['cc']);
$data['bcc'] = array_unique($data['bcc']); $data['bcc'] = array_unique($data['bcc']);
$data['audience'] = array_unique($data['audience']);
if (($key = array_search($item['author-link'], $data['to'])) !== false) { if (($key = array_search($item['author-link'], $data['to'])) !== false) {
unset($data['to'][$key]); unset($data['to'][$key]);
@ -782,6 +794,10 @@ class Transmitter
unset($data['bcc'][$key]); unset($data['bcc'][$key]);
} }
if (($key = array_search($item['author-link'], $data['audience'])) !== false) {
unset($data['audience'][$key]);
}
foreach ($data['to'] as $to) { foreach ($data['to'] as $to) {
if (($key = array_search($to, $data['cc'])) !== false) { if (($key = array_search($to, $data['cc'])) !== false) {
unset($data['cc'][$key]); unset($data['cc'][$key]);
@ -800,6 +816,13 @@ class Transmitter
$receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])]; $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
if (!empty($data['audience'])) {
$receivers['audience'] = array_values($data['audience']);
if (count($receivers['audience']) == 1) {
$receivers['audience'] = $receivers['audience'][0];
}
}
if (!$blindcopy) { if (!$blindcopy) {
unset($receivers['bcc']); unset($receivers['bcc']);
} }
@ -935,7 +958,7 @@ class Transmitter
*/ */
public static function fetchTargetInboxes(array $item, int $uid, bool $personal = false, int $last_id = 0): array public static function fetchTargetInboxes(array $item, int $uid, bool $personal = false, int $last_id = 0): array
{ {
$permissions = self::createPermissionBlockForItem($item, true, $last_id); $permissions = self::createPermissionBlockForItem($item, true, true, $last_id);
if (empty($permissions)) { if (empty($permissions)) {
return []; return [];
} }
@ -1067,7 +1090,7 @@ class Transmitter
$data['actor'] = $mail['author-link']; $data['actor'] = $mail['author-link'];
$data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM); $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
$data['instrument'] = self::getService(); $data['instrument'] = self::getService();
$data = array_merge($data, self::createPermissionBlockForItem($mail, true)); $data = array_merge($data, self::createPermissionBlockForItem($mail, true, false));
if (empty($data['to']) && !empty($data['cc'])) { if (empty($data['to']) && !empty($data['cc'])) {
$data['to'] = $data['cc']; $data['to'] = $data['cc'];
@ -1292,7 +1315,7 @@ class Transmitter
$data['instrument'] = self::getService(); $data['instrument'] = self::getService();
$data = array_merge($data, self::createPermissionBlockForItem($item, false)); $data = array_merge($data, self::createPermissionBlockForItem($item, false, false));
if (in_array($data['type'], ['Create', 'Update', 'Delete'])) { if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
$data['object'] = self::createNote($item, $api_mode); $data['object'] = self::createNote($item, $api_mode);
@ -1640,7 +1663,7 @@ class Transmitter
$data['name'] = BBCode::toPlaintext($item['title'], false); $data['name'] = BBCode::toPlaintext($item['title'], false);
} }
$permission_block = self::createPermissionBlockForItem($item, false); $permission_block = self::createPermissionBlockForItem($item, false, false);
$real_quote = false; $real_quote = false;