aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorwinter2024-12-19 18:01:29 +0000
committerwinter2024-12-19 18:01:29 +0000
commit1e070a7a0097c74f8ac30678d39267f19c76f9f6 (patch)
tree669ba608d8182971752f6bc2ae2ade0b3ff627cf
parent6f767c4562df4a686720ef96396cea0b2f0c6334 (diff)
process pushes in job + fix token expiry job
-rw-r--r--Digitigrade/Job.php21
-rw-r--r--Digitigrade/Job/ExpireInboundAuthToken.php1
-rw-r--r--Digitigrade/Job/ProcessIncomingPush.php41
-rw-r--r--routes/push.php24
4 files changed, 65 insertions, 22 deletions
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 @@
+<?php
+namespace Digitigrade\Job;
+
+use Digitigrade\Job;
+use Digitigrade\Logger;
+use Digitigrade\Model\Actor;
+use Digitigrade\Model\Interaction;
+use Digitigrade\Model\Note;
+
+class ProcessIncomingPush extends Job {
+ public \stdClass $object;
+
+ public function __construct(\stdClass $object) {
+ $this->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
diff --git a/routes/push.php b/routes/push.php
index 6fc8229..f8771f2 100644
--- a/routes/push.php
+++ b/routes/push.php
@@ -3,10 +3,8 @@
use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\HttpResponseStatus\Forbidden;
use Digitigrade\HttpResponseStatus\Unauthorized;
-use Digitigrade\Model\Actor;
+use Digitigrade\Job\ProcessIncomingPush;
use Digitigrade\Model\Instance;
-use Digitigrade\Model\Interaction;
-use Digitigrade\Model\Note;
use Digitigrade\Router;
Router::getInstance()->mount('/push', function (array $args) {
@@ -33,21 +31,9 @@ Router::getInstance()->mount('/push', function (array $args) {
if (hostname_from_uri($obj->self) != $instance->domain) {
throw new Forbidden('you may not push objects belonging to a different instance');
}
-
- switch ($obj->type) {
- case 'actor':
- Actor::importFromReceivedObject($obj);
- break;
- case 'note':
- Note::importFromReceivedObject($obj);
- break;
- case 'interaction':
- Interaction::importFromReceivedObject($obj);
- break;
- case 'extension':
- case 'tombstone':
- throw new \RuntimeException('object type not yet implemented :(');
- default:
- throw new BadRequest('invalid object type');
+ if (!in_array($obj->type, ['actor', 'note', 'interaction', 'extension', 'tombstone'])) {
+ throw new BadRequest('invalid object type!');
}
+
+ (new ProcessIncomingPush($obj))->submit();
}); \ No newline at end of file