aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-14 17:31:26 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad (patch)
treef4191f3c04a5230fcf8ca3d6ca3248643fc4151d /server/lib
parente34c85e527100c0b5c44567bd951e95be41b8d7e (diff)
downloadPeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.tar.gz
PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.tar.zst
PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.zip
Follow works
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/activitypub/process-add.ts2
-rw-r--r--server/lib/activitypub/process-follow.ts18
-rw-r--r--server/lib/activitypub/send-request.ts38
-rw-r--r--server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts1
-rw-r--r--server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts1
-rw-r--r--server/lib/jobs/job-scheduler.ts13
6 files changed, 42 insertions, 31 deletions
diff --git a/server/lib/activitypub/process-add.ts b/server/lib/activitypub/process-add.ts
index 024dee559..06d23a2ea 100644
--- a/server/lib/activitypub/process-add.ts
+++ b/server/lib/activitypub/process-add.ts
@@ -54,7 +54,7 @@ async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string
54 54
55 // Don't block on request 55 // Don't block on request
56 generateThumbnailFromUrl(video, videoToCreateData.icon) 56 generateThumbnailFromUrl(video, videoToCreateData.icon)
57 .catch(err => logger.warning('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) 57 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
58 58
59 const videoCreated = await video.save(sequelizeOptions) 59 const videoCreated = await video.save(sequelizeOptions)
60 60
diff --git a/server/lib/activitypub/process-follow.ts b/server/lib/activitypub/process-follow.ts
index ee5d97a0b..a805c0757 100644
--- a/server/lib/activitypub/process-follow.ts
+++ b/server/lib/activitypub/process-follow.ts
@@ -36,14 +36,18 @@ async function follow (account: AccountInstance, targetAccountURL: string) {
36 if (targetAccount === undefined) throw new Error('Unknown account') 36 if (targetAccount === undefined) throw new Error('Unknown account')
37 if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') 37 if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')
38 38
39 const sequelizeOptions = { 39 await db.AccountFollow.findOrCreate({
40 where: {
41 accountId: account.id,
42 targetAccountId: targetAccount.id
43 },
44 defaults: {
45 accountId: account.id,
46 targetAccountId: targetAccount.id,
47 state: 'accepted'
48 },
40 transaction: t 49 transaction: t
41 } 50 })
42 await db.AccountFollow.create({
43 accountId: account.id,
44 targetAccountId: targetAccount.id,
45 state: 'accepted'
46 }, sequelizeOptions)
47 51
48 // Target sends to account he accepted the follow request 52 // Target sends to account he accepted the follow request
49 return sendAccept(targetAccount, account, t) 53 return sendAccept(targetAccount, account, t)
diff --git a/server/lib/activitypub/send-request.ts b/server/lib/activitypub/send-request.ts
index c18a69784..d47040d6d 100644
--- a/server/lib/activitypub/send-request.ts
+++ b/server/lib/activitypub/send-request.ts
@@ -10,60 +10,60 @@ import { httpRequestJobScheduler } from '../jobs'
10import { signObject, activityPubContextify } from '../../helpers' 10import { signObject, activityPubContextify } from '../../helpers'
11import { Activity } from '../../../shared' 11import { Activity } from '../../../shared'
12 12
13function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { 13async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
14 const videoChannelObject = videoChannel.toActivityPubObject() 14 const videoChannelObject = videoChannel.toActivityPubObject()
15 const data = createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) 15 const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
16 16
17 return broadcastToFollowers(data, videoChannel.Account, t) 17 return broadcastToFollowers(data, videoChannel.Account, t)
18} 18}
19 19
20function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { 20async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
21 const videoChannelObject = videoChannel.toActivityPubObject() 21 const videoChannelObject = videoChannel.toActivityPubObject()
22 const data = updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) 22 const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
23 23
24 return broadcastToFollowers(data, videoChannel.Account, t) 24 return broadcastToFollowers(data, videoChannel.Account, t)
25} 25}
26 26
27function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { 27async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
28 const data = deleteActivityData(videoChannel.url, videoChannel.Account) 28 const data = await deleteActivityData(videoChannel.url, videoChannel.Account)
29 29
30 return broadcastToFollowers(data, videoChannel.Account, t) 30 return broadcastToFollowers(data, videoChannel.Account, t)
31} 31}
32 32
33function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) { 33async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
34 const videoObject = video.toActivityPubObject() 34 const videoObject = video.toActivityPubObject()
35 const data = addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject) 35 const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
36 36
37 return broadcastToFollowers(data, video.VideoChannel.Account, t) 37 return broadcastToFollowers(data, video.VideoChannel.Account, t)
38} 38}
39 39
40function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) { 40async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
41 const videoObject = video.toActivityPubObject() 41 const videoObject = video.toActivityPubObject()
42 const data = updateActivityData(video.url, video.VideoChannel.Account, videoObject) 42 const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject)
43 43
44 return broadcastToFollowers(data, video.VideoChannel.Account, t) 44 return broadcastToFollowers(data, video.VideoChannel.Account, t)
45} 45}
46 46
47function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) { 47async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
48 const data = deleteActivityData(video.url, video.VideoChannel.Account) 48 const data = await deleteActivityData(video.url, video.VideoChannel.Account)
49 49
50 return broadcastToFollowers(data, video.VideoChannel.Account, t) 50 return broadcastToFollowers(data, video.VideoChannel.Account, t)
51} 51}
52 52
53function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) { 53async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
54 const data = deleteActivityData(account.url, account) 54 const data = await deleteActivityData(account.url, account)
55 55
56 return broadcastToFollowers(data, account, t) 56 return broadcastToFollowers(data, account, t)
57} 57}
58 58
59function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { 59async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
60 const data = acceptActivityData(fromAccount) 60 const data = await acceptActivityData(fromAccount)
61 61
62 return unicastTo(data, toAccount, t) 62 return unicastTo(data, toAccount, t)
63} 63}
64 64
65function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { 65async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
66 const data = followActivityData(toAccount.url, fromAccount) 66 const data = await followActivityData(toAccount.url, fromAccount)
67 67
68 return unicastTo(data, toAccount, t) 68 return unicastTo(data, toAccount, t)
69} 69}
@@ -97,7 +97,7 @@ async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t:
97 97
98async function unicastTo (data: any, toAccount: AccountInstance, t: Sequelize.Transaction) { 98async function unicastTo (data: any, toAccount: AccountInstance, t: Sequelize.Transaction) {
99 const jobPayload = { 99 const jobPayload = {
100 uris: [ toAccount.url ], 100 uris: [ toAccount.inboxUrl ],
101 body: data 101 body: data
102 } 102 }
103 103
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
index 799b86e1c..2f1d9ee92 100644
--- 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
@@ -6,6 +6,7 @@ async function process (payload: HTTPRequestPayload, jobId: number) {
6 logger.info('Processing broadcast in job %d.', jobId) 6 logger.info('Processing broadcast in job %d.', jobId)
7 7
8 const options = { 8 const options = {
9 method: 'POST',
9 uri: '', 10 uri: '',
10 json: payload.body 11 json: payload.body
11 } 12 }
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
index 13451f042..3a1a7fabf 100644
--- 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
@@ -7,6 +7,7 @@ async function process (payload: HTTPRequestPayload, jobId: number) {
7 7
8 const uri = payload.uris[0] 8 const uri = payload.uris[0]
9 const options = { 9 const options = {
10 method: 'POST',
10 uri, 11 uri,
11 json: payload.body 12 json: payload.body
12 } 13 }
diff --git a/server/lib/jobs/job-scheduler.ts b/server/lib/jobs/job-scheduler.ts
index f10f745b3..b25bb7ab3 100644
--- a/server/lib/jobs/job-scheduler.ts
+++ b/server/lib/jobs/job-scheduler.ts
@@ -4,6 +4,7 @@ import { JobCategory } from '../../../shared'
4import { logger } from '../../helpers' 4import { logger } from '../../helpers'
5import { database as db, JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers' 5import { database as db, JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers'
6import { JobInstance } from '../../models' 6import { JobInstance } from '../../models'
7import { error } from 'util'
7 8
8export interface JobHandler<P, T> { 9export interface JobHandler<P, T> {
9 process (data: object, jobId: number): Promise<T> 10 process (data: object, jobId: number): Promise<T>
@@ -80,8 +81,12 @@ class JobScheduler<P, T> {
80 private async processJob (job: JobInstance, callback: (err: Error) => void) { 81 private async processJob (job: JobInstance, callback: (err: Error) => void) {
81 const jobHandler = this.jobHandlers[job.handlerName] 82 const jobHandler = this.jobHandlers[job.handlerName]
82 if (jobHandler === undefined) { 83 if (jobHandler === undefined) {
83 logger.error('Unknown job handler for job %s.', job.handlerName) 84 const errorString = 'Unknown job handler ' + job.handlerName + ' for job ' + job.id
84 return callback(null) 85 logger.error(errorString)
86
87 const error = new Error(errorString)
88 await this.onJobError(jobHandler, job, error)
89 return callback(error)
85 } 90 }
86 91
87 logger.info('Processing job %d with handler %s.', job.id, job.handlerName) 92 logger.info('Processing job %d with handler %s.', job.id, job.handlerName)
@@ -103,7 +108,7 @@ class JobScheduler<P, T> {
103 } 108 }
104 } 109 }
105 110
106 callback(null) 111 return callback(null)
107 } 112 }
108 113
109 private async onJobError (jobHandler: JobHandler<P, T>, job: JobInstance, err: Error) { 114 private async onJobError (jobHandler: JobHandler<P, T>, job: JobInstance, err: Error) {
@@ -111,7 +116,7 @@ class JobScheduler<P, T> {
111 116
112 try { 117 try {
113 await job.save() 118 await job.save()
114 await jobHandler.onError(err, job.id) 119 if (jobHandler) await jobHandler.onError(err, job.id)
115 } catch (err) { 120 } catch (err) {
116 this.cannotSaveJobError(err) 121 this.cannotSaveJobError(err)
117 } 122 }