diff options
| author | winter | 2025-01-19 22:17:12 +0000 |
|---|---|---|
| committer | winter | 2025-01-19 22:17:12 +0000 |
| commit | 8c6dad8c48d5d730b102d9de0079fee9752b0d35 (patch) | |
| tree | 2cfe07a8c7958f78c767fd0d1444c3f8fef70511 /Digitigrade | |
| parent | 2d9bd87fb1c565ee161f93219ce2533e8dbffe06 (diff) | |
implement notifications
Diffstat (limited to 'Digitigrade')
| -rw-r--r-- | Digitigrade/Job/ProcessIncomingPush.php | 2 | ||||
| -rw-r--r-- | Digitigrade/Model/HomeTimelineItem.php (renamed from Digitigrade/Timeline/HomeTimelineItem.php) | 3 | ||||
| -rw-r--r-- | Digitigrade/Model/Interaction.php | 37 | ||||
| -rw-r--r-- | Digitigrade/Model/Note.php | 32 | ||||
| -rw-r--r-- | Digitigrade/Model/Notification.php | 57 | ||||
| -rw-r--r-- | Digitigrade/Notification/Notifyable.php | 12 | ||||
| -rw-r--r-- | Digitigrade/Timeline/HomeTimeline.php | 1 |
7 files changed, 139 insertions, 5 deletions
diff --git a/Digitigrade/Job/ProcessIncomingPush.php b/Digitigrade/Job/ProcessIncomingPush.php index 1306ea5..eb6bdd5 100644 --- a/Digitigrade/Job/ProcessIncomingPush.php +++ b/Digitigrade/Job/ProcessIncomingPush.php @@ -35,11 +35,13 @@ class ProcessIncomingPush extends Job { $log->info('importing note with uri ' . $this->object->self); $note = Note::importFromReceivedObject($this->object); $note?->processTimelineAdditions(); + $note?->processNotifications(); break; case 'interaction': $log->info('importing interaction with uri ' . $this->object->self); $interaction = Interaction::importFromReceivedObject($this->object); $interaction?->processTimelineAdditions(); + $interaction?->processNotifications(); break; case 'extension': throw new \RuntimeException('object type ' . $this->object->type . ' not yet implemented :('); diff --git a/Digitigrade/Timeline/HomeTimelineItem.php b/Digitigrade/Model/HomeTimelineItem.php index cb2b6e4..7e2c062 100644 --- a/Digitigrade/Timeline/HomeTimelineItem.php +++ b/Digitigrade/Model/HomeTimelineItem.php @@ -1,8 +1,9 @@ <?php -namespace Digitigrade\Timeline; +namespace Digitigrade\Model; use Digitigrade\Model; use Digitigrade\Model\UserAccount; +use Digitigrade\Timeline\TimelineItem; class HomeTimelineItem extends Model { public ?int $id; diff --git a/Digitigrade/Model/Interaction.php b/Digitigrade/Model/Interaction.php index e5619bb..cf2be01 100644 --- a/Digitigrade/Model/Interaction.php +++ b/Digitigrade/Model/Interaction.php @@ -1,11 +1,12 @@ <?php namespace Digitigrade\Model; -use Digitigrade\Timeline\HomeTimelineItem; +use Digitigrade\Model\HomeTimelineItem; +use Digitigrade\Notification\Notifyable; use Digitigrade\Timeline\ReasonReshared; use Digitigrade\Timeline\TimelineItem; -class Interaction extends PushableModel { +class Interaction extends PushableModel implements Notifyable { public ?int $id; public string $uri; public \DateTimeImmutable $created; @@ -61,6 +62,30 @@ class Interaction extends PushableModel { return array_unique($instances); } + public function getNotificationTitle(): string { + return sprintf(__('notifications.interaction.' . $this->kind->value . '.title'), $this->author->displayName); + } + public function getNotificationTitleLink(): ?string { + return '/@/' . $this->target->author->getFullHandle() . '/note/' . $this->target->id; + } + public function getNotificationBody(): ?string { + return $this->target->plainContent; + } + public function getNotificationImageUrl(): ?string { + return $this->author->avatar; + } + public function getNotificationImageLink(): ?string { + return '/@/' . $this->author->getFullHandle(); + } + + public function toJsonReference(): mixed { + return $this->id; + } + + public static function fromJsonReference(mixed $reference): self { + return self::find($reference); + } + public function processTimelineAdditions() { // basically, if this is a reshare, we need to put a home timeline item // for all followers who can see the shared note @@ -80,6 +105,14 @@ class Interaction extends PushableModel { } } + public function processNotifications() { + if (!$this->target->author->isLocal || $this->author == $this->target->author) { + return; + } + $recipient = UserAccount::findByLinkedActor($this->target->author); + Notification::fromNotifyable($this, $recipient)->send(); + } + public function jsonSerialize(): array { if ($this->deleted) { return [ diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php index f6f492d..cf84ee0 100644 --- a/Digitigrade/Model/Note.php +++ b/Digitigrade/Model/Note.php @@ -2,11 +2,12 @@ namespace Digitigrade\Model; use Digitigrade\Db; -use Digitigrade\Timeline\HomeTimelineItem; +use Digitigrade\Model\HomeTimelineItem; +use Digitigrade\Notification\Notifyable; use Digitigrade\Timeline\TimelineIncludeable; use Digitigrade\Timeline\TimelineItem; -class Note extends PushableModel implements TimelineIncludeable { +class Note extends PushableModel implements TimelineIncludeable, Notifyable { public ?int $id; public string $uri; public \DateTimeImmutable $created; @@ -195,6 +196,33 @@ class Note extends PushableModel implements TimelineIncludeable { return self::find($reference); } + public function getNotificationTitle(): string { + // assuming that this notification is because the note is a reply for now + return sprintf(__('notifications.interaction.reply.title'), $this->author->displayName); + } + public function getNotificationTitleLink(): ?string { + return '/@/' . $this->author->getFullHandle() . '/note/' . $this->id; + } + public function getNotificationBody(): ?string { + return $this->plainContent; + } + public function getNotificationImageUrl(): ?string { + return $this->author->avatar; + } + public function getNotificationImageLink(): ?string { + return '/@/' . $this->author->getFullHandle(); + } + + public function processNotifications() { + foreach ($this->mentions as $a) { + if (!$a->isLocal) { + continue; + } + $user = UserAccount::findByLinkedActor($a); + Notification::fromNotifyable($this, $user)->send(); + } + } + public function processTimelineAdditions() { $item = new TimelineItem($this); foreach ($this->getRelevantActors() as $actor) { diff --git a/Digitigrade/Model/Notification.php b/Digitigrade/Model/Notification.php new file mode 100644 index 0000000..ee9d64a --- /dev/null +++ b/Digitigrade/Model/Notification.php @@ -0,0 +1,57 @@ +<?php +namespace Digitigrade\Model; + +use Digitigrade\Model; +use Digitigrade\Notification\Notifyable; + +class Notification extends Model { + public ?int $id; + public UserAccount $userAccount; + public string $itemType; + public string $itemData; + public \DateTimeImmutable $created; + + /** + * Creates a new Notification from a Notifyable object, but does not send it. + * @param Notifyable $item the item to send in the notification + * @param UserAccount $user the user to send the notification to + * @return Notification + */ + public static function fromNotifyable(Notifyable $item, UserAccount $user): self { + $notif = new self(); + $notif->userAccount = $user; + $notif->itemType = $item::class; + $notif->itemData = json_encode($item->toJsonReference()); + $notif->created = new \DateTimeImmutable(); + + $notif->save(); + return $notif; + } + + public function toNotifyable(): Notifyable { + return $this->itemType::fromJsonReference(json_decode($this->itemData)); + } + + /** + * Saves this object and delivers the notification to the recipient user. + * @return void + */ + public function send() { + $this->save(); + // TODO: send it via any push mechanisms + } + + /** + * @return self[] + */ + public static function findAllForUser(UserAccount $user, ?int $limit = null, int $offset = 0, ?\DateTimeInterface $since = null) { + $where = 'user_account = ?'; + $params = [$user->id]; + if (isset($since)) { + $where .= ' AND created > ?'; + $params[] = $since->format('c'); + } + return self::findAllWhere($where, $params, $limit, $offset, 'id DESC'); + } + +}
\ No newline at end of file diff --git a/Digitigrade/Notification/Notifyable.php b/Digitigrade/Notification/Notifyable.php new file mode 100644 index 0000000..510437e --- /dev/null +++ b/Digitigrade/Notification/Notifyable.php @@ -0,0 +1,12 @@ +<?php +namespace Digitigrade\Notification; + +interface Notifyable { + public function toJsonReference(): mixed; + public static function fromJsonReference(mixed $reference): self; + public function getNotificationTitle(): string; + public function getNotificationTitleLink(): ?string; + public function getNotificationBody(): ?string; + public function getNotificationImageUrl(): ?string; + public function getNotificationImageLink(): ?string; +}
\ No newline at end of file diff --git a/Digitigrade/Timeline/HomeTimeline.php b/Digitigrade/Timeline/HomeTimeline.php index c2baf3d..0867541 100644 --- a/Digitigrade/Timeline/HomeTimeline.php +++ b/Digitigrade/Timeline/HomeTimeline.php @@ -2,6 +2,7 @@ namespace Digitigrade\Timeline; use Digitigrade\Model\UserAccount; +use Digitigrade\Model\HomeTimelineItem; class HomeTimeline extends Timeline { private UserAccount $user; |
