mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-23 21:00:38 +01:00
feat: logic for flag note deletion, #8499
This commit is contained in:
24
src/flags.js
24
src/flags.js
@@ -228,6 +228,21 @@ Flags.validate = async function (payload) {
|
||||
|
||||
Flags.getNotes = async function (flagId) {
|
||||
let notes = await db.getSortedSetRevRangeWithScores('flag:' + flagId + ':notes', 0, -1);
|
||||
notes = await modifyNotes(notes);
|
||||
return notes;
|
||||
};
|
||||
|
||||
Flags.getNote = async function (flagId, datetime) {
|
||||
let notes = await db.getSortedSetRangeByScoreWithScores('flag:' + flagId + ':notes', 0, 1, datetime, datetime);
|
||||
if (!notes.length) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
notes = await modifyNotes(notes);
|
||||
return notes[0];
|
||||
};
|
||||
|
||||
async function modifyNotes(notes) {
|
||||
const uids = [];
|
||||
notes = notes.map(function (note) {
|
||||
const noteObj = JSON.parse(note.value);
|
||||
@@ -245,6 +260,15 @@ Flags.getNotes = async function (flagId) {
|
||||
note.content = validator.escape(note.content);
|
||||
return note;
|
||||
});
|
||||
}
|
||||
|
||||
Flags.deleteNote = async function (flagId, datetime) {
|
||||
const note = await db.getSortedSetRangeByScore('flag:' + flagId + ':notes', 0, 1, datetime, datetime);
|
||||
if (!note.length) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
await db.sortedSetRemove('flag:' + flagId + ':notes', note[0]);
|
||||
};
|
||||
|
||||
Flags.create = async function (type, id, uid, reason, timestamp) {
|
||||
|
||||
@@ -62,4 +62,23 @@ SocketFlags.appendNote = async function (socket, data) {
|
||||
return { notes: notes, history: history };
|
||||
};
|
||||
|
||||
SocketFlags.deleteNote = async function (socket, data) {
|
||||
if (!data || !(data.flagId && data.datetime)) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
const note = await flags.getNote(data.flagId, data.datetime);
|
||||
if (note.uid !== socket.uid) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
await flags.deleteNote(data.flagId, data.datetime);
|
||||
|
||||
const [notes, history] = await Promise.all([
|
||||
flags.getNotes(data.flagId),
|
||||
flags.getHistory(data.flagId),
|
||||
]);
|
||||
return { notes: notes, history: history };
|
||||
};
|
||||
|
||||
require('../promisify')(SocketFlags);
|
||||
|
||||
Reference in New Issue
Block a user