aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/cache/videos-preview-cache.ts2
-rw-r--r--server/lib/friends.ts138
-rw-r--r--server/lib/index.ts2
-rw-r--r--server/lib/user.ts46
-rw-r--r--server/lib/video-channel.ts42
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
48type QaduParam = { videoId: number, type: RequestVideoQaduType } 54type QaduParam = { videoId: number, type: RequestVideoQaduType }
@@ -62,7 +68,7 @@ function activateSchedulers () {
62 68
63function addVideoToFriends (videoData: RemoteVideoCreateData, transaction: Sequelize.Transaction) { 69function 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
73function updateVideoToFriends (videoData: RemoteVideoUpdateData, transaction: Sequelize.Transaction) { 79function 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
83function removeVideoToFriends (videoParams: RemoteVideoRemoveData, transaction: Sequelize.Transaction) { 89function 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
99function 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
109function 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
119function 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
129function 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
139function 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
93function reportAbuseVideoToFriend (reportData: RemoteVideoReportAbuseData, video: VideoInstance, transaction: Sequelize.Transaction) { 149function 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
266function sendOwnedDataToPod (podId: number) {
267 // First send authors
268 return sendOwnedAuthorsToPod(podId)
269 .then(() => sendOwnedChannelsToPod(podId))
270 .then(() => sendOwnedVideosToPod(podId))
271}
272
273function 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
295function 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
210function sendOwnedVideosToPod (podId: number) { 317function 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
239function fetchRemotePreview (pod: PodInstance, video: VideoInstance) { 346function 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 () {
274export { 381export {
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
399type CreateRequestOptions = { 511type 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'
3export * from './request' 3export * from './request'
4export * from './friends' 4export * from './friends'
5export * from './oauth-model' 5export * from './oauth-model'
6export * from './user'
7export * 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 @@
1import { database as db } from '../initializers'
2import { UserInstance } from '../models'
3import { addVideoAuthorToFriends } from './friends'
4import { createVideoChannel } from './video-channel'
5
6function 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
44export {
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 @@
1import * as Sequelize from 'sequelize'
2
3import { addVideoChannelToFriends } from './friends'
4import { database as db } from '../initializers'
5import { AuthorInstance } from '../models'
6import { VideoChannelCreate } from '../../shared/models'
7
8function 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
40export {
41 createVideoChannel
42}