From 8c6dad8c48d5d730b102d9de0079fee9752b0d35 Mon Sep 17 00:00:00 2001 From: winter Date: Sun, 19 Jan 2025 22:17:12 +0000 Subject: implement notifications --- Digitigrade/Model/Notification.php | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Digitigrade/Model/Notification.php (limited to 'Digitigrade/Model/Notification.php') 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 @@ +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 -- cgit v1.3