aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model
diff options
context:
space:
mode:
authorwinter2024-12-20 17:57:36 +0000
committerwinter2024-12-20 18:01:26 +0000
commit8ca2452dd05f614d55d2fd3ce188d9d04f44d285 (patch)
tree7c850737c216df179ce00fa874b1de508894eb71 /Digitigrade/Model
parent583b1f8a865935dd4ae1b0c9fa40a4b347ea3d4d (diff)
implement deletion/tombstones?
Diffstat (limited to 'Digitigrade/Model')
-rw-r--r--Digitigrade/Model/Actor.php15
-rw-r--r--Digitigrade/Model/FetchableModel.php10
-rw-r--r--Digitigrade/Model/Interaction.php15
-rw-r--r--Digitigrade/Model/Note.php23
-rw-r--r--Digitigrade/Model/PushableModel.php42
5 files changed, 90 insertions, 15 deletions
diff --git a/Digitigrade/Model/Actor.php b/Digitigrade/Model/Actor.php
index 07f24ec..8059409 100644
--- a/Digitigrade/Model/Actor.php
+++ b/Digitigrade/Model/Actor.php
@@ -196,7 +196,22 @@ class Actor extends PushableModel implements RpcReceiver {
return Instance::findByHostname(hostname_from_uri($this->uri));
}
+ protected function getRelevantServers(): array {
+ $actors = array_unique(array_merge($this->findFollowers(), $this->findFollowees()));
+ $instances = array_unique(array_map(function (Actor $actor) {
+ return Instance::findByHostname(hostname_from_uri($actor->uri));
+ }, $actors));
+ return $instances;
+ }
+
public function jsonSerialize(): array {
+ if ($this->deleted) {
+ return [
+ 'type' => 'tombstone',
+ 'self' => path_to_uri("/user/$this->handle"),
+ 'previousType' => 'actor'
+ ];
+ }
return [
'type' => 'actor',
'self' => path_to_uri("/user/$this->handle"),
diff --git a/Digitigrade/Model/FetchableModel.php b/Digitigrade/Model/FetchableModel.php
index 5e4d1e8..e14cc2e 100644
--- a/Digitigrade/Model/FetchableModel.php
+++ b/Digitigrade/Model/FetchableModel.php
@@ -62,6 +62,16 @@ abstract class FetchableModel extends Model implements RemoteFetchable {
if ($data === null)
return null;
+ if (($data->type ?? null) == 'tombstone') {
+ // handle deletes
+ $obj = static::findWhere('uri = ?', [$data->self]);
+ if ($obj == null)
+ return null;
+ $obj->deleted = true;
+ $obj->save();
+ return null;
+ }
+
$obj = static::createFromJson($data, $autoSave, $uri);
$obj->finaliseAfterFetch($uri);
diff --git a/Digitigrade/Model/Interaction.php b/Digitigrade/Model/Interaction.php
index bf0745e..38065c1 100644
--- a/Digitigrade/Model/Interaction.php
+++ b/Digitigrade/Model/Interaction.php
@@ -47,7 +47,22 @@ class Interaction extends PushableModel {
return self::findAllWhere('target = ?', [$target->id]);
}
+ protected function getRelevantServers(): array {
+ $instances = array_map(function (Actor $actor) {
+ return Instance::findByHostname(hostname_from_uri($actor->uri));
+ }, $this->author->findFollowers());
+ $instances[] = Instance::findByHostname(hostname_from_uri($this->target->uri));
+ return $instances;
+ }
+
public function jsonSerialize(): array {
+ if ($this->deleted) {
+ return [
+ 'type' => 'tombstone',
+ 'self' => path_to_uri("/interaction/$this->id"),
+ 'previousType' => 'interaction'
+ ];
+ }
return [
'type' => 'interaction',
'self' => path_to_uri("/interaction/$this->id"),
diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php
index 7ffb67e..9d73a89 100644
--- a/Digitigrade/Model/Note.php
+++ b/Digitigrade/Model/Note.php
@@ -110,15 +110,7 @@ class Note extends PushableModel {
return Interaction::findAllWithTarget($this);
}
- /**
- * Sends this note to all instances it's relevant to.
- *
- * For example, this may include the servers of any followers if the scope is at
- * least 'followers', anyone mentioned, and anyone in the alsoVisibleTo field.
- * If the scope is 'public' it may also send it to other servers? i'm not sure
- * @return void
- */
- public function publish() {
+ protected function getRelevantServers(): array {
$recipientActors = match ($this->privacy->scope) {
NotePrivacyScope::NONE => [],
NotePrivacyScope::MUTUALS => $this->author->findMutualFollows(),
@@ -129,14 +121,17 @@ class Note extends PushableModel {
$instances = array_map(function (Actor $a) {
return $a->findHomeInstance();
}, $recipientActors);
- $instances = array_unique($instances);
-
- foreach ($instances as $instance) {
- (new PushObject($this, $instance))->submit();
- }
+ return array_unique($instances);
}
public function jsonSerialize(): array {
+ if ($this->deleted) {
+ return [
+ 'type' => 'tombstone',
+ 'self' => path_to_uri("/post/$this->id"),
+ 'previousType' => 'note'
+ ];
+ }
return [
'type' => 'note',
'self' => path_to_uri("/post/$this->id"),
diff --git a/Digitigrade/Model/PushableModel.php b/Digitigrade/Model/PushableModel.php
index c008320..9de3507 100644
--- a/Digitigrade/Model/PushableModel.php
+++ b/Digitigrade/Model/PushableModel.php
@@ -1,11 +1,24 @@
<?php
namespace Digitigrade\Model;
+use Digitigrade\Job\PushObject;
+
abstract class PushableModel extends FetchableModel implements \JsonSerializable {
public ?int $id;
public string $uri;
+ public bool $deleted = false;
+
+ public static function importFromReceivedObject(\stdClass $data, bool $autoSave = true): ?static {
+ if ($data->type == 'tombstone') {
+ // actually just delete the object if we know it and do nothing if not
+ $obj = static::findWhere('uri = ?', [$data->self]);
+ if ($obj == null)
+ return null;
+ $obj->deleted = true;
+ $obj->save();
+ return null;
+ }
- public static function importFromReceivedObject(\stdClass $data, bool $autoSave = true): static {
$obj = static::createFromJson($data, $autoSave);
if ($autoSave) {
$obj->save();
@@ -13,4 +26,31 @@ abstract class PushableModel extends FetchableModel implements \JsonSerializable
}
return $obj;
}
+
+ /**
+ * @return Instance[] a list of instances who this object should be pushed to
+ */
+ protected abstract function getRelevantServers(): array;
+
+ /**
+ * Sends this object to all instances it's relevant to.
+ *
+ * Please don't call this on non-local objects :3
+ * @return void
+ */
+ public function publish() {
+ foreach ($this->getRelevantServers() as $instance) {
+ (new PushObject($this, $instance))->submit();
+ }
+ }
+
+ /**
+ * Marks this object as deleted and publishes a tombstone in its place.
+ * @return void
+ */
+ public function markDeleted() {
+ $this->deleted = true;
+ $this->save();
+ $this->publish();
+ }
} \ No newline at end of file