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/video-rates.ts | |
parent | df66d81583e07ce049daeeef1edc6a87b57b3684 (diff) | |
download | PeerTube-5c6d985faeef1d6793d3f44ca6374f1a9b722806.tar.gz PeerTube-5c6d985faeef1d6793d3f44ca6374f1a9b722806.tar.zst PeerTube-5c6d985faeef1d6793d3f44ca6374f1a9b722806.zip |
Check activities host
Diffstat (limited to 'server/lib/activitypub/video-rates.ts')
-rw-r--r-- | server/lib/activitypub/video-rates.ts | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts index 1619251c3..1854b44c4 100644 --- a/server/lib/activitypub/video-rates.ts +++ b/server/lib/activitypub/video-rates.ts | |||
@@ -8,13 +8,35 @@ import { getOrCreateActorAndServerAndModel } from './actor' | |||
8 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | 8 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
9 | import { logger } from '../../helpers/logger' | 9 | import { logger } from '../../helpers/logger' |
10 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers' | 10 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers' |
11 | import { doRequest } from '../../helpers/requests' | ||
12 | import { checkUrlsSameHost, getActorUrl } from '../../helpers/activitypub' | ||
13 | import { ActorModel } from '../../models/activitypub/actor' | ||
14 | import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url' | ||
11 | 15 | ||
12 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { | 16 | async function createRates (ratesUrl: string[], video: VideoModel, rate: VideoRateType) { |
13 | let rateCounts = 0 | 17 | let rateCounts = 0 |
14 | 18 | ||
15 | await Bluebird.map(actorUrls, async actorUrl => { | 19 | await Bluebird.map(ratesUrl, async rateUrl => { |
16 | try { | 20 | try { |
21 | // Fetch url | ||
22 | const { body } = await doRequest({ | ||
23 | uri: rateUrl, | ||
24 | json: true, | ||
25 | activityPub: true | ||
26 | }) | ||
27 | if (!body || !body.actor) throw new Error('Body or body actor is invalid') | ||
28 | |||
29 | const actorUrl = getActorUrl(body.actor) | ||
30 | if (checkUrlsSameHost(actorUrl, rateUrl) !== true) { | ||
31 | throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`) | ||
32 | } | ||
33 | |||
34 | if (checkUrlsSameHost(body.id, rateUrl) !== true) { | ||
35 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) | ||
36 | } | ||
37 | |||
17 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | 38 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) |
39 | |||
18 | const [ , created ] = await AccountVideoRateModel | 40 | const [ , created ] = await AccountVideoRateModel |
19 | .findOrCreate({ | 41 | .findOrCreate({ |
20 | where: { | 42 | where: { |
@@ -24,13 +46,14 @@ async function createRates (actorUrls: string[], video: VideoModel, rate: VideoR | |||
24 | defaults: { | 46 | defaults: { |
25 | videoId: video.id, | 47 | videoId: video.id, |
26 | accountId: actor.Account.id, | 48 | accountId: actor.Account.id, |
27 | type: rate | 49 | type: rate, |
50 | url: body.id | ||
28 | } | 51 | } |
29 | }) | 52 | }) |
30 | 53 | ||
31 | if (created) rateCounts += 1 | 54 | if (created) rateCounts += 1 |
32 | } catch (err) { | 55 | } catch (err) { |
33 | logger.warn('Cannot add rate %s for actor %s.', rate, actorUrl, { err }) | 56 | logger.warn('Cannot add rate %s.', rateUrl, { err }) |
34 | } | 57 | } |
35 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | 58 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) |
36 | 59 | ||
@@ -62,7 +85,12 @@ async function sendVideoRateChange (account: AccountModel, | |||
62 | if (dislikes > 0) await sendCreateDislike(actor, video, t) | 85 | if (dislikes > 0) await sendCreateDislike(actor, video, t) |
63 | } | 86 | } |
64 | 87 | ||
88 | function getRateUrl (rateType: VideoRateType, actor: ActorModel, video: VideoModel) { | ||
89 | return rateType === 'like' ? getVideoLikeActivityPubUrl(actor, video) : getVideoDislikeActivityPubUrl(actor, video) | ||
90 | } | ||
91 | |||
65 | export { | 92 | export { |
93 | getRateUrl, | ||
66 | createRates, | 94 | createRates, |
67 | sendVideoRateChange | 95 | sendVideoRateChange |
68 | } | 96 | } |