Merge pull request #9001 from annando/ap-forum

Improvements for delivering forum posts
This commit is contained in:
Hypolite Petovan 2020-08-11 07:38:41 -04:00 committed by GitHub
commit 8451939708
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 192 additions and 71 deletions

View file

@ -5710,7 +5710,7 @@ function api_friendica_activity($type)
$id = $_REQUEST['id'] ?? 0;
$res = Item::performActivity($id, $verb);
$res = Item::performActivity($id, $verb, api_user());
if ($res) {
if ($type == "xml") {

View file

@ -712,8 +712,13 @@ function conversation_fetch_comments($thread_items, $pinned) {
$lineno = 0;
$actor = [];
$received = '';
$owner = '';
while ($row = Item::fetch($thread_items)) {
if (($row['verb'] == Activity::ANNOUNCE) && ($row['author-link'] == $owner)) {
continue;
}
if (($row['verb'] == Activity::ANNOUNCE) && !empty($row['contact-uid']) && ($row['received'] > $received) && ($row['thr-parent'] == $row['parent-uri'])) {
$actor = ['link' => $row['author-link'], 'avatar' => $row['author-avatar'], 'name' => $row['author-name']];
$received = $row['received'];
@ -724,6 +729,10 @@ function conversation_fetch_comments($thread_items, $pinned) {
$parentlines[] = $lineno;
}
if (($row['gravity'] == GRAVITY_PARENT) && ($row['author-link'] != $row['owner-link'])) {
$owner = $row['owner-link'];
}
if ($row['gravity'] == GRAVITY_PARENT) {
$row['pinned'] = $pinned;
}
@ -765,7 +774,7 @@ function conversation_add_children(array $parents, $block_authors, $order, $uid)
$max_comments = DI::config()->get('system', 'max_display_comments', 1000);
}
$params = ['order' => ['uid', 'commented' => true]];
$params = ['order' => ['gravity', 'uid', 'commented' => true]];
if ($max_comments > 0) {
$params['limit'] = $max_comments;

View file

@ -34,7 +34,7 @@ function subthread_content(App $a)
$item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
if (!Item::performActivity($item_id, 'follow')) {
if (!Item::performActivity($item_id, 'follow', local_user())) {
Logger::info('Following item failed', ['item' => $item_id]);
throw new HTTPException\BadRequestException();
}

View file

@ -1578,6 +1578,8 @@ class Item
return GRAVITY_COMMENT;
} elseif ($activity->match($item['verb'], Activity::FOLLOW)) {
return GRAVITY_ACTIVITY;
} elseif ($activity->match($item['verb'], Activity::ANNOUNCE)) {
return GRAVITY_ACTIVITY;
}
Logger::info('Unknown gravity for verb', ['verb' => $item['verb']]);
return GRAVITY_UNKNOWN; // Should not happen
@ -1798,6 +1800,7 @@ class Item
// It is mainly used in the "post_local" hook.
unset($item['api_source']);
self::transformToForumPost($item);
// Check for hashtags in the body and repair or add hashtag links
$item['body'] = self::setHashtags($item['body']);
@ -1988,6 +1991,44 @@ class Item
return $current_post;
}
/**
* Convert items to forum posts
*
* (public) forum posts in the new format consist of the regular post by the author
* followed by an announce message sent from the forum account.
* This means we have to look out for an announce message send by a forum account.
*
* @param array $item
* @return void
*/
private static function transformToForumPost(array $item)
{
if ($item["verb"] != Activity::ANNOUNCE) {
// No announce message, so don't do anything
return;
}
$pcontact = Contact::selectFirst(['nurl'], ['id' => $item['author-id'], 'contact-type' => Contact::TYPE_COMMUNITY]);
if (empty($pcontact['nurl'])) {
// The announce message wasn't created by a forum account, so we don't need to continue
return;
}
$contact = Contact::selectFirst(['id'], ['nurl' => $pcontact['nurl'], 'uid' => $item['uid']]);
if (!empty($contact['id'])) {
$condition = ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']];
Item::update(['owner-id' => $item['author-id'], 'contact-id' => $contact['id']], $condition);
$forum_item = Item::selectFirst(['id'], $condition);
if (!empty($forum_item['id'])) {
// This will trigger notifications like "X shared a new post"
UserItem::setNotification($forum_item['id']);
check_user_notification($forum_item['id']);
}
LOgger::info('Convert message into a forum message', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $contact['id']]);
}
}
/**
* Distribute the given item to users who subscribed to their tags
*
@ -2673,6 +2714,10 @@ class Item
Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id);
/// @todo This code should be activated by the end of the year 2020
// See also "createActivityFromItem"
//Item::performActivity($item_id, 'announce', $uid);
return false;
}
@ -3002,11 +3047,12 @@ class Item
*
* Toggle activities as like,dislike,attend of an item
*
* @param string $item_id
* @param int $item_id
* @param string $verb
* Activity verb. One of
* like, unlike, dislike, undislike, attendyes, unattendyes,
* attendno, unattendno, attendmaybe, unattendmaybe
* attendno, unattendno, attendmaybe, unattendmaybe,
* announce, unannouce
* @return bool
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
@ -3014,15 +3060,15 @@ class Item
* array $arr
* 'post_id' => ID of posted item
*/
public static function performActivity($item_id, $verb)
public static function performActivity(int $item_id, string $verb, int $uid)
{
if (!Session::isAuthenticated()) {
if (empty($uid)) {
return false;
}
Logger::log('like: verb ' . $verb . ' item ' . $item_id);
Logger::notice('Start create activity', ['verb' => $verb, 'item' => $item_id, 'user' => $uid]);
$item = self::selectFirst(self::ITEM_FIELDLIST, ['`id` = ? OR `uri` = ?', $item_id, $item_id]);
$item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
if (!DBA::isResult($item)) {
Logger::log('like: unknown item ' . $item_id);
return false;
@ -3030,48 +3076,35 @@ class Item
$item_uri = $item['uri'];
$uid = $item['uid'];
if (($uid == 0) && local_user()) {
$uid = local_user();
}
if (!Security::canWriteToUserWall($uid)) {
Logger::log('like: unable to write on wall ' . $uid);
if (!in_array($item['uid'], [0, $uid])) {
return false;
}
if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
$stored = self::storeForUserByUriId($item['parent-uri-id'], $uid);
if (($item['parent-uri-id'] == $item['uri-id']) && !empty($stored)) {
$item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $stored]);
if (!DBA::isResult($item)) {
Logger::info('Could not fetch just created item - should not happen', ['stored' => $stored, 'uid' => $uid, 'item-uri' => $item_uri]);
return false;
}
}
}
// Retrieves the local post owner
$owner_self_contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]);
if (!DBA::isResult($owner_self_contact)) {
Logger::log('like: unknown owner ' . $uid);
$owner = User::getOwnerDataById($uid);
if (empty($owner)) {
Logger::info('Empty owner for user', ['uid' => $uid]);
return false;
}
// Retrieve the current logged in user's public contact
$author_id = public_contact();
$author_contact = DBA::selectFirst('contact', ['url'], ['id' => $author_id]);
if (!DBA::isResult($author_contact)) {
Logger::log('like: unknown author ' . $author_id);
$author_id = Contact::getIdForURL($owner['url']);
if (empty($author_id)) {
Logger::info('Empty public contact');
return false;
}
// Contact-id is the uid-dependant author contact
if (local_user() == $uid) {
$item_contact_id = $owner_self_contact['id'];
} else {
$item_contact_id = Contact::getIdForURL($author_contact['url'], $uid);
$item_contact = DBA::selectFirst('contact', [], ['id' => $item_contact_id]);
if (!DBA::isResult($item_contact)) {
Logger::log('like: unknown item contact ' . $item_contact_id);
return false;
}
}
$activity = null;
switch ($verb) {
case 'like':
@ -3098,8 +3131,12 @@ class Item
case 'unfollow':
$activity = Activity::FOLLOW;
break;
case 'announce':
case 'unannounce':
$activity = Activity::ANNOUNCE;
break;
default:
Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id);
Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
return false;
}
@ -3170,7 +3207,7 @@ class Item
'guid' => System::createUUID(),
'uri' => self::newURI($item['uid']),
'uid' => $item['uid'],
'contact-id' => $item_contact_id,
'contact-id' => $owner['id'],
'wall' => $item['wall'],
'origin' => 1,
'network' => Protocol::DFRN,

View file

@ -51,7 +51,7 @@ class Like extends BaseModule
// @TODO: Replace with parameter from router
$itemId = (($app->argc > 1) ? Strings::escapeTags(trim($app->argv[1])) : 0);
if (!Item::performActivity($itemId, $verb)) {
if (!Item::performActivity($itemId, $verb, local_user())) {
throw new HTTPException\BadRequestException();
}

View file

@ -387,29 +387,23 @@ class Receiver
case 'as:Announce':
if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
$profile = APContact::getByURL($object_data['actor']);
// Reshared posts from persons appear as summary at the bottom
// If this isn't set, then a single reshare appears on top. This is used for groups.
$object_data['thread-completion'] = ($profile['type'] != 'Group');
$object_data['thread-completion'] = true;
$item = ActivityPub\Processor::createItem($object_data);
ActivityPub\Processor::postItem($object_data, $item);
// Add the bottom reshare information only for persons
if ($profile['type'] != 'Group') {
$announce_object_data = self::processObject($activity);
$announce_object_data['name'] = $type;
$announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
$announce_object_data['object_id'] = $object_data['object_id'];
$announce_object_data['object_type'] = $object_data['object_type'];
$announce_object_data['push'] = $push;
$announce_object_data = self::processObject($activity);
$announce_object_data['name'] = $type;
$announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
$announce_object_data['object_id'] = $object_data['object_id'];
$announce_object_data['object_type'] = $object_data['object_type'];
$announce_object_data['push'] = $push;
if (!empty($body)) {
$announce_object_data['raw'] = $body;
}
ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
if (!empty($body)) {
$announce_object_data['raw'] = $body;
}
ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
}
break;

View file

@ -356,12 +356,14 @@ class Transmitter
}
$always_bcc = false;
$isforum = false;
// Check if we should always deliver our stuff via BCC
if (!empty($item['uid'])) {
$profile = Profile::getByUID($item['uid']);
$profile = User::getOwnerDataById($item['uid']);
if (!empty($profile)) {
$always_bcc = $profile['hide-friends'];
$isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
}
}
@ -369,7 +371,7 @@ class Transmitter
$always_bcc = true;
}
if (self::isAnnounce($item) || DI::config()->get('debug', 'total_ap_delivery')) {
if ((self::isAnnounce($item) && !$isforum) || DI::config()->get('debug', 'total_ap_delivery')) {
// Will be activated in a later step
$networks = Protocol::FEDERATED;
} else {
@ -423,6 +425,10 @@ class Transmitter
continue;
}
if ($isforum && DBA::isResult($contact) && ($contact['dfrn'] == Protocol::DFRN)) {
continue;
}
if (!empty($profile = APContact::getByURL($contact['url'], false))) {
$data['to'][] = $profile['url'];
}
@ -435,6 +441,10 @@ class Transmitter
continue;
}
if ($isforum && DBA::isResult($contact) && ($contact['dfrn'] == Protocol::DFRN)) {
continue;
}
if (!empty($profile = APContact::getByURL($contact['url'], false))) {
if ($contact['hidden'] || $always_bcc) {
$data['bcc'][] = $profile['url'];
@ -454,7 +464,9 @@ class Transmitter
if ($item['gravity'] != GRAVITY_PARENT) {
// Comments to forums are directed to the forum
// But comments to forums aren't directed to the followers collection
if ($profile['type'] == 'Group') {
// This rule is only valid when the actor isn't the forum.
// The forum needs to transmit their content to their followers.
if (($profile['type'] == 'Group') && ($profile['url'] != $actor_profile['url'])) {
$data['to'][] = $profile['url'];
} else {
$data['cc'][] = $profile['url'];
@ -555,6 +567,15 @@ class Transmitter
{
$inboxes = [];
$isforum = false;
if (!empty($item['uid'])) {
$profile = User::getOwnerDataById($item['uid']);
if (!empty($profile)) {
$isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
}
}
if (DI::config()->get('debug', 'total_ap_delivery')) {
// Will be activated in a later step
$networks = Protocol::FEDERATED;
@ -579,6 +600,10 @@ class Transmitter
continue;
}
if ($isforum && ($contact['dfrn'] == Protocol::DFRN)) {
continue;
}
if (Network::isUrlBlocked($contact['url'])) {
continue;
}
@ -627,6 +652,8 @@ class Transmitter
$item_profile = APContact::getByURL($item['owner-link'], false);
}
$profile_uid = User::getIdForURL($item_profile['url']);
foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
if (empty($permissions[$element])) {
continue;
@ -639,7 +666,7 @@ class Transmitter
continue;
}
if ($item_profile && $receiver == $item_profile['followers']) {
if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
$inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
} else {
if (Contact::isLocal($receiver)) {
@ -807,6 +834,8 @@ class Transmitter
$type = 'Follow';
} elseif ($item['verb'] == Activity::TAG) {
$type = 'Add';
} elseif ($item['verb'] == Activity::ANNOUNCE) {
$type = 'Announce';
} else {
$type = '';
}
@ -851,12 +880,13 @@ class Transmitter
*/
public static function createActivityFromItem($item_id, $object_mode = false)
{
Logger::info('Fetching activity', ['item' => $item_id]);
$item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
if (!DBA::isResult($item)) {
return false;
}
/// @todo This code should be removed by the end of the year 2020
if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
$owner = User::getOwnerDataById($item['uid']);
if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
@ -868,6 +898,21 @@ class Transmitter
}
}
/*
/// @todo This code should be activated by the end of the year 2020
// See also "tagDeliver";
// In case of a forum post ensure to return the original post if author and forum are on the same machine
if (!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'])) {
$item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
}
}
}
*/
if (empty($type)) {
$condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
$conversation = DBA::selectFirst('conversation', ['source'], $condition);
@ -879,6 +924,7 @@ class Transmitter
unset($data['@context']);
unset($data['signature']);
}
Logger::info('Return stored conversation', ['item' => $item_id]);
return $data;
} elseif (in_array('as:' . $data['type'], Receiver::CONTENT_TYPES)) {
if (!empty($data['@context'])) {
@ -909,7 +955,7 @@ class Transmitter
$data['id'] = $item['uri'] . '/' . $type;
$data['type'] = $type;
if (Item::isForumPost($item) && ($type != 'Announce')) {
if (($type != 'Announce') || ($item['gravity'] != GRAVITY_PARENT)) {
$data['actor'] = $item['author-link'];
} else {
$data['actor'] = $item['owner-link'];
@ -926,7 +972,11 @@ class Transmitter
} elseif ($data['type'] == 'Add') {
$data = self::createAddTag($item, $data);
} elseif ($data['type'] == 'Announce') {
$data = self::createAnnounce($item, $data);
if ($item['verb'] == ACTIVITY::ANNOUNCE) {
$data['object'] = $item['thr-parent'];
} else {
$data = self::createAnnounce($item, $data);
}
} elseif ($data['type'] == 'Follow') {
$data['object'] = $item['parent-uri'];
} elseif ($data['type'] == 'Undo') {
@ -947,7 +997,10 @@ class Transmitter
$owner = User::getOwnerDataById($uid);
if (!$object_mode && !empty($owner)) {
Logger::info('Fetched activity', ['item' => $item_id, 'uid' => $uid]);
// We don't sign if we aren't the actor. This is important for relaying content especially for forums
if (!$object_mode && !empty($owner) && ($data['actor'] == $owner['url'])) {
return LDSignature::sign($data, $owner);
} else {
return $data;
@ -1009,6 +1062,9 @@ class Transmitter
$tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']];
} else {
$contact = Contact::getByURL($term['url'], false, ['addr']);
if (empty($contact)) {
continue;
}
if (!empty($contact['addr'])) {
$mention = '@' . $contact['addr'];
} else {
@ -1491,6 +1547,10 @@ class Transmitter
*/
public static function isAnnounce($item)
{
if ($item['verb'] == Activity::ANNOUNCE) {
return true;
}
$announce = self::getAnnounceArray($item);
if (empty($announce)) {
return false;

View file

@ -2162,6 +2162,7 @@ class DFRN
|| ($item["verb"] == Activity::ATTEND)
|| ($item["verb"] == Activity::ATTENDNO)
|| ($item["verb"] == Activity::ATTENDMAYBE)
|| ($item["verb"] == Activity::ANNOUNCE)
) {
$is_like = true;
$item["gravity"] = GRAVITY_ACTIVITY;

View file

@ -36,7 +36,7 @@ use Friendica\Model\Post;
use Friendica\Model\PushSubscriber;
use Friendica\Model\Tag;
use Friendica\Model\User;
use Friendica\Network\Probe;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
@ -301,8 +301,10 @@ class Notifier
// 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)) {
if ((intval($parent['forum_mode']) == 1) && !$top_level && ($cmd !== Delivery::UPLINK)
&& ($target_item['verb'] != Activity::ANNOUNCE)) {
Worker::add($a->queue['priority'], 'Notifier', Delivery::UPLINK, $target_id);
}
@ -393,7 +395,7 @@ class Notifier
if ($followup) {
$recipients = $recipients_followup;
}
$condition = ['id' => $recipients, 'self' => false,
$condition = ['id' => $recipients, 'self' => false, 'uid' => [0, $uid],
'blocked' => false, 'pending' => false, 'archive' => false];
if (!empty($networks)) {
$condition['network'] = $networks;
@ -439,7 +441,7 @@ class Notifier
// Add the relay to the list, avoid duplicates.
// Don't send community posts to the relay. Forum posts via the Diaspora protocol are looking ugly.
if (!$followup && !Item::isForumPost($target_item, $owner)) {
if (!$followup && !Item::isForumPost($target_item, $owner) && !self::isForumPost($target_item)) {
$relay_list = Diaspora::relayList($target_id, $relay_list);
}
}
@ -473,7 +475,7 @@ class Notifier
continue;
}
if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $cmd)) {
if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $owner, $cmd)) {
Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['id' => $target_id, 'url' => $rr['url']]);
continue;
}
@ -528,7 +530,7 @@ class Notifier
continue;
}
if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $cmd)) {
if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $owner, $cmd)) {
Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'url' => $contact['url']]);
continue;
}
@ -646,12 +648,13 @@ class Notifier
* @param array $item The post
* @param array $parent The parent
* @param array $thr_parent The thread parent
* @param array $owner Owner array
* @param string $cmd Notifier command
* @return bool
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
private static function skipDFRN($contact, $item, $parent, $thr_parent, $cmd)
private static function skipDFRN($contact, $item, $parent, $thr_parent, $owner, $cmd)
{
if (empty($parent['network'])) {
return false;
@ -682,6 +685,12 @@ class Notifier
return false;
}
// For the time being we always deliver forum post via DFRN if possible
// This can be removed possible at the end of 2020 when hopefully most system can process AP forum posts
if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
return false;
}
// We deliver reshares via AP whenever possible
if (ActivityPub\Transmitter::isAnnounce($item)) {
return true;
@ -812,4 +821,15 @@ class Notifier
return $delivery_queue_count;
}
/**
* Check if the delivered item is a forum post
*
* @param array $item
* @return boolean
*/
public static function isForumPost(array $item)
{
return !empty($item['forum_mode']);
}
}