diff options
Diffstat (limited to 'Digitigrade/Notification/PokeNotif.php')
| -rw-r--r-- | Digitigrade/Notification/PokeNotif.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/Digitigrade/Notification/PokeNotif.php b/Digitigrade/Notification/PokeNotif.php new file mode 100644 index 0000000..5446098 --- /dev/null +++ b/Digitigrade/Notification/PokeNotif.php @@ -0,0 +1,62 @@ +<?php +namespace Digitigrade\Notification; + +use Digitigrade\Model\Actor; +use Digitigrade\Model\Notification; +use Digitigrade\Model\UserAccount; +use Digitigrade\PokeVerb; + +class PokeNotif implements Notifyable { + private Actor $subject; + private PokeVerb $verb; + private Actor $object; + private bool $urgent; + + public function __construct(Actor $subject, PokeVerb $verb, Actor $object, bool $urgent) { + $this->subject = $subject; + $this->verb = $verb; + $this->object = $object; + $this->urgent = $urgent; + } + + public function toJsonReference(): mixed { + return [$this->subject->id, $this->verb->value, $this->object->id, $this->urgent]; + } + + public static function fromJsonReference(mixed $reference): ?self { + return new self( + Actor::find($reference[0]), + PokeVerb::from($reference[1]), + Actor::find($reference[2]), + $reference[3] + ); + } + + public function getNotificationTitle(): string { + $kind = $this->urgent ? 'urgent' : 'normal'; + $verb = $this->verb->value; + return __f("notifications.poke.$kind", $this->subject->displayName, __("notifications.poke.verb.$verb")); + } + + public function getNotificationTitleLink(): ?string { + return $this->subject->getLocalUiHref(); + } + + public function getNotificationBody(): ?string { + return null; + } + + public function getNotificationImageUrl(): ?string { + return $this->subject->avatar ?? '/static/default-avatar.png'; + } + + public function getNotificationImageLink(): ?string { + return $this->subject->getLocalUiHref(); + } + + public function processNotifications() { + if (!$this->object->isLocal) + return; + Notification::fromNotifyable($this, UserAccount::findByLinkedActor($this->object))->send(); + } +}
\ No newline at end of file |
