From 1e070a7a0097c74f8ac30678d39267f19c76f9f6 Mon Sep 17 00:00:00 2001 From: winter Date: Thu, 19 Dec 2024 18:01:29 +0000 Subject: process pushes in job + fix token expiry job --- Digitigrade/Job.php | 21 ++++++++++++--- Digitigrade/Job/ExpireInboundAuthToken.php | 1 + Digitigrade/Job/ProcessIncomingPush.php | 41 ++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 Digitigrade/Job/ProcessIncomingPush.php (limited to 'Digitigrade') diff --git a/Digitigrade/Job.php b/Digitigrade/Job.php index 146e4b2..d460b7e 100644 --- a/Digitigrade/Job.php +++ b/Digitigrade/Job.php @@ -2,15 +2,28 @@ namespace Digitigrade; abstract class Job implements \JsonSerializable { + private static \JsonMapper $mapper; + public int $remainingTries; public int $doneTries = 0; + public static function __initStatic() { + self::$mapper = new \JsonMapper(); + // to parse DateTime(Immutable)s properly + self::$mapper->bStrictObjectTypeChecking = false; + self::$mapper->classMap[\DateTimeInterface::class] = \DateTimeImmutable::class; + } + public function jsonSerialize(): array { $array = ['jobSubtype' => static::class]; foreach ((new \ReflectionObject($this))->getProperties() as $prop) { assert($prop instanceof \ReflectionProperty); // for intellisense $value = $prop->getValue($this); - $array[$prop->getName()] = is_object($value) ? json_encode($value) : $value; + if ($value instanceof \DateTimeInterface) { + $array[$prop->getName()] = $value->format('c'); + } else { + $array[$prop->getName()] = $value; + } } return $array; } @@ -20,7 +33,7 @@ abstract class Job implements \JsonSerializable { if (!property_exists($obj, 'jobSubtype')) { return null; } - return (new \JsonMapper())->map($obj, $obj->jobSubtype); + return self::$mapper->map($obj, $obj->jobSubtype); } /** @@ -36,4 +49,6 @@ abstract class Job implements \JsonSerializable { public function submit() { JobQueue::getInstance()->submitNormal($this); } -} \ No newline at end of file +} + +Job::__initStatic(); \ No newline at end of file diff --git a/Digitigrade/Job/ExpireInboundAuthToken.php b/Digitigrade/Job/ExpireInboundAuthToken.php index 1ec3585..84a5b21 100644 --- a/Digitigrade/Job/ExpireInboundAuthToken.php +++ b/Digitigrade/Job/ExpireInboundAuthToken.php @@ -26,6 +26,7 @@ class ExpireInboundAuthToken extends Job { return; } $instance->auth->inboundToken = null; + $instance->auth->save(); } public function submit() { diff --git a/Digitigrade/Job/ProcessIncomingPush.php b/Digitigrade/Job/ProcessIncomingPush.php new file mode 100644 index 0000000..fefa122 --- /dev/null +++ b/Digitigrade/Job/ProcessIncomingPush.php @@ -0,0 +1,41 @@ +object = $object; + $this->remainingTries = 3; + } + + public function run() { + assert(isset($this->object->type)); + $log = Logger::getInstance(); + switch ($this->object->type) { + case 'actor': + $log->info('importing actor with uri ' . $this->object->self); + Actor::importFromReceivedObject($this->object); + break; + case 'note': + $log->info('importing note with uri ' . $this->object->self); + Note::importFromReceivedObject($this->object); + break; + case 'interaction': + $log->info('importing interaction with uri ' . $this->object->self); + Interaction::importFromReceivedObject($this->object); + break; + case 'extension': + case 'tombstone': + throw new \RuntimeException('object type ' . $this->object->type . ' not yet implemented :('); + default: + throw new \RuntimeException('invalid object type: ' . $this->object->type); + } + } +} \ No newline at end of file -- cgit v1.3