PeerTube/server/helpers/middlewares/abuses.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-08-15 09:53:26 +00:00
import { Response } from 'express'
2020-07-01 14:05:30 +00:00
import { AbuseModel } from '../../models/abuse/abuse'
import { fetchVideo } from '../video'
2019-07-23 08:40:39 +00:00
2020-07-01 14:05:30 +00:00
// FIXME: deprecated in 2.3. Remove this function
async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) {
2019-10-21 12:50:55 +00:00
const abuseId = parseInt(abuseIdArg + '', 10)
2020-07-01 14:05:30 +00:00
let abuse = await AbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID)
2020-07-01 14:05:30 +00:00
if (!abuse) {
const userId = res.locals.oauth?.token.User.id
const video = await fetchVideo(videoUUID, 'all', userId)
2020-07-01 14:05:30 +00:00
if (video) abuse = await AbuseModel.loadByIdAndVideoId(abuseId, video.id)
}
2019-07-23 08:40:39 +00:00
2020-07-01 14:05:30 +00:00
if (abuse === null) {
2019-07-23 08:40:39 +00:00
res.status(404)
2019-08-15 09:53:26 +00:00
.json({ error: 'Video abuse not found' })
2019-07-23 08:40:39 +00:00
return false
}
2020-07-01 14:05:30 +00:00
res.locals.abuse = abuse
2019-07-23 08:40:39 +00:00
return true
}
2019-08-15 09:53:26 +00:00
2020-07-07 08:57:04 +00:00
async function doesAbuseExist (abuseId: number | string, res: Response) {
const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10))
2020-07-01 14:05:30 +00:00
2020-07-07 08:57:04 +00:00
if (!abuse) {
res.status(404)
2020-07-08 13:51:46 +00:00
.json({ error: 'Abuse not found' })
2020-07-07 08:57:04 +00:00
return false
}
res.locals.abuse = abuse
return true
2020-07-01 14:05:30 +00:00
}
2019-08-15 09:53:26 +00:00
// ---------------------------------------------------------------------------
export {
2020-07-01 14:05:30 +00:00
doesAbuseExist,
2019-08-15 09:53:26 +00:00
doesVideoAbuseExist
}