feat: flag rescinding logic + api method

This commit is contained in:
Julian Lam
2023-10-11 12:06:33 -04:00
parent cc8fe4353d
commit e012edea3b
7 changed files with 62 additions and 1 deletions

View File

@@ -192,6 +192,8 @@ paths:
$ref: 'write/flags.yaml'
/flags/{flagId}:
$ref: 'write/flags/flagId.yaml'
/flags/{flagId}/report:
$ref: 'write/flags/flagId/report.yaml'
/flags/{flagId}/notes:
$ref: 'write/flags/flagId/notes.yaml'
/flags/{flagId}/notes/{datetime}:

View File

@@ -0,0 +1,26 @@
delete:
tags:
- flags
summary: rescind a flag report
description: This operation rescinds the report made for a given flag.
parameters:
- in: path
name: flagId
schema:
type: number
required: true
description: a valid flag id
example: 2
responses:
'200':
description: Flag report rescinded
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../components/schemas/Status.yaml#/Status
response:
type: object
properties: {}

View File

@@ -49,6 +49,16 @@ flagsApi.update = async (caller, data) => {
flagsApi.delete = async (_, { flagId }) => await flags.purge([flagId]);
flagsApi.rescind = async ({ uid }, { flagId }) => {
const { type, targetId } = await flags.get(flagId);
const exists = await flags.exists(type, targetId, uid);
if (!exists) {
throw new Error('[[error:no-flag]]');
}
await flags.rescindReport(type, targetId, uid);
};
flagsApi.appendNote = async (caller, data) => {
const allowed = await user.isPrivileged(caller.uid);
if (!allowed) {

View File

@@ -32,6 +32,11 @@ Flags.delete = async (req, res) => {
helpers.formatApiResponse(200, res);
};
Flags.rescind = async (req, res) => {
await api.flags.rescind(req, { flagId: req.params.flagId });
helpers.formatApiResponse(200, res);
};
Flags.appendNote = async (req, res) => {
const { note, datetime } = req.body;
const payload = await api.flags.appendNote(req, {

View File

@@ -109,7 +109,7 @@ Flags.get = async function (flagId) {
Flags.getReports(flagId),
]);
if (!base) {
return;
throw new Error('[[error:no-flag]]');
}
const flagObj = {
state: 'open',

View File

@@ -17,6 +17,8 @@ module.exports = function () {
setupApiRoute(router, 'put', '/:flagId', [...middlewares, middleware.assert.flag], controllers.write.flags.update);
setupApiRoute(router, 'delete', '/:flagId', [...middlewares, middleware.assert.flag], controllers.write.flags.delete);
setupApiRoute(router, 'delete', '/:flagId/report', middlewares, controllers.write.flags.rescind);
setupApiRoute(router, 'post', '/:flagId/notes', [...middlewares, middleware.assert.flag], controllers.write.flags.appendNote);
setupApiRoute(router, 'delete', '/:flagId/notes/:datetime', [...middlewares, middleware.assert.flag], controllers.write.flags.deleteNote);

View File

@@ -1004,6 +1004,22 @@ describe('Flags', () => {
});
});
describe('.rescind()', () => {
it('should remove a flag\'s report', async () => {
const response = await request({
method: 'delete',
uri: `${nconf.get('url')}/api/v3/flags/2/report`,
jar,
headers: {
'x-csrf-token': csrfToken,
},
resolveWithFullResponse: true,
});
assert.strictEqual(response.statusCode, 200);
});
});
describe('.appendNote()', () => {
it('should append a note to the flag', async () => {
const { response } = await request({