* feat: new routes for flags API

+ flag get
+ flag creation, migration from socket method
+ flag update, migration from socket method
* fixed bug where you could not unassign someone from a flag

* feat: tests for new flags API

added missing files for schema update

* fix: flag tests to use Write API instead of sockets

* feat: flag notes API + tests

* chore: remove debug line

* test: fix breaking test on mongo
This commit is contained in:
Julian Lam
2021-07-16 13:44:42 -04:00
committed by GitHub
parent 71bc258731
commit cc6cbfcdc4
23 changed files with 752 additions and 331 deletions

80
src/api/flags.js Normal file
View File

@@ -0,0 +1,80 @@
'use strict';
const user = require('../user');
const flags = require('../flags');
const flagsApi = module.exports;
flagsApi.create = async (caller, data) => {
const required = ['type', 'id', 'reason'];
if (!required.every(prop => !!data[prop])) {
throw new Error('[[error:invalid-data]]');
}
const { type, id, reason } = data;
await flags.validate({
uid: caller.uid,
type: type,
id: id,
});
const flagObj = await flags.create(type, id, caller.uid, reason);
flags.notify(flagObj, caller.uid);
return flagObj;
};
flagsApi.update = async (caller, data) => {
const allowed = await user.isPrivileged(caller.uid);
if (!allowed) {
throw new Error('[[no-privileges]]');
}
const { flagId } = data;
delete data.flagId;
await flags.update(flagId, caller.uid, data);
return await flags.getHistory(flagId);
};
flagsApi.appendNote = async (caller, data) => {
const allowed = await user.isPrivileged(caller.uid);
if (!allowed) {
throw new Error('[[error:no-privileges]]');
}
if (data.datetime && data.flagId) {
try {
const note = await flags.getNote(data.flagId, data.datetime);
if (note.uid !== caller.uid) {
throw new Error('[[error:no-privileges]]');
}
} catch (e) {
// Okay if not does not exist in database
if (!e.message === '[[error:invalid-data]]') {
throw e;
}
}
}
await flags.appendNote(data.flagId, caller.uid, data.note, data.datetime);
const [notes, history] = await Promise.all([
flags.getNotes(data.flagId),
flags.getHistory(data.flagId),
]);
return { notes: notes, history: history };
};
flagsApi.deleteNote = async (caller, data) => {
const note = await flags.getNote(data.flagId, data.datetime);
if (note.uid !== caller.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 };
};

View File

@@ -6,4 +6,5 @@ module.exports = {
topics: require('./topics'),
posts: require('./posts'),
categories: require('./categories'),
flags: require('./flags'),
};