Skip to content

Commit

Permalink
fix(api-filters): Add Filters API endpoints to API docs generation ZM…
Browse files Browse the repository at this point in the history
…S-121 (#611)

* added List all FIltes endpoint to api docs generation

* List Filters for a User endpoint added to API docs generation

* Request Filter information endpoint added to API docs generation. Add schemas

* fix description

* added Delete a Filter api endpoint to API docs generation

* New schemas for filter api endpoints. Added Create a mew Filter api endpoint to API docs generation

* fix schemas. Added Update Filter information endpoint to api docs generation
  • Loading branch information
NickOvt authored Feb 5, 2024
1 parent c7a1ab4 commit 95f829d
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 112 deletions.
332 changes: 221 additions & 111 deletions lib/api/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,55 @@ const tools = require('../tools');
const roles = require('../roles');
const { nextPageCursorSchema, previousPageCursorSchema, pageNrSchema, sessSchema, sessIPSchema, booleanSchema, metaDataSchema } = require('../schemas');
const { publish, FILTER_DELETED, FILTER_CREATED, FORWARD_ADDED } = require('../events');
const { successRes, totalRes, previousCursorRes, nextCursorRes } = require('../schemas/response/general-schemas');
const { GetAllFiltersResult, GetFiltersResult } = require('../schemas/response/filters-schemas');
const { FilterQuery, FilterAction } = require('../schemas/request/filters-schemas');
const { userId, filterId } = require('../schemas/request/general-schemas');

module.exports = (db, server, userHandler, settingsHandler) => {
server.get(
{ name: 'filters', path: '/filters' },
{
name: 'filters',
path: '/filters',
summary: 'List all Filters',
tags: ['Filters'],
validationObjs: {
requestBody: {},
queryParams: {
forward: Joi.string().trim().empty('').max(255).description('Partial match of a forward email address or URL'),
metaData: booleanSchema.description('If true, then includes metaData in the response'),
limit: Joi.number().default(20).min(1).max(250).description('How many records to return'),
next: nextPageCursorSchema,
previous: previousPageCursorSchema,
page: pageNrSchema,
sess: sessSchema,
ip: sessIPSchema
},
pathParams: {},
response: {
200: {
description: 'Success',
model: Joi.object({
success: successRes,
total: totalRes,
page: Joi.number().required().description('Current page number. Derived from page query argument.'),
previousCursor: previousCursorRes,
nextCursor: nextCursorRes,
results: Joi.array().items(GetAllFiltersResult).required().description('Address listing')
})
}
}
}
},
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');

const schema = Joi.object().keys({
forward: Joi.string().trim().empty('').max(255),
metaData: booleanSchema,
limit: Joi.number().default(20).min(1).max(250),
next: nextPageCursorSchema,
previous: previousPageCursorSchema,
page: pageNrSchema,
sess: sessSchema,
ip: sessIPSchema
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;

const schema = Joi.object({
...pathParams,
...requestBody,
...queryParams
});

const result = schema.validate(req.params, {
Expand Down Expand Up @@ -173,15 +206,46 @@ module.exports = (db, server, userHandler, settingsHandler) => {
);

server.get(
'/users/:user/filters',
{
path: '/users/:user/filters',
summary: 'List Filters for a User',
tags: ['Filters'],
validationObjs: {
requestBody: {},
queryParams: {
metaData: booleanSchema.description('If true, then includes metaData in the response'),
sess: sessSchema,
ip: sessIPSchema
},
pathParams: {
user: userId
},
response: {
200: {
description: 'Success',
model: Joi.object({
success: successRes,
limits: Joi.object({
allowed: Joi.number().description('How many filters are allowed'),
used: Joi.number().description('How many filters have been created')
})
.required()
.description('Filter usage limits for the user account'),
results: Joi.array().items(GetFiltersResult).required().description('Filter description')
})
}
}
}
},
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');

const schema = Joi.object().keys({
user: Joi.string().hex().lowercase().length(24).required(),
metaData: booleanSchema,
sess: sessSchema,
ip: sessIPSchema
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;

const schema = Joi.object({
...pathParams,
...requestBody,
...queryParams
});

const result = schema.validate(req.params, {
Expand Down Expand Up @@ -317,15 +381,46 @@ module.exports = (db, server, userHandler, settingsHandler) => {
);

server.get(
'/users/:user/filters/:filter',
{
path: '/users/:user/filters/:filter',
summary: 'Request Filter information',
tags: ['Filters'],
validationObjs: {
requestBody: {},
queryParams: {
sess: sessSchema,
ip: sessIPSchema
},
pathParams: {
user: userId,
filter: filterId
},
response: {
200: {
description: 'Success',
model: Joi.object({
success: successRes,
id: filterId,
name: Joi.string().required().description('Name for the filter'),
created: Joi.date().required().description('Datestring of the time the filter was created'),
query: FilterQuery.required(),
action: FilterAction.required(),
disabled: booleanSchema.required().description('If true, then this filter is ignored'),
metaData: Joi.object().description('Custom metadata value')
})
}
}
}
},
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');

const schema = Joi.object().keys({
user: Joi.string().hex().lowercase().length(24).required(),
filter: Joi.string().hex().lowercase().length(24).required(),
sess: sessSchema,
ip: sessIPSchema
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;

const schema = Joi.object({
...pathParams,
...requestBody,
...queryParams
});

const result = schema.validate(req.params, {
Expand Down Expand Up @@ -443,15 +538,39 @@ module.exports = (db, server, userHandler, settingsHandler) => {
);

server.del(
'/users/:user/filters/:filter',
{
path: '/users/:user/filters/:filter',
summary: 'Delete a Filter',
tags: ['Filters'],
validationObjs: {
requestBody: {},
queryParams: {
sess: sessSchema,
ip: sessIPSchema
},
pathParams: {
user: userId,
filter: filterId
},
response: {
200: {
description: 'Success',
model: Joi.object({
success: successRes
})
}
}
}
},
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');

const schema = Joi.object().keys({
user: Joi.string().hex().lowercase().length(24).required(),
filter: Joi.string().hex().lowercase().length(24).required(),
sess: sessSchema,
ip: sessIPSchema
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;

const schema = Joi.object({
...pathParams,
...requestBody,
...queryParams
});

const result = schema.validate(req.params, {
Expand Down Expand Up @@ -514,52 +633,48 @@ module.exports = (db, server, userHandler, settingsHandler) => {
);

server.post(
'/users/:user/filters',
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');
{
path: '/users/:user/filters',
summary: 'Create a new Filter',
tags: ['Filters'],
validationObjs: {
requestBody: {
name: Joi.string().trim().max(255).empty('').description('Name of the Filter'),

const schema = Joi.object().keys({
user: Joi.string().hex().lowercase().length(24).required(),
query: FilterQuery,
action: FilterAction,

name: Joi.string().trim().max(255).empty(''),
disabled: booleanSchema.default(false).description('If true then this filter is ignored'),

query: Joi.object()
.keys({
from: Joi.string().trim().max(255).empty(''),
to: Joi.string().trim().max(255).empty(''),
subject: Joi.string().trim().max(255).empty(''),
listId: Joi.string().trim().max(255).empty(''),
text: Joi.string().trim().max(255).empty(''),
ha: booleanSchema,
size: Joi.number().empty('')
})
.default({}),
action: Joi.object()
.keys({
seen: booleanSchema,
flag: booleanSchema,
delete: booleanSchema,
spam: booleanSchema,
mailbox: Joi.string().hex().lowercase().length(24).empty(''),
targets: Joi.array()
.items(
Joi.string().email({ tlds: false }),
Joi.string().uri({
scheme: [/smtps?/, /https?/],
allowRelative: false,
relativeOnly: false
})
)
.empty('')
})
.default({}),
metaData: metaDataSchema.label('metaData').description('Optional metadata, must be an object or JSON formatted string'),

disabled: booleanSchema.default(false),
sess: sessSchema,
ip: sessIPSchema
},
queryParams: {},
pathParams: {
user: userId
},
response: {
200: {
description: 'Success',
model: Joi.object({
success: successRes,
id: Joi.string().required().description('ID for the created filter')
})
}
}
}
},
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');

metaData: metaDataSchema.label('metaData'),
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;

sess: sessSchema,
ip: sessIPSchema
const schema = Joi.object({
...pathParams,
...requestBody,
...queryParams
});

const result = schema.validate(req.params, {
Expand Down Expand Up @@ -788,53 +903,48 @@ module.exports = (db, server, userHandler, settingsHandler) => {
);

server.put(
'/users/:user/filters/:filter',
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');
{
path: '/users/:user/filters/:filter',
summary: 'Update Filter information',
tags: ['Filters'],
validationObjs: {
requestBody: {
name: Joi.string().trim().max(255).empty('').description('Name of the Filter'),

const schema = Joi.object().keys({
user: Joi.string().hex().lowercase().length(24).required(),
filter: Joi.string().hex().lowercase().length(24).required(),

name: Joi.string().trim().max(255).empty(''),

query: Joi.object()
.keys({
from: Joi.string().trim().max(255).empty(''),
to: Joi.string().trim().max(255).empty(''),
subject: Joi.string().trim().max(255).empty(''),
listId: Joi.string().trim().max(255).empty(''),
text: Joi.string().trim().max(255).empty(''),
ha: booleanSchema,
size: Joi.number().empty('')
})
.default({}),
action: Joi.object()
.keys({
seen: booleanSchema,
flag: booleanSchema,
delete: booleanSchema,
spam: booleanSchema,
mailbox: Joi.string().hex().lowercase().length(24).empty(''),
targets: Joi.array()
.items(
Joi.string().email({ tlds: false }),
Joi.string().uri({
scheme: [/smtps?/, /https?/],
allowRelative: false,
relativeOnly: false
})
)
.empty('')
})
.default({}),
query: FilterQuery,
action: FilterAction,

disabled: booleanSchema.description('If true then this filter is ignored'),

disabled: booleanSchema,
metaData: metaDataSchema.label('metaData').description('Optional metadata, must be an object or JSON formatted string'),

sess: sessSchema,
ip: sessIPSchema
},
queryParams: {},
pathParams: {
user: userId,
filter: filterId
},
response: {
200: {
description: 'Success',
model: Joi.object({
success: successRes
})
}
}
}
},
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');

metaData: metaDataSchema.label('metaData'),
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;

sess: sessSchema,
ip: sessIPSchema
const schema = Joi.object({
...pathParams,
...requestBody,
...queryParams
});

const result = schema.validate(req.params, {
Expand Down
Loading

0 comments on commit 95f829d

Please sign in to comment.