Private forums are now working via ActivityPub

This commit is contained in:
Michael 2022-02-12 18:38:36 +00:00
parent cdee2b44db
commit e394143148
13 changed files with 97 additions and 154 deletions

View file

@ -1277,7 +1277,7 @@ CREATE TABLE IF NOT EXISTS `post-thread-user` (
`wall` boolean NOT NULL DEFAULT '0' COMMENT 'This item was posted to the wall of uid',
`mention` boolean NOT NULL DEFAULT '0' COMMENT '',
`pubmail` boolean NOT NULL DEFAULT '0' COMMENT '',
`forum_mode` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '',
`forum_mode` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'Deprecated',
`contact-id` int unsigned NOT NULL DEFAULT 0 COMMENT 'contact.id',
`unseen` boolean NOT NULL DEFAULT '1' COMMENT 'post has not been seen',
`hidden` boolean NOT NULL DEFAULT '0' COMMENT 'Marker to hide the post from the user',
@ -1612,7 +1612,6 @@ CREATE VIEW `post-user-view` AS SELECT
`post-user`.`deleted` AS `deleted`,
`post-user`.`origin` AS `origin`,
`post-thread-user`.`origin` AS `parent-origin`,
`post-thread-user`.`forum_mode` AS `forum_mode`,
`post-thread-user`.`mention` AS `mention`,
`post-user`.`global` AS `global`,
`post-user`.`network` AS `network`,
@ -1773,7 +1772,6 @@ CREATE VIEW `post-thread-user-view` AS SELECT
`post-thread-user`.`unseen` AS `unseen`,
`post-user`.`deleted` AS `deleted`,
`post-thread-user`.`origin` AS `origin`,
`post-thread-user`.`forum_mode` AS `forum_mode`,
`post-thread-user`.`mention` AS `mention`,
`post-user`.`global` AS `global`,
`post-thread-user`.`network` AS `network`,

View file

@ -24,7 +24,7 @@ Fields
| wall | This item was posted to the wall of uid | boolean | NO | | 0 | |
| mention | | boolean | NO | | 0 | |
| pubmail | | boolean | NO | | 0 | |
| forum_mode | | tinyint unsigned | NO | | 0 | |
| forum_mode | Deprecated | tinyint unsigned | NO | | 0 | |
| contact-id | contact.id | int unsigned | NO | | 0 | |
| unseen | post has not been seen | boolean | NO | | 1 | |
| hidden | Marker to hide the post from the user | boolean | NO | | 0 | |

View file

@ -29,7 +29,6 @@
*/
use Friendica\App;
use Friendica\Content\Item as ItemHelper;
use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook;
@ -40,11 +39,11 @@ use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\APContact;
use Friendica\Model\Attach;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
use Friendica\Model\FileTag;
use Friendica\Model\Group;
use Friendica\Model\Item;
use Friendica\Model\ItemURI;
use Friendica\Model\Notification;
@ -404,7 +403,7 @@ function item_post(App $a) {
}
$inform .= 'cid:' . $contact['id'];
if (!$toplevel_item_id || empty($contact['cid']) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY)) {
if ($toplevel_item_id || empty($contact['cid']) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY)) {
continue;
}
@ -437,26 +436,15 @@ function item_post(App $a) {
$postopts = '';
}
if (!$private_forum) {
$str_contact_deny = '';
$str_group_deny = '';
if ($private_forum) {
$str_contact_allow = '<' . $private_id . '>';
$str_group_allow = '<' . Group::getIdForForum($forum_contact['id']) . '>';
} else {
$str_contact_allow = '';
$str_group_allow = '';
$str_contact_deny = '';
$str_group_deny = '';
}
if ($private_forum || !APContact::getByURL($forum_contact['url'])) {
$str_group_allow = '';
$str_contact_deny = '';
$str_group_deny = '';
if ($private_forum) {
$str_contact_allow = '<' . $private_id . '>';
} else {
$str_contact_allow = '';
}
$contact_id = $private_id;
$contact_record = $forum_contact;
$_REQUEST['origin'] = false;
$wall = 0;
}
}

