]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/video-comments.ts
Basic video redundancy implementation
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / video-comments.ts
CommitLineData
6d852470 1import * as validator from 'validator'
5cf13500 2import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
66ee325f 3import { exists, isArray, isDateValid } from '../misc'
6d852470 4import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
6d852470
C
5
6function isVideoCommentCreateActivityValid (activity: any) {
7 return isBaseActivityValid(activity, 'Create') &&
5cf13500 8 sanitizeAndCheckVideoCommentObject(activity.object)
6d852470
C
9}
10
5cf13500
C
11function sanitizeAndCheckVideoCommentObject (comment: any) {
12 if (comment.type !== 'Note') return false
13
14 normalizeComment(comment)
15
16 return isActivityPubUrlValid(comment.id) &&
6d852470
C
17 isCommentContentValid(comment.content) &&
18 isActivityPubUrlValid(comment.inReplyTo) &&
19 isDateValid(comment.published) &&
66ee325f
C
20 isActivityPubUrlValid(comment.url) &&
21 isArray(comment.to) &&
20760d91
C
22 (
23 comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
24 comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
25 ) // Only accept public comments
6d852470
C
26}
27
4cb6d457
C
28function isVideoCommentDeleteActivityValid (activity: any) {
29 return isBaseActivityValid(activity, 'Delete')
30}
31
6d852470
C
32// ---------------------------------------------------------------------------
33
34export {
4cb6d457 35 isVideoCommentCreateActivityValid,
2ccaeeb3 36 isVideoCommentDeleteActivityValid,
5cf13500 37 sanitizeAndCheckVideoCommentObject
6d852470
C
38}
39
40// ---------------------------------------------------------------------------
41
6d852470
C
42function isCommentContentValid (content: any) {
43 return exists(content) && validator.isLength('' + content, { min: 1 })
44}
5cf13500
C
45
46function normalizeComment (comment: any) {
47 if (!comment) return
48
938d3fa0 49 if (typeof comment.url !== 'string') {
5cf13500
C
50 comment.url = comment.url.href || comment.url.url
51 }
52
53 return
54}