aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/Notification.php
diff options
context:
space:
mode:
authorwinter2025-01-19 22:17:12 +0000
committerwinter2025-01-19 22:17:12 +0000
commit8c6dad8c48d5d730b102d9de0079fee9752b0d35 (patch)
tree2cfe07a8c7958f78c767fd0d1444c3f8fef70511 /Digitigrade/Model/Notification.php
parent2d9bd87fb1c565ee161f93219ce2533e8dbffe06 (diff)
implement notifications
Diffstat (limited to 'Digitigrade/Model/Notification.php')
-rw-r--r--Digitigrade/Model/Notification.php57
1 files changed, 57 insertions, 0 deletions
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