]>
Commit | Line | Data |
---|---|---|
1 | import validator from 'validator' | |
2 | import { ACTIVITY_PUB } from '../../../initializers/constants' | |
3 | import { exists, isArray, isDateValid } from '../misc' | |
4 | import { isActivityPubUrlValid } from './misc' | |
5 | ||
6 | function sanitizeAndCheckVideoCommentObject (comment: any) { | |
7 | if (!comment) return false | |
8 | ||
9 | if (!isCommentTypeValid(comment)) return false | |
10 | ||
11 | normalizeComment(comment) | |
12 | ||
13 | if (comment.type === 'Tombstone') { | |
14 | return isActivityPubUrlValid(comment.id) && | |
15 | isDateValid(comment.published) && | |
16 | isDateValid(comment.deleted) && | |
17 | isActivityPubUrlValid(comment.url) | |
18 | } | |
19 | ||
20 | return isActivityPubUrlValid(comment.id) && | |
21 | isCommentContentValid(comment.content) && | |
22 | isActivityPubUrlValid(comment.inReplyTo) && | |
23 | isDateValid(comment.published) && | |
24 | isActivityPubUrlValid(comment.url) && | |
25 | isArray(comment.to) && | |
26 | ( | |
27 | comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 || | |
28 | comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 | |
29 | ) // Only accept public comments | |
30 | } | |
31 | ||
32 | // --------------------------------------------------------------------------- | |
33 | ||
34 | export { | |
35 | sanitizeAndCheckVideoCommentObject | |
36 | } | |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | function isCommentContentValid (content: any) { | |
41 | return exists(content) && validator.isLength('' + content, { min: 1 }) | |
42 | } | |
43 | ||
44 | function normalizeComment (comment: any) { | |
45 | if (!comment) return | |
46 | ||
47 | if (typeof comment.url !== 'string') { | |
48 | if (typeof comment.url === 'object') comment.url = comment.url.href || comment.url.url | |
49 | else comment.url = comment.id | |
50 | } | |
51 | } | |
52 | ||
53 | function isCommentTypeValid (comment: any): boolean { | |
54 | if (comment.type === 'Note') return true | |
55 | ||
56 | if (comment.type === 'Tombstone' && comment.formerType === 'Note') return true | |
57 | ||
58 | return false | |
59 | } |