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