From 0c1a77e9ccf915184c431145a8b326d4ce271b46 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 12:01:59 +0200 Subject: Introduce abuse command --- shared/extra-utils/index.ts | 2 +- shared/extra-utils/moderation/abuses-command.ts | 205 ++++++++++++++++++++ shared/extra-utils/moderation/abuses.ts | 244 ------------------------ shared/extra-utils/moderation/index.ts | 1 + shared/extra-utils/server/servers.ts | 3 + shared/extra-utils/shared/abstract-command.ts | 6 +- 6 files changed, 215 insertions(+), 246 deletions(-) create mode 100644 shared/extra-utils/moderation/abuses-command.ts delete mode 100644 shared/extra-utils/moderation/abuses.ts create mode 100644 shared/extra-utils/moderation/index.ts (limited to 'shared') diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 7d2887209..bde269052 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -5,8 +5,8 @@ export * from './feeds' export * from './logs' export * from './miscs' export * from './mock-servers' +export * from './moderation' -export * from './moderation/abuses' export * from './plugins/mock-blocklist' export * from './requests/check-api-params' diff --git a/shared/extra-utils/moderation/abuses-command.ts b/shared/extra-utils/moderation/abuses-command.ts new file mode 100644 index 000000000..59126d0a9 --- /dev/null +++ b/shared/extra-utils/moderation/abuses-command.ts @@ -0,0 +1,205 @@ +import { pick } from 'lodash' +import { + AbuseFilter, + AbuseMessage, + AbusePredefinedReasonsString, + AbuseState, + AbuseUpdate, + AbuseVideoIs, + AdminAbuse, + ResultList, + UserAbuse +} from '@shared/models' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { AbstractCommand, OverrideCommandOptions } from '../shared' +import { unwrapBody } from '../requests/requests' + +export class AbusesCommand extends AbstractCommand { + + report (options: OverrideCommandOptions & { + reason: string + + accountId?: number + videoId?: number + commentId?: number + + predefinedReasons?: AbusePredefinedReasonsString[] + + startAt?: number + endAt?: number + }) { + const path = '/api/v1/abuses' + + const video = options.videoId + ? { + id: options.videoId, + startAt: options.startAt, + endAt: options.endAt + } + : undefined + + const comment = options.commentId + ? { id: options.commentId } + : undefined + + const account = options.accountId + ? { id: options.accountId } + : undefined + + const body = { + account, + video, + comment, + + reason: options.reason, + predefinedReasons: options.predefinedReasons + } + + return unwrapBody<{ abuse: { id: number } }>(this.postBodyRequest({ + ...options, + + path, + fields: body, + defaultExpectedStatus: HttpStatusCode.OK_200 + })) + } + + getAdminList (options: OverrideCommandOptions & { + start?: number + count?: number + sort?: string + + id?: number + predefinedReason?: AbusePredefinedReasonsString + search?: string + filter?: AbuseFilter + state?: AbuseState + videoIs?: AbuseVideoIs + searchReporter?: string + searchReportee?: string + searchVideo?: string + searchVideoChannel?: string + } = {}) { + const toPick = [ + 'count', + 'filter', + 'id', + 'predefinedReason', + 'search', + 'searchReportee', + 'searchReporter', + 'searchVideo', + 'searchVideoChannel', + 'sort', + 'start', + 'state', + 'videoIs' + ] + + const path = '/api/v1/abuses' + + const defaultQuery = { sort: 'createdAt' } + const query = { ...defaultQuery, ...pick(options, toPick) } + + return this.getRequestBody>({ + ...options, + + path, + query, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + getUserList (options: OverrideCommandOptions & { + start?: number + count?: number + sort?: string + + id?: number + search?: string + state?: AbuseState + }) { + const toPick = [ + 'id', + 'search', + 'state', + 'start', + 'count', + 'sort' + ] + + const path = '/api/v1/users/me/abuses' + + const defaultQuery = { sort: 'createdAt' } + const query = { ...defaultQuery, ...pick(options, toPick) } + + return this.getRequestBody>({ + ...options, + + path, + query, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + update (options: OverrideCommandOptions & { + abuseId: number + body: AbuseUpdate + }) { + const { abuseId, body } = options + const path = '/api/v1/abuses/' + abuseId + + return this.putBodyRequest({ + ...options, + + path, + fields: body, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + delete (options: OverrideCommandOptions & { + abuseId: number + }) { + const { abuseId } = options + const path = '/api/v1/abuses/' + abuseId + + return this.deleteRequest({ ...options, path, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) + } + + listMessages (options: OverrideCommandOptions & { + abuseId: number + }) { + const { abuseId } = options + const path = '/api/v1/abuses/' + abuseId + '/messages' + + return this.getRequestBody>({ ...options, path, defaultExpectedStatus: HttpStatusCode.OK_200 }) + } + + deleteMessage (options: OverrideCommandOptions & { + abuseId: number + messageId: number + }) { + const { abuseId, messageId } = options + const path = '/api/v1/abuses/' + abuseId + '/messages/' + messageId + + return this.deleteRequest({ ...options, path, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) + } + + addMessage (options: OverrideCommandOptions & { + abuseId: number + message: string + }) { + const { abuseId, message } = options + const path = '/api/v1/abuses/' + abuseId + '/messages' + + return this.postBodyRequest({ + ...options, + + path, + fields: { message }, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + +} diff --git a/shared/extra-utils/moderation/abuses.ts b/shared/extra-utils/moderation/abuses.ts deleted file mode 100644 index c0fda722f..000000000 --- a/shared/extra-utils/moderation/abuses.ts +++ /dev/null @@ -1,244 +0,0 @@ -import { AbuseFilter, AbusePredefinedReasonsString, AbuseState, AbuseUpdate, AbuseVideoIs } from '@shared/models' -import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -function reportAbuse (options: { - url: string - token: string - - reason: string - - accountId?: number - videoId?: number - commentId?: number - - predefinedReasons?: AbusePredefinedReasonsString[] - - startAt?: number - endAt?: number - - statusCodeExpected?: number -}) { - const path = '/api/v1/abuses' - - const video = options.videoId - ? { - id: options.videoId, - startAt: options.startAt, - endAt: options.endAt - } - : undefined - - const comment = options.commentId - ? { id: options.commentId } - : undefined - - const account = options.accountId - ? { id: options.accountId } - : undefined - - const body = { - account, - video, - comment, - - reason: options.reason, - predefinedReasons: options.predefinedReasons - } - - return makePostBodyRequest({ - url: options.url, - path, - token: options.token, - - fields: body, - statusCodeExpected: options.statusCodeExpected || HttpStatusCode.OK_200 - }) -} - -function getAdminAbusesList (options: { - url: string - token: string - - start?: number - count?: number - sort?: string - - id?: number - predefinedReason?: AbusePredefinedReasonsString - search?: string - filter?: AbuseFilter - state?: AbuseState - videoIs?: AbuseVideoIs - searchReporter?: string - searchReportee?: string - searchVideo?: string - searchVideoChannel?: string -}) { - const { - url, - token, - start, - count, - sort, - id, - predefinedReason, - search, - filter, - state, - videoIs, - searchReporter, - searchReportee, - searchVideo, - searchVideoChannel - } = options - const path = '/api/v1/abuses' - - const query = { - id, - predefinedReason, - search, - state, - filter, - videoIs, - start, - count, - sort: sort || 'createdAt', - searchReporter, - searchReportee, - searchVideo, - searchVideoChannel - } - - return makeGetRequest({ - url, - path, - token, - query, - statusCodeExpected: HttpStatusCode.OK_200 - }) -} - -function getUserAbusesList (options: { - url: string - token: string - - start?: number - count?: number - sort?: string - - id?: number - search?: string - state?: AbuseState -}) { - const { - url, - token, - start, - count, - sort, - id, - search, - state - } = options - const path = '/api/v1/users/me/abuses' - - const query = { - id, - search, - state, - start, - count, - sort: sort || 'createdAt' - } - - return makeGetRequest({ - url, - path, - token, - query, - statusCodeExpected: HttpStatusCode.OK_200 - }) -} - -function updateAbuse ( - url: string, - token: string, - abuseId: number, - body: AbuseUpdate, - statusCodeExpected = HttpStatusCode.NO_CONTENT_204 -) { - const path = '/api/v1/abuses/' + abuseId - - return makePutBodyRequest({ - url, - token, - path, - fields: body, - statusCodeExpected - }) -} - -function deleteAbuse (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { - const path = '/api/v1/abuses/' + abuseId - - return makeDeleteRequest({ - url, - token, - path, - statusCodeExpected - }) -} - -function listAbuseMessages (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.OK_200) { - const path = '/api/v1/abuses/' + abuseId + '/messages' - - return makeGetRequest({ - url, - token, - path, - statusCodeExpected - }) -} - -function deleteAbuseMessage ( - url: string, - token: string, - abuseId: number, - messageId: number, - statusCodeExpected = HttpStatusCode.NO_CONTENT_204 -) { - const path = '/api/v1/abuses/' + abuseId + '/messages/' + messageId - - return makeDeleteRequest({ - url, - token, - path, - statusCodeExpected - }) -} - -function addAbuseMessage (url: string, token: string, abuseId: number, message: string, statusCodeExpected = HttpStatusCode.OK_200) { - const path = '/api/v1/abuses/' + abuseId + '/messages' - - return makePostBodyRequest({ - url, - token, - path, - fields: { message }, - statusCodeExpected - }) -} - -// --------------------------------------------------------------------------- - -export { - reportAbuse, - getAdminAbusesList, - updateAbuse, - deleteAbuse, - getUserAbusesList, - listAbuseMessages, - deleteAbuseMessage, - addAbuseMessage -} diff --git a/shared/extra-utils/moderation/index.ts b/shared/extra-utils/moderation/index.ts new file mode 100644 index 000000000..b37643956 --- /dev/null +++ b/shared/extra-utils/moderation/index.ts @@ -0,0 +1 @@ +export * from './abuses-command' diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 4343eab93..f05f0dbbe 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -12,6 +12,7 @@ import { CustomPagesCommand } from '../custom-pages' import { FeedCommand } from '../feeds' import { LogsCommand } from '../logs' import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' +import { AbusesCommand } from '../moderation' import { makeGetRequest } from '../requests/requests' interface ServerInfo { @@ -71,6 +72,7 @@ interface ServerInfo { customPageCommand?: CustomPagesCommand feedCommand?: FeedCommand logsCommand?: LogsCommand + abusesCommand?: AbusesCommand } function parallelTests () { @@ -281,6 +283,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.customPageCommand = new CustomPagesCommand(server) server.feedCommand = new FeedCommand(server) server.logsCommand = new LogsCommand(server) + server.abusesCommand = new AbusesCommand(server) res(server) }) diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index a57222216..3ee5cd865 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -1,5 +1,5 @@ import { HttpStatusCode } from '@shared/core-utils' -import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrapBody, unwrapText } from '../requests/requests' +import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrapBody, unwrapText } from '../requests/requests' import { ServerInfo } from '../server/servers' export interface OverrideCommandOptions { @@ -44,6 +44,10 @@ abstract class AbstractCommand { return unwrapText(this.getRequest(options)) } + protected deleteRequest (options: CommonCommandOptions) { + return makeDeleteRequest(this.buildCommonRequestOptions(options)) + } + protected putBodyRequest (options: CommonCommandOptions & { fields?: { [ fieldName: string ]: any } }) { -- cgit v1.2.3