web-base/core/Api/NotificationsAPI.class.php

231 lines
7.4 KiB
PHP
Raw Normal View History

2020-06-20 20:13:51 +02:00
<?php
namespace Api {
2022-06-20 19:52:31 +02:00
use Objects\Context;
abstract class NotificationsAPI extends Request {
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
parent::__construct($context, $externalCall, $params);
}
2020-06-20 20:13:51 +02:00
}
}
namespace Api\Notifications {
use Api\NotificationsAPI;
use Api\Parameter\Parameter;
use Api\Parameter\StringType;
2021-12-08 16:53:43 +01:00
use Driver\SQL\Column\Column;
2020-06-20 20:13:51 +02:00
use Driver\SQL\Condition\Compare;
2020-06-26 23:32:45 +02:00
use Driver\SQL\Condition\CondIn;
use Driver\SQL\Query\Select;
2022-06-20 19:52:31 +02:00
use Objects\Context;
use Objects\DatabaseEntity\Group;
use Objects\DatabaseEntity\Notification;
use Objects\DatabaseEntity\User;
2020-06-20 20:13:51 +02:00
class Create extends NotificationsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
2020-06-20 20:13:51 +02:00
'groupId' => new Parameter('groupId', Parameter::TYPE_INT, true),
'userId' => new Parameter('userId', Parameter::TYPE_INT, true),
'title' => new StringType('title', 32),
'message' => new StringType('message', 256),
));
$this->isPublic = false;
}
2022-06-20 19:52:31 +02:00
private function insertUserNotification($userId, $notificationId): bool {
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$res = $sql->insert("UserNotification", array("user_id", "notification_id"))
->addRow($userId, $notificationId)
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
return $this->success;
}
2022-06-20 19:52:31 +02:00
private function insertGroupNotification($groupId, $notificationId): bool {
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$res = $sql->insert("GroupNotification", array("group_id", "notification_id"))
->addRow($groupId, $notificationId)
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
return $this->success;
}
2022-06-20 19:52:31 +02:00
private function createNotification($title, $message): bool|int {
$sql = $this->context->getSQL();
$notification = new Notification();
$notification->title = $title;
$notification->message = $message;
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
$this->success = ($notification->save($sql) !== FALSE);
2020-06-20 20:13:51 +02:00
$this->lastError = $sql->getLastError();
if ($this->success) {
2022-06-20 19:52:31 +02:00
return $notification->getId();
2020-06-20 20:13:51 +02:00
}
return $this->success;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$userId = $this->getParam("userId");
$groupId = $this->getParam("groupId");
$title = $this->getParam("title");
$message = $this->getParam("message");
if (is_null($userId) && is_null($groupId)) {
return $this->createError("Either userId or groupId must be specified.");
} else if(!is_null($userId) && !is_null($groupId)) {
return $this->createError("Only one of userId and groupId must be specified.");
} else if(!is_null($userId)) {
2022-06-20 19:52:31 +02:00
if (User::exists($sql, $userId)) {
2020-06-20 20:13:51 +02:00
$id = $this->createNotification($title, $message);
if ($this->success) {
return $this->insertUserNotification($userId, $id);
}
2022-06-20 19:52:31 +02:00
} else {
return $this->createError("User not found: $userId");
2020-06-20 20:13:51 +02:00
}
} else if(!is_null($groupId)) {
2022-06-20 19:52:31 +02:00
if (Group::exists($sql, $groupId)) {
2020-06-20 20:13:51 +02:00
$id = $this->createNotification($title, $message);
if ($this->success) {
return $this->insertGroupNotification($groupId, $id);
}
2022-06-20 19:52:31 +02:00
} else {
return $this->createError("Group not found: $groupId");
2020-06-20 20:13:51 +02:00
}
}
return $this->success;
}
}
class Fetch extends NotificationsAPI {
private array $notifications;
2022-06-20 19:52:31 +02:00
private array $notificationIds;
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
2020-06-26 23:32:45 +02:00
'new' => new Parameter('new', Parameter::TYPE_BOOLEAN, true, true)
));
2020-06-20 20:13:51 +02:00
$this->loginRequired = true;
}
2022-06-20 19:52:31 +02:00
private function fetchUserNotifications(): bool {
2020-06-26 23:32:45 +02:00
$onlyNew = $this->getParam('new');
2022-06-20 19:52:31 +02:00
$userId = $this->context->getUser()->getId();
$sql = $this->context->getSQL();
$query = $sql->select($sql->distinct("Notification.id"), "created_at", "title", "message", "type")
2020-06-20 20:13:51 +02:00
->from("Notification")
2022-06-20 19:52:31 +02:00
->innerJoin("UserNotification", "UserNotification.notification_id", "Notification.id")
2020-06-20 20:13:51 +02:00
->where(new Compare("UserNotification.user_id", $userId))
2020-06-26 23:32:45 +02:00
->orderBy("created_at")->descending();
2020-06-20 20:13:51 +02:00
2020-06-26 23:32:45 +02:00
if ($onlyNew) {
$query->where(new Compare("UserNotification.seen", false));
2020-06-20 20:13:51 +02:00
}
2020-06-26 23:32:45 +02:00
return $this->fetchNotifications($query);
2020-06-20 20:13:51 +02:00
}
2022-06-20 19:52:31 +02:00
private function fetchGroupNotifications(): bool {
2020-06-26 23:32:45 +02:00
$onlyNew = $this->getParam('new');
2022-06-20 19:52:31 +02:00
$userId = $this->context->getUser()->getId();
$sql = $this->context->getSQL();
$query = $sql->select($sql->distinct("Notification.id"), "created_at", "title", "message", "type")
2020-06-20 20:13:51 +02:00
->from("Notification")
2022-06-20 19:52:31 +02:00
->innerJoin("GroupNotification", "GroupNotification.notification_id", "Notification.id")
2020-06-20 20:13:51 +02:00
->innerJoin("UserGroup", "GroupNotification.group_id", "UserGroup.group_id")
->where(new Compare("UserGroup.user_id", $userId))
2020-06-26 23:32:45 +02:00
->orderBy("created_at")->descending();
2020-06-20 20:13:51 +02:00
2020-06-26 23:32:45 +02:00
if ($onlyNew) {
$query->where(new Compare("GroupNotification.seen", false));
}
return $this->fetchNotifications($query);
}
2022-06-20 19:52:31 +02:00
private function fetchNotifications(Select $query): bool {
$sql = $this->context->getSQL();
2020-06-26 23:32:45 +02:00
$res = $query->execute();
2020-06-20 20:13:51 +02:00
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
foreach($res as $row) {
2022-06-20 19:52:31 +02:00
$id = $row["id"];
if (!in_array($id, $this->notificationIds)) {
$this->notificationIds[] = $id;
2020-06-20 20:13:51 +02:00
$this->notifications[] = array(
2022-06-20 19:52:31 +02:00
"id" => $id,
2020-06-20 20:13:51 +02:00
"title" => $row["title"],
"message" => $row["message"],
"created_at" => $row["created_at"],
2020-06-26 23:32:45 +02:00
"type" => $row["type"]
2020-06-20 20:13:51 +02:00
);
}
}
}
return $this->success;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-20 20:13:51 +02:00
$this->notifications = array();
2022-06-20 19:52:31 +02:00
$this->notificationIds = array();
2020-06-20 20:13:51 +02:00
if ($this->fetchUserNotifications() && $this->fetchGroupNotifications()) {
$this->result["notifications"] = $this->notifications;
}
return $this->success;
}
}
2020-06-26 23:32:45 +02:00
class Seen extends NotificationsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array());
2020-06-26 23:32:45 +02:00
$this->loginRequired = true;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-26 23:32:45 +02:00
2022-06-20 19:52:31 +02:00
$currentUser = $this->context->getUser();
$sql = $this->context->getSQL();
2020-06-26 23:32:45 +02:00
$res = $sql->update("UserNotification")
->set("seen", true)
2022-06-20 19:52:31 +02:00
->where(new Compare("user_id", $currentUser->getId()))
2020-06-26 23:32:45 +02:00
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
$res = $sql->update("GroupNotification")
->set("seen", true)
2021-12-08 16:53:43 +01:00
->where(new CondIn(new Column("group_id"),
2020-06-26 23:32:45 +02:00
$sql->select("group_id")
->from("UserGroup")
2022-06-20 19:52:31 +02:00
->where(new Compare("user_id", $currentUser->getId()))))
2020-06-26 23:32:45 +02:00
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
}
return $this->success;
}
}
2020-06-20 20:13:51 +02:00
}