]>
Commit | Line | Data |
---|---|---|
db4b15f2 | 1 | import * as Bluebird from 'bluebird' |
2ccaeeb3 | 2 | import { Transaction } from 'sequelize' |
db4b15f2 | 3 | import { doJSONRequest } from '@server/helpers/requests' |
1297eb5d | 4 | import { VideoRateType } from '../../../shared/models/videos' |
db4b15f2 | 5 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' |
1297eb5d | 6 | import { logger } from '../../helpers/logger' |
74dc3bca | 7 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
db4b15f2 | 8 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
26d6bf65 | 9 | import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models' |
db4b15f2 C |
10 | import { getOrCreateActorAndServerAndModel } from './actor' |
11 | import { sendLike, sendUndoDislike, sendUndoLike } from './send' | |
12 | import { sendDislike } from './send/send-dislike' | |
13 | import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url' | |
1297eb5d | 14 | |
453e83ea | 15 | async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) { |
5c6d985f | 16 | await Bluebird.map(ratesUrl, async rateUrl => { |
1297eb5d | 17 | try { |
49af5ac8 | 18 | await createRate(rateUrl, video, rate) |
1297eb5d | 19 | } catch (err) { |
5c6d985f | 20 | logger.warn('Cannot add rate %s.', rateUrl, { err }) |
1297eb5d C |
21 | } |
22 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
1297eb5d | 23 | } |
2ccaeeb3 | 24 | |
453e83ea C |
25 | async function sendVideoRateChange ( |
26 | account: MAccountActor, | |
27 | video: MVideoAccountLight, | |
28 | likes: number, | |
29 | dislikes: number, | |
30 | t: Transaction | |
31 | ) { | |
2ccaeeb3 C |
32 | const actor = account.Actor |
33 | ||
34 | // Keep the order: first we undo and then we create | |
35 | ||
36 | // Undo Like | |
07197db4 | 37 | if (likes < 0) await sendUndoLike(actor, video, t) |
2ccaeeb3 | 38 | // Undo Dislike |
07197db4 | 39 | if (dislikes < 0) await sendUndoDislike(actor, video, t) |
2ccaeeb3 C |
40 | |
41 | // Like | |
07197db4 | 42 | if (likes > 0) await sendLike(actor, video, t) |
2ccaeeb3 | 43 | // Dislike |
1e7eb25f | 44 | if (dislikes > 0) await sendDislike(actor, video, t) |
2ccaeeb3 C |
45 | } |
46 | ||
de94ac86 | 47 | function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) { |
453e83ea | 48 | return rateType === 'like' |
de94ac86 C |
49 | ? getVideoLikeActivityPubUrlByLocalActor(actor, video) |
50 | : getVideoDislikeActivityPubUrlByLocalActor(actor, video) | |
5c6d985f C |
51 | } |
52 | ||
49af5ac8 C |
53 | // --------------------------------------------------------------------------- |
54 | ||
2ccaeeb3 | 55 | export { |
de94ac86 | 56 | getLocalRateUrl, |
1297eb5d | 57 | createRates, |
07197db4 | 58 | sendVideoRateChange |
2ccaeeb3 | 59 | } |
49af5ac8 C |
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 | } |