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 | |
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')
7 files changed, 187 insertions, 73 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index aff58515a..9622a1801 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts | |||
@@ -9,18 +9,20 @@ import { VideoChannelObject } from '../../shared/models/activitypub/objects/vide | |||
9 | import { ResultList } from '../../shared/models/result-list.model' | 9 | import { ResultList } from '../../shared/models/result-list.model' |
10 | import { database as db, REMOTE_SCHEME } from '../initializers' | 10 | import { database as db, REMOTE_SCHEME } from '../initializers' |
11 | import { ACTIVITY_PUB, CONFIG, STATIC_PATHS } from '../initializers/constants' | 11 | import { ACTIVITY_PUB, CONFIG, STATIC_PATHS } from '../initializers/constants' |
12 | import { videoChannelActivityObjectToDBAttributes } from '../lib/activitypub/misc' | 12 | import { videoChannelActivityObjectToDBAttributes } from '../lib/activitypub/process/misc' |
13 | import { sendVideoAnnounce } from '../lib/activitypub/send-request' | 13 | import { sendVideoAnnounce } from '../lib/activitypub/send/send-announce' |
14 | import { sendVideoChannelAnnounce } from '../lib/index' | 14 | import { sendVideoChannelAnnounce } from '../lib/index' |
15 | import { AccountFollowInstance } from '../models/account/account-follow-interface' | ||
15 | import { AccountInstance } from '../models/account/account-interface' | 16 | import { AccountInstance } from '../models/account/account-interface' |
17 | import { VideoAbuseInstance } from '../models/video/video-abuse-interface' | ||
16 | import { VideoChannelInstance } from '../models/video/video-channel-interface' | 18 | import { VideoChannelInstance } from '../models/video/video-channel-interface' |
17 | import { VideoInstance } from '../models/video/video-interface' | 19 | import { VideoInstance } from '../models/video/video-interface' |
18 | import { isRemoteAccountValid } from './custom-validators' | 20 | import { isRemoteAccountValid } from './custom-validators' |
19 | import { isVideoChannelObjectValid } from './custom-validators/activitypub/videos' | ||
20 | import { logger } from './logger' | 21 | import { logger } from './logger' |
21 | import { signObject } from './peertube-crypto' | 22 | import { signObject } from './peertube-crypto' |
22 | import { doRequest, doRequestAndSaveToFile } from './requests' | 23 | import { doRequest, doRequestAndSaveToFile } from './requests' |
23 | import { getServerAccount } from './utils' | 24 | import { getServerAccount } from './utils' |
25 | import { isVideoChannelObjectValid } from './custom-validators/activitypub/video-channels' | ||
24 | 26 | ||
25 | function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { | 27 | function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { |
26 | const thumbnailName = video.getThumbnailName() | 28 | const thumbnailName = video.getThumbnailName() |
@@ -55,13 +57,46 @@ async function shareVideoByServer (video: VideoInstance, t: Sequelize.Transactio | |||
55 | return sendVideoAnnounce(serverAccount, video, t) | 57 | return sendVideoAnnounce(serverAccount, video, t) |
56 | } | 58 | } |
57 | 59 | ||
58 | function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account' | 'videoAbuse', id: string) { | 60 | function getVideoActivityPubUrl (video: VideoInstance) { |
59 | if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + id | 61 | return CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid |
60 | else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + id | 62 | } |
61 | else if (type === 'account') return CONFIG.WEBSERVER.URL + '/account/' + id | 63 | |
62 | else if (type === 'videoAbuse') return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + id | 64 | function getVideoChannelActivityPubUrl (videoChannel: VideoChannelInstance) { |
65 | return CONFIG.WEBSERVER.URL + '/video-channels/' + videoChannel.uuid | ||
66 | } | ||
67 | |||
68 | function getAccountActivityPubUrl (accountName: string) { | ||
69 | return CONFIG.WEBSERVER.URL + '/account/' + accountName | ||
70 | } | ||
71 | |||
72 | function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseInstance) { | ||
73 | return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + videoAbuse.id | ||
74 | } | ||
75 | |||
76 | function getAccountFollowActivityPubUrl (accountFollow: AccountFollowInstance) { | ||
77 | const me = accountFollow.AccountFollower | ||
78 | const following = accountFollow.AccountFollowing | ||
79 | |||
80 | return me.url + '#follows/' + following.id | ||
81 | } | ||
82 | |||
83 | function getAccountFollowAcceptActivityPubUrl (accountFollow: AccountFollowInstance) { | ||
84 | const follower = accountFollow.AccountFollower | ||
85 | const me = accountFollow.AccountFollowing | ||
86 | |||
87 | return follower.url + '#accepts/follows/' + me.id | ||
88 | } | ||
89 | |||
90 | function getAnnounceActivityPubUrl (originalUrl: string, byAccount: AccountInstance) { | ||
91 | return originalUrl + '#announces/' + byAccount.id | ||
92 | } | ||
93 | |||
94 | function getUpdateActivityPubUrl (originalUrl: string, updatedAt: string) { | ||
95 | return originalUrl + '#updates/' + updatedAt | ||
96 | } | ||
63 | 97 | ||
64 | return '' | 98 | function getUndoActivityPubUrl (originalUrl: string) { |
99 | return originalUrl + '/undo' | ||
65 | } | 100 | } |
66 | 101 | ||
67 | async function getOrCreateAccount (accountUrl: string) { | 102 | async function getOrCreateAccount (accountUrl: string) { |
@@ -257,7 +292,6 @@ export { | |||
257 | fetchRemoteAccountAndCreateServer, | 292 | fetchRemoteAccountAndCreateServer, |
258 | activityPubContextify, | 293 | activityPubContextify, |
259 | activityPubCollectionPagination, | 294 | activityPubCollectionPagination, |
260 | getActivityPubUrl, | ||
261 | generateThumbnailFromUrl, | 295 | generateThumbnailFromUrl, |
262 | getOrCreateAccount, | 296 | getOrCreateAccount, |
263 | fetchRemoteVideoPreview, | 297 | fetchRemoteVideoPreview, |
@@ -265,7 +299,16 @@ export { | |||
265 | shareVideoChannelByServer, | 299 | shareVideoChannelByServer, |
266 | shareVideoByServer, | 300 | shareVideoByServer, |
267 | getOrCreateVideoChannel, | 301 | getOrCreateVideoChannel, |
268 | buildSignedActivity | 302 | buildSignedActivity, |
303 | getVideoActivityPubUrl, | ||
304 | getVideoChannelActivityPubUrl, | ||
305 | getAccountActivityPubUrl, | ||
306 | getVideoAbuseActivityPubUrl, | ||
307 | getAccountFollowActivityPubUrl, | ||
308 | getAccountFollowAcceptActivityPubUrl, | ||
309 | getAnnounceActivityPubUrl, | ||
310 | getUpdateActivityPubUrl, | ||
311 | getUndoActivityPubUrl | ||
269 | } | 312 | } |
270 | 313 | ||
271 | // --------------------------------------------------------------------------- | 314 | // --------------------------------------------------------------------------- |
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 | // --------------------------------------------------------------------------- |