aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-20 09:43:39 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:52 +0100
commit54141398354e6e7b94aa3065a705a1251390111c (patch)
tree8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/helpers
parenteb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff)
downloadPeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz
PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst
PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip
Refractor activity pub lib/helpers
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/activitypub.ts65
-rw-r--r--server/helpers/custom-validators/activitypub/activity.ts73
-rw-r--r--server/helpers/custom-validators/activitypub/announce.ts15
-rw-r--r--server/helpers/custom-validators/activitypub/index.ts4
-rw-r--r--server/helpers/custom-validators/activitypub/undo.ts13
-rw-r--r--server/helpers/custom-validators/activitypub/video-channels.ts36
-rw-r--r--server/helpers/custom-validators/activitypub/videos.ts54
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
9import { ResultList } from '../../shared/models/result-list.model' 9import { ResultList } from '../../shared/models/result-list.model'
10import { database as db, REMOTE_SCHEME } from '../initializers' 10import { database as db, REMOTE_SCHEME } from '../initializers'
11import { ACTIVITY_PUB, CONFIG, STATIC_PATHS } from '../initializers/constants' 11import { ACTIVITY_PUB, CONFIG, STATIC_PATHS } from '../initializers/constants'
12import { videoChannelActivityObjectToDBAttributes } from '../lib/activitypub/misc' 12import { videoChannelActivityObjectToDBAttributes } from '../lib/activitypub/process/misc'
13import { sendVideoAnnounce } from '../lib/activitypub/send-request' 13import { sendVideoAnnounce } from '../lib/activitypub/send/send-announce'
14import { sendVideoChannelAnnounce } from '../lib/index' 14import { sendVideoChannelAnnounce } from '../lib/index'
15import { AccountFollowInstance } from '../models/account/account-follow-interface'
15import { AccountInstance } from '../models/account/account-interface' 16import { AccountInstance } from '../models/account/account-interface'
17import { VideoAbuseInstance } from '../models/video/video-abuse-interface'
16import { VideoChannelInstance } from '../models/video/video-channel-interface' 18import { VideoChannelInstance } from '../models/video/video-channel-interface'
17import { VideoInstance } from '../models/video/video-interface' 19import { VideoInstance } from '../models/video/video-interface'
18import { isRemoteAccountValid } from './custom-validators' 20import { isRemoteAccountValid } from './custom-validators'
19import { isVideoChannelObjectValid } from './custom-validators/activitypub/videos'
20import { logger } from './logger' 21import { logger } from './logger'
21import { signObject } from './peertube-crypto' 22import { signObject } from './peertube-crypto'
22import { doRequest, doRequestAndSaveToFile } from './requests' 23import { doRequest, doRequestAndSaveToFile } from './requests'
23import { getServerAccount } from './utils' 24import { getServerAccount } from './utils'
25import { isVideoChannelObjectValid } from './custom-validators/activitypub/video-channels'
24 26
25function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { 27function 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
58function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account' | 'videoAbuse', id: string) { 60function 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 64function getVideoChannelActivityPubUrl (videoChannel: VideoChannelInstance) {
65 return CONFIG.WEBSERVER.URL + '/video-channels/' + videoChannel.uuid
66}
67
68function getAccountActivityPubUrl (accountName: string) {
69 return CONFIG.WEBSERVER.URL + '/account/' + accountName
70}
71
72function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseInstance) {
73 return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + videoAbuse.id
74}
75
76function getAccountFollowActivityPubUrl (accountFollow: AccountFollowInstance) {
77 const me = accountFollow.AccountFollower
78 const following = accountFollow.AccountFollowing
79
80 return me.url + '#follows/' + following.id
81}
82
83function getAccountFollowAcceptActivityPubUrl (accountFollow: AccountFollowInstance) {
84 const follower = accountFollow.AccountFollower
85 const me = accountFollow.AccountFollowing
86
87 return follower.url + '#accepts/follows/' + me.id
88}
89
90function getAnnounceActivityPubUrl (originalUrl: string, byAccount: AccountInstance) {
91 return originalUrl + '#announces/' + byAccount.id
92}
93
94function getUpdateActivityPubUrl (originalUrl: string, updatedAt: string) {
95 return originalUrl + '#updates/' + updatedAt
96}
63 97
64 return '' 98function getUndoActivityPubUrl (originalUrl: string) {
99 return originalUrl + '/undo'
65} 100}
66 101
67async function getOrCreateAccount (accountUrl: string) { 102async 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 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity'
2import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account' 3import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account'
4import { isAnnounceValid } from './announce'
3import { isActivityPubUrlValid } from './misc' 5import { isActivityPubUrlValid } from './misc'
6import { isUndoValid } from './undo'
7import { isVideoChannelCreateActivityValid, isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels'
4import { 8import {
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
28const 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
28function isActivityValid (activity: any) { 39function 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
56function checkCreateActivity (activity: any) {
57 return isVideoChannelCreateActivityValid(activity) ||
58 isVideoFlagValid(activity)
59}
60
61function checkAddActivity (activity: any) {
62 return isVideoTorrentAddActivityValid(activity)
63}
64
65function checkUpdateActivity (activity: any) {
66 return isVideoTorrentUpdateActivityValid(activity) ||
67 isVideoChannelUpdateActivityValid(activity)
68}
69
70function checkDeleteActivity (activity: any) {
71 return isVideoTorrentDeleteActivityValid(activity) ||
72 isVideoChannelDeleteActivityValid(activity) ||
73 isAccountDeleteActivityValid(activity)
74}
75
76function checkFollowActivity (activity: any) {
77 return isAccountFollowActivityValid(activity)
78}
79
80function checkAcceptActivity (activity: any) {
81 return isAccountAcceptActivityValid(activity)
82}
83
84function checkAnnounceActivity (activity: any) {
85 return isAnnounceValid(activity)
86}
87
88function 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 @@
1import { isBaseActivityValid } from './misc'
2import { isVideoTorrentAddActivityValid } from './videos'
3import { isVideoChannelCreateActivityValid } from './video-channels'
4
5function isAnnounceValid (activity: any) {
6 return isBaseActivityValid(activity, 'Announce') &&
7 (
8 isVideoChannelCreateActivityValid(activity.object) ||
9 isVideoTorrentAddActivityValid(activity.object)
10 )
11}
12
13export {
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 @@
1export * from './account' 1export * from './account'
2export * from './activity' 2export * from './activity'
3export * from './signature'
4export * from './misc' 3export * from './misc'
4export * from './signature'
5export * from './undo'
6export * from './video-channels'
5export * from './videos' 7export * 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 @@
1import { isAccountFollowActivityValid } from './account'
2import { isBaseActivityValid } from './misc'
3
4function isUndoValid (activity: any) {
5 return isBaseActivityValid(activity, 'Undo') &&
6 (
7 isAccountFollowActivityValid(activity.object)
8 )
9}
10
11export {
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 @@
1import { isDateValid, isUUIDValid } from '../misc'
2import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
3import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
4
5function isVideoChannelCreateActivityValid (activity: any) {
6 return isBaseActivityValid(activity, 'Create') &&
7 isVideoChannelObjectValid(activity.object)
8}
9
10function isVideoChannelUpdateActivityValid (activity: any) {
11 return isBaseActivityValid(activity, 'Update') &&
12 isVideoChannelObjectValid(activity.object)
13}
14
15function isVideoChannelDeleteActivityValid (activity: any) {
16 return isBaseActivityValid(activity, 'Delete')
17}
18
19function 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
31export {
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 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2import { ACTIVITY_PUB } from '../../../initializers' 2import { ACTIVITY_PUB } from '../../../initializers'
3import { exists, isDateValid, isUUIDValid } from '../misc' 3import { exists, isDateValid, isUUIDValid } from '../misc'
4import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
5import { 4import {
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
30function 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
31function isActivityPubVideoDurationValid (value: string) { 37function 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
60function 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
67function isAnnounceValid (activity: any) {
68 return isBaseActivityValid(activity, 'Announce') &&
69 (
70 isVideoChannelCreateActivityValid(activity.object) ||
71 isVideoTorrentAddActivityValid(activity.object)
72 )
73}
74
75function isVideoChannelCreateActivityValid (activity: any) {
76 return isBaseActivityValid(activity, 'Create') &&
77 isVideoChannelObjectValid(activity.object)
78}
79
80function isVideoChannelUpdateActivityValid (activity: any) {
81 return isBaseActivityValid(activity, 'Update') &&
82 isVideoChannelObjectValid(activity.object)
83}
84
85function isVideoChannelDeleteActivityValid (activity: any) {
86 return isBaseActivityValid(activity, 'Delete')
87}
88
89function 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
101export { 68export {
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// ---------------------------------------------------------------------------