Skip to content

Commit c0386f1

Browse files
authoredJan 18, 2025··
Implemented submission removal via API (#22)
1 parent 0a2baa8 commit c0386f1

File tree

1 file changed

+78
-12
lines changed

1 file changed

+78
-12
lines changed
 

‎source/db-server/main.js

+78-12
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ function GetCurrentTimeAsString() {
2626
return "" + (new Date()).valueOf()
2727
}
2828

29-
let startupTime = GetCurrentTimeAsString()
30-
let databaseModificationTime = startupTime
29+
const startupTime = GetCurrentTimeAsString()
30+
let databaseLastDeleteTime = startupTime
31+
let databaseLastModificationTime = startupTime
3132

3233
function isObjectAllowedInDB(inObj) {
3334
let isAllowed = true;
@@ -89,7 +90,7 @@ api.get('/get_submission', (req, res) => {
8990
return
9091
}
9192

92-
let etag = req.query.ID > latestReportID ? "0" : startupTime
93+
let etag = req.query.ID > latestReportID ? "0" : databaseLastDeleteTime
9394
if (req.headers['if-none-match'] == etag) {
9495
res.status(304)
9596
res.send()
@@ -100,7 +101,12 @@ api.get('/get_submission', (req, res) => {
100101
let rows = getSubmissionStatement.all([req.query.ID])
101102
rows = rows.map(database_common.unpackDatabaseObject)
102103

103-
res.header("Cache-Control", "public, max-age=120")
104+
let cacheLifetime = 300
105+
if (rows.length != 0) {
106+
cacheLifetime = 86400
107+
}
108+
109+
res.header("Cache-Control", "public, max-age=" + cacheLifetime)
104110
res.header("ETag", etag)
105111
res.send(JSON.stringify(rows))
106112
}
@@ -116,19 +122,18 @@ api.get('/get_submission', (req, res) => {
116122

117123
const getAllSubmissionsStatement = db.prepare("SELECT * FROM Submissions")
118124
api.get('/get_all_submissions', (req, res) => {
119-
let etag = databaseModificationTime
125+
let etag = databaseLastModificationTime
120126
if (req.headers['if-none-match'] == etag) {
121127
res.status(304)
122128
res.send()
123129
return
124130
}
125131

126132
try {
127-
128133
let rows = getAllSubmissionsStatement.all()
129134
rows = rows.map(database_common.unpackDatabaseObject)
130-
res.header("Cache-Control", "public, max-age=120")
131-
res.header("ETag", databaseModificationTime)
135+
res.header("Cache-Control", "public, max-age=300")
136+
res.header("ETag", databaseLastModificationTime)
132137
res.send(JSON.stringify(rows))
133138
}
134139
catch (e) {
@@ -205,12 +210,12 @@ api.post('/post_submission', (req, res) => {
205210
let info = postSubmissionStatement.run(parameterList)
206211

207212
res.send("" + info.lastInsertRowid)
208-
databaseModificationTime = GetCurrentTimeAsString()
209-
latestReportID = info.lastInsertRowid
213+
databaseLastModificationTime = GetCurrentTimeAsString()
214+
latestReportID = Math.max(info.lastInsertRowid, latestReportID)
210215

211216
console.log(`Inserted submission ID ${info.lastInsertRowid} - ${newSubmission["DXGI_ADAPTER_DESC3.Description"]}`)
212217

213-
notification_handler.notify({
218+
let notification = {
214219
"embeds": [
215220
{
216221
"title": newSubmission["DXGI_ADAPTER_DESC3.Description"],
@@ -247,7 +252,17 @@ api.post('/post_submission', (req, res) => {
247252
}
248253
}
249254
]
250-
})
255+
}
256+
257+
if (process.env.SubmissionRemovalPassword) {
258+
notification.embeds[0].fields.push({
259+
"name": "Remove submission",
260+
"value": `[x](https://d3d12infodb.boolka.dev/remove_submission?ID=${info.lastInsertRowid}&password=${process.env.SubmissionRemovalPassword})`,
261+
"inline": true
262+
})
263+
}
264+
265+
notification_handler.notify(notification)
251266

252267
return
253268
}
@@ -261,6 +276,57 @@ api.post('/post_submission', (req, res) => {
261276
}
262277
})
263278

279+
if (process.env.SubmissionRemovalPassword) {
280+
const removeSubmissionStatement = db.prepare("DELETE FROM Submissions WHERE ID = ?")
281+
api.get('/remove_submission', (req, res) => {
282+
const password = req.query.password
283+
const ID = Number(req.query.ID)
284+
285+
if (password !== process.env.SubmissionRemovalPassword) {
286+
res.status(401)
287+
res.send('Unauthorized')
288+
console.log('Unauthorized removal attempt')
289+
return
290+
}
291+
292+
if (!ID) {
293+
res.status(400)
294+
res.send('ID is missing')
295+
console.log('Failed removal attempt. ID is missing.')
296+
return
297+
}
298+
299+
if (!Number.isInteger(ID) || ID < 1 || ID > latestReportID) {
300+
res.status(400)
301+
res.send('ID is not a valid number')
302+
console.log('Failed removal attempt. ID is not a valid number.')
303+
return
304+
}
305+
306+
try {
307+
const info = removeSubmissionStatement.run(ID)
308+
if (info.changes === 0) {
309+
res.status(404)
310+
res.send('Submission not found')
311+
console.log(`Failed removal attempt. Submission not found ID ${ID}.`)
312+
return
313+
}
314+
res.send('OK')
315+
databaseLastDeleteTime = GetCurrentTimeAsString()
316+
databaseLastModificationTime = databaseLastDeleteTime
317+
console.log(`Removed submission ID ${ID}`)
318+
}
319+
catch (e) {
320+
console.log('DB Error')
321+
console.log(e)
322+
323+
res.status(500)
324+
res.send('DB Error')
325+
return
326+
}
327+
})
328+
}
329+
264330
if (process.env.SSLKey && process.env.SSLCert) {
265331
console.log('Starting HTTPS Server')
266332
const options = {

0 commit comments

Comments
 (0)
Please sign in to comment.