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