Merge pull request #11253 from annando/forum3

More rework to make private communities working
This commit is contained in:
Hypolite Petovan 2022-02-18 12:19:57 -05:00 committed by GitHub
commit c03ff7833b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 304 additions and 295 deletions

View file

@ -556,7 +556,7 @@ class Group
*
* @param integer $id Contact ID
*/
public static function getMembersForForum(int $id)
public static function updateMembersForForum(int $id)
{
Logger::info('Update forum members', ['id' => $id]);

View file

@ -1236,8 +1236,11 @@ class Item
return;
}
$self_contact = Contact::selectFirst(['id'], ['uid' => $item['uid'], 'self' => true]);
$self = !empty($self_contact) ? $self_contact['id'] : 0;
$cid = Contact::getIdForURL($author['url'], $item['uid']);
if (empty($cid) || !Contact::isSharing($cid, $item['uid'])) {
if (empty($cid) || (!Contact::isSharing($cid, $item['uid']) && ($cid != $self))) {
Logger::info('The resharer is not a following contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid'], 'cid' => $cid]);
return;
}
@ -1471,7 +1474,7 @@ class Item
}
// When the post belongs to a a forum then all forum users are allowed to access it
foreach (Tag::getByURIId($uriid, [Tag::EXCLUSIVE_MENTION]) as $tag) {
foreach (Tag::getByURIId($uriid, [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $tag) {
if (DBA::exists('contact', ['uid' => $uid, 'nurl' => Strings::normaliseLink($tag['url']), 'contact-type' => Contact::TYPE_COMMUNITY])) {
$target_uid = User::getIdForURL($tag['url']);
if (!empty($target_uid)) {
@ -1945,7 +1948,7 @@ class Item
$owner = User::getOwnerDataById($uid);
if (!DBA::isResult($owner)) {
Logger::warning('User not found, quitting.', ['uid' => $uid]);
Logger::warning('User not found, quitting here.', ['uid' => $uid]);
return false;
}
@ -1954,55 +1957,50 @@ class Item
return false;
}
$item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
$item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'origin' => false]);
if (!DBA::isResult($item)) {
Logger::warning('Post not found, quitting.', ['id' => $item_id]);
Logger::debug('Post is an activity or origin or not found at all, quitting here.', ['id' => $item_id]);
return false;
}
if ($item['wall'] || $item['origin'] || ($item['gravity'] != GRAVITY_PARENT)) {
Logger::debug('Wall item, origin item or no parent post, quitting here.', ['wall' => $item['wall'], 'origin' => $item['origin'], 'gravity' => $item['gravity'], 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
return false;
}
$tags = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
foreach ($tags as $tag) {
if (Strings::compareLink($owner['url'], $tag['url'])) {
$mention = true;
Logger::info('Mention found in tag.', ['url' => $tag['url'], 'uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
}
}
// This check can most likely be removed since we always are having the tags
if (!$mention) {
$cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
if (Strings::compareLink($owner['url'], $mtch[1])) {
$mention = true;
Logger::notice('Mention found in body.', ['mention' => $mtch[2], 'uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
}
if ($item['gravity'] == GRAVITY_PARENT) {
$tags = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
foreach ($tags as $tag) {
if (Strings::compareLink($owner['url'], $tag['url'])) {
$mention = true;
Logger::info('Mention found in tag.', ['url' => $tag['url'], 'uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
}
}
if (!$mention) {
Logger::info('Top-level post without mention is deleted.', ['uri' => $item['uri'], $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
Post\User::delete(['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
return true;
}
$arr = ['item' => $item, 'user' => $owner];
Hook::callAll('tagged', $arr);
} else {
$tags = Tag::getByURIId($item['parent-uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
foreach ($tags as $tag) {
if (Strings::compareLink($owner['url'], $tag['url'])) {
$mention = true;
Logger::info('Mention found in parent tag.', ['url' => $tag['url'], 'uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
}
}
if (!$mention) {
Logger::debug('No mentions found in parent, quitting here.', ['id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
return false;
}
}
if (!$mention) {
Logger::info('Top-level post without mention is deleted.', ['uri' => $item['uri'], $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
Post\User::delete(['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
return true;
}
$arr = ['item' => $item, 'user' => $owner];
Hook::callAll('tagged', $arr);
Logger::info('Community post will be distributed', ['uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
if ($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) {
Group::getMembersForForum($owner['id']);
$allow_cid = '<' . $owner['id'] . '>';
$allow_gid = '<' . Group::getIdForForum($owner['id']) . '>';
$allow_cid = '';
$allow_gid = '<' . Group::FOLLOWERS . '>';
$deny_cid = '';
$deny_gid = '';
self::performActivity($item['id'], 'announce', $uid, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
@ -3215,30 +3213,20 @@ class Item
}
/**
* Is the given item array a post that is sent as starting post to a forum?
* Does the given uri-id belongs to a post that is sent as starting post to a forum?
*
* @param array $item
* @param array $owner
* @param int $uri_id
*
* @return boolean "true" when it is a forum post
*/
public static function isForumPost(array $item, array $owner = [])
public static function isForumPost(int $uri_id)
{
if (empty($owner)) {
$owner = User::getOwnerDataById($item['uid']);
if (empty($owner)) {
return false;
foreach (Tag::getByURIId($uri_id, [Tag::EXCLUSIVE_MENTION]) as $tag) {
if (DBA::exists('contact', ['uid' => 0, 'nurl' => Strings::normaliseLink($tag['url']), 'contact-type' => Contact::TYPE_COMMUNITY])) {
return true;
}
}
if (($item['author-id'] == $item['owner-id']) ||
($owner['id'] == $item['contact-id']) ||
($item['uri-id'] != $item['parent-uri-id']) ||
$item['origin']) {
return false;
}
return Contact::isForum($item['contact-id']);
return false;
}
/**

View file

@ -182,6 +182,11 @@ class UserNotification
return;
}
$author = Contact::getById($item['author-id'], ['contact-type']);
if (empty($author)) {
return;
}
$notification_type = self::TYPE_NONE;
if (self::checkShared($item, $uid)) {
@ -232,7 +237,7 @@ class UserNotification
}
}
if (self::checkDirectCommentedThread($item, $contacts)) {
if (($contact['contact-type'] != Contact::TYPE_COMMUNITY) && self::checkDirectCommentedThread($item, $contacts)) {
$notification_type = $notification_type | self::TYPE_DIRECT_THREAD_COMMENT;
if (!$notified) {
self::insertNotificationByItem(self::TYPE_DIRECT_THREAD_COMMENT, $uid, $item);

View file

@ -81,6 +81,7 @@ class Objects extends BaseModule
$requester = HTTPSignature::getSigner('', $_SERVER);
if (!empty($requester)) {
$receivers = Item::enumeratePermissions($item, false);
$receivers[] = $item['contact-id'];
$validated = in_array(Contact::getIdForURL($requester, $item['uid']), $receivers);
if (!$validated) {

View file

@ -82,7 +82,7 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
{
$message = [];
$causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'pending']);
$causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']);
if (empty($causer)) {
$this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
return $message;
@ -124,7 +124,7 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
}
if (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_SHARED])) {
$author = Contact::getById($item['author-id'], ['id', 'name', 'url']);
$author = Contact::getById($item['author-id'], ['id', 'name', 'url', 'contact-type']);
if (empty($author)) {
$this->logger->info('Author not found', ['author' => $item['author-id']]);
return $message;

View file

@ -667,6 +667,15 @@ class Receiver
$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;
}

View file

@ -509,28 +509,33 @@ class Transmitter
/**
* Creates an array of permissions from an item thread
*
* @param array $item Item array
* @param boolean $blindcopy addressing via "bcc" or "cc"?
* @param integer $last_id Last item id for adding receivers
* @param boolean $forum_post "true" means that we are sending content to a forum
* @param array $item Item array
* @param boolean $blindcopy addressing via "bcc" or "cc"?
* @param integer $last_id Last item id for adding receivers
*
* @return array with permission data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_post = false)
private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0)
{
if ($last_id == 0) {
$last_id = $item['id'];
}
$always_bcc = false;
$is_forum = false;
$follower = '';
// Check if we should always deliver our stuff via BCC
if (!empty($item['uid'])) {
$profile = User::getOwnerDataById($item['uid']);
if (!empty($profile)) {
$always_bcc = $profile['hide-friends'];
$owner = User::getOwnerDataById($item['uid']);
if (!empty($owner)) {
$always_bcc = $owner['hide-friends'];
$is_forum = ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && $owner['manually-approve'];
$profile = APContact::getByURL($owner['url'], false);
$follower = $profile['followers'] ?? '';
}
}
@ -613,7 +618,9 @@ class Transmitter
}
}
if (!$exclusive) {
if ($is_forum && !$exclusive && !empty($follower)) {
$data['cc'][] = $follower;
} elseif (!$exclusive) {
foreach ($receiver_list as $receiver) {
$contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
@ -652,9 +659,7 @@ class Transmitter
}
} elseif (!$exclusive) {
// Public thread parent post always are directed to the followers.
// This mustn't be done by posts that are directed to forum servers via the exclusive mention.
// But possibly in that case we could add the "followers" collection of the forum to the message.
if (($item['private'] != Item::PRIVATE) && !$forum_post) {
if ($item['private'] != Item::PRIVATE) {
$data['cc'][] = $actor_profile['followers'];
}
}
@ -820,18 +825,17 @@ class Transmitter
/**
* Fetches an array of inboxes for the given item and user
*
* @param array $item Item array
* @param integer $uid User ID
* @param boolean $personal fetch personal inboxes
* @param integer $last_id Last item id for adding receivers
* @param boolean $forum_post "true" means that we are sending content to a forum
* @param array $item Item array
* @param integer $uid User ID
* @param boolean $personal fetch personal inboxes
* @param integer $last_id Last item id for adding receivers
* @return array with inboxes
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_post = false)
public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0)
{
$permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_post);
$permissions = self::createPermissionBlockForItem($item, true, $last_id);
if (empty($permissions)) {
return [];
}

View file

@ -235,13 +235,13 @@ class Notifier
}
// Special treatment for forum posts
if (Item::isForumPost($target_item, $owner)) {
if (Item::isForumPost($target_item['uri-id'])) {
$relay_to_owner = true;
$direct_forum_delivery = true;
}
// Avoid that comments in a forum thread are sent to OStatus
if (Item::isForumPost($parent, $owner)) {
if (Item::isForumPost($parent['uri-id'])) {
$direct_forum_delivery = true;
}
@ -729,6 +729,14 @@ class Notifier
$uid = $target_item['contact-uid'] ?: $target_item['uid'];
// Update the locally stored follower list when we deliver to a forum
foreach (Tag::getByURIId($target_item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $tag) {
$target_contact = Contact::getByURL(Strings::normaliseLink($tag['url']), null, [], $uid);
if (($target_contact['contact-type'] == Contact::TYPE_COMMUNITY) && $target_contact['manually-approve']) {
Group::updateMembersForForum($target_contact['id']);
}
}
if ($target_item['origin']) {
$inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
@ -738,9 +746,6 @@ class Notifier
}
Logger::info('Origin item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.');
} elseif (Item::isForumPost($target_item, $owner)) {
$inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid, false, 0, true);
Logger::info('Forum item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.');
} elseif (!DBA::exists('conversation', ['item-uri' => $target_item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB])) {
Logger::info('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.');
return ['count' => 0, 'contacts' => []];