diff options
author | Chocobozzz <me@florianbigard.com> | 2018-11-14 15:01:28 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-11-14 16:32:27 +0100 |
commit | 5c6d985faeef1d6793d3f44ca6374f1a9b722806 (patch) | |
tree | 567e31a84e721bf762189582f92ac2ec5c402bcc /server/lib/activitypub/process | |
parent | df66d81583e07ce049daeeef1edc6a87b57b3684 (diff) | |
download | PeerTube-5c6d985faeef1d6793d3f44ca6374f1a9b722806.tar.gz PeerTube-5c6d985faeef1d6793d3f44ca6374f1a9b722806.tar.zst PeerTube-5c6d985faeef1d6793d3f44ca6374f1a9b722806.zip |
Check activities host
Diffstat (limited to 'server/lib/activitypub/process')
-rw-r--r-- | server/lib/activitypub/process/index.ts | 8 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 5 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-like.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-undo.ts | 6 | ||||
-rw-r--r-- | server/lib/activitypub/process/process.ts | 25 |
5 files changed, 29 insertions, 19 deletions
diff --git a/server/lib/activitypub/process/index.ts b/server/lib/activitypub/process/index.ts index db4980a72..5466739c1 100644 --- a/server/lib/activitypub/process/index.ts +++ b/server/lib/activitypub/process/index.ts | |||
@@ -1,9 +1 @@ | |||
1 | export * from './process' | export * from './process' | |
2 | export * from './process-accept' | ||
3 | export * from './process-announce' | ||
4 | export * from './process-create' | ||
5 | export * from './process-delete' | ||
6 | export * from './process-follow' | ||
7 | export * from './process-like' | ||
8 | export * from './process-undo' | ||
9 | export * from './process-update' | ||
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index cefe89db0..920d02cd2 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -12,6 +12,8 @@ import { getOrCreateVideoAndAccountAndChannel } from '../videos' | |||
12 | import { forwardVideoRelatedActivity } from '../send/utils' | 12 | import { forwardVideoRelatedActivity } from '../send/utils' |
13 | import { Redis } from '../../redis' | 13 | import { Redis } from '../../redis' |
14 | import { createOrUpdateCacheFile } from '../cache-file' | 14 | import { createOrUpdateCacheFile } from '../cache-file' |
15 | import { immutableAssign } from '../../../tests/utils' | ||
16 | import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' | ||
15 | 17 | ||
16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { | 18 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { |
17 | const activityObject = activity.object | 19 | const activityObject = activity.object |
@@ -65,9 +67,10 @@ async function processCreateDislike (byActor: ActorModel, activity: ActivityCrea | |||
65 | videoId: video.id, | 67 | videoId: video.id, |
66 | accountId: byAccount.id | 68 | accountId: byAccount.id |
67 | } | 69 | } |
70 | |||
68 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ | 71 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
69 | where: rate, | 72 | where: rate, |
70 | defaults: rate, | 73 | defaults: immutableAssign(rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }), |
71 | transaction: t | 74 | transaction: t |
72 | }) | 75 | }) |
73 | if (created === true) await video.increment('dislikes', { transaction: t }) | 76 | if (created === true) await video.increment('dislikes', { transaction: t }) |
diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts index f7200db61..0dca17551 100644 --- a/server/lib/activitypub/process/process-like.ts +++ b/server/lib/activitypub/process/process-like.ts | |||
@@ -5,6 +5,8 @@ import { AccountVideoRateModel } from '../../../models/account/account-video-rat | |||
5 | import { ActorModel } from '../../../models/activitypub/actor' | 5 | import { ActorModel } from '../../../models/activitypub/actor' |
6 | import { forwardVideoRelatedActivity } from '../send/utils' | 6 | import { forwardVideoRelatedActivity } from '../send/utils' |
7 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | 7 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
8 | import { immutableAssign } from '../../../tests/utils' | ||
9 | import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' | ||
8 | 10 | ||
9 | async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) { | 11 | async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) { |
10 | return retryTransactionWrapper(processLikeVideo, byActor, activity) | 12 | return retryTransactionWrapper(processLikeVideo, byActor, activity) |
@@ -34,7 +36,7 @@ async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) { | |||
34 | } | 36 | } |
35 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ | 37 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
36 | where: rate, | 38 | where: rate, |
37 | defaults: rate, | 39 | defaults: immutableAssign(rate, { url: getVideoLikeActivityPubUrl(byActor, video) }), |
38 | transaction: t | 40 | transaction: t |
39 | }) | 41 | }) |
40 | if (created === true) await video.increment('likes', { transaction: t }) | 42 | if (created === true) await video.increment('likes', { transaction: t }) |
diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index ff019cd8c..438a013b6 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts | |||
@@ -55,7 +55,8 @@ async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) { | |||
55 | return sequelizeTypescript.transaction(async t => { | 55 | return sequelizeTypescript.transaction(async t => { |
56 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) | 56 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) |
57 | 57 | ||
58 | const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t) | 58 | let rate = await AccountVideoRateModel.loadByUrl(likeActivity.id, t) |
59 | if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t) | ||
59 | if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`) | 60 | if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`) |
60 | 61 | ||
61 | await rate.destroy({ transaction: t }) | 62 | await rate.destroy({ transaction: t }) |
@@ -78,7 +79,8 @@ async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) | |||
78 | return sequelizeTypescript.transaction(async t => { | 79 | return sequelizeTypescript.transaction(async t => { |
79 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) | 80 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) |
80 | 81 | ||
81 | const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t) | 82 | let rate = await AccountVideoRateModel.loadByUrl(dislike.id, t) |
83 | if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t) | ||
82 | if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`) | 84 | if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`) |
83 | 85 | ||
84 | await rate.destroy({ transaction: t }) | 86 | await rate.destroy({ transaction: t }) |
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index b263f1ea2..b9b255ddf 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub' | 1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub' |
2 | import { getActorUrl } from '../../../helpers/activitypub' | 2 | import { checkUrlsSameHost, getActorUrl } from '../../../helpers/activitypub' |
3 | import { logger } from '../../../helpers/logger' | 3 | import { logger } from '../../../helpers/logger' |
4 | import { ActorModel } from '../../../models/activitypub/actor' | 4 | import { ActorModel } from '../../../models/activitypub/actor' |
5 | import { processAcceptActivity } from './process-accept' | 5 | import { processAcceptActivity } from './process-accept' |
@@ -25,11 +25,17 @@ const processActivity: { [ P in ActivityType ]: (activity: Activity, byActor: Ac | |||
25 | Like: processLikeActivity | 25 | Like: processLikeActivity |
26 | } | 26 | } |
27 | 27 | ||
28 | async function processActivities (activities: Activity[], signatureActor?: ActorModel, inboxActor?: ActorModel) { | 28 | async function processActivities ( |
29 | activities: Activity[], | ||
30 | options: { | ||
31 | signatureActor?: ActorModel | ||
32 | inboxActor?: ActorModel | ||
33 | outboxUrl?: string | ||
34 | } = {}) { | ||
29 | const actorsCache: { [ url: string ]: ActorModel } = {} | 35 | const actorsCache: { [ url: string ]: ActorModel } = {} |
30 | 36 | ||
31 | for (const activity of activities) { | 37 | for (const activity of activities) { |
32 | if (!signatureActor && [ 'Create', 'Announce', 'Like' ].indexOf(activity.type) === -1) { | 38 | if (!options.signatureActor && [ 'Create', 'Announce', 'Like' ].indexOf(activity.type) === -1) { |
33 | logger.error('Cannot process activity %s (type: %s) without the actor signature.', activity.id, activity.type) | 39 | logger.error('Cannot process activity %s (type: %s) without the actor signature.', activity.id, activity.type) |
34 | continue | 40 | continue |
35 | } | 41 | } |
@@ -37,12 +43,17 @@ async function processActivities (activities: Activity[], signatureActor?: Actor | |||
37 | const actorUrl = getActorUrl(activity.actor) | 43 | const actorUrl = getActorUrl(activity.actor) |
38 | 44 | ||
39 | // When we fetch remote data, we don't have signature | 45 | // When we fetch remote data, we don't have signature |
40 | if (signatureActor && actorUrl !== signatureActor.url) { | 46 | if (options.signatureActor && actorUrl !== options.signatureActor.url) { |
41 | logger.warn('Signature mismatch between %s and %s.', actorUrl, signatureActor.url) | 47 | logger.warn('Signature mismatch between %s and %s, skipping.', actorUrl, options.signatureActor.url) |
42 | continue | 48 | continue |
43 | } | 49 | } |
44 | 50 | ||
45 | const byActor = signatureActor || actorsCache[actorUrl] || await getOrCreateActorAndServerAndModel(actorUrl) | 51 | if (options.outboxUrl && checkUrlsSameHost(options.outboxUrl, actorUrl) !== true) { |
52 | logger.warn('Host mismatch between outbox URL %s and actor URL %s, skipping.', options.outboxUrl, actorUrl) | ||
53 | continue | ||
54 | } | ||
55 | |||
56 | const byActor = options.signatureActor || actorsCache[actorUrl] || await getOrCreateActorAndServerAndModel(actorUrl) | ||
46 | actorsCache[actorUrl] = byActor | 57 | actorsCache[actorUrl] = byActor |
47 | 58 | ||
48 | const activityProcessor = processActivity[activity.type] | 59 | const activityProcessor = processActivity[activity.type] |
@@ -52,7 +63,7 @@ async function processActivities (activities: Activity[], signatureActor?: Actor | |||
52 | } | 63 | } |
53 | 64 | ||
54 | try { | 65 | try { |
55 | await activityProcessor(activity, byActor, inboxActor) | 66 | await activityProcessor(activity, byActor, options.inboxActor) |
56 | } catch (err) { | 67 | } catch (err) { |
57 | logger.warn('Cannot process activity %s.', activity.type, { err }) | 68 | logger.warn('Cannot process activity %s.', activity.type, { err }) |
58 | } | 69 | } |