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; $id = $_REQUEST['id'] ?? 0;
$res = Item::performActivity($id, $verb); $res = Item::performActivity($id, $verb, api_user());
if ($res) { if ($res) {
if ($type == "xml") { 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); $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]); Logger::info('Following item failed', ['item' => $item_id]);
throw new HTTPException\BadRequestException(); throw new HTTPException\BadRequestException();
} }

View File

@ -1578,6 +1578,8 @@ class Item
return GRAVITY_COMMENT; return GRAVITY_COMMENT;
} elseif ($activity->match($item['verb'], Activity::FOLLOW)) { } elseif ($activity->match($item['verb'], Activity::FOLLOW)) {
return GRAVITY_ACTIVITY; return GRAVITY_ACTIVITY;
} elseif ($activity->match($item['verb'], Activity::ANNOUNCE)) {
return GRAVITY_ACTIVITY;
} }
Logger::info('Unknown gravity for verb', ['verb' => $item['verb']]); Logger::info('Unknown gravity for verb', ['verb' => $item['verb']]);
return GRAVITY_UNKNOWN; // Should not happen 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); Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id);
Item::performActivity($item_id, 'announce', $uid);
return false; return false;
} }
@ -3002,11 +3006,12 @@ class Item
* *
* Toggle activities as like,dislike,attend of an item * Toggle activities as like,dislike,attend of an item
* *
* @param string $item_id * @param int $item_id
* @param string $verb * @param string $verb
* Activity verb. One of * Activity verb. One of
* like, unlike, dislike, undislike, attendyes, unattendyes, * like, unlike, dislike, undislike, attendyes, unattendyes,
* attendno, unattendno, attendmaybe, unattendmaybe * attendno, unattendno, attendmaybe, unattendmaybe,
* announce, unannouce
* @return bool * @return bool
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
@ -3014,15 +3019,15 @@ class Item
* array $arr * array $arr
* 'post_id' => ID of posted item * '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; 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)) { if (!DBA::isResult($item)) {
Logger::log('like: unknown item ' . $item_id); Logger::log('like: unknown item ' . $item_id);
return false; return false;
@ -3030,48 +3035,35 @@ class Item
$item_uri = $item['uri']; $item_uri = $item['uri'];
$uid = $item['uid']; if (!in_array($item['uid'], [0, $uid])) {
if (($uid == 0) && local_user()) {
$uid = local_user();
}
if (!Security::canWriteToUserWall($uid)) {
Logger::log('like: unable to write on wall ' . $uid);
return false; return false;
} }
if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) { if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
$stored = self::storeForUserByUriId($item['parent-uri-id'], $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 // Retrieves the local post owner
$owner_self_contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]); $owner = User::getOwnerDataById($uid);
if (!DBA::isResult($owner_self_contact)) { if (empty($owner)) {
Logger::log('like: unknown owner ' . $uid); Logger::info('Empty owner for user', ['uid' => $uid]);
return false; return false;
} }
// Retrieve the current logged in user's public contact // Retrieve the current logged in user's public contact
$author_id = public_contact(); $author_id = Contact::getIdForURL($owner['url']);
if (empty($author_id)) {
$author_contact = DBA::selectFirst('contact', ['url'], ['id' => $author_id]); Logger::info('Empty public contact');
if (!DBA::isResult($author_contact)) {
Logger::log('like: unknown author ' . $author_id);
return false; 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; $activity = null;
switch ($verb) { switch ($verb) {
case 'like': case 'like':
@ -3098,8 +3090,12 @@ class Item
case 'unfollow': case 'unfollow':
$activity = Activity::FOLLOW; $activity = Activity::FOLLOW;
break; break;
case 'announce':
case 'unannounce':
$activity = Activity::ANNOUNCE;
break;
default: default:
Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id); Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
return false; return false;
} }
@ -3170,7 +3166,7 @@ class Item
'guid' => System::createUUID(), 'guid' => System::createUUID(),
'uri' => self::newURI($item['uid']), 'uri' => self::newURI($item['uid']),
'uid' => $item['uid'], 'uid' => $item['uid'],
'contact-id' => $item_contact_id, 'contact-id' => $owner['id'],
'wall' => $item['wall'], 'wall' => $item['wall'],
'origin' => 1, 'origin' => 1,
'network' => Protocol::DFRN, 'network' => Protocol::DFRN,

View File

@ -51,7 +51,7 @@ class Like extends BaseModule
// @TODO: Replace with parameter from router // @TODO: Replace with parameter from router
$itemId = (($app->argc > 1) ? Strings::escapeTags(trim($app->argv[1])) : 0); $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(); throw new HTTPException\BadRequestException();
} }

View File

@ -454,7 +454,9 @@ class Transmitter
if ($item['gravity'] != GRAVITY_PARENT) { if ($item['gravity'] != GRAVITY_PARENT) {
// Comments to forums are directed to the forum // Comments to forums are directed to the forum
// But comments to forums aren't directed to the followers collection // 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']; $data['to'][] = $profile['url'];
} else { } else {
$data['cc'][] = $profile['url']; $data['cc'][] = $profile['url'];
@ -627,6 +629,8 @@ class Transmitter
$item_profile = APContact::getByURL($item['owner-link'], false); $item_profile = APContact::getByURL($item['owner-link'], false);
} }
$profile_uid = User::getIdForURL($item_profile['url']);
foreach (['to', 'cc', 'bto', 'bcc'] as $element) { foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
if (empty($permissions[$element])) { if (empty($permissions[$element])) {
continue; continue;
@ -639,7 +643,7 @@ class Transmitter
continue; 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)); $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
} else { } else {
if (Contact::isLocal($receiver)) { if (Contact::isLocal($receiver)) {
@ -807,6 +811,8 @@ class Transmitter
$type = 'Follow'; $type = 'Follow';
} elseif ($item['verb'] == Activity::TAG) { } elseif ($item['verb'] == Activity::TAG) {
$type = 'Add'; $type = 'Add';
} elseif ($item['verb'] == Activity::ANNOUNCE) {
$type = 'Announce';
} else { } else {
$type = ''; $type = '';
} }
@ -851,20 +857,20 @@ class Transmitter
*/ */
public static function createActivityFromItem($item_id, $object_mode = false) 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]); $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
if (!DBA::isResult($item)) { if (!DBA::isResult($item)) {
return false; return false;
} }
if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) { // In case of a forum post ensure to return the original post if author and forum are on the same machine
$owner = User::getOwnerDataById($item['uid']); if (!empty($item['forum_mode'])) {
if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) { $author = Contact::getById($item['author-id'], ['nurl']);
$type = 'Announce'; if (!empty($author['nurl'])) {
$self = Contact::selectFirst(['uid'], ['nurl' => $author['nurl'], 'self' => true]);
// Disguise forum posts as reshares. Will later be converted to a real announce if (!empty($self['uid'])) {
$item['body'] = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
$item['plink'], $item['created'], $item['guid']) . $item['body'] . '[/share]'; }
} }
} }
@ -879,6 +885,7 @@ class Transmitter
unset($data['@context']); unset($data['@context']);
unset($data['signature']); unset($data['signature']);
} }
Logger::info('Return stored conversation', ['item' => $item_id]);
return $data; return $data;
} elseif (in_array('as:' . $data['type'], Receiver::CONTENT_TYPES)) { } elseif (in_array('as:' . $data['type'], Receiver::CONTENT_TYPES)) {
if (!empty($data['@context'])) { if (!empty($data['@context'])) {
@ -909,7 +916,7 @@ class Transmitter
$data['id'] = $item['uri'] . '/' . $type; $data['id'] = $item['uri'] . '/' . $type;
$data['type'] = $type; $data['type'] = $type;
if (Item::isForumPost($item) && ($type != 'Announce')) { if (($type != 'Announce') || ($item['gravity'] != GRAVITY_PARENT)) {
$data['actor'] = $item['author-link']; $data['actor'] = $item['author-link'];
} else { } else {
$data['actor'] = $item['owner-link']; $data['actor'] = $item['owner-link'];
@ -926,7 +933,11 @@ class Transmitter
} elseif ($data['type'] == 'Add') { } elseif ($data['type'] == 'Add') {
$data = self::createAddTag($item, $data); $data = self::createAddTag($item, $data);
} elseif ($data['type'] == 'Announce') { } 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') { } elseif ($data['type'] == 'Follow') {
$data['object'] = $item['parent-uri']; $data['object'] = $item['parent-uri'];
} elseif ($data['type'] == 'Undo') { } elseif ($data['type'] == 'Undo') {
@ -947,7 +958,10 @@ class Transmitter
$owner = User::getOwnerDataById($uid); $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); return LDSignature::sign($data, $owner);
} else { } else {
return $data; return $data;
@ -1009,6 +1023,9 @@ class Transmitter
$tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']]; $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']];
} else { } else {
$contact = Contact::getByURL($term['url'], false, ['addr']); $contact = Contact::getByURL($term['url'], false, ['addr']);
if (empty($contact)) {
continue;
}
if (!empty($contact['addr'])) { if (!empty($contact['addr'])) {
$mention = '@' . $contact['addr']; $mention = '@' . $contact['addr'];
} else { } else {
@ -1491,6 +1508,10 @@ class Transmitter
*/ */
public static function isAnnounce($item) public static function isAnnounce($item)
{ {
if ($item['verb'] == Activity::ANNOUNCE) {
return true;
}
$announce = self::getAnnounceArray($item); $announce = self::getAnnounceArray($item);
if (empty($announce)) { if (empty($announce)) {
return false; return false;

View File

@ -36,7 +36,7 @@ use Friendica\Model\Post;
use Friendica\Model\PushSubscriber; use Friendica\Model\PushSubscriber;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\Probe; use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora; use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus; 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 // 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 // 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); Worker::add($a->queue['priority'], 'Notifier', Delivery::UPLINK, $target_id);
} }
@ -393,7 +395,7 @@ class Notifier
if ($followup) { if ($followup) {
$recipients = $recipients_followup; $recipients = $recipients_followup;
} }
$condition = ['id' => $recipients, 'self' => false, $condition = ['id' => $recipients, 'self' => false, 'uid' => [0, $uid],
'blocked' => false, 'pending' => false, 'archive' => false]; 'blocked' => false, 'pending' => false, 'archive' => false];
if (!empty($networks)) { if (!empty($networks)) {
$condition['network'] = $networks; $condition['network'] = $networks;
@ -439,7 +441,7 @@ class Notifier
// Add the relay to the list, avoid duplicates. // 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. // 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); $relay_list = Diaspora::relayList($target_id, $relay_list);
} }
} }
@ -812,4 +814,15 @@ class Notifier
return $delivery_queue_count; 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']);
}
} }