[ActivityPub][QUEUES] Add Like, Undo and Delete

This commit is contained in:
Diogo Cordeiro 2020-07-05 03:22:10 +01:00 committed by Diogo Peralta Cordeiro
parent 2b4aaca14a
commit 4bbfb2dd56
3 changed files with 197 additions and 212 deletions

View File

@ -821,182 +821,6 @@ class ActivityPubPlugin extends Plugin
return true; return true;
} }
/**
* Notify remote users when their notices get favourited.
*
* @param Profile $profile of local user doing the faving
* @param Notice $notice Notice being favored
* @return bool return value
* @throws HTTP_Request2_Exception
* @throws InvalidUrlException
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public function onEndFavorNotice(Profile $profile, Notice $notice)
{
// Only distribute local users' favor actions, remote users
// will have already distributed theirs.
if (!$profile->isLocal()) {
return true;
}
$other = [];
try {
$other[] = Activitypub_profile::from_profile($notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$notice->getAttentionProfiles()
));
if ($notice->reply_to) {
try {
$parent_notice = $notice->getParent();
try {
$other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$parent_notice->getAttentionProfiles()
));
} catch (NoParentNoticeException $e) {
// This is not a reply to something (has no parent)
} catch (NoResultException $e) {
// Parent author's profile not found! Complain louder?
common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage());
}
}
$postman = new Activitypub_postman($profile, $other);
$postman->like($notice);
return true;
}
/**
* Notify remote users when their notices get de-favourited.
*
* @param Profile $profile of local user doing the de-faving
* @param Notice $notice Notice being favored
* @return bool return value
* @throws HTTP_Request2_Exception
* @throws InvalidUrlException
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public function onEndDisfavorNotice(Profile $profile, Notice $notice)
{
// Only distribute local users' favor actions, remote users
// will have already distributed theirs.
if (!$profile->isLocal()) {
return true;
}
$other = [];
try {
$other[] = Activitypub_profile::from_profile($notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$notice->getAttentionProfiles()
));
if ($notice->reply_to) {
try {
$parent_notice = $notice->getParent();
try {
$other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$parent_notice->getAttentionProfiles()
));
} catch (NoParentNoticeException $e) {
// This is not a reply to something (has no parent)
} catch (NoResultException $e) {
// Parent author's profile not found! Complain louder?
common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage());
}
}
$postman = new Activitypub_postman($profile, $other);
$postman->undo_like($notice);
return true;
}
/**
* Notify remote users when their notices get deleted
*
* @param $user
* @param $notice
* @return boolean hook flag
* @throws HTTP_Request2_Exception
* @throws InvalidUrlException
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public function onStartDeleteOwnNotice($user, $notice)
{
$profile = $user->getProfile();
// Only distribute local users' delete actions, remote users
// will have already distributed theirs.
if (!$profile->isLocal()) {
return true;
}
// Handle delete locally either because:
// 1. There's no undo-share logic yet
// 2. The deleting user has previleges to do so (locally)
if ($notice->isRepeat() || ($notice->getProfile()->getID() != $profile->getID())) {
return true;
}
$other = Activitypub_profile::from_profile_collection(
$notice->getAttentionProfiles()
);
if ($notice->reply_to) {
try {
$parent_notice = $notice->getParent();
try {
$other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$parent_notice->getAttentionProfiles()
));
} catch (NoParentNoticeException $e) {
// This is not a reply to something (has no parent)
} catch (NoResultException $e) {
// Parent author's profile not found! Complain louder?
common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage());
}
}
$postman = new Activitypub_postman($profile, $other);
$postman->delete_note($notice);
return true;
}
/** /**
* Notify remote followers when a user gets deleted * Notify remote followers when a user gets deleted
* *

View File

@ -64,12 +64,8 @@ class ActivityPubQueueHandler extends QueueHandler
return true; return true;
} }
// Ignore activity/non-post/share-verb notices if ($notice->source == 'activity') {
$is_valid_verb = ($notice->verb == ActivityVerb::POST || common_log(LOG_ERR, "Ignoring distribution of notice:{$notice->id}: activity source");
$notice->verb == ActivityVerb::SHARE);
if ($notice->source == 'activity' || !$is_valid_verb) {
common_log(LOG_ERR, "Ignoring distribution of notice:{$notice->id}: activity source or invalid Verb");
return true; return true;
} }
@ -77,7 +73,103 @@ class ActivityPubQueueHandler extends QueueHandler
$notice->getAttentionProfiles() $notice->getAttentionProfiles()
); );
// Handling a Like?
if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::FAVORITE])) {
return $this->onEndFavorNotice($profile, $notice, $other);
}
// Handling a Undo Like?
if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::UNFAVORITE])) {
return $this->onEndDisfavorNotice($profile, $notice, $other);
}
// Handling a Delete Note?
if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::DELETE])) {
return $this->onStartDeleteOwnNotice($profile, $notice, $other);
}
// Handling a reply? // Handling a reply?
if ($notice->reply_to) {
return $this->handle_reply($profile, $notice, $other);
}
// Handling an Announce?
if ($notice->isRepeat()) {
return $this->handle_announce($profile, $notice, $other);
}
return true;
}
private function handle_reply($profile, $notice, $other)
{
try {
$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) {
// Local user can be ignored
}
}
} catch (NoParentNoticeException $e) {
// This is not a reply to something (has no parent)
} catch (NoResultException $e) {
// Parent author's profile not found! Complain louder?
common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage());
}
// That was it
$postman = new Activitypub_postman($profile, $other);
$postman->create_note($notice);
return true;
}
private function handle_announce($profile, $notice, $other)
{
$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) {
// Local user can be ignored
}
// That was it
$postman = new Activitypub_postman($profile, $other);
$postman->announce($repeated_notice);
}
// either made the announce or found nothing to repeat
return true;
}
/**
* Notify remote users when their notices get favourited.
*
* @param Profile $profile of local user doing the faving
* @param Notice $notice Notice being favored
* @return bool return value
* @throws HTTP_Request2_Exception
* @throws InvalidUrlException
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public function onEndFavorNotice(Profile $profile, Notice $notice, $other)
{
if ($notice->reply_to) { if ($notice->reply_to) {
try { try {
$parent_notice = $notice->getParent(); $parent_notice = $notice->getParent();
@ -88,13 +180,12 @@ class ActivityPubQueueHandler extends QueueHandler
// Local user can be ignored // Local user can be ignored
} }
foreach ($parent_notice->getAttentionProfiles() as $mention) { $other = array_merge(
try { $other,
$other[] = Activitypub_profile::from_profile($mention); Activitypub_profile::from_profile_collection(
} catch (Exception $e) { $parent_notice->getAttentionProfiles()
// Local user can be ignored )
} );
}
} catch (NoParentNoticeException $e) { } catch (NoParentNoticeException $e) {
// This is not a reply to something (has no parent) // This is not a reply to something (has no parent)
} catch (NoResultException $e) { } catch (NoResultException $e) {
@ -103,33 +194,103 @@ class ActivityPubQueueHandler extends QueueHandler
} }
} }
// Handling an Announce? $postman = new Activitypub_postman($profile, $other);
if ($notice->isRepeat()) { $postman->like($notice);
$repeated_notice = Notice::getKV('id', $notice->repeat_of);
if ($repeated_notice instanceof Notice) { return true;
$other = array_merge($other, }
Activitypub_profile::from_profile_collection(
$repeated_notice->getAttentionProfiles() /**
)); * Notify remote users when their notices get de-favourited.
*
* @param Profile $profile of local user doing the de-faving
* @param Notice $notice Notice being favored
* @return bool return value
* @throws HTTP_Request2_Exception
* @throws InvalidUrlException
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public function onEndDisfavorNotice(Profile $profile, Notice $notice, $other)
{
if ($notice->reply_to) {
try {
$parent_notice = $notice->getParent();
try { try {
$other[] = Activitypub_profile::from_profile($repeated_notice->getProfile()); $other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
} catch (Exception $e) { } catch (Exception $e) {
// Local user can be ignored // Local user can be ignored
} }
// That was it $other = array_merge(
$postman = new Activitypub_postman($profile, $other); $other,
$postman->announce($repeated_notice); Activitypub_profile::from_profile_collection(
$parent_notice->getAttentionProfiles()
)
);
} catch (NoParentNoticeException $e) {
// This is not a reply to something (has no parent)
} catch (NoResultException $e) {
// Parent author's profile not found! Complain louder?
common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage());
} }
}
// either made the announce or found nothing to repeat $postman = new Activitypub_postman($profile, $other);
$postman->undo_like($notice);
return true;
}
/**
* Notify remote users when their notices get deleted
*
* @param $user
* @param $notice
* @return boolean hook flag
* @throws HTTP_Request2_Exception
* @throws InvalidUrlException
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public function onStartDeleteOwnNotice($profile, $notice, $other)
{
// Handle delete locally either because:
// 1. There's no undo-share logic yet
// 2. The deleting user has privileges to do so (locally)
if ($notice->isRepeat() || ($notice->getProfile()->getID() != $profile->getID())) {
return true; return true;
} }
// That was it $other = Activitypub_profile::from_profile_collection(
$notice->getAttentionProfiles()
);
if ($notice->reply_to) {
try {
$parent_notice = $notice->getParent();
try {
$other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
$other = array_merge(
$other,
Activitypub_profile::from_profile_collection(
$parent_notice->getAttentionProfiles()
)
);
} catch (NoParentNoticeException $e) {
// This is not a reply to something (has no parent)
} catch (NoResultException $e) {
// Parent author's profile not found! Complain louder?
common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage());
}
}
$postman = new Activitypub_postman($profile, $other); $postman = new Activitypub_postman($profile, $other);
$postman->create_note($notice); $postman->delete_note($notice);
return true; return true;
} }
} }