View file

@ -685,7 +685,7 @@ class Contact
*/
public static function updateSelfFromUserID($uid, $update_avatar = false)
{
$fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey',
$fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey', 'manually-approve',
'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
'photo', 'thumb', 'micro', 'header', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network'];
$self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
@ -757,6 +757,7 @@ class Contact
$fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
$fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
$fields['unsearchable'] = !$profile['net-publish'];
$fields['manually-approve'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
$update = false;

View file

@ -41,7 +41,7 @@ class Group
public static function getByUserId($uid, $includesDeleted = false)
{
$conditions = ['uid' => $uid];
$conditions = ['uid' => $uid, 'cid' => null];
if (!$includesDeleted) {
$conditions['deleted'] = false;
@ -408,7 +408,7 @@ class Group
]
];
$stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
$stmt = DBA::select('group', [], ['deleted' => false, 'uid' => $uid, 'cid' => null], ['order' => ['name']]);
while ($group = DBA::fetch($stmt)) {
$display_groups[] = [
'name' => $group['name'],
@ -465,7 +465,7 @@ class Group
$member_of = self::getIdsByContactId($cid);
}
$stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
$stmt = DBA::select('group', [], ['deleted' => false, 'uid' => local_user(), 'cid' => null], ['order' => ['name']]);
while ($group = DBA::fetch($stmt)) {
$selected = (($group_id == $group['id']) ? ' group-selected' : '');
@ -522,19 +522,17 @@ class Group
}
/**
* Fetch the followers of a given contact id and store them as group members
* Fetch the group id for the given contact id
*
* @param integer $id Contact ID
* @return integer Group IO
*/
public static function getMembersForForum(int $id) {
$contact = Contact::getById($id, ['uid', 'url', 'name']);
if (empty($contact)) {
return;
}
$apcontact = APContact::getByURL($contact['url']);
if (empty($apcontact['followers'])) {
return;
public static function getIdForForum(int $id)
{
Logger::info('Get id for forum id', ['id' => $id]);
$contact = Contact::getById($id, ['uid', 'name', 'contact-type', 'manually-approve']);
if (empty($contact) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY) || !$contact['manually-approve']) {
return 0;
}
$group = DBA::selectFirst('group', ['id'], ['uid' => $contact['uid'], 'cid' => $id]);
@ -550,6 +548,33 @@ class Group
$gid = $group['id'];
}
return $gid;
}
/**
* Fetch the followers of a given contact id and store them as group members
*
* @param integer $id Contact ID
*/
public static function getMembersForForum(int $id)
{
Logger::info('Update forum members', ['id' => $id]);
$contact = Contact::getById($id, ['uid', 'url']);
if (empty($contact)) {
return;
}
$apcontact = APContact::getByURL($contact['url']);
if (empty($apcontact['followers'])) {
return;
}
$gid = self::getIdForForum($id);
if (empty($gid)) {
return;
}
$group_members = DBA::selectToArray('group_member', ['contact-id'], ['gid' => $gid]);
if (!empty($group_members)) {
$current = array_unique(array_column($group_members, 'contact-id'));
@ -557,7 +582,7 @@ class Group
$current = [];
}
foreach (ActivityPub::fetchItems($apcontact['followers']) as $follower) {
foreach (ActivityPub::fetchItems($apcontact['followers'], $contact['uid']) as $follower) {
$id = Contact::getIdForURL($follower);
if (!in_array($id, $current)) {
DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $id]);
@ -568,5 +593,6 @@ class Group
}
DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $current]);
Logger::info('Updated forum members', ['id' => $id, 'count' => DBA::count('group_member', ['gid' => $gid])]);
}
}

View file

@ -100,7 +100,7 @@ class Item
'inform', 'deleted', 'extid', 'post-type', 'post-reason', 'gravity',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'author-id', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
'signed_text', 'network', 'wall', 'contact-id', 'plink', 'forum_mode', 'origin',
'signed_text', 'network', 'wall', 'contact-id', 'plink', 'origin',
'thr-parent-id', 'parent-uri-id', 'postopts', 'pubmail',
'event-created', 'event-edited', 'event-start', 'event-finish',
'event-summary', 'event-desc', 'event-location', 'event-type',
@ -114,7 +114,7 @@ class Item
'postopts', 'plink', 'resource-id', 'event-id', 'inform',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type', 'post-reason',
'private', 'pubmail', 'visible', 'starred',
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'network',
'unseen', 'deleted', 'origin', 'mention', 'global', 'network',
'title', 'content-warning', 'body', 'location', 'coord', 'app',
'rendered-hash', 'rendered-html', 'object-type', 'object', 'target-type', 'target',
'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
@ -655,7 +655,7 @@ class Item
$fields = ['uid', 'uri', 'parent-uri', 'id', 'deleted',
'uri-id', 'parent-uri-id',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'wall', 'private', 'forum_mode', 'origin', 'author-id'];
'wall', 'private', 'origin', 'author-id'];
$condition = ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']];
$params = ['order' => ['id' => false]];
$parent = Post::selectFirst($fields, $condition, $params);
@ -881,10 +881,15 @@ class Item
$item['parent-uri'] = $toplevel_parent['uri'];
$item['parent-uri-id'] = $toplevel_parent['uri-id'];
$item['deleted'] = $toplevel_parent['deleted'];
$item['allow_cid'] = $toplevel_parent['allow_cid'];
$item['allow_gid'] = $toplevel_parent['allow_gid'];
$item['deny_cid'] = $toplevel_parent['deny_cid'];
$item['deny_gid'] = $toplevel_parent['deny_gid'];
// Reshares have to keep their permissions to allow forums to work
if (!$item['origin'] || ($item['verb'] != Activity::ANNOUNCE)) {
$item['allow_cid'] = $toplevel_parent['allow_cid'];
$item['allow_gid'] = $toplevel_parent['allow_gid'];
$item['deny_cid'] = $toplevel_parent['deny_cid'];
$item['deny_gid'] = $toplevel_parent['deny_gid'];
}
$parent_origin = $toplevel_parent['origin'];
// Don't federate received participation messages
@ -905,15 +910,6 @@ class Item
$item['private'] = $toplevel_parent['private'];
}
/*
* Edge case. We host a public forum that was originally posted to privately.
* The original author commented, but as this is a comment, the permissions
* weren't fixed up so it will still show the comment as private unless we fix it here.
*/
if ((intval($toplevel_parent['forum_mode']) == 1) && ($toplevel_parent['private'] != self::PUBLIC)) {
$item['private'] = self::PUBLIC;
}
// If its a post that originated here then tag the thread as "mention"
if ($item['origin'] && $item['uid']) {
DBA::update('post-thread-user', ['mention' => true], ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
@ -1451,7 +1447,6 @@ class Item
unset($item['pinned']);
unset($item['ignored']);
unset($item['pubmail']);
unset($item['forum_mode']);
unset($item['event-id']);
unset($item['hidden']);
@ -1928,41 +1923,18 @@ class Item
Logger::info('Community post will be distributed', ['uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
self::performActivity($item['id'], 'announce', $uid);
if ($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) {
Group::getMembersForForum($owner['id']);
/**
* All the following lines are only needed for private forums and compatibility to older systems without AP support.
* A possible way would be that the followers list of a forum would always be readable by all followers.
* So this would mean that the comment distribution could be done exactly for the intended audience.
* Or possibly we could store the receivers that had been in the "announce" message above and use this.
*/
// also reset all the privacy bits to the forum default permissions
if ($owner['allow_cid'] || $owner['allow_gid'] || $owner['deny_cid'] || $owner['deny_gid']) {
$private = self::PRIVATE;
} elseif (DI::pConfig()->get($owner['uid'], 'system', 'unlisted')) {
$private = self::UNLISTED;
$allow_cid = '<' . $owner['id'] . '>';
$allow_gid = '<' . Group::getIdForForum($owner['id']) . '>';
$deny_cid = '';
$deny_gid = '';
self::performActivity($item['id'], 'announce', $uid, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
} else {
$private = self::PUBLIC;
self::performActivity($item['id'], 'announce', $uid);
}
$permissionSet = DI::permissionSet()->selectOrCreate(
DI::permissionSetFactory()->createFromString(
$owner['uid'],
$owner['allow_cid'],
$owner['allow_gid'],
$owner['deny_cid'],
$owner['deny_gid']
));
$forum_mode = ($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) ? 2 : 1;
$fields = ['wall' => true, 'origin' => true, 'forum_mode' => $forum_mode, 'contact-id' => $owner['id'],
'owner-id' => Contact::getPublicIdByUserId($uid), 'private' => $private, 'psid' => $permissionSet->id];
self::update($fields, ['id' => $item['id']]);
Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, (int)$item['uri-id'], (int)$item['uid']);
Logger::info('Community post had been distributed', ['uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
return false;
}
@ -2325,12 +2297,17 @@ class Item
*
* Toggle activities as like,dislike,attend of an item
*
* @param int $item_id
* @param int $item_id
* @param string $verb
* Activity verb. One of
* like, unlike, dislike, undislike, attendyes, unattendyes,
* attendno, unattendno, attendmaybe, unattendmaybe,
* announce, unannouce
* @param int $uid
* @param string $allow_cid
* @param string $allow_gid
* @param string $deny_cid
* @param string $deny_gid
* @return bool
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
@ -2338,7 +2315,7 @@ class Item
* array $arr
* 'post_id' => ID of posted item
*/
public static function performActivity(int $item_id, string $verb, int $uid)
public static function performActivity(int $item_id, string $verb, int $uid, string $allow_cid = null, string $allow_gid = null, string $deny_cid = null, string $deny_gid = null)
{
if (empty($uid)) {
return false;
@ -2479,6 +2456,11 @@ class Item
return true;
}
$allow_cid = $allow_cid ?? $item['allow_cid'];
$allow_gid = $allow_gid ?? $item['allow_gid'];
$deny_cid = $deny_cid ?? $item['deny_cid'];
$deny_gid = $deny_gid ?? $item['deny_gid'];
$objtype = $item['resource-id'] ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE;
$new_item = [
@ -2499,10 +2481,10 @@ class Item
'body' => $activity,
'verb' => $activity,
'object-type' => $objtype,
'allow_cid' => $item['allow_cid'],
'allow_gid' => $item['allow_gid'],
'deny_cid' => $item['deny_cid'],
'deny_gid' => $item['deny_gid'],
'allow_cid' => $allow_cid,
'allow_gid' => $allow_gid,
'deny_cid' => $deny_cid,
'deny_gid' => $deny_gid,
'visible' => 1,
'unseen' => 1,
];

View file

@ -56,7 +56,7 @@ class Ownership extends BaseApi
BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
$uid = BaseApi::getCurrentUserID();
$groups = $this->dba->select('group', [], ['deleted' => false, 'uid' => $uid]);
$groups = $this->dba->select('group', [], ['deleted' => false, 'uid' => $uid, 'cid' => null]);
// loop through all groups
$lists = [];

View file

@ -184,7 +184,7 @@ class Transmitter
// Allow fetching the contact list when the requester is part of the list.
if (($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) && !empty($requester)) {
$show_contacts = DBA::exists('contact', ['nurl' => Strings::normaliseLink($requester), 'rel' => $rel]);
$show_contacts = DBA::exists('contact', ['nurl' => Strings::normaliseLink($requester), 'uid' => $owner['uid'], 'blocked' => false]);
}
if (!$show_contacts) {
@ -1079,20 +1079,6 @@ class Transmitter
return false;
}
// In case of a forum post ensure to return the original post if author and forum are on the same machine
if (($item['gravity'] == GRAVITY_PARENT) && !empty($item['forum_mode'])) {
$author = Contact::getById($item['author-id'], ['nurl']);
if (!empty($author['nurl'])) {
$self = Contact::selectFirst(['uid'], ['nurl' => $author['nurl'], 'self' => true]);
if (!empty($self['uid'])) {
$forum_item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
if (DBA::isResult($forum_item)) {
$item = $forum_item;
}
}
}
}
if (empty($item['uri-id'])) {
Logger::warning('Item without uri-id', ['item' => $item]);
return false;

View file

@ -1557,22 +1557,11 @@ class DFRN
// was the top-level post for this action written by somebody on this site?
// Specifically, the recipient?
$parent = Post::selectFirst(['forum_mode', 'wall'],
$parent = Post::selectFirst(['wall'],
["`uri` = ? AND `uid` = ?" . $sql_extra, $item["thr-parent"], $importer["importer_uid"]]);
$is_a_remote_action = DBA::isResult($parent);
/*
* Does this have the characteristics of a community or private group action?
* If it's an action to a wall post on a community/prvgroup page it's a
* valid community action. Also forum_mode makes it valid for sure.
* If neither, it's not.
*/
if ($is_a_remote_action && $community && (!$parent["forum_mode"]) && (!$parent["wall"])) {
$is_a_remote_action = false;
Logger::notice("not a community action");
}
if ($is_a_remote_action) {
return DFRN::REPLY_RC;
} else {

View file

@ -46,7 +46,6 @@ class Delivery
const DELETION = 'drop';
const POST = 'wall-new';
const POKE = 'poke';
const UPLINK = 'uplink';
const REMOVAL = 'removeme';
const PROFILEUPDATE = 'profileupdate';

View file

@ -223,10 +223,6 @@ class Notifier
$relay_to_owner = true;
}
if (($cmd === Delivery::UPLINK) && (intval($parent['forum_mode']) == 1) && !$top_level) {
$relay_to_owner = true;
}
// until the 'origin' flag has been in use for several months
// we will just use it as a fallback test
// later we will be able to use it as the primary test of whether or not to relay.
@ -333,15 +329,6 @@ class Notifier
$deny_people = $aclFormatter->expand($parent['deny_cid']);
$deny_groups = Group::expand($uid, $aclFormatter->expand($parent['deny_gid']));
// if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
// a delivery fork. private groups (forum_mode == 2) do not uplink
/// @todo Possibly we should not uplink when the author is the forum itself?
if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== Delivery::UPLINK)
&& ($target_item['verb'] != Activity::ANNOUNCE)) {
Worker::add($a->getQueueValue('priority'), 'Notifier', Delivery::UPLINK, $post_uriid, $sender_uid);
}
foreach ($items as $item) {
$recipients[] = $item['contact-id'];
// pull out additional tagged people to notify (if public message)
@ -813,15 +800,4 @@ class Notifier
return ['count' => $delivery_queue_count, 'contacts' => $contacts];
}
/**
* Check if the delivered item is a forum post
*
* @param array $item
* @return boolean
*/
public static function isForumPost(array $item)
{
return ($item['gravity'] == GRAVITY_PARENT) && !empty($item['forum_mode']);
}
}

View file

@ -1301,7 +1301,7 @@ return [
"wall" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "This item was posted to the wall of uid"],
"mention" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"pubmail" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"forum_mode" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => ""],
"forum_mode" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "Deprecated"],
"contact-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id"], "comment" => "contact.id"],
"unseen" => ["type" => "boolean", "not null" => "1", "default" => "1", "comment" => "post has not been seen"],
"hidden" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Marker to hide the post from the user"],

View file

@ -91,7 +91,6 @@
"deleted" => ["post-user", "deleted"],
"origin" => ["post-user", "origin"],
"parent-origin" => ["post-thread-user", "origin"],
"forum_mode" => ["post-thread-user", "forum_mode"],
"mention" => ["post-thread-user", "mention"],
"global" => ["post-user", "global"],
"network" => ["post-user", "network"],
@ -250,7 +249,6 @@
"unseen" => ["post-thread-user", "unseen"],
"deleted" => ["post-user", "deleted"],
"origin" => ["post-thread-user", "origin"],
"forum_mode" => ["post-thread-user", "forum_mode"],
"mention" => ["post-thread-user", "mention"],
"global" => ["post-user", "global"],
"network" => ["post-thread-user", "network"],