diff options
Diffstat (limited to 'Digitigrade/Model/PushableModel.php')
| -rw-r--r-- | Digitigrade/Model/PushableModel.php | 42 |
1 files changed, 41 insertions, 1 deletions
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 |
