diff --git a/api.php b/api.php index cd45386..3f5a40f 100644 --- a/api.php +++ b/api.php @@ -355,6 +355,91 @@ switch ($VARS['action']) { } exit(json_encode(["status" => "OK", "pinvalid" => ($pin == $VARS['pin'])])); break; + case "getnotifications": + if (!empty($VARS['username'])) { + $user = User::byUsername($VARS['username']); + } else if (!empty($VARS['uid'])) { + $user = new User($VARS['uid']); + } else { + http_response_code(400); + die("\"400 Bad Request\""); + } + if ($user->exists()) { + $notifications = $database->select('notifications', ['notificationid (id)', 'timestamp', 'title', 'content', 'url', 'seen', 'sensitive'], ['uid' => $user->getUID()]); + for ($i = 0; $i < count($notifications); $i++) { + $notifications[$i]['id'] = $notifications[$i]['id'] * 1; + $notifications[$i]['seen'] = ($notifications[$i]['seen'] == "1" ? true : false); + $notifications[$i]['sensitive'] = ($notifications[$i]['sensitive'] == "1" ? true : false); + } + exit(json_encode(["status" => "OK", "notifications" => $notifications])); + } + exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("user does not exist", false)])); + case "readnotification": + if (!empty($VARS['username'])) { + $user = User::byUsername($VARS['username']); + } else if (!empty($VARS['uid'])) { + $user = new User($VARS['uid']); + } else { + http_response_code(400); + die("\"400 Bad Request\""); + } + + if ($user->exists()) { + if ($database->has('notifications', ['AND' => ['uid' => $user->getUID(), 'notificationid' => $VARS['id']]])) { + $database->update('notifications', ['seen' => 1], ['AND' => ['uid' => $user->getUID(), 'notificationid' => $VARS['id']]]); + exit(json_encode(["status" => "OK"])); + } + exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("invalid parameters", false)])); + } + exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("user does not exist", false)])); + case "addnotification": + if (!empty($VARS['username'])) { + $user = User::byUsername($VARS['username']); + } else if (!empty($VARS['uid'])) { + $user = new User($VARS['uid']); + } else { + http_response_code(400); + die("\"400 Bad Request\""); + } + + if ($user->exists()) { + if (empty($VARS['title']) || empty($VARS['content'])) { + exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("invalid parameters", false)])); + } + $timestamp = date("Y-m-d H:i:s"); + if (!empty($VARS['timestamp'])) { + $timestamp = date("Y-m-d H:i:s", strtotime($VARS['timestamp'])); + } + $url = ""; + if (!empty($VARS['url'])) { + $url = $VARS['url']; + } + $sensitive = 0; + if (isset($VARS['sensitive'])) { + $sensitive = 1; + } + $database->insert('notifications', ['uid' => $user->getUID(), 'timestamp' => $timestamp, 'title' => $VARS['title'], 'content' => $VARS['content'], 'url' => $url, 'seen' => 0, 'sensitive' => $sensitive]); + exit(json_encode(["status" => "OK", "id" => $database->id() * 1])); + } + exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("user does not exist", false)])); + case "deletenotification": + if (!empty($VARS['username'])) { + $user = User::byUsername($VARS['username']); + } else if (!empty($VARS['uid'])) { + $user = new User($VARS['uid']); + } else { + http_response_code(400); + die("\"400 Bad Request\""); + } + + if ($user->exists()) { + if ($database->has('notifications', ['AND' => ['uid' => $user->getUID(), 'notificationid' => $VARS['id']]])) { + $database->delete('notifications', ['AND' => ['uid' => $user->getUID(), 'notificationid' => $VARS['id']]]); + exit(json_encode(["status" => "OK"])); + } + exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("invalid parameters", false)])); + } + exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("user does not exist", false)])); default: http_response_code(404); die(json_encode("404 Not Found: the requested action is not available.")); diff --git a/database.mwb b/database.mwb index 4cd3632..361362e 100644 Binary files a/database.mwb and b/database.mwb differ diff --git a/database_upgrade/1.0.1_2.0.sql b/database_upgrade/1.0.1_2.0.sql index 0bcd983..5534e26 100644 --- a/database_upgrade/1.0.1_2.0.sql +++ b/database_upgrade/1.0.1_2.0.sql @@ -13,6 +13,27 @@ CREATE TABLE IF NOT EXISTS `onetimekeys` ( ENGINE = InnoDB DEFAULT CHARACTER SET = utf8; +CREATE TABLE IF NOT EXISTS `notifications` ( + `notificationid` INT(11) NOT NULL AUTO_INCREMENT, + `uid` INT(11) NOT NULL, + `timestamp` DATETIME NOT NULL, + `title` VARCHAR(255) NOT NULL, + `content` TINYTEXT NOT NULL, + `url` VARCHAR(255) NOT NULL, + `seen` TINYINT(1) NOT NULL DEFAULT 0, + `sensitive` TINYINT(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`notificationid`, `uid`), + UNIQUE INDEX `notificationid_UNIQUE` (`notificationid` ASC), + INDEX `fk_notifications_accounts1_idx` (`uid` ASC), + CONSTRAINT `fk_notifications_accounts1` + FOREIGN KEY (`uid`) + REFERENCES `accounthub`.`accounts` (`uid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 + + SET FOREIGN_KEY_CHECKS = 0; ALTER TABLE `groups` CHANGE COLUMN `groupid` `groupid` INT(11) NOT NULL AUTO_INCREMENT;