diff options
Diffstat (limited to 'server/lib/activitypub/video-rates.ts')
-rw-r--r-- | server/lib/activitypub/video-rates.ts | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts index f40c07fea..091f4ec23 100644 --- a/server/lib/activitypub/video-rates.ts +++ b/server/lib/activitypub/video-rates.ts | |||
@@ -15,30 +15,7 @@ import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlBy | |||
15 | async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) { | 15 | async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) { |
16 | await Bluebird.map(ratesUrl, async rateUrl => { | 16 | await Bluebird.map(ratesUrl, async rateUrl => { |
17 | try { | 17 | try { |
18 | // Fetch url | 18 | await createRate(rateUrl, video, rate) |
19 | const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true }) | ||
20 | if (!body || !body.actor) throw new Error('Body or body actor is invalid') | ||
21 | |||
22 | const actorUrl = getAPId(body.actor) | ||
23 | if (checkUrlsSameHost(actorUrl, rateUrl) !== true) { | ||
24 | throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`) | ||
25 | } | ||
26 | |||
27 | if (checkUrlsSameHost(body.id, rateUrl) !== true) { | ||
28 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) | ||
29 | } | ||
30 | |||
31 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
32 | |||
33 | const entry = { | ||
34 | videoId: video.id, | ||
35 | accountId: actor.Account.id, | ||
36 | type: rate, | ||
37 | url: body.id | ||
38 | } | ||
39 | |||
40 | // Video "likes"/"dislikes" will be updated by the caller | ||
41 | await AccountVideoRateModel.upsert(entry) | ||
42 | } catch (err) { | 19 | } catch (err) { |
43 | logger.warn('Cannot add rate %s.', rateUrl, { err }) | 20 | logger.warn('Cannot add rate %s.', rateUrl, { err }) |
44 | } | 21 | } |
@@ -73,8 +50,39 @@ function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVid | |||
73 | : getVideoDislikeActivityPubUrlByLocalActor(actor, video) | 50 | : getVideoDislikeActivityPubUrlByLocalActor(actor, video) |
74 | } | 51 | } |
75 | 52 | ||
53 | // --------------------------------------------------------------------------- | ||
54 | |||
76 | export { | 55 | export { |
77 | getLocalRateUrl, | 56 | getLocalRateUrl, |
78 | createRates, | 57 | createRates, |
79 | sendVideoRateChange | 58 | sendVideoRateChange |
80 | } | 59 | } |
60 | |||
61 | // --------------------------------------------------------------------------- | ||
62 | |||
63 | async function createRate (rateUrl: string, video: MVideo, rate: VideoRateType) { | ||
64 | // Fetch url | ||
65 | const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true }) | ||
66 | if (!body || !body.actor) throw new Error('Body or body actor is invalid') | ||
67 | |||
68 | const actorUrl = getAPId(body.actor) | ||
69 | if (checkUrlsSameHost(actorUrl, rateUrl) !== true) { | ||
70 | throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`) | ||
71 | } | ||
72 | |||
73 | if (checkUrlsSameHost(body.id, rateUrl) !== true) { | ||
74 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) | ||
75 | } | ||
76 | |||
77 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
78 | |||
79 | const entry = { | ||
80 | videoId: video.id, | ||
81 | accountId: actor.Account.id, | ||
82 | type: rate, | ||
83 | url: body.id | ||
84 | } | ||
85 | |||
86 | // Video "likes"/"dislikes" will be updated by the caller | ||
87 | await AccountVideoRateModel.upsert(entry) | ||
88 | } | ||