diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-20 09:43:39 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | 54141398354e6e7b94aa3065a705a1251390111c (patch) | |
tree | 8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/helpers/custom-validators | |
parent | eb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff) | |
download | PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip |
Refractor activity pub lib/helpers
Diffstat (limited to 'server/helpers/custom-validators')
6 files changed, 133 insertions, 62 deletions
diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts index 8084cf7b0..9305e092c 100644 --- a/server/helpers/custom-validators/activitypub/activity.ts +++ b/server/helpers/custom-validators/activitypub/activity.ts | |||
@@ -1,11 +1,11 @@ | |||
1 | import * as validator from 'validator' | 1 | import * as validator from 'validator' |
2 | import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' | ||
2 | import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account' | 3 | import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account' |
4 | import { isAnnounceValid } from './announce' | ||
3 | import { isActivityPubUrlValid } from './misc' | 5 | import { isActivityPubUrlValid } from './misc' |
6 | import { isUndoValid } from './undo' | ||
7 | import { isVideoChannelCreateActivityValid, isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels' | ||
4 | import { | 8 | import { |
5 | isAnnounceValid, | ||
6 | isVideoChannelCreateActivityValid, | ||
7 | isVideoChannelDeleteActivityValid, | ||
8 | isVideoChannelUpdateActivityValid, | ||
9 | isVideoFlagValid, | 9 | isVideoFlagValid, |
10 | isVideoTorrentAddActivityValid, | 10 | isVideoTorrentAddActivityValid, |
11 | isVideoTorrentDeleteActivityValid, | 11 | isVideoTorrentDeleteActivityValid, |
@@ -25,18 +25,23 @@ function isRootActivityValid (activity: any) { | |||
25 | ) | 25 | ) |
26 | } | 26 | } |
27 | 27 | ||
28 | const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean } = { | ||
29 | Create: checkCreateActivity, | ||
30 | Add: checkAddActivity, | ||
31 | Update: checkUpdateActivity, | ||
32 | Delete: checkDeleteActivity, | ||
33 | Follow: checkFollowActivity, | ||
34 | Accept: checkAcceptActivity, | ||
35 | Announce: checkAnnounceActivity, | ||
36 | Undo: checkUndoActivity | ||
37 | } | ||
38 | |||
28 | function isActivityValid (activity: any) { | 39 | function isActivityValid (activity: any) { |
29 | return isVideoTorrentAddActivityValid(activity) || | 40 | const checker = activityCheckers[activity.type] |
30 | isVideoChannelCreateActivityValid(activity) || | 41 | // Unknown activity type |
31 | isVideoTorrentUpdateActivityValid(activity) || | 42 | if (!checker) return false |
32 | isVideoChannelUpdateActivityValid(activity) || | 43 | |
33 | isVideoTorrentDeleteActivityValid(activity) || | 44 | return checker(activity) |
34 | isVideoChannelDeleteActivityValid(activity) || | ||
35 | isAccountDeleteActivityValid(activity) || | ||
36 | isAccountFollowActivityValid(activity) || | ||
37 | isAccountAcceptActivityValid(activity) || | ||
38 | isVideoFlagValid(activity) || | ||
39 | isAnnounceValid(activity) | ||
40 | } | 45 | } |
41 | 46 | ||
42 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
@@ -45,3 +50,41 @@ export { | |||
45 | isRootActivityValid, | 50 | isRootActivityValid, |
46 | isActivityValid | 51 | isActivityValid |
47 | } | 52 | } |
53 | |||
54 | // --------------------------------------------------------------------------- | ||
55 | |||
56 | function checkCreateActivity (activity: any) { | ||
57 | return isVideoChannelCreateActivityValid(activity) || | ||
58 | isVideoFlagValid(activity) | ||
59 | } | ||
60 | |||
61 | function checkAddActivity (activity: any) { | ||
62 | return isVideoTorrentAddActivityValid(activity) | ||
63 | } | ||
64 | |||
65 | function checkUpdateActivity (activity: any) { | ||
66 | return isVideoTorrentUpdateActivityValid(activity) || | ||
67 | isVideoChannelUpdateActivityValid(activity) | ||
68 | } | ||
69 | |||
70 | function checkDeleteActivity (activity: any) { | ||
71 | return isVideoTorrentDeleteActivityValid(activity) || | ||
72 | isVideoChannelDeleteActivityValid(activity) || | ||
73 | isAccountDeleteActivityValid(activity) | ||
74 | } | ||
75 | |||
76 | function checkFollowActivity (activity: any) { | ||
77 | return isAccountFollowActivityValid(activity) | ||
78 | } | ||
79 | |||
80 | function checkAcceptActivity (activity: any) { | ||
81 | return isAccountAcceptActivityValid(activity) | ||
82 | } | ||
83 | |||
84 | function checkAnnounceActivity (activity: any) { | ||
85 | return isAnnounceValid(activity) | ||
86 | } | ||
87 | |||
88 | function checkUndoActivity (activity: any) { | ||
89 | return isUndoValid(activity) | ||
90 | } | ||
diff --git a/server/helpers/custom-validators/activitypub/announce.ts b/server/helpers/custom-validators/activitypub/announce.ts new file mode 100644 index 000000000..4ba99d1ea --- /dev/null +++ b/server/helpers/custom-validators/activitypub/announce.ts | |||
@@ -0,0 +1,15 @@ | |||
1 | import { isBaseActivityValid } from './misc' | ||
2 | import { isVideoTorrentAddActivityValid } from './videos' | ||
3 | import { isVideoChannelCreateActivityValid } from './video-channels' | ||
4 | |||
5 | function isAnnounceValid (activity: any) { | ||
6 | return isBaseActivityValid(activity, 'Announce') && | ||
7 | ( | ||
8 | isVideoChannelCreateActivityValid(activity.object) || | ||
9 | isVideoTorrentAddActivityValid(activity.object) | ||
10 | ) | ||
11 | } | ||
12 | |||
13 | export { | ||
14 | isAnnounceValid | ||
15 | } | ||
diff --git a/server/helpers/custom-validators/activitypub/index.ts b/server/helpers/custom-validators/activitypub/index.ts index 0eba06a7b..6685b269f 100644 --- a/server/helpers/custom-validators/activitypub/index.ts +++ b/server/helpers/custom-validators/activitypub/index.ts | |||
@@ -1,5 +1,7 @@ | |||
1 | export * from './account' | 1 | export * from './account' |
2 | export * from './activity' | 2 | export * from './activity' |
3 | export * from './signature' | ||
4 | export * from './misc' | 3 | export * from './misc' |
4 | export * from './signature' | ||
5 | export * from './undo' | ||
6 | export * from './video-channels' | ||
5 | export * from './videos' | 7 | export * from './videos' |
diff --git a/server/helpers/custom-validators/activitypub/undo.ts b/server/helpers/custom-validators/activitypub/undo.ts new file mode 100644 index 000000000..a9a2a3a41 --- /dev/null +++ b/server/helpers/custom-validators/activitypub/undo.ts | |||
@@ -0,0 +1,13 @@ | |||
1 | import { isAccountFollowActivityValid } from './account' | ||
2 | import { isBaseActivityValid } from './misc' | ||
3 | |||
4 | function isUndoValid (activity: any) { | ||
5 | return isBaseActivityValid(activity, 'Undo') && | ||
6 | ( | ||
7 | isAccountFollowActivityValid(activity.object) | ||
8 | ) | ||
9 | } | ||
10 | |||
11 | export { | ||
12 | isUndoValid | ||
13 | } | ||
diff --git a/server/helpers/custom-validators/activitypub/video-channels.ts b/server/helpers/custom-validators/activitypub/video-channels.ts new file mode 100644 index 000000000..9fd3bb149 --- /dev/null +++ b/server/helpers/custom-validators/activitypub/video-channels.ts | |||
@@ -0,0 +1,36 @@ | |||
1 | import { isDateValid, isUUIDValid } from '../misc' | ||
2 | import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels' | ||
3 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' | ||
4 | |||
5 | function isVideoChannelCreateActivityValid (activity: any) { | ||
6 | return isBaseActivityValid(activity, 'Create') && | ||
7 | isVideoChannelObjectValid(activity.object) | ||
8 | } | ||
9 | |||
10 | function isVideoChannelUpdateActivityValid (activity: any) { | ||
11 | return isBaseActivityValid(activity, 'Update') && | ||
12 | isVideoChannelObjectValid(activity.object) | ||
13 | } | ||
14 | |||
15 | function isVideoChannelDeleteActivityValid (activity: any) { | ||
16 | return isBaseActivityValid(activity, 'Delete') | ||
17 | } | ||
18 | |||
19 | function isVideoChannelObjectValid (videoChannel: any) { | ||
20 | return videoChannel.type === 'VideoChannel' && | ||
21 | isActivityPubUrlValid(videoChannel.id) && | ||
22 | isVideoChannelNameValid(videoChannel.name) && | ||
23 | isVideoChannelDescriptionValid(videoChannel.content) && | ||
24 | isDateValid(videoChannel.published) && | ||
25 | isDateValid(videoChannel.updated) && | ||
26 | isUUIDValid(videoChannel.uuid) | ||
27 | } | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | export { | ||
32 | isVideoChannelCreateActivityValid, | ||
33 | isVideoChannelUpdateActivityValid, | ||
34 | isVideoChannelDeleteActivityValid, | ||
35 | isVideoChannelObjectValid | ||
36 | } | ||
diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 728511e3d..faeedd3df 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts | |||
@@ -1,7 +1,6 @@ | |||
1 | import * as validator from 'validator' | 1 | import * as validator from 'validator' |
2 | import { ACTIVITY_PUB } from '../../../initializers' | 2 | import { ACTIVITY_PUB } from '../../../initializers' |
3 | import { exists, isDateValid, isUUIDValid } from '../misc' | 3 | import { exists, isDateValid, isUUIDValid } from '../misc' |
4 | import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels' | ||
5 | import { | 4 | import { |
6 | isVideoAbuseReasonValid, | 5 | isVideoAbuseReasonValid, |
7 | isVideoDurationValid, | 6 | isVideoDurationValid, |
@@ -28,6 +27,13 @@ function isVideoTorrentDeleteActivityValid (activity: any) { | |||
28 | return isBaseActivityValid(activity, 'Delete') | 27 | return isBaseActivityValid(activity, 'Delete') |
29 | } | 28 | } |
30 | 29 | ||
30 | function isVideoFlagValid (activity: any) { | ||
31 | return isBaseActivityValid(activity, 'Create') && | ||
32 | activity.object.type === 'Flag' && | ||
33 | isVideoAbuseReasonValid(activity.object.content) && | ||
34 | isActivityPubUrlValid(activity.object.object) | ||
35 | } | ||
36 | |||
31 | function isActivityPubVideoDurationValid (value: string) { | 37 | function isActivityPubVideoDurationValid (value: string) { |
32 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration | 38 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration |
33 | return exists(value) && | 39 | return exists(value) && |
@@ -57,57 +63,13 @@ function isVideoTorrentObjectValid (video: any) { | |||
57 | video.url.length !== 0 | 63 | video.url.length !== 0 |
58 | } | 64 | } |
59 | 65 | ||
60 | function isVideoFlagValid (activity: any) { | ||
61 | return isBaseActivityValid(activity, 'Create') && | ||
62 | activity.object.type === 'Flag' && | ||
63 | isVideoAbuseReasonValid(activity.object.content) && | ||
64 | isActivityPubUrlValid(activity.object.object) | ||
65 | } | ||
66 | |||
67 | function isAnnounceValid (activity: any) { | ||
68 | return isBaseActivityValid(activity, 'Announce') && | ||
69 | ( | ||
70 | isVideoChannelCreateActivityValid(activity.object) || | ||
71 | isVideoTorrentAddActivityValid(activity.object) | ||
72 | ) | ||
73 | } | ||
74 | |||
75 | function isVideoChannelCreateActivityValid (activity: any) { | ||
76 | return isBaseActivityValid(activity, 'Create') && | ||
77 | isVideoChannelObjectValid(activity.object) | ||
78 | } | ||
79 | |||
80 | function isVideoChannelUpdateActivityValid (activity: any) { | ||
81 | return isBaseActivityValid(activity, 'Update') && | ||
82 | isVideoChannelObjectValid(activity.object) | ||
83 | } | ||
84 | |||
85 | function isVideoChannelDeleteActivityValid (activity: any) { | ||
86 | return isBaseActivityValid(activity, 'Delete') | ||
87 | } | ||
88 | |||
89 | function isVideoChannelObjectValid (videoChannel: any) { | ||
90 | return videoChannel.type === 'VideoChannel' && | ||
91 | isActivityPubUrlValid(videoChannel.id) && | ||
92 | isVideoChannelNameValid(videoChannel.name) && | ||
93 | isVideoChannelDescriptionValid(videoChannel.content) && | ||
94 | isDateValid(videoChannel.published) && | ||
95 | isDateValid(videoChannel.updated) && | ||
96 | isUUIDValid(videoChannel.uuid) | ||
97 | } | ||
98 | |||
99 | // --------------------------------------------------------------------------- | 66 | // --------------------------------------------------------------------------- |
100 | 67 | ||
101 | export { | 68 | export { |
102 | isVideoTorrentAddActivityValid, | 69 | isVideoTorrentAddActivityValid, |
103 | isVideoChannelCreateActivityValid, | ||
104 | isVideoTorrentUpdateActivityValid, | 70 | isVideoTorrentUpdateActivityValid, |
105 | isVideoChannelUpdateActivityValid, | ||
106 | isVideoChannelDeleteActivityValid, | ||
107 | isVideoTorrentDeleteActivityValid, | 71 | isVideoTorrentDeleteActivityValid, |
108 | isVideoFlagValid, | 72 | isVideoFlagValid |
109 | isAnnounceValid, | ||
110 | isVideoChannelObjectValid | ||
111 | } | 73 | } |
112 | 74 | ||
113 | // --------------------------------------------------------------------------- | 75 | // --------------------------------------------------------------------------- |