aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/notifications.php
blob: 05fb89da31ffd9a9b4edcb34bf94bf3611bfb99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\Model\Notification;
use Digitigrade\Model\UserAccount;
use Digitigrade\Router;

Router::getInstance()->mount('/notifications/partial', function (array $args) {
    $user = UserAccount::requireByCurrentSession();
    if (!isset($_GET['since'])) {
        throw new BadRequest('get parameter `since` required');
    }
    $when = new \DateTimeImmutable($_GET['since']);
    $notifs = Notification::findAllForUser($user, since: $when);

    foreach ($notifs as $notif) {
        $notif = $notif->toNotifyable();
        render_template('notification', [
            'title' => $notif->getNotificationTitle(),
            'body' => $notif->getNotificationBody(),
            'imageUrl' => $notif->getNotificationImageUrl(),
            'titleLink' => $notif->getNotificationTitleLink(),
            'imageLink' => $notif->getNotificationImageLink()
        ]);
    }

    render_template('live_notifications_updater', [
        'isUpdate' => true,
        'since' => new \DateTimeImmutable()
    ]);
});