Merge pull request #13752 from annando/capabilities

Store Pixelfed's capabilities
This commit is contained in:
Hypolite Petovan 2023-12-21 11:20:36 -05:00 committed by GitHub
commit a0f6f2e73e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 16 deletions

View File

@ -60,6 +60,10 @@ class Tag
const AUDIENCE = 14;
const ATTRIBUTED = 15;
const CAN_ANNOUNCE = 20;
const CAN_LIKE = 21;
const CAN_REPLY = 22;
const ACCOUNT = 1;
const GENERAL_COLLECTION = 2;
const FOLLOWER_COLLECTION = 3;

View File

@ -918,6 +918,16 @@ class Processor
self::storeReceivers($item['uri-id'], $activity['receiver_urls'] ?? []);
if (!empty($activity['capabilities'])) {
$restrictions = self::storeCapabilities($item['uri-id'], $activity['capabilities']);
} elseif (!is_null($activity['can-comment']) && !$activity['can-comment']) {
$restrictions = [Tag::CAN_REPLY];
} else {
$restrictions = [];
}
// @todo Store restrictions
$item['location'] = $activity['location'];
if (!empty($activity['latitude']) && !empty($activity['longitude'])) {
@ -1322,27 +1332,53 @@ class Processor
public static function storeReceivers(int $uriid, array $receivers)
{
foreach (['as:to' => Tag::TO, 'as:cc' => Tag::CC, 'as:bto' => Tag::BTO, 'as:bcc' => Tag::BCC, 'as:audience' => Tag::AUDIENCE, 'as:attributedTo' => Tag::ATTRIBUTED] as $element => $type) {
if (!empty($receivers[$element])) {
foreach ($receivers[$element] as $receiver) {
if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
$name = Receiver::PUBLIC_COLLECTION;
} elseif ($path = parse_url($receiver, PHP_URL_PATH)) {
$name = trim($path, '/');
} elseif ($host = parse_url($receiver, PHP_URL_HOST)) {
$name = $host;
} else {
Logger::warning('Unable to coerce name from receiver', ['element' => $element, 'type' => $type, 'receiver' => $receiver]);
$name = '';
}
$target = Tag::getTargetType($receiver);
Logger::debug('Got target type', ['type' => $target, 'url' => $receiver]);
Tag::store($uriid, $type, $name, $receiver, $target);
foreach ($receivers[$element] ?? [] as $receiver) {
if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
$name = Receiver::PUBLIC_COLLECTION;
} elseif ($path = parse_url($receiver, PHP_URL_PATH)) {
$name = trim($path, '/');
} elseif ($host = parse_url($receiver, PHP_URL_HOST)) {
$name = $host;
} else {
Logger::warning('Unable to coerce name from receiver', ['element' => $element, 'type' => $type, 'receiver' => $receiver]);
$name = '';
}
$target = Tag::getTargetType($receiver);
Logger::debug('Got target type', ['type' => $target, 'url' => $receiver]);
Tag::store($uriid, $type, $name, $receiver, $target);
}
}
}
private static function storeCapabilities(int $uriid, array $capabilities): array
{
$restrictions = [];
foreach (['pixelfed:canAnnounce' => Tag::CAN_ANNOUNCE, 'pixelfed:canLike' => Tag::CAN_LIKE, 'pixelfed:canReply' => Tag::CAN_REPLY] as $element => $type) {
$restricted = true;
foreach ($capabilities[$element] ?? [] as $capability) {
if ($capability == ActivityPub::PUBLIC_COLLECTION) {
$name = Receiver::PUBLIC_COLLECTION;
} elseif (empty($capability) || ($capability == '[]')) {
continue;
} elseif ($path = parse_url($capability, PHP_URL_PATH)) {
$name = trim($path, '/');
} elseif ($host = parse_url($capability, PHP_URL_HOST)) {
$name = $host;
} else {
Logger::warning('Unable to coerce name from capability', ['element' => $element, 'type' => $type, 'capability' => $capability]);
$name = '';
}
$restricted = false;
Tag::store($uriid, $type, $name, $capability);
}
if ($restricted) {
$restrictions[] = $type;
}
}
return $restrictions;
}
/**
* Creates an mail post
*

View File

@ -1943,6 +1943,10 @@ class Receiver
$object_data['receiver'] = $receivers;
$object_data['reception_type'] = $reception_types;
if (!empty($object['pixelfed:capabilities'])) {
$object_data['capabilities'] = self::getCapabilities($object);
}
$object_data['unlisted'] = in_array(-1, $object_data['receiver']);
unset($object_data['receiver'][-1]);
unset($object_data['reception_type'][-1]);
@ -1950,6 +1954,18 @@ class Receiver
return $object_data;
}
private static function getCapabilities($object) {
$capabilities = [];
foreach (['pixelfed:canAnnounce', 'pixelfed:canLike', 'pixelfed:canReply'] as $element) {
$capabilities_list = JsonLD::fetchElementArray($object['pixelfed:capabilities'], $element, '@id');
if (empty($capabilities_list)) {
continue;
}
$capabilities[$element] = $capabilities_list;
}
return $capabilities;
}
/**
* Create an object data array from a given activity
*
@ -2058,6 +2074,11 @@ class Receiver
$object_data['attachments'] = array_merge($object_data['attachments'], self::processAttachmentUrls($object['as:url'] ?? []));
}
$object_data['can-comment'] = JsonLD::fetchElement($object, 'pt:commentsEnabled', '@value');
if (is_null($object_data['can-comment'])) {
$object_data['can-comment'] = JsonLD::fetchElement($object, 'pixelfed:commentsEnabled', '@value');
}
// Support for quoted posts (Pleroma, Fedibird and Misskey)
$object_data['quote-url'] = JsonLD::fetchElement($object, 'as:quoteUrl', '@value');
if (empty($object_data['quote-url'])) {

View File

@ -171,6 +171,7 @@ class JsonLD
'mobilizon' => (object)['@id' => 'https://joinmobilizon.org/ns#', '@type' => '@id'],
'fedibird' => (object)['@id' => 'http://fedibird.com/ns#', '@type' => '@id'],
'misskey' => (object)['@id' => 'https://misskey-hub.net/ns#', '@type' => '@id'],
'pixelfed' => (object)['@id' => 'http://pixelfed.org/ns#', '@type' => '@id'],
];
$orig_json = $json;