diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-09 17:51:58 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | e4f97babf701481b55cc10fb3448feab5f97c867 (patch) | |
tree | af37402a594dc5ff09f71ecb0687e8cfe4cdb471 /server/lib | |
parent | 343ad675f2a26c15b86150a9a3552e619d5d44f4 (diff) | |
download | PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.gz PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.zst PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.zip |
Begin activitypub
Diffstat (limited to 'server/lib')
19 files changed, 404 insertions, 48 deletions
diff --git a/server/lib/activitypub/index.ts b/server/lib/activitypub/index.ts new file mode 100644 index 000000000..740800606 --- /dev/null +++ b/server/lib/activitypub/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './process-create' | ||
2 | export * from './process-flag' | ||
3 | export * from './process-update' | ||
diff --git a/server/lib/activitypub/process-create.ts b/server/lib/activitypub/process-create.ts new file mode 100644 index 000000000..114ff1848 --- /dev/null +++ b/server/lib/activitypub/process-create.ts | |||
@@ -0,0 +1,104 @@ | |||
1 | import { | ||
2 | ActivityCreate, | ||
3 | VideoTorrentObject, | ||
4 | VideoChannelObject | ||
5 | } from '../../../shared' | ||
6 | import { database as db } from '../../initializers' | ||
7 | import { logger, retryTransactionWrapper } from '../../helpers' | ||
8 | |||
9 | function processCreateActivity (activity: ActivityCreate) { | ||
10 | const activityObject = activity.object | ||
11 | const activityType = activityObject.type | ||
12 | |||
13 | if (activityType === 'Video') { | ||
14 | return processCreateVideo(activityObject as VideoTorrentObject) | ||
15 | } else if (activityType === 'VideoChannel') { | ||
16 | return processCreateVideoChannel(activityObject as VideoChannelObject) | ||
17 | } | ||
18 | |||
19 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | ||
20 | return Promise.resolve() | ||
21 | } | ||
22 | |||
23 | // --------------------------------------------------------------------------- | ||
24 | |||
25 | export { | ||
26 | processCreateActivity | ||
27 | } | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | function processCreateVideo (video: VideoTorrentObject) { | ||
32 | const options = { | ||
33 | arguments: [ video ], | ||
34 | errorMessage: 'Cannot insert the remote video with many retries.' | ||
35 | } | ||
36 | |||
37 | return retryTransactionWrapper(addRemoteVideo, options) | ||
38 | } | ||
39 | |||
40 | async function addRemoteVideo (videoToCreateData: VideoTorrentObject) { | ||
41 | logger.debug('Adding remote video %s.', videoToCreateData.url) | ||
42 | |||
43 | await db.sequelize.transaction(async t => { | ||
44 | const sequelizeOptions = { | ||
45 | transaction: t | ||
46 | } | ||
47 | |||
48 | const videoFromDatabase = await db.Video.loadByUUID(videoToCreateData.uuid) | ||
49 | if (videoFromDatabase) throw new Error('UUID already exists.') | ||
50 | |||
51 | const videoChannel = await db.VideoChannel.loadByHostAndUUID(fromPod.host, videoToCreateData.channelUUID, t) | ||
52 | if (!videoChannel) throw new Error('Video channel ' + videoToCreateData.channelUUID + ' not found.') | ||
53 | |||
54 | const tags = videoToCreateData.tags | ||
55 | const tagInstances = await db.Tag.findOrCreateTags(tags, t) | ||
56 | |||
57 | const videoData = { | ||
58 | name: videoToCreateData.name, | ||
59 | uuid: videoToCreateData.uuid, | ||
60 | category: videoToCreateData.category, | ||
61 | licence: videoToCreateData.licence, | ||
62 | language: videoToCreateData.language, | ||
63 | nsfw: videoToCreateData.nsfw, | ||
64 | description: videoToCreateData.truncatedDescription, | ||
65 | channelId: videoChannel.id, | ||
66 | duration: videoToCreateData.duration, | ||
67 | createdAt: videoToCreateData.createdAt, | ||
68 | // FIXME: updatedAt does not seems to be considered by Sequelize | ||
69 | updatedAt: videoToCreateData.updatedAt, | ||
70 | views: videoToCreateData.views, | ||
71 | likes: videoToCreateData.likes, | ||
72 | dislikes: videoToCreateData.dislikes, | ||
73 | remote: true, | ||
74 | privacy: videoToCreateData.privacy | ||
75 | } | ||
76 | |||
77 | const video = db.Video.build(videoData) | ||
78 | await db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData) | ||
79 | const videoCreated = await video.save(sequelizeOptions) | ||
80 | |||
81 | const tasks = [] | ||
82 | for (const fileData of videoToCreateData.files) { | ||
83 | const videoFileInstance = db.VideoFile.build({ | ||
84 | extname: fileData.extname, | ||
85 | infoHash: fileData.infoHash, | ||
86 | resolution: fileData.resolution, | ||
87 | size: fileData.size, | ||
88 | videoId: videoCreated.id | ||
89 | }) | ||
90 | |||
91 | tasks.push(videoFileInstance.save(sequelizeOptions)) | ||
92 | } | ||
93 | |||
94 | await Promise.all(tasks) | ||
95 | |||
96 | await videoCreated.setTags(tagInstances, sequelizeOptions) | ||
97 | }) | ||
98 | |||
99 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | ||
100 | } | ||
101 | |||
102 | function processCreateVideoChannel (videoChannel: VideoChannelObject) { | ||
103 | |||
104 | } | ||
diff --git a/server/lib/activitypub/process-flag.ts b/server/lib/activitypub/process-flag.ts new file mode 100644 index 000000000..6fa862ee9 --- /dev/null +++ b/server/lib/activitypub/process-flag.ts | |||
@@ -0,0 +1,17 @@ | |||
1 | import { | ||
2 | ActivityCreate, | ||
3 | VideoTorrentObject, | ||
4 | VideoChannelObject | ||
5 | } from '../../../shared' | ||
6 | |||
7 | function processFlagActivity (activity: ActivityCreate) { | ||
8 | // empty | ||
9 | } | ||
10 | |||
11 | // --------------------------------------------------------------------------- | ||
12 | |||
13 | export { | ||
14 | processFlagActivity | ||
15 | } | ||
16 | |||
17 | // --------------------------------------------------------------------------- | ||
diff --git a/server/lib/activitypub/process-update.ts b/server/lib/activitypub/process-update.ts new file mode 100644 index 000000000..187c7be7c --- /dev/null +++ b/server/lib/activitypub/process-update.ts | |||
@@ -0,0 +1,29 @@ | |||
1 | import { | ||
2 | ActivityCreate, | ||
3 | VideoTorrentObject, | ||
4 | VideoChannelObject | ||
5 | } from '../../../shared' | ||
6 | |||
7 | function processUpdateActivity (activity: ActivityCreate) { | ||
8 | if (activity.object.type === 'Video') { | ||
9 | return processUpdateVideo(activity.object) | ||
10 | } else if (activity.object.type === 'VideoChannel') { | ||
11 | return processUpdateVideoChannel(activity.object) | ||
12 | } | ||
13 | } | ||
14 | |||
15 | // --------------------------------------------------------------------------- | ||
16 | |||
17 | export { | ||
18 | processUpdateActivity | ||
19 | } | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | function processUpdateVideo (video: VideoTorrentObject) { | ||
24 | |||
25 | } | ||
26 | |||
27 | function processUpdateVideoChannel (videoChannel: VideoChannelObject) { | ||
28 | |||
29 | } | ||
diff --git a/server/lib/activitypub/send-request.ts b/server/lib/activitypub/send-request.ts new file mode 100644 index 000000000..6a31c226d --- /dev/null +++ b/server/lib/activitypub/send-request.ts | |||
@@ -0,0 +1,129 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | import { | ||
4 | AccountInstance, | ||
5 | VideoInstance, | ||
6 | VideoChannelInstance | ||
7 | } from '../../models' | ||
8 | import { httpRequestJobScheduler } from '../jobs' | ||
9 | import { signObject, activityPubContextify } from '../../helpers' | ||
10 | import { Activity } from '../../../shared' | ||
11 | |||
12 | function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { | ||
13 | const videoChannelObject = videoChannel.toActivityPubObject() | ||
14 | const data = createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) | ||
15 | |||
16 | return broadcastToFollowers(data, t) | ||
17 | } | ||
18 | |||
19 | function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { | ||
20 | const videoChannelObject = videoChannel.toActivityPubObject() | ||
21 | const data = updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) | ||
22 | |||
23 | return broadcastToFollowers(data, t) | ||
24 | } | ||
25 | |||
26 | function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { | ||
27 | const videoChannelObject = videoChannel.toActivityPubObject() | ||
28 | const data = deleteActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) | ||
29 | |||
30 | return broadcastToFollowers(data, t) | ||
31 | } | ||
32 | |||
33 | function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) { | ||
34 | const videoObject = video.toActivityPubObject() | ||
35 | const data = addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject) | ||
36 | |||
37 | return broadcastToFollowers(data, t) | ||
38 | } | ||
39 | |||
40 | function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) { | ||
41 | const videoObject = video.toActivityPubObject() | ||
42 | const data = updateActivityData(video.url, video.VideoChannel.Account, videoObject) | ||
43 | |||
44 | return broadcastToFollowers(data, t) | ||
45 | } | ||
46 | |||
47 | function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) { | ||
48 | const videoObject = video.toActivityPubObject() | ||
49 | const data = deleteActivityData(video.url, video.VideoChannel.Account, videoObject) | ||
50 | |||
51 | return broadcastToFollowers(data, t) | ||
52 | } | ||
53 | |||
54 | // --------------------------------------------------------------------------- | ||
55 | |||
56 | export { | ||
57 | |||
58 | } | ||
59 | |||
60 | // --------------------------------------------------------------------------- | ||
61 | |||
62 | function broadcastToFollowers (data: any, t: Sequelize.Transaction) { | ||
63 | return httpRequestJobScheduler.createJob(t, 'http-request', 'httpRequestBroadcastHandler', data) | ||
64 | } | ||
65 | |||
66 | function buildSignedActivity (byAccount: AccountInstance, data: Object) { | ||
67 | const activity = activityPubContextify(data) | ||
68 | |||
69 | return signObject(byAccount, activity) as Promise<Activity> | ||
70 | } | ||
71 | |||
72 | async function getPublicActivityTo (account: AccountInstance) { | ||
73 | const inboxUrls = await account.getFollowerSharedInboxUrls() | ||
74 | |||
75 | return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public') | ||
76 | } | ||
77 | |||
78 | async function createActivityData (url: string, byAccount: AccountInstance, object: any) { | ||
79 | const to = await getPublicActivityTo(byAccount) | ||
80 | const base = { | ||
81 | type: 'Create', | ||
82 | id: url, | ||
83 | actor: byAccount.url, | ||
84 | to, | ||
85 | object | ||
86 | } | ||
87 | |||
88 | return buildSignedActivity(byAccount, base) | ||
89 | } | ||
90 | |||
91 | async function updateActivityData (url: string, byAccount: AccountInstance, object: any) { | ||
92 | const to = await getPublicActivityTo(byAccount) | ||
93 | const base = { | ||
94 | type: 'Update', | ||
95 | id: url, | ||
96 | actor: byAccount.url, | ||
97 | to, | ||
98 | object | ||
99 | } | ||
100 | |||
101 | return buildSignedActivity(byAccount, base) | ||
102 | } | ||
103 | |||
104 | async function deleteActivityData (url: string, byAccount: AccountInstance, object: any) { | ||
105 | const to = await getPublicActivityTo(byAccount) | ||
106 | const base = { | ||
107 | type: 'Update', | ||
108 | id: url, | ||
109 | actor: byAccount.url, | ||
110 | to, | ||
111 | object | ||
112 | } | ||
113 | |||
114 | return buildSignedActivity(byAccount, base) | ||
115 | } | ||
116 | |||
117 | async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) { | ||
118 | const to = await getPublicActivityTo(byAccount) | ||
119 | const base = { | ||
120 | type: 'Add', | ||
121 | id: url, | ||
122 | actor: byAccount.url, | ||
123 | to, | ||
124 | object, | ||
125 | target | ||
126 | } | ||
127 | |||
128 | return buildSignedActivity(byAccount, base) | ||
129 | } | ||
diff --git a/server/lib/index.ts b/server/lib/index.ts index d1534b085..bfb415ad2 100644 --- a/server/lib/index.ts +++ b/server/lib/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './activitypub' | ||
1 | export * from './cache' | 2 | export * from './cache' |
2 | export * from './jobs' | 3 | export * from './jobs' |
3 | export * from './request' | 4 | export * from './request' |
diff --git a/server/lib/jobs/handlers/index.ts b/server/lib/jobs/handlers/index.ts deleted file mode 100644 index cef1f89a9..000000000 --- a/server/lib/jobs/handlers/index.ts +++ /dev/null | |||
@@ -1,17 +0,0 @@ | |||
1 | import * as videoFileOptimizer from './video-file-optimizer' | ||
2 | import * as videoFileTranscoder from './video-file-transcoder' | ||
3 | |||
4 | export interface JobHandler<T> { | ||
5 | process (data: object, jobId: number): T | ||
6 | onError (err: Error, jobId: number) | ||
7 | onSuccess (jobId: number, jobResult: T) | ||
8 | } | ||
9 | |||
10 | const jobHandlers: { [ handlerName: string ]: JobHandler<any> } = { | ||
11 | videoFileOptimizer, | ||
12 | videoFileTranscoder | ||
13 | } | ||
14 | |||
15 | export { | ||
16 | jobHandlers | ||
17 | } | ||
diff --git a/server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts b/server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts new file mode 100644 index 000000000..6b6946d02 --- /dev/null +++ b/server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | |||
3 | import { database as db } from '../../../initializers/database' | ||
4 | import { logger } from '../../../helpers' | ||
5 | |||
6 | async function process (data: { videoUUID: string }, jobId: number) { | ||
7 | |||
8 | } | ||
9 | |||
10 | function onError (err: Error, jobId: number) { | ||
11 | logger.error('Error when optimized video file in job %d.', jobId, err) | ||
12 | return Promise.resolve() | ||
13 | } | ||
14 | |||
15 | async function onSuccess (jobId: number) { | ||
16 | |||
17 | } | ||
18 | |||
19 | // --------------------------------------------------------------------------- | ||
20 | |||
21 | export { | ||
22 | process, | ||
23 | onError, | ||
24 | onSuccess | ||
25 | } | ||
diff --git a/server/lib/jobs/http-request-job-scheduler/http-request-job-scheduler.ts b/server/lib/jobs/http-request-job-scheduler/http-request-job-scheduler.ts new file mode 100644 index 000000000..42cb9139c --- /dev/null +++ b/server/lib/jobs/http-request-job-scheduler/http-request-job-scheduler.ts | |||
@@ -0,0 +1,17 @@ | |||
1 | import { JobScheduler, JobHandler } from '../job-scheduler' | ||
2 | |||
3 | import * as httpRequestBroadcastHandler from './http-request-broadcast-handler' | ||
4 | import * as httpRequestUnicastHandler from './http-request-unicast-handler' | ||
5 | import { JobCategory } from '../../../../shared' | ||
6 | |||
7 | const jobHandlers: { [ handlerName: string ]: JobHandler<any> } = { | ||
8 | httpRequestBroadcastHandler, | ||
9 | httpRequestUnicastHandler | ||
10 | } | ||
11 | const jobCategory: JobCategory = 'http-request' | ||
12 | |||
13 | const httpRequestJobScheduler = new JobScheduler(jobCategory, jobHandlers) | ||
14 | |||
15 | export { | ||
16 | httpRequestJobScheduler | ||
17 | } | ||
diff --git a/server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts b/server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts new file mode 100644 index 000000000..6b6946d02 --- /dev/null +++ b/server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | |||
3 | import { database as db } from '../../../initializers/database' | ||
4 | import { logger } from '../../../helpers' | ||
5 | |||
6 | async function process (data: { videoUUID: string }, jobId: number) { | ||
7 | |||
8 | } | ||
9 | |||
10 | function onError (err: Error, jobId: number) { | ||
11 | logger.error('Error when optimized video file in job %d.', jobId, err) | ||
12 | return Promise.resolve() | ||
13 | } | ||
14 | |||
15 | async function onSuccess (jobId: number) { | ||
16 | |||
17 | } | ||
18 | |||
19 | // --------------------------------------------------------------------------- | ||
20 | |||
21 | export { | ||
22 | process, | ||
23 | onError, | ||
24 | onSuccess | ||
25 | } | ||
diff --git a/server/lib/jobs/http-request-job-scheduler/index.ts b/server/lib/jobs/http-request-job-scheduler/index.ts new file mode 100644 index 000000000..4d2573296 --- /dev/null +++ b/server/lib/jobs/http-request-job-scheduler/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './http-request-job-scheduler' | |||
diff --git a/server/lib/jobs/index.ts b/server/lib/jobs/index.ts index b18a3d845..a92743707 100644 --- a/server/lib/jobs/index.ts +++ b/server/lib/jobs/index.ts | |||
@@ -1 +1,2 @@ | |||
1 | export * from './job-scheduler' | 1 | export * from './http-request-job-scheduler' |
2 | export * from './transcoding-job-scheduler' | ||
diff --git a/server/lib/jobs/job-scheduler.ts b/server/lib/jobs/job-scheduler.ts index 61d483268..89a4bca88 100644 --- a/server/lib/jobs/job-scheduler.ts +++ b/server/lib/jobs/job-scheduler.ts | |||
@@ -1,39 +1,41 @@ | |||
1 | import { AsyncQueue, forever, queue } from 'async' | 1 | import { AsyncQueue, forever, queue } from 'async' |
2 | import * as Sequelize from 'sequelize' | 2 | import * as Sequelize from 'sequelize' |
3 | 3 | ||
4 | import { database as db } from '../../initializers/database' | ||
5 | import { | 4 | import { |
5 | database as db, | ||
6 | JOBS_FETCHING_INTERVAL, | 6 | JOBS_FETCHING_INTERVAL, |
7 | JOBS_FETCH_LIMIT_PER_CYCLE, | 7 | JOBS_FETCH_LIMIT_PER_CYCLE, |
8 | JOB_STATES | 8 | JOB_STATES |
9 | } from '../../initializers' | 9 | } from '../../initializers' |
10 | import { logger } from '../../helpers' | 10 | import { logger } from '../../helpers' |
11 | import { JobInstance } from '../../models' | 11 | import { JobInstance } from '../../models' |
12 | import { JobHandler, jobHandlers } from './handlers' | 12 | import { JobCategory } from '../../../shared' |
13 | 13 | ||
14 | export interface JobHandler<T> { | ||
15 | process (data: object, jobId: number): T | ||
16 | onError (err: Error, jobId: number) | ||
17 | onSuccess (jobId: number, jobResult: T) | ||
18 | } | ||
14 | type JobQueueCallback = (err: Error) => void | 19 | type JobQueueCallback = (err: Error) => void |
15 | 20 | ||
16 | class JobScheduler { | 21 | class JobScheduler<T> { |
17 | |||
18 | private static instance: JobScheduler | ||
19 | 22 | ||
20 | private constructor () { } | 23 | constructor ( |
21 | 24 | private jobCategory: JobCategory, | |
22 | static get Instance () { | 25 | private jobHandlers: { [ id: string ]: JobHandler<T> } |
23 | return this.instance || (this.instance = new this()) | 26 | ) {} |
24 | } | ||
25 | 27 | ||
26 | async activate () { | 28 | async activate () { |
27 | const limit = JOBS_FETCH_LIMIT_PER_CYCLE | 29 | const limit = JOBS_FETCH_LIMIT_PER_CYCLE[this.jobCategory] |
28 | 30 | ||
29 | logger.info('Jobs scheduler activated.') | 31 | logger.info('Jobs scheduler %s activated.', this.jobCategory) |
30 | 32 | ||
31 | const jobsQueue = queue<JobInstance, JobQueueCallback>(this.processJob.bind(this)) | 33 | const jobsQueue = queue<JobInstance, JobQueueCallback>(this.processJob.bind(this)) |
32 | 34 | ||
33 | // Finish processing jobs from a previous start | 35 | // Finish processing jobs from a previous start |
34 | const state = JOB_STATES.PROCESSING | 36 | const state = JOB_STATES.PROCESSING |
35 | try { | 37 | try { |
36 | const jobs = await db.Job.listWithLimit(limit, state) | 38 | const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) |
37 | 39 | ||
38 | this.enqueueJobs(jobsQueue, jobs) | 40 | this.enqueueJobs(jobsQueue, jobs) |
39 | } catch (err) { | 41 | } catch (err) { |
@@ -49,7 +51,7 @@ class JobScheduler { | |||
49 | 51 | ||
50 | const state = JOB_STATES.PENDING | 52 | const state = JOB_STATES.PENDING |
51 | try { | 53 | try { |
52 | const jobs = await db.Job.listWithLimit(limit, state) | 54 | const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) |
53 | 55 | ||
54 | this.enqueueJobs(jobsQueue, jobs) | 56 | this.enqueueJobs(jobsQueue, jobs) |
55 | } catch (err) { | 57 | } catch (err) { |
@@ -64,9 +66,10 @@ class JobScheduler { | |||
64 | ) | 66 | ) |
65 | } | 67 | } |
66 | 68 | ||
67 | createJob (transaction: Sequelize.Transaction, handlerName: string, handlerInputData: object) { | 69 | createJob (transaction: Sequelize.Transaction, category: JobCategory, handlerName: string, handlerInputData: object) { |
68 | const createQuery = { | 70 | const createQuery = { |
69 | state: JOB_STATES.PENDING, | 71 | state: JOB_STATES.PENDING, |
72 | category, | ||
70 | handlerName, | 73 | handlerName, |
71 | handlerInputData | 74 | handlerInputData |
72 | } | 75 | } |
@@ -80,7 +83,7 @@ class JobScheduler { | |||
80 | } | 83 | } |
81 | 84 | ||
82 | private async processJob (job: JobInstance, callback: (err: Error) => void) { | 85 | private async processJob (job: JobInstance, callback: (err: Error) => void) { |
83 | const jobHandler = jobHandlers[job.handlerName] | 86 | const jobHandler = this.jobHandlers[job.handlerName] |
84 | if (jobHandler === undefined) { | 87 | if (jobHandler === undefined) { |
85 | logger.error('Unknown job handler for job %s.', job.handlerName) | 88 | logger.error('Unknown job handler for job %s.', job.handlerName) |
86 | return callback(null) | 89 | return callback(null) |
diff --git a/server/lib/jobs/transcoding-job-scheduler/index.ts b/server/lib/jobs/transcoding-job-scheduler/index.ts new file mode 100644 index 000000000..73152a1be --- /dev/null +++ b/server/lib/jobs/transcoding-job-scheduler/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './transcoding-job-scheduler' | |||
diff --git a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts new file mode 100644 index 000000000..d7c614fb8 --- /dev/null +++ b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts | |||
@@ -0,0 +1,17 @@ | |||
1 | import { JobScheduler, JobHandler } from '../job-scheduler' | ||
2 | |||
3 | import * as videoFileOptimizer from './video-file-optimizer-handler' | ||
4 | import * as videoFileTranscoder from './video-file-transcoder-handler' | ||
5 | import { JobCategory } from '../../../../shared' | ||
6 | |||
7 | const jobHandlers: { [ handlerName: string ]: JobHandler<any> } = { | ||
8 | videoFileOptimizer, | ||
9 | videoFileTranscoder | ||
10 | } | ||
11 | const jobCategory: JobCategory = 'transcoding' | ||
12 | |||
13 | const transcodingJobScheduler = new JobScheduler(jobCategory, jobHandlers) | ||
14 | |||
15 | export { | ||
16 | transcodingJobScheduler | ||
17 | } | ||
diff --git a/server/lib/jobs/handlers/video-file-optimizer.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts index ccded4721..ccded4721 100644 --- a/server/lib/jobs/handlers/video-file-optimizer.ts +++ b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts | |||
diff --git a/server/lib/jobs/handlers/video-file-transcoder.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts index 853645510..853645510 100644 --- a/server/lib/jobs/handlers/video-file-transcoder.ts +++ b/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts | |||
diff --git a/server/lib/user.ts b/server/lib/user.ts index a92f4777b..57c653e55 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts | |||
@@ -1,9 +1,9 @@ | |||
1 | import { database as db } from '../initializers' | 1 | import { database as db } from '../initializers' |
2 | import { UserInstance } from '../models' | 2 | import { UserInstance } from '../models' |
3 | import { addVideoAuthorToFriends } from './friends' | 3 | import { addVideoAccountToFriends } from './friends' |
4 | import { createVideoChannel } from './video-channel' | 4 | import { createVideoChannel } from './video-channel' |
5 | 5 | ||
6 | async function createUserAuthorAndChannel (user: UserInstance, validateUser = true) { | 6 | async function createUserAccountAndChannel (user: UserInstance, validateUser = true) { |
7 | const res = await db.sequelize.transaction(async t => { | 7 | const res = await db.sequelize.transaction(async t => { |
8 | const userOptions = { | 8 | const userOptions = { |
9 | transaction: t, | 9 | transaction: t, |
@@ -11,25 +11,25 @@ async function createUserAuthorAndChannel (user: UserInstance, validateUser = tr | |||
11 | } | 11 | } |
12 | 12 | ||
13 | const userCreated = await user.save(userOptions) | 13 | const userCreated = await user.save(userOptions) |
14 | const authorInstance = db.Author.build({ | 14 | const accountInstance = db.Account.build({ |
15 | name: userCreated.username, | 15 | name: userCreated.username, |
16 | podId: null, // It is our pod | 16 | podId: null, // It is our pod |
17 | userId: userCreated.id | 17 | userId: userCreated.id |
18 | }) | 18 | }) |
19 | 19 | ||
20 | const authorCreated = await authorInstance.save({ transaction: t }) | 20 | const accountCreated = await accountInstance.save({ transaction: t }) |
21 | 21 | ||
22 | const remoteVideoAuthor = authorCreated.toAddRemoteJSON() | 22 | const remoteVideoAccount = accountCreated.toAddRemoteJSON() |
23 | 23 | ||
24 | // Now we'll add the video channel's meta data to our friends | 24 | // Now we'll add the video channel's meta data to our friends |
25 | const author = await addVideoAuthorToFriends(remoteVideoAuthor, t) | 25 | const account = await addVideoAccountToFriends(remoteVideoAccount, t) |
26 | 26 | ||
27 | const videoChannelInfo = { | 27 | const videoChannelInfo = { |
28 | name: `Default ${userCreated.username} channel` | 28 | name: `Default ${userCreated.username} channel` |
29 | } | 29 | } |
30 | const videoChannel = await createVideoChannel(videoChannelInfo, authorCreated, t) | 30 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) |
31 | 31 | ||
32 | return { author, videoChannel } | 32 | return { account, videoChannel } |
33 | }) | 33 | }) |
34 | 34 | ||
35 | return res | 35 | return res |
@@ -38,5 +38,5 @@ async function createUserAuthorAndChannel (user: UserInstance, validateUser = tr | |||
38 | // --------------------------------------------------------------------------- | 38 | // --------------------------------------------------------------------------- |
39 | 39 | ||
40 | export { | 40 | export { |
41 | createUserAuthorAndChannel | 41 | createUserAccountAndChannel |
42 | } | 42 | } |
diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts index 678ffe643..a6dd4d061 100644 --- a/server/lib/video-channel.ts +++ b/server/lib/video-channel.ts | |||
@@ -3,15 +3,15 @@ import * as Sequelize from 'sequelize' | |||
3 | import { addVideoChannelToFriends } from './friends' | 3 | import { addVideoChannelToFriends } from './friends' |
4 | import { database as db } from '../initializers' | 4 | import { database as db } from '../initializers' |
5 | import { logger } from '../helpers' | 5 | import { logger } from '../helpers' |
6 | import { AuthorInstance } from '../models' | 6 | import { AccountInstance } from '../models' |
7 | import { VideoChannelCreate } from '../../shared/models' | 7 | import { VideoChannelCreate } from '../../shared/models' |
8 | 8 | ||
9 | async function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) { | 9 | async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) { |
10 | const videoChannelData = { | 10 | const videoChannelData = { |
11 | name: videoChannelInfo.name, | 11 | name: videoChannelInfo.name, |
12 | description: videoChannelInfo.description, | 12 | description: videoChannelInfo.description, |
13 | remote: false, | 13 | remote: false, |
14 | authorId: author.id | 14 | authorId: account.id |
15 | } | 15 | } |
16 | 16 | ||
17 | const videoChannel = db.VideoChannel.build(videoChannelData) | 17 | const videoChannel = db.VideoChannel.build(videoChannelData) |
@@ -19,8 +19,8 @@ async function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: | |||
19 | 19 | ||
20 | const videoChannelCreated = await videoChannel.save(options) | 20 | const videoChannelCreated = await videoChannel.save(options) |
21 | 21 | ||
22 | // Do not forget to add Author information to the created video channel | 22 | // Do not forget to add Account information to the created video channel |
23 | videoChannelCreated.Author = author | 23 | videoChannelCreated.Account = account |
24 | 24 | ||
25 | const remoteVideoChannel = videoChannelCreated.toAddRemoteJSON() | 25 | const remoteVideoChannel = videoChannelCreated.toAddRemoteJSON() |
26 | 26 | ||