]>
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' |
de94ac86 | 11 | import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url' |
1e7eb25f | 12 | import { sendDislike } from './send/send-dislike' |
26d6bf65 | 13 | import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models' |
1297eb5d | 14 | |
453e83ea | 15 | async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) { |
5c6d985f | 16 | await Bluebird.map(ratesUrl, async rateUrl => { |
1297eb5d | 17 | try { |
5c6d985f | 18 | // Fetch url |
366caf8b | 19 | const { body } = await doRequest<any>({ |
5c6d985f C |
20 | uri: rateUrl, |
21 | json: true, | |
22 | activityPub: true | |
23 | }) | |
24 | if (!body || !body.actor) throw new Error('Body or body actor is invalid') | |
25 | ||
848f499d | 26 | const actorUrl = getAPId(body.actor) |
5c6d985f C |
27 | if (checkUrlsSameHost(actorUrl, rateUrl) !== true) { |
28 | throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`) | |
29 | } | |
30 | ||
31 | if (checkUrlsSameHost(body.id, rateUrl) !== true) { | |
32 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) | |
33 | } | |
34 | ||
1297eb5d | 35 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) |
5c6d985f | 36 | |
2ba92871 C |
37 | const entry = { |
38 | videoId: video.id, | |
39 | accountId: actor.Account.id, | |
40 | type: rate, | |
41 | url: body.id | |
42 | } | |
43 | ||
b49f22d8 C |
44 | // Video "likes"/"dislikes" will be updated by the caller |
45 | await AccountVideoRateModel.upsert(entry) | |
1297eb5d | 46 | } catch (err) { |
5c6d985f | 47 | logger.warn('Cannot add rate %s.', rateUrl, { err }) |
1297eb5d C |
48 | } |
49 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
1297eb5d | 50 | } |
2ccaeeb3 | 51 | |
453e83ea C |
52 | async function sendVideoRateChange ( |
53 | account: MAccountActor, | |
54 | video: MVideoAccountLight, | |
55 | likes: number, | |
56 | dislikes: number, | |
57 | t: Transaction | |
58 | ) { | |
2ccaeeb3 C |
59 | const actor = account.Actor |
60 | ||
61 | // Keep the order: first we undo and then we create | |
62 | ||
63 | // Undo Like | |
07197db4 | 64 | if (likes < 0) await sendUndoLike(actor, video, t) |
2ccaeeb3 | 65 | // Undo Dislike |
07197db4 | 66 | if (dislikes < 0) await sendUndoDislike(actor, video, t) |
2ccaeeb3 C |
67 | |
68 | // Like | |
07197db4 | 69 | if (likes > 0) await sendLike(actor, video, t) |
2ccaeeb3 | 70 | // Dislike |
1e7eb25f | 71 | if (dislikes > 0) await sendDislike(actor, video, t) |
2ccaeeb3 C |
72 | } |
73 | ||
de94ac86 | 74 | function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) { |
453e83ea | 75 | return rateType === 'like' |
de94ac86 C |
76 | ? getVideoLikeActivityPubUrlByLocalActor(actor, video) |
77 | : getVideoDislikeActivityPubUrlByLocalActor(actor, video) | |
5c6d985f C |
78 | } |
79 | ||
2ccaeeb3 | 80 | export { |
de94ac86 | 81 | getLocalRateUrl, |
1297eb5d | 82 | createRates, |
07197db4 | 83 | sendVideoRateChange |
2ccaeeb3 | 84 | } |