[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,8 +73,36 @@ 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) { 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 { try {
$parent_notice = $notice->getParent(); $parent_notice = $notice->getParent();
@ -101,16 +125,23 @@ class ActivityPubQueueHandler extends QueueHandler
// Parent author's profile not found! Complain louder? // Parent author's profile not found! Complain louder?
common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage()); 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;
} }
// Handling an Announce? private function handle_announce($profile, $notice, $other)
if ($notice->isRepeat()) { {
$repeated_notice = Notice::getKV('id', $notice->repeat_of); $repeated_notice = Notice::getKV('id', $notice->repeat_of);
if ($repeated_notice instanceof Notice) { if ($repeated_notice instanceof Notice) {
$other = array_merge($other, $other = array_merge(
$other,
Activitypub_profile::from_profile_collection( Activitypub_profile::from_profile_collection(
$repeated_notice->getAttentionProfiles() $repeated_notice->getAttentionProfiles()
)); )
);
try { try {
$other[] = Activitypub_profile::from_profile($repeated_notice->getProfile()); $other[] = Activitypub_profile::from_profile($repeated_notice->getProfile());
@ -127,9 +158,139 @@ class ActivityPubQueueHandler extends QueueHandler
return true; return true;
} }
// That was it /**
* 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) {
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->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, $other)
{
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($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;
}
$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; return true;
} }
} }