AP Improvements for forums

This commit is contained in:
Michael 2020-08-09 18:42:25 +00:00
parent e733055485
commit bf7128b4b2
6 changed files with 86 additions and 56 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

@ -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
@ -2673,6 +2675,8 @@ class Item
Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id);
Item::performActivity($item_id, 'announce', $uid);
return false;
}
@ -3002,11 +3006,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 +3019,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 +3035,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 +3090,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 +3166,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

@ -454,7 +454,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'];
@ -627,6 +629,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 +643,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 +811,8 @@ class Transmitter
$type = 'Follow';
} elseif ($item['verb'] == Activity::TAG) {
$type = 'Add';
} elseif ($item['verb'] == Activity::ANNOUNCE) {
$type = 'Announce';
} else {
$type = '';
}
@ -851,20 +857,20 @@ 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;
}
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'])) {
$type = 'Announce';
// Disguise forum posts as reshares. Will later be converted to a real announce
$item['body'] = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'],
$item['plink'], $item['created'], $item['guid']) . $item['body'] . '[/share]';
// 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']]);
}
}
}
@ -879,6 +885,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 +916,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 +933,11 @@ class Transmitter
} elseif ($data['type'] == 'Add') {
$data = self::createAddTag($item, $data);
} elseif ($data['type'] == 'Announce') {
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 +958,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 +1023,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 +1508,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

@ -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);
}
}
@ -812,4 +814,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']);
}
}