PeerTube/shared/extra-utils/moderation/abuses.ts

245 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-07-07 10:57:04 +02:00
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'
2020-07-07 10:57:04 +02:00
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'
2021-02-03 09:33:05 +01:00
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
2020-07-07 10:57:04 +02:00
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
2020-07-07 10:57:04 +02:00
})
2020-07-01 16:05:30 +02:00
}
2020-07-24 15:05:51 +02:00
function getAdminAbusesList (options: {
2020-07-01 16:05:30 +02:00
url: string
token: string
2020-07-08 15:51:46 +02:00
start?: number
count?: number
sort?: string
2020-07-01 16:05:30 +02:00
id?: number
predefinedReason?: AbusePredefinedReasonsString
search?: string
2020-07-08 15:51:46 +02:00
filter?: AbuseFilter
2020-07-01 16:05:30 +02:00
state?: AbuseState
videoIs?: AbuseVideoIs
searchReporter?: string
searchReportee?: string
searchVideo?: string
searchVideoChannel?: string
}) {
const {
url,
token,
2020-07-08 15:51:46 +02:00
start,
count,
sort,
2020-07-01 16:05:30 +02:00
id,
predefinedReason,
search,
2020-07-07 10:57:04 +02:00
filter,
2020-07-01 16:05:30 +02:00
state,
videoIs,
searchReporter,
searchReportee,
searchVideo,
searchVideoChannel
} = options
2020-07-07 10:57:04 +02:00
const path = '/api/v1/abuses'
2020-07-01 16:05:30 +02:00
const query = {
id,
predefinedReason,
search,
state,
2020-07-07 10:57:04 +02:00
filter,
2020-07-01 16:05:30 +02:00
videoIs,
2020-07-08 15:51:46 +02:00
start,
count,
sort: sort || 'createdAt',
2020-07-01 16:05:30 +02:00
searchReporter,
searchReportee,
searchVideo,
searchVideoChannel
}
return makeGetRequest({
url,
path,
token,
query,
statusCodeExpected: HttpStatusCode.OK_200
2020-07-01 16:05:30 +02:00
})
}
2020-07-24 15:05:51 +02:00
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
2020-07-24 15:05:51 +02:00
})
}
2020-07-01 16:05:30 +02:00
function updateAbuse (
url: string,
token: string,
2020-07-07 10:57:04 +02:00
abuseId: number,
2020-07-01 16:05:30 +02:00
body: AbuseUpdate,
statusCodeExpected = HttpStatusCode.NO_CONTENT_204
2020-07-01 16:05:30 +02:00
) {
2020-07-07 10:57:04 +02:00
const path = '/api/v1/abuses/' + abuseId
2020-07-01 16:05:30 +02:00
return makePutBodyRequest({
url,
token,
path,
fields: body,
statusCodeExpected
})
}
function deleteAbuse (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
2020-07-07 10:57:04 +02:00
const path = '/api/v1/abuses/' + abuseId
2020-07-01 16:05:30 +02:00
return makeDeleteRequest({
url,
token,
path,
statusCodeExpected
})
}
function listAbuseMessages (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.OK_200) {
2020-07-24 15:05:51 +02:00
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
) {
2020-07-24 15:05:51 +02:00
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) {
2020-07-24 15:05:51 +02:00
const path = '/api/v1/abuses/' + abuseId + '/messages'
return makePostBodyRequest({
url,
token,
path,
fields: { message },
statusCodeExpected
})
}
2020-07-01 16:05:30 +02:00
// ---------------------------------------------------------------------------
export {
reportAbuse,
2020-07-24 15:05:51 +02:00
getAdminAbusesList,
2020-07-01 16:05:30 +02:00
updateAbuse,
2020-07-24 15:05:51 +02:00
deleteAbuse,
getUserAbusesList,
listAbuseMessages,
deleteAbuseMessage,
addAbuseMessage
2020-07-01 16:05:30 +02:00
}