Add NTFY support

This commit is contained in:
Christopher Cookman 2024-05-06 00:28:48 -06:00
parent 6eed27b186
commit 6f17c2f17c
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42
4 changed files with 4009 additions and 35 deletions

View file

@ -7,8 +7,6 @@ const bcrypt = require("bcrypt");
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("database.db");
const SQLiteStore = require('connect-sqlite3')(session);
const ntfy = require("ntfy");
@ -49,6 +47,17 @@ const genPass = () => {
}).join("");
}
// NTFY
const ntfyPublish = (data) => {
const fetch = require('node-fetch');
data.topic = config.ntfy.topic;
fetch(config.ntfy.server, {
method: 'POST',
body: JSON.stringify(data)
})
console.log(data)
}
// Create user
const createUser = (username, password, authLevel) => {
return new Promise((resolve, reject) => {
@ -164,7 +173,7 @@ const createTicket = (title, description, status, user, priority) => {
reject(err);
return;
}
if (config.ntfy) ntfy.publish({server: config.ntfy.server, authorization: config.ntfy.authorization, topic: config.ntfy.topic, title: `New Ticket`, message: `${title}: ${description}`, priority, tags: [user, new Date(timestamp).toISOString()]});
if (config.ntfy) ntfyPublish({title: `New Ticket`, message: `${title}: ${description}`, priority: Number(priority), tags: [user.username, new Date(timestamp).toISOString()]});
resolve(this.lastID);
}
);
@ -180,7 +189,7 @@ const deleteTicket = (id) => {
reject(err);
return;
}
if (config.ntfy) ntfy.publish({server: config.ntfy.server, authorization: config.ntfy.authorization, topic: config.ntfy.topic, title: `Ticket Deleted`, message: `Ticket ${id} has been deleted`, tags: [new Date().toISOString()]});
if (config.ntfy) ntfyPublish({title: `Ticket Deleted`, message: `Ticket ${id} has been deleted`, tags: [new Date().toISOString()]});
resolve();
});
});
@ -350,13 +359,14 @@ app.post("/ticket/:id/:action", isAuthenticated, (req, res) => {
switch (req.params.action) {
case "delete":
// Delete ticket
db.run("DELETE FROM tickets WHERE id = ?", [req.params.id], (err) => {
if (err) {
deleteTicket(req.params.id)
.then(() => {
res.redirect("/dashboard");
})
.catch((err) => {
console.error(err);
return res.status(500).send("Internal Server Error");
}
res.redirect("/dashboard");
});
res.status(500).send("Internal Server Error");
});
break;
case "add-message":
// Add message
@ -373,7 +383,7 @@ app.post("/ticket/:id/:action", isAuthenticated, (req, res) => {
console.error(err);
return res.status(500).send("Internal Server Error");
}
if (config.ntfy) ntfy.publish({server: config.ntfy.server, authorization: config.ntfy.authorization, topic: config.ntfy.topic, title: `New Message on ticket ${req.params.id}`, message: `${req.body.message}`, tags: [req.session.userData.username, new Date(timestamp).toISOString()]});
if (config.ntfy) ntfyPublish({title: `New Message on ticket ${req.params.id}`, message: `${req.body.message}`, tags: [req.session.userData.username, new Date(timestamp).toISOString()]});
res.redirect(`/ticket/${req.params.id}`);
});
});
@ -395,7 +405,7 @@ app.post("/ticket/:id/:action", isAuthenticated, (req, res) => {
console.error(err);
return res.status(500).send("Internal Server Error");
}
if(config.ntfy) ntfy.publish({server: config.ntfy.server, authorization: config.ntfy.authorization, topic: config.ntfy.topic, title: `Ticket ${req.params.id} status changed`, message: `Status changed to ${req.body.status}`, tags: [req.session.userData.username, new Date(Date.now()).toISOString()]});
if(config.ntfy) ntfyPublish({title: `Ticket ${req.params.id} status changed`, message: `Status changed to ${req.body.status}`, tags: [req.session.userData.username, new Date(Date.now()).toISOString()]});
res.redirect(`/ticket/${req.params.id}`);
});
});
@ -418,7 +428,7 @@ app.post("/ticket/:id/:action", isAuthenticated, (req, res) => {
console.error(err);
return res.status(500).send("Internal Server Error");
}
if(config.ntfy) ntfy.publish({server: config.ntfy.server, authorization: config.ntfy.authorization, topic: config.ntfy.topic, title: `Ticket ${req.params.id} priority changed`, message: `Priority changed to ${req.body.priority}`, tags: [req.session.userData.username, new Date(Date.now()).toISOString()]});
if(config.ntfy) ntfyPublish({title: `Ticket ${req.params.id} priority changed`, message: `Priority changed to ${req.body.priority}`, tags: [req.session.userData.username, new Date(Date.now()).toISOString()]});
res.redirect(`/ticket/${req.params.id}`);
});
});

4003
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -16,7 +16,8 @@
"ejs": "^3.1.10",
"express": "^4.19.2",
"express-session": "^1.18.0",
"ntfy": "^1.5.3",
"install": "^0.13.0",
"npm": "^10.7.0",
"sqlite3": "^5.1.7"
}
}

View file

@ -54,8 +54,8 @@
</select>
</div>
<button type="submit" class="btn btn-primary">Create Ticket</button>
<button onclick="window.location = '/dashboard'" class="btn btn-danger" style="display:inline;">Cancel</button>
</form>
<!-- <button onclick="window.location = '/dashboard'" class="btn btn-danger" style="display:inline;">Cancel</button> --> <!--Doesnt work rn-->
</div>
</div>
</div>