[ActivityPub] Ensuring notice distribution

ActivityPubPlugin:
- Fix of accepted activity verbs to include SHARES
- Add attention profiles to delivery when announcing

Activitypub_notice:
- New local function to retrieve original URL
- Removal of unnecessary 'Atom*' attributes
- Small fix to the ensuring of actor profile

Activitypub_profile:
- New local function to fetch AP profiles from a collection

Activitypub_postman:
- Fix url passed in the announce activity
This commit is contained in:
brunoccast 2019-07-17 16:25:24 +01:00 committed by Diogo Peralta Cordeiro
parent 25390bf96f
commit 233bdf57ac
4 changed files with 66 additions and 32 deletions

View File

@ -805,37 +805,37 @@ class ActivityPubPlugin extends Plugin
return true;
}
// Ignore for activity/non-post-verb notices
// Ignore for activity/non-(post/share)-verb notices
if (method_exists('ActivityUtils', 'compareVerbs')) {
$is_post_verb = ActivityUtils::compareVerbs(
$notice->verb,
[ActivityVerb::POST]
);
$is_valid_verb = ActivityUtils::compareVerbs($notice->verb,
[ActivityVerb::POST,
ActivityVerb::SHARE]);
} else {
$is_post_verb = ($notice->verb == ActivityVerb::POST ? true : false);
$is_valid_verb = ($notice->verb == ActivityVerb::POST ||
$notice->verb == ActivityVerb::SHARE);
}
if ($notice->source == 'activity' || !$is_post_verb) {
if ($notice->source == 'activity' || !$is_valid_verb) {
common_log(LOG_DEBUG, "Ignoring distribution of notice with id {$notice->id} due to invalid Verb");
return true;
}
$other = [];
foreach ($notice->getAttentionProfiles() as $mention) {
try {
$other[] = Activitypub_profile::from_profile($mention);
} catch (Exception $e) {
// Local user can be ignored
}
}
$other = Activitypub_profile::from_profile_collection(
$notice->getAttentionProfiles()
);
// Is a reply?
if ($notice->reply_to) {
try {
$other[] = Activitypub_profile::from_profile($notice->getParent()->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
try {
foreach ($notice->getParent()->getAttentionProfiles() as $mention) {
$parent_notice = $notice->getParent();
try {
$other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
foreach ($parent_notice->getAttentionProfiles() as $mention) {
try {
$other[] = Activitypub_profile::from_profile($mention);
} catch (Exception $e) {
@ -854,6 +854,11 @@ class ActivityPubPlugin extends Plugin
if ($notice->isRepeat()) {
$repeated_notice = Notice::getKV('id', $notice->repeat_of);
if ($repeated_notice instanceof Notice) {
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$repeated_notice->getAttentionProfiles()
));
try {
$other[] = Activitypub_profile::from_profile($repeated_notice->getProfile());
} catch (Exception $e) {
@ -863,8 +868,10 @@ class ActivityPubPlugin extends Plugin
// That was it
$postman = new Activitypub_postman($profile, $other);
$postman->announce($repeated_notice);
return true;
}
// either made the announce or found nothing to repeat
return true;
}
// That was it

View File

@ -72,14 +72,13 @@ class Activitypub_notice extends Managed_DataObject
$item = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => common_local_url('apNotice', ['id' => $notice->getID()]),
'id' => self::getUrl($notice),
'type' => 'Note',
'published' => str_replace(' ', 'T', $notice->getCreated()).'Z',
'url' => $notice->getUrl(),
'url' => self::getUrl($notice),
'attributedTo' => ActivityPubPlugin::actor_uri($profile),
'to' => ['https://www.w3.org/ns/activitystreams#Public'],
'cc' => $cc,
'atomUri' => $notice->getUrl(),
'conversation' => $notice->getConversationUrl(),
'content' => $notice->getRendered(),
'isLocal' => $notice->isLocal(),
@ -89,8 +88,7 @@ class Activitypub_notice extends Managed_DataObject
// Is this a reply?
if (!empty($notice->reply_to)) {
$item['inReplyTo'] = common_local_url('apNotice', ['id' => $notice->getID()]);
$item['inReplyToAtomUri'] = Notice::getById($notice->reply_to)->getUrl();
$item['inReplyTo'] = self::getUrl(Notice::getById($notice->reply_to));
}
// Do we have a location for this notice?
@ -135,7 +133,7 @@ class Activitypub_notice extends Managed_DataObject
// Ensure Actor Profile
if (is_null($actor_profile)) {
$actor_profile = ActivityPub_explorer::get_profile_from_url($object['actor']);
$actor_profile = ActivityPub_explorer::get_profile_from_url($object['attributedTo']);
}
$act = new Activity();
@ -192,9 +190,9 @@ class Activitypub_notice extends Managed_DataObject
}
/* Reject notice if it is too long (without the HTML)
if (Notice::contentTooLong($content)) {
throw new Exception('That\'s too long. Maximum notice size is %d character.');
}*/
if (Notice::contentTooLong($content)) {
throw new Exception('That\'s too long. Maximum notice size is %d character.');
}*/
$actobj = new ActivityObject();
$actobj->type = ActivityObject::NOTE;
@ -249,4 +247,19 @@ class Activitypub_notice extends Managed_DataObject
}
return true;
}
/**
* Get the original representation URL of a given notice.
*
* @param Notice $notice notice from which to retrieve the URL
* @return string URL
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
*/
public static function getUrl(Notice $notice): string {
if ($notice->isLocal()) {
return common_local_url('apNotice', ['id' => $notice->getID()]);
} else {
return $notice->getUrl();
}
}
}

View File

@ -229,6 +229,20 @@ class Activitypub_profile extends Managed_DataObject
return $aprofile;
}
public static function from_profile_collection(array $profiles): array {
$ap_profiles = [];
foreach ($profiles as $profile) {
try {
$ap_profiles[] = self::from_profile($profile);
} catch (Exception $e) {
// Don't mind local profiles
}
}
return $ap_profiles;
}
/**
* Given an existent local profile creates an ActivityPub profile.
* One must be careful not to give a user profile to this function

View File

@ -283,7 +283,7 @@ class Activitypub_postman
{
$data = Activitypub_announce::announce_to_array(
ActivityPubPlugin::actor_uri($this->actor),
$notice->getUri()
Activitypub_notice::getUrl($notice)
);
$data = json_encode($data, JSON_UNESCAPED_SLASHES);