add error codes and deletion route

This commit is contained in:
red binder 2026-08-09 01:37:15 +02:00
commit e3b5aa1430

View file

@ -22,7 +22,7 @@ async function authenticateUser(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Missing or malformed Authorization header' });
return res.status(401).json({ code: "0100", error: 'Missing or malformed Authorization header' });
}
const token = authHeader.split(' ')[1];
try {
@ -46,12 +46,12 @@ async function authenticateUser(req, res, next) {
function authenticateServer(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Missing or malformed Authorization header' });
return res.status(404).json({ error: 'Not found' });
}
const token = authHeader.split(' ')[1];
if (token !== SERVER_STATIC_TOKEN) {
return res.status(403).json({ error: 'Forbidden: Invalid server token' });
return res.status(404).json({ error: 'Not found' });
}
next();
}
@ -83,7 +83,7 @@ app.get('/api/v1/@me', authenticateUser, async (req, res) => {
return res.status(200).json(responseData);
} catch (err) {
return res.status(500).json({ code: "0402", error: 'Internal server error' });
return res.status(500).json({ code: "0200", error: 'Internal server error' });
}
});
@ -93,7 +93,7 @@ app.get('/api/v1/presence/:pid', authenticateUser, async (req, res) => {
const userPid = req.user.pid;
if (isNaN(targetPid)) {
return res.status(400).json({ error: 'Invalid target PID' });
return res.status(400).json({ code: "0300", error: 'Invalid target PID' });
}
try {
@ -106,7 +106,7 @@ app.get('/api/v1/presence/:pid', authenticateUser, async (req, res) => {
const checkResult = await pool.query(friendshipCheck, [userPid, targetPid]);
if (!checkResult.rows[0].is_friend) {
return res.status(403).json({ error: 'Request denied: target user is not in your friends list' });
return res.status(403).json({ code: "0301", error: 'Request denied: target user is not in your friends list' });
}
const presenceQuery = `SELECT presence, updated_at FROM user_presences WHERE pid = $1;`;
@ -122,7 +122,7 @@ app.get('/api/v1/presence/:pid', authenticateUser, async (req, res) => {
last_updated: presenceResult.rows[0].updated_at
});
} catch (err) {
return res.status(500).json({ error: 'Internal server error' });
return res.status(500).json({ code: "0302", error: 'Internal server error' });
}
});
@ -147,6 +147,22 @@ app.post('/api/v1/presence', authenticateServer, async (req, res) => {
}
});
app.delete('/api/v1/presence/:pid', authenticateServer, async (req, res) => {
const targetPid = parseInt(req.params.pid, 10);
if (isNaN(targetPid)) {
return res.status(400).json({ error: 'Invalid PID' });
}
try {
await pool.query('DELETE FROM user_presences WHERE pid = $1;', [targetPid]);
return res.status(200).json({ status: 'deleted', pid: targetPid });
} catch (err) {
console.error('db error in DELETE /api/v1/presence/:pid:', err);
return res.status(500).json({ error: 'Internal server error' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`running on port ${PORT}`);