]>
Commit | Line | Data |
---|---|---|
2ccaeeb3 | 1 | import { Transaction } from 'sequelize' |
1e7eb25f | 2 | import { sendLike, sendUndoDislike, sendUndoLike } from './send' |
1297eb5d C |
3 | import { VideoRateType } from '../../../shared/models/videos' |
4 | import * as Bluebird from 'bluebird' | |
5 | import { getOrCreateActorAndServerAndModel } from './actor' | |
6 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
7 | import { logger } from '../../helpers/logger' | |
74dc3bca | 8 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
5c6d985f | 9 | import { doRequest } from '../../helpers/requests' |
848f499d | 10 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' |
5c6d985f | 11 | import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url' |
1e7eb25f | 12 | import { sendDislike } from './send/send-dislike' |
453e83ea | 13 | import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../typings/models' |
1297eb5d | 14 | |
453e83ea | 15 | async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) { |
1297eb5d C |
16 | let rateCounts = 0 |
17 | ||
5c6d985f | 18 | await Bluebird.map(ratesUrl, async rateUrl => { |
1297eb5d | 19 | try { |
5c6d985f | 20 | // Fetch url |
366caf8b | 21 | const { body } = await doRequest<any>({ |
5c6d985f C |
22 | uri: rateUrl, |
23 | json: true, | |
24 | activityPub: true | |
25 | }) | |
26 | if (!body || !body.actor) throw new Error('Body or body actor is invalid') | |
27 | ||
848f499d | 28 | const actorUrl = getAPId(body.actor) |
5c6d985f C |
29 | if (checkUrlsSameHost(actorUrl, rateUrl) !== true) { |
30 | throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`) | |
31 | } | |
32 | ||
33 | if (checkUrlsSameHost(body.id, rateUrl) !== true) { | |
34 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) | |
35 | } | |
36 | ||
1297eb5d | 37 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) |
5c6d985f | 38 | |
2ba92871 C |
39 | const entry = { |
40 | videoId: video.id, | |
41 | accountId: actor.Account.id, | |
42 | type: rate, | |
43 | url: body.id | |
44 | } | |
45 | ||
46 | const created = await AccountVideoRateModel.upsert(entry) | |
1297eb5d C |
47 | |
48 | if (created) rateCounts += 1 | |
49 | } catch (err) { | |
5c6d985f | 50 | logger.warn('Cannot add rate %s.', rateUrl, { err }) |
1297eb5d C |
51 | } |
52 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
53 | ||
54 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | |
55 | ||
56 | // This is "likes" and "dislikes" | |
1735c825 C |
57 | if (rateCounts !== 0) { |
58 | const field = rate === 'like' ? 'likes' : 'dislikes' | |
59 | await video.increment(field, { by: rateCounts }) | |
60 | } | |
1297eb5d | 61 | } |
2ccaeeb3 | 62 | |
453e83ea C |
63 | async function sendVideoRateChange ( |
64 | account: MAccountActor, | |
65 | video: MVideoAccountLight, | |
66 | likes: number, | |
67 | dislikes: number, | |
68 | t: Transaction | |
69 | ) { | |
2ccaeeb3 C |
70 | const actor = account.Actor |
71 | ||
72 | // Keep the order: first we undo and then we create | |
73 | ||
74 | // Undo Like | |
07197db4 | 75 | if (likes < 0) await sendUndoLike(actor, video, t) |
2ccaeeb3 | 76 | // Undo Dislike |
07197db4 | 77 | if (dislikes < 0) await sendUndoDislike(actor, video, t) |
2ccaeeb3 C |
78 | |
79 | // Like | |
07197db4 | 80 | if (likes > 0) await sendLike(actor, video, t) |
2ccaeeb3 | 81 | // Dislike |
1e7eb25f | 82 | if (dislikes > 0) await sendDislike(actor, video, t) |
2ccaeeb3 C |
83 | } |
84 | ||
453e83ea C |
85 | function getRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) { |
86 | return rateType === 'like' | |
87 | ? getVideoLikeActivityPubUrl(actor, video) | |
88 | : getVideoDislikeActivityPubUrl(actor, video) | |
5c6d985f C |
89 | } |
90 | ||
2ccaeeb3 | 91 | export { |
5c6d985f | 92 | getRateUrl, |
1297eb5d | 93 | createRates, |
07197db4 | 94 | sendVideoRateChange |
2ccaeeb3 | 95 | } |