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

237 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-07-01 16:05:30 +02:00
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'
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 || 200
})
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: 200
})
}
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: 200
})
}
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 = 204
) {
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
})
}
2020-07-07 10:57:04 +02:00
function deleteAbuse (url: string, token: string, abuseId: number, statusCodeExpected = 204) {
const path = '/api/v1/abuses/' + abuseId
2020-07-01 16:05:30 +02:00
return makeDeleteRequest({
url,
token,
path,
statusCodeExpected
})
}
2020-07-24 15:05:51 +02:00
function listAbuseMessages (url: string, token: string, abuseId: number, statusCodeExpected = 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 = 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 = 200) {
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
}