[ActivityPub] Ensuring Notice Favor/Disfavor

ActivityPubPlugin:
- Minor re-write of favor/disfavor event handlers

Activitypub_postman:
like/undo-like:
- fix proper getUrl() call
misc:
- make all activities accumulate errors (may be needed later) and log some information about it
This commit is contained in:
brunoccast 2019-07-18 19:18:23 +01:00 committed by Diogo Cordeiro
parent 1b356d3bf2
commit b19ee7b894
2 changed files with 89 additions and 52 deletions

View File

@ -624,33 +624,32 @@ class ActivityPubPlugin extends Plugin
}
$other = [];
try {
$other[] = Activitypub_profile::from_profile($notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
foreach ($notice->getAttentionProfiles() as $to_profile) {
try {
$other[] = Activitypub_profile::from_profile($to_profile);
} catch (Exception $e) {
// Local user can be ignored
}
}
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$notice->getAttentionProfiles()
));
if ($notice->reply_to) {
try {
$other[] = Activitypub_profile::from_profile($notice->getParent()->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
try {
$mentions = $notice->getParent()->getAttentionProfiles();
foreach ($mentions as $to_profile) {
try {
$other[] = Activitypub_profile::from_profile($to_profile);
} catch (Exception $e) {
// Local user can be ignored
}
$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) {
@ -660,7 +659,6 @@ class ActivityPubPlugin extends Plugin
}
$postman = new Activitypub_postman($profile, $other);
$postman->like($notice);
return true;
@ -685,33 +683,32 @@ class ActivityPubPlugin extends Plugin
}
$other = [];
try {
$other[] = Activitypub_profile::from_profile($notice->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
foreach ($notice->getAttentionProfiles() as $to_profile) {
try {
$other[] = Activitypub_profile::from_profile($to_profile);
} catch (Exception $e) {
// Local user can be ignored
}
}
$other = array_merge($other,
Activitypub_profile::from_profile_collection(
$notice->getAttentionProfiles()
));
if ($notice->reply_to) {
try {
$other[] = Activitypub_profile::from_profile($notice->getParent()->getProfile());
} catch (Exception $e) {
// Local user can be ignored
}
try {
$mentions = $notice->getParent()->getAttentionProfiles();
foreach ($mentions as $to_profile) {
try {
$other[] = Activitypub_profile::from_profile($to_profile);
} catch (Exception $e) {
// Local user can be ignored
}
$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) {
@ -721,7 +718,6 @@ class ActivityPubPlugin extends Plugin
}
$postman = new Activitypub_postman($profile, $other);
$postman->undo_like($notice);
return true;

View File

@ -215,12 +215,23 @@ class Activitypub_postman
{
$data = Activitypub_like::like_to_array(
ActivityPubPlugin::actor_uri($this->actor),
$notice->getUrl()
Activitypub_notice::getUrl($notice)
);
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
foreach ($this->to_inbox() as $inbox) {
$this->send($data, $inbox);
$res = $this->send($data, $inbox);
// accummulate errors for later use, if needed
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
$res_body = json_decode($res->getBody(), true);
$errors[] = isset($res_body[0]['error']) ?
$res_body[0]['error'] : "An unknown error occurred.";
}
}
if (!empty($errors)) {
common_log(LOG_ERR, sizeof($errors) . " instance/s failed to handle the like activity!");
}
}
@ -238,13 +249,24 @@ class Activitypub_postman
$data = Activitypub_undo::undo_to_array(
Activitypub_like::like_to_array(
ActivityPubPlugin::actor_uri($this->actor),
$notice->getUrl()
Activitypub_notice::getUrl($notice)
)
);
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
foreach ($this->to_inbox() as $inbox) {
$this->send($data, $inbox);
$res = $this->send($data, $inbox);
// accummulate errors for later use, if needed
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
$res_body = json_decode($res->getBody(), true);
$errors[] = isset($res_body[0]['error']) ?
$res_body[0]['error'] : "An unknown error occurred.";
}
}
if (!empty($errors)) {
common_log(LOG_ERR, sizeof($errors) . " instance/s failed to handle the undo-like activity!");
}
}
@ -267,7 +289,18 @@ class Activitypub_postman
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
foreach ($this->to_inbox() as $inbox) {
$this->send($data, $inbox);
$res = $this->send($data, $inbox);
// accummulate errors for later use, if needed
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
$res_body = json_decode($res->getBody(), true);
$errors[] = isset($res_body[0]['error']) ?
$res_body[0]['error'] : "An unknown error occurred.";
}
}
if (!empty($errors)) {
common_log(LOG_ERR, sizeof($errors) . " instance/s failed to handle the create-note activity!");
}
}
@ -288,7 +321,18 @@ class Activitypub_postman
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
foreach ($this->to_inbox() as $inbox) {
$this->send($data, $inbox);
$res = $this->send($data, $inbox);
// accummulate errors for later use, if needed
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
$res_body = json_decode($res->getBody(), true);
$errors[] = isset($res_body[0]['error']) ?
$res_body[0]['error'] : "An unknown error occurred.";
}
}
if (!empty($errors)) {
common_log(LOG_ERR, sizeof($errors) . " instance/s failed to handle the announce activity!");
}
}
@ -312,13 +356,10 @@ class Activitypub_postman
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
foreach ($this->to_inbox() as $inbox) {
$res = $this->send($data, $inbox);
if (!$res->getStatus() == 200) {
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
$res_body = json_decode($res->getBody(), true);
if (isset($res_body[0]['error'])) {
$errors[] = ($res_body[0]['error']);
continue;
}
$errors[] = ("An unknown error occurred.");
$errors[] = isset($res_body[0]['error']) ?
$res_body[0]['error'] : "An unknown error occurred.";
}
}
if (!empty($errors)) {