Force removal from relay list

This commit is contained in:
Michael 2020-09-29 05:06:37 +00:00
parent 7252e0b37c
commit eff4d18209
4 changed files with 34 additions and 14 deletions

View file

@ -49,7 +49,7 @@ console relay - Manage ActivityPub relay configuration
Synopsis Synopsis
bin/console relay list [-h|--help|-?] [-v] bin/console relay list [-h|--help|-?] [-v]
bin/console relay add <actor> [-h|--help|-?] [-v] bin/console relay add <actor> [-h|--help|-?] [-v]
bin/console relay remove <actor> [-h|--help|-?] [-v] bin/console relay remove <actor> [-f|--force] [-h|--help|-?] [-v]
Description Description
bin/console relay list bin/console relay list
@ -62,6 +62,7 @@ Description
Remove a relay actor in the format https://relayserver.tld/actor Remove a relay actor in the format https://relayserver.tld/actor
Options Options
-f|--force Change the relay status in the system even if the unsubscribe message failed
-h|--help|-? Show help information -h|--help|-? Show help information
-v Show more debug information. -v Show more debug information.
HELP; HELP;
@ -119,10 +120,14 @@ HELP;
$this->out($actor . " couldn't be added"); $this->out($actor . " couldn't be added");
} }
} elseif ($mode == 'remove') { } elseif ($mode == 'remove') {
if (Transmitter::sendRelayUndoFollow($actor)) { $force = $this->getOption(['f', 'force'], false);
if (Transmitter::sendRelayUndoFollow($actor, $force)) {
$this->out('Successfully removed ' . $actor); $this->out('Successfully removed ' . $actor);
} else { } elseif (!$force) {
$this->out($actor . " couldn't be removed"); $this->out($actor . " couldn't be removed");
} else {
$this->out($actor . " is forcefully removed");
} }
} else { } else {
throw new CommandArgsException($mode . ' is no valid command'); throw new CommandArgsException($mode . ' is no valid command');

View file

@ -126,6 +126,7 @@ class Contact
* Relationship types * Relationship types
* @{ * @{
*/ */
const NOTHING = 0;
const FOLLOWER = 1; const FOLLOWER = 1;
const SHARING = 2; const SHARING = 2;
const FRIEND = 3; const FRIEND = 3;

View file

@ -173,6 +173,17 @@ class Receiver
return; return;
} }
$contact = Contact::getByURL($actor);
if (empty($contact)) {
Logger::info('Relay contact not found', ['actor' => $actor]);
return;
}
if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
Logger::notice('Relay is no sharer', ['actor' => $actor]);
return;
}
Logger::info('Got relayed message id', ['id' => $object_id]); Logger::info('Got relayed message id', ['id' => $object_id]);
$item_id = Item::searchByLink($object_id); $item_id = Item::searchByLink($object_id);

View file

@ -88,15 +88,16 @@ class Transmitter
*/ */
public static function sendRelayFollow(string $url) public static function sendRelayFollow(string $url)
{ {
$contact_id = Contact::getIdForURL($url); $contact = Contact::getByURL($url);
if (!$contact_id) { if (empty($contact)) {
return false; return false;
} }
$activity_id = ActivityPub\Transmitter::activityIDFromContact($contact_id); $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
$success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id); $success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id);
if ($success) { if ($success) {
DBA::update('contact', ['rel' => Contact::FRIEND], ['id' => $contact_id]); $rel = $contact['rel'] == Contact::SHARING ? Contact::FRIEND : Contact::FOLLOWER;
DBA::update('contact', ['rel' => $rel], ['id' => $contact['id']]);
} }
return $success; return $success;
@ -105,19 +106,21 @@ class Transmitter
/** /**
* Unsubscribe from a relay * Unsubscribe from a relay
* *
* @param string $url Subscribe actor url * @param string $url Subscribe actor url
* @param bool $force Set the relay status as non follower even if unsubscribe hadn't worked
* @return bool success * @return bool success
*/ */
public static function sendRelayUndoFollow(string $url) public static function sendRelayUndoFollow(string $url, bool $force = false)
{ {
$contact_id = Contact::getIdForURL($url); $contact = Contact::getByURL($url);
if (!$contact_id) { if (empty($contact)) {
return false; return false;
} }
$success = self::sendContactUndo($url, $contact_id, 0); $success = self::sendContactUndo($url, $contact['id'], 0);
if ($success) { if ($success || $force) {
DBA::update('contact', ['rel' => Contact::SHARING], ['id' => $contact_id]); $rel = $contact['rel'] == Contact::FRIEND ? Contact::SHARING : Contact::NOTHING;
DBA::update('contact', ['rel' => $rel], ['id' => $contact['id']]);
} }
return $success; return $success;