Skip to content

Commit

Permalink
switch from object to array predefinedReasons
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelk committed Jun 18, 2020
1 parent 5f1bdab commit b65ff55
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions client/src/app/shared/video/modals/video-report.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class VideoReportComponent extends FormReactive implements OnInit {
@ViewChild('modal', { static: true }) modal: NgbModal

error: string = null
predefinedReasons: { id: keyof VideoAbusePredefinedReasonsIn, label: string, description?: string, help?: string }[] = []
predefinedReasons: { id: VideoAbusePredefinedReasonsIn, label: string, description?: string, help?: string }[] = []
embedHtml: SafeHtml

private openedModal: NgbModalRef
Expand Down Expand Up @@ -138,7 +138,7 @@ export class VideoReportComponent extends FormReactive implements OnInit {

report () {
const reason = this.form.get('reason').value
const predefinedReasons = pickBy(this.form.get('predefinedReasons').value)
const predefinedReasons = keys(pickBy(this.form.get('predefinedReasons').value)) as VideoAbusePredefinedReasonsIn[]
const { hasStart, startAt, hasEnd, endAt } = this.form.get('timestamp').value

this.videoAbuseService.reportVideo({
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/api/videos/abuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) {

const videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
reporterAccount = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
const predefinedReasons = keys(pickBy(body.predefinedReasons)).map(r => VideoAbusePredefinedReasonsIn[r])
const predefinedReasons = body.predefinedReasons.map(r => VideoAbusePredefinedReasonsIn[r])

const abuseToCreate = {
reporterAccountId: reporterAccount.id,
Expand Down
6 changes: 4 additions & 2 deletions server/helpers/custom-validators/video-abuses.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import validator from 'validator'
import { keys } from 'lodash'

import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
import { exists } from './misc'
import { VideoAbuseVideoIs } from '@shared/models/videos/abuse/video-abuse-video-is.type'
import { VideoAbusePredefinedReasonsIn } from '@shared/models/videos/abuse/video-abuse-reason.model'

const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES

function isVideoAbuseReasonValid (value: string) {
return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
}

function isVideoAbusePredefinedReasonsValid (value: {}) {
return exists(value)
function isVideoAbusePredefinedReasonsValid (value: VideoAbusePredefinedReasonsIn[]) {
return exists(value) && value.every(element => keys(VideoAbusePredefinedReasonsIn).includes(element))
}

function isVideoAbuseModerationCommentValid (value: string) {
Expand Down
3 changes: 1 addition & 2 deletions server/middlewares/validators/videos/video-abuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
isVideoAbuseModerationCommentValid,
isVideoAbuseReasonValid,
isVideoAbuseStateValid,
isVideoAbusePredefinedReasonsValid,
isVideoAbuseTimestampValid
isVideoAbusePredefinedReasonsValid
} from '../../../helpers/custom-validators/video-abuses'
import { logger } from '../../../helpers/logger'
import { doesVideoAbuseExist, doesVideoExist } from '../../../helpers/middlewares'
Expand Down
4 changes: 3 additions & 1 deletion shared/models/videos/abuse/video-abuse-create.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { VideoAbusePredefinedReasonsIn } from './video-abuse-reason.model'

export interface VideoAbuseCreate {
reason: string
predefinedReasons?: {[key: string]: boolean} // see VideoAbusePredefinedReasonsIn
predefinedReasons?: VideoAbusePredefinedReasonsIn[]
startAt?: number
endAt?: number
}
21 changes: 10 additions & 11 deletions shared/models/videos/abuse/video-abuse-reason.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ export enum VideoAbusePredefinedReasons {
CAPTIONS
}

export interface VideoAbusePredefinedReasonsIn {
violentOrRepulsive: VideoAbusePredefinedReasons
hatefulOrAbusive: VideoAbusePredefinedReasons
spamOrMisleading: VideoAbusePredefinedReasons
privacy: VideoAbusePredefinedReasons
rights: VideoAbusePredefinedReasons
serverRules: VideoAbusePredefinedReasons
thumbnails: VideoAbusePredefinedReasons
captions: VideoAbusePredefinedReasons
}
export type VideoAbusePredefinedReasonsIn =
'violentOrRepulsive' |
'hatefulOrAbusive' |
'spamOrMisleading' |
'privacy' |
'rights' |
'serverRules' |
'thumbnails' |
'captions'

export const VideoAbusePredefinedReasonsIn: VideoAbusePredefinedReasonsIn = {
export const VideoAbusePredefinedReasonsIn = {
violentOrRepulsive: VideoAbusePredefinedReasons.VIOLENT_OR_REPULSIVE,
hatefulOrAbusive: VideoAbusePredefinedReasons.HATEFUL_OR_ABUSIVE,
spamOrMisleading: VideoAbusePredefinedReasons.SPAM_OR_MISLEADING,
Expand Down

0 comments on commit b65ff55

Please sign in to comment.