Browse Source

Add notification API, close #7

tags/v2.0
Skylar Ittner 1 year ago
parent
commit
5374fa0611
3 changed files with 106 additions and 0 deletions
  1. 85
    0
      api.php
  2. BIN
      database.mwb
  3. 21
    0
      database_upgrade/1.0.1_2.0.sql

+ 85
- 0
api.php View File

@@ -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."));

BIN
database.mwb View File


+ 21
- 0
database_upgrade/1.0.1_2.0.sql View File

@@ -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;

Loading…
Cancel
Save