diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-24 19:41:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-26 09:11:38 +0200 |
commit | 72c7248b6fdcdb2175e726ff51b42e7555f2bd84 (patch) | |
tree | 1bfdee99dbe2392cc997edba8e314e2a8a401c72 /server/lib | |
parent | 8113a93a0d9f31aa9e23702bbc31b8a76275ae22 (diff) | |
download | PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.gz PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.zst PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.zip |
Add video channels
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/cache/videos-preview-cache.ts | 2 | ||||
-rw-r--r-- | server/lib/friends.ts | 138 | ||||
-rw-r--r-- | server/lib/index.ts | 2 | ||||
-rw-r--r-- | server/lib/user.ts | 46 | ||||
-rw-r--r-- | server/lib/video-channel.ts | 42 |
5 files changed, 216 insertions, 14 deletions
diff --git a/server/lib/cache/videos-preview-cache.ts b/server/lib/cache/videos-preview-cache.ts index 09e2e9a0d..fecdca6ef 100644 --- a/server/lib/cache/videos-preview-cache.ts +++ b/server/lib/cache/videos-preview-cache.ts | |||
@@ -55,7 +55,7 @@ class VideosPreviewCache { | |||
55 | } | 55 | } |
56 | 56 | ||
57 | private saveRemotePreviewAndReturnPath (video: VideoInstance) { | 57 | private saveRemotePreviewAndReturnPath (video: VideoInstance) { |
58 | const req = fetchRemotePreview(video.Author.Pod, video) | 58 | const req = fetchRemotePreview(video) |
59 | 59 | ||
60 | return new Promise<string>((res, rej) => { | 60 | return new Promise<string>((res, rej) => { |
61 | const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) | 61 | const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) |
diff --git a/server/lib/friends.ts b/server/lib/friends.ts index 65349ef5f..f035b099b 100644 --- a/server/lib/friends.ts +++ b/server/lib/friends.ts | |||
@@ -42,7 +42,13 @@ import { | |||
42 | RemoteVideoRemoveData, | 42 | RemoteVideoRemoveData, |
43 | RemoteVideoReportAbuseData, | 43 | RemoteVideoReportAbuseData, |
44 | ResultList, | 44 | ResultList, |
45 | Pod as FormattedPod | 45 | RemoteVideoRequestType, |
46 | Pod as FormattedPod, | ||
47 | RemoteVideoChannelCreateData, | ||
48 | RemoteVideoChannelUpdateData, | ||
49 | RemoteVideoChannelRemoveData, | ||
50 | RemoteVideoAuthorCreateData, | ||
51 | RemoteVideoAuthorRemoveData | ||
46 | } from '../../shared' | 52 | } from '../../shared' |
47 | 53 | ||
48 | type QaduParam = { videoId: number, type: RequestVideoQaduType } | 54 | type QaduParam = { videoId: number, type: RequestVideoQaduType } |
@@ -62,7 +68,7 @@ function activateSchedulers () { | |||
62 | 68 | ||
63 | function addVideoToFriends (videoData: RemoteVideoCreateData, transaction: Sequelize.Transaction) { | 69 | function addVideoToFriends (videoData: RemoteVideoCreateData, transaction: Sequelize.Transaction) { |
64 | const options = { | 70 | const options = { |
65 | type: ENDPOINT_ACTIONS.ADD, | 71 | type: ENDPOINT_ACTIONS.ADD_VIDEO, |
66 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | 72 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
67 | data: videoData, | 73 | data: videoData, |
68 | transaction | 74 | transaction |
@@ -72,7 +78,7 @@ function addVideoToFriends (videoData: RemoteVideoCreateData, transaction: Seque | |||
72 | 78 | ||
73 | function updateVideoToFriends (videoData: RemoteVideoUpdateData, transaction: Sequelize.Transaction) { | 79 | function updateVideoToFriends (videoData: RemoteVideoUpdateData, transaction: Sequelize.Transaction) { |
74 | const options = { | 80 | const options = { |
75 | type: ENDPOINT_ACTIONS.UPDATE, | 81 | type: ENDPOINT_ACTIONS.UPDATE_VIDEO, |
76 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | 82 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
77 | data: videoData, | 83 | data: videoData, |
78 | transaction | 84 | transaction |
@@ -82,7 +88,7 @@ function updateVideoToFriends (videoData: RemoteVideoUpdateData, transaction: Se | |||
82 | 88 | ||
83 | function removeVideoToFriends (videoParams: RemoteVideoRemoveData, transaction: Sequelize.Transaction) { | 89 | function removeVideoToFriends (videoParams: RemoteVideoRemoveData, transaction: Sequelize.Transaction) { |
84 | const options = { | 90 | const options = { |
85 | type: ENDPOINT_ACTIONS.REMOVE, | 91 | type: ENDPOINT_ACTIONS.REMOVE_VIDEO, |
86 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | 92 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
87 | data: videoParams, | 93 | data: videoParams, |
88 | transaction | 94 | transaction |
@@ -90,12 +96,62 @@ function removeVideoToFriends (videoParams: RemoteVideoRemoveData, transaction: | |||
90 | return createRequest(options) | 96 | return createRequest(options) |
91 | } | 97 | } |
92 | 98 | ||
99 | function addVideoAuthorToFriends (authorData: RemoteVideoAuthorCreateData, transaction: Sequelize.Transaction) { | ||
100 | const options = { | ||
101 | type: ENDPOINT_ACTIONS.ADD_AUTHOR, | ||
102 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | ||
103 | data: authorData, | ||
104 | transaction | ||
105 | } | ||
106 | return createRequest(options) | ||
107 | } | ||
108 | |||
109 | function removeVideoAuthorToFriends (authorData: RemoteVideoAuthorRemoveData, transaction: Sequelize.Transaction) { | ||
110 | const options = { | ||
111 | type: ENDPOINT_ACTIONS.REMOVE_AUTHOR, | ||
112 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | ||
113 | data: authorData, | ||
114 | transaction | ||
115 | } | ||
116 | return createRequest(options) | ||
117 | } | ||
118 | |||
119 | function addVideoChannelToFriends (videoChannelData: RemoteVideoChannelCreateData, transaction: Sequelize.Transaction) { | ||
120 | const options = { | ||
121 | type: ENDPOINT_ACTIONS.ADD_CHANNEL, | ||
122 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | ||
123 | data: videoChannelData, | ||
124 | transaction | ||
125 | } | ||
126 | return createRequest(options) | ||
127 | } | ||
128 | |||
129 | function updateVideoChannelToFriends (videoChannelData: RemoteVideoChannelUpdateData, transaction: Sequelize.Transaction) { | ||
130 | const options = { | ||
131 | type: ENDPOINT_ACTIONS.UPDATE_CHANNEL, | ||
132 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | ||
133 | data: videoChannelData, | ||
134 | transaction | ||
135 | } | ||
136 | return createRequest(options) | ||
137 | } | ||
138 | |||
139 | function removeVideoChannelToFriends (videoChannelParams: RemoteVideoChannelRemoveData, transaction: Sequelize.Transaction) { | ||
140 | const options = { | ||
141 | type: ENDPOINT_ACTIONS.REMOVE_CHANNEL, | ||
142 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | ||
143 | data: videoChannelParams, | ||
144 | transaction | ||
145 | } | ||
146 | return createRequest(options) | ||
147 | } | ||
148 | |||
93 | function reportAbuseVideoToFriend (reportData: RemoteVideoReportAbuseData, video: VideoInstance, transaction: Sequelize.Transaction) { | 149 | function reportAbuseVideoToFriend (reportData: RemoteVideoReportAbuseData, video: VideoInstance, transaction: Sequelize.Transaction) { |
94 | const options = { | 150 | const options = { |
95 | type: ENDPOINT_ACTIONS.REPORT_ABUSE, | 151 | type: ENDPOINT_ACTIONS.REPORT_ABUSE, |
96 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | 152 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
97 | data: reportData, | 153 | data: reportData, |
98 | toIds: [ video.Author.podId ], | 154 | toIds: [ video.VideoChannel.Author.podId ], |
99 | transaction | 155 | transaction |
100 | } | 156 | } |
101 | return createRequest(options) | 157 | return createRequest(options) |
@@ -207,15 +263,66 @@ function quitFriends () { | |||
207 | .finally(() => requestScheduler.activate()) | 263 | .finally(() => requestScheduler.activate()) |
208 | } | 264 | } |
209 | 265 | ||
266 | function sendOwnedDataToPod (podId: number) { | ||
267 | // First send authors | ||
268 | return sendOwnedAuthorsToPod(podId) | ||
269 | .then(() => sendOwnedChannelsToPod(podId)) | ||
270 | .then(() => sendOwnedVideosToPod(podId)) | ||
271 | } | ||
272 | |||
273 | function sendOwnedChannelsToPod (podId: number) { | ||
274 | return db.VideoChannel.listOwned() | ||
275 | .then(videoChannels => { | ||
276 | const tasks = [] | ||
277 | videoChannels.forEach(videoChannel => { | ||
278 | const remoteVideoChannel = videoChannel.toAddRemoteJSON() | ||
279 | const options = { | ||
280 | type: 'add-channel' as 'add-channel', | ||
281 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | ||
282 | data: remoteVideoChannel, | ||
283 | toIds: [ podId ], | ||
284 | transaction: null | ||
285 | } | ||
286 | |||
287 | const p = createRequest(options) | ||
288 | tasks.push(p) | ||
289 | }) | ||
290 | |||
291 | return Promise.all(tasks) | ||
292 | }) | ||
293 | } | ||
294 | |||
295 | function sendOwnedAuthorsToPod (podId: number) { | ||
296 | return db.Author.listOwned() | ||
297 | .then(authors => { | ||
298 | const tasks = [] | ||
299 | authors.forEach(author => { | ||
300 | const remoteAuthor = author.toAddRemoteJSON() | ||
301 | const options = { | ||
302 | type: 'add-author' as 'add-author', | ||
303 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | ||
304 | data: remoteAuthor, | ||
305 | toIds: [ podId ], | ||
306 | transaction: null | ||
307 | } | ||
308 | |||
309 | const p = createRequest(options) | ||
310 | tasks.push(p) | ||
311 | }) | ||
312 | |||
313 | return Promise.all(tasks) | ||
314 | }) | ||
315 | } | ||
316 | |||
210 | function sendOwnedVideosToPod (podId: number) { | 317 | function sendOwnedVideosToPod (podId: number) { |
211 | db.Video.listOwnedAndPopulateAuthorAndTags() | 318 | return db.Video.listOwnedAndPopulateAuthorAndTags() |
212 | .then(videosList => { | 319 | .then(videosList => { |
213 | const tasks = [] | 320 | const tasks = [] |
214 | videosList.forEach(video => { | 321 | videosList.forEach(video => { |
215 | const promise = video.toAddRemoteJSON() | 322 | const promise = video.toAddRemoteJSON() |
216 | .then(remoteVideo => { | 323 | .then(remoteVideo => { |
217 | const options = { | 324 | const options = { |
218 | type: 'add', | 325 | type: 'add-video' as 'add-video', |
219 | endpoint: REQUEST_ENDPOINTS.VIDEOS, | 326 | endpoint: REQUEST_ENDPOINTS.VIDEOS, |
220 | data: remoteVideo, | 327 | data: remoteVideo, |
221 | toIds: [ podId ], | 328 | toIds: [ podId ], |
@@ -236,8 +343,8 @@ function sendOwnedVideosToPod (podId: number) { | |||
236 | }) | 343 | }) |
237 | } | 344 | } |
238 | 345 | ||
239 | function fetchRemotePreview (pod: PodInstance, video: VideoInstance) { | 346 | function fetchRemotePreview (video: VideoInstance) { |
240 | const host = video.Author.Pod.host | 347 | const host = video.VideoChannel.Author.Pod.host |
241 | const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) | 348 | const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) |
242 | 349 | ||
243 | return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) | 350 | return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) |
@@ -274,7 +381,9 @@ function getRequestVideoEventScheduler () { | |||
274 | export { | 381 | export { |
275 | activateSchedulers, | 382 | activateSchedulers, |
276 | addVideoToFriends, | 383 | addVideoToFriends, |
384 | removeVideoAuthorToFriends, | ||
277 | updateVideoToFriends, | 385 | updateVideoToFriends, |
386 | addVideoAuthorToFriends, | ||
278 | reportAbuseVideoToFriend, | 387 | reportAbuseVideoToFriend, |
279 | quickAndDirtyUpdateVideoToFriends, | 388 | quickAndDirtyUpdateVideoToFriends, |
280 | quickAndDirtyUpdatesVideoToFriends, | 389 | quickAndDirtyUpdatesVideoToFriends, |
@@ -285,11 +394,14 @@ export { | |||
285 | quitFriends, | 394 | quitFriends, |
286 | removeFriend, | 395 | removeFriend, |
287 | removeVideoToFriends, | 396 | removeVideoToFriends, |
288 | sendOwnedVideosToPod, | 397 | sendOwnedDataToPod, |
289 | getRequestScheduler, | 398 | getRequestScheduler, |
290 | getRequestVideoQaduScheduler, | 399 | getRequestVideoQaduScheduler, |
291 | getRequestVideoEventScheduler, | 400 | getRequestVideoEventScheduler, |
292 | fetchRemotePreview | 401 | fetchRemotePreview, |
402 | addVideoChannelToFriends, | ||
403 | updateVideoChannelToFriends, | ||
404 | removeVideoChannelToFriends | ||
293 | } | 405 | } |
294 | 406 | ||
295 | // --------------------------------------------------------------------------- | 407 | // --------------------------------------------------------------------------- |
@@ -373,7 +485,7 @@ function makeRequestsToWinningPods (cert: string, podsList: PodInstance[]) { | |||
373 | .then(podCreated => { | 485 | .then(podCreated => { |
374 | 486 | ||
375 | // Add our videos to the request scheduler | 487 | // Add our videos to the request scheduler |
376 | sendOwnedVideosToPod(podCreated.id) | 488 | sendOwnedDataToPod(podCreated.id) |
377 | }) | 489 | }) |
378 | .catch(err => { | 490 | .catch(err => { |
379 | logger.error('Cannot add friend %s pod.', pod.host, err) | 491 | logger.error('Cannot add friend %s pod.', pod.host, err) |
@@ -397,7 +509,7 @@ function makeRequestsToWinningPods (cert: string, podsList: PodInstance[]) { | |||
397 | 509 | ||
398 | // Wrapper that populate "toIds" argument with all our friends if it is not specified | 510 | // Wrapper that populate "toIds" argument with all our friends if it is not specified |
399 | type CreateRequestOptions = { | 511 | type CreateRequestOptions = { |
400 | type: string | 512 | type: RemoteVideoRequestType |
401 | endpoint: RequestEndpoint | 513 | endpoint: RequestEndpoint |
402 | data: Object | 514 | data: Object |
403 | toIds?: number[] | 515 | toIds?: number[] |
diff --git a/server/lib/index.ts b/server/lib/index.ts index 8628da4dd..d1534b085 100644 --- a/server/lib/index.ts +++ b/server/lib/index.ts | |||
@@ -3,3 +3,5 @@ export * from './jobs' | |||
3 | export * from './request' | 3 | export * from './request' |
4 | export * from './friends' | 4 | export * from './friends' |
5 | export * from './oauth-model' | 5 | export * from './oauth-model' |
6 | export * from './user' | ||
7 | export * from './video-channel' | ||
diff --git a/server/lib/user.ts b/server/lib/user.ts new file mode 100644 index 000000000..8609e72d8 --- /dev/null +++ b/server/lib/user.ts | |||
@@ -0,0 +1,46 @@ | |||
1 | import { database as db } from '../initializers' | ||
2 | import { UserInstance } from '../models' | ||
3 | import { addVideoAuthorToFriends } from './friends' | ||
4 | import { createVideoChannel } from './video-channel' | ||
5 | |||
6 | function createUserAuthorAndChannel (user: UserInstance, validateUser = true) { | ||
7 | return db.sequelize.transaction(t => { | ||
8 | const userOptions = { | ||
9 | transaction: t, | ||
10 | validate: validateUser | ||
11 | } | ||
12 | |||
13 | return user.save(userOptions) | ||
14 | .then(user => { | ||
15 | const author = db.Author.build({ | ||
16 | name: user.username, | ||
17 | podId: null, // It is our pod | ||
18 | userId: user.id | ||
19 | }) | ||
20 | |||
21 | return author.save({ transaction: t }) | ||
22 | .then(author => ({ author, user })) | ||
23 | }) | ||
24 | .then(({ author, user }) => { | ||
25 | const remoteVideoAuthor = author.toAddRemoteJSON() | ||
26 | |||
27 | // Now we'll add the video channel's meta data to our friends | ||
28 | return addVideoAuthorToFriends(remoteVideoAuthor, t) | ||
29 | .then(() => ({ author, user })) | ||
30 | }) | ||
31 | .then(({ author, user }) => { | ||
32 | const videoChannelInfo = { | ||
33 | name: `Default ${user.username} channel` | ||
34 | } | ||
35 | |||
36 | return createVideoChannel(videoChannelInfo, author, t) | ||
37 | .then(videoChannel => ({ author, user, videoChannel })) | ||
38 | }) | ||
39 | }) | ||
40 | } | ||
41 | |||
42 | // --------------------------------------------------------------------------- | ||
43 | |||
44 | export { | ||
45 | createUserAuthorAndChannel | ||
46 | } | ||
diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts new file mode 100644 index 000000000..224179973 --- /dev/null +++ b/server/lib/video-channel.ts | |||
@@ -0,0 +1,42 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | import { addVideoChannelToFriends } from './friends' | ||
4 | import { database as db } from '../initializers' | ||
5 | import { AuthorInstance } from '../models' | ||
6 | import { VideoChannelCreate } from '../../shared/models' | ||
7 | |||
8 | function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) { | ||
9 | let videoChannelUUID = '' | ||
10 | |||
11 | const videoChannelData = { | ||
12 | name: videoChannelInfo.name, | ||
13 | description: videoChannelInfo.description, | ||
14 | remote: false, | ||
15 | authorId: author.id | ||
16 | } | ||
17 | |||
18 | const videoChannel = db.VideoChannel.build(videoChannelData) | ||
19 | const options = { transaction: t } | ||
20 | |||
21 | return videoChannel.save(options) | ||
22 | .then(videoChannelCreated => { | ||
23 | // Do not forget to add Author information to the created video channel | ||
24 | videoChannelCreated.Author = author | ||
25 | videoChannelUUID = videoChannelCreated.uuid | ||
26 | |||
27 | return videoChannelCreated | ||
28 | }) | ||
29 | .then(videoChannel => { | ||
30 | const remoteVideoChannel = videoChannel.toAddRemoteJSON() | ||
31 | |||
32 | // Now we'll add the video channel's meta data to our friends | ||
33 | return addVideoChannelToFriends(remoteVideoChannel, t) | ||
34 | }) | ||
35 | .then(() => videoChannelUUID) // Return video channel UUID | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | export { | ||
41 | createVideoChannel | ||
42 | } | ||