]>
Commit | Line | Data |
---|---|---|
2ccaeeb3 C |
1 | import { Transaction } from 'sequelize' |
2 | import { AccountModel } from '../../models/account/account' | |
3 | import { VideoModel } from '../../models/video/video' | |
1e7eb25f | 4 | import { sendLike, sendUndoDislike, sendUndoLike } from './send' |
1297eb5d C |
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' | |
5c6d985f | 11 | import { doRequest } from '../../helpers/requests' |
848f499d | 12 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' |
5c6d985f C |
13 | import { ActorModel } from '../../models/activitypub/actor' |
14 | import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url' | |
1e7eb25f | 15 | import { sendDislike } from './send/send-dislike' |
1297eb5d | 16 | |
5c6d985f | 17 | async function createRates (ratesUrl: string[], video: VideoModel, rate: VideoRateType) { |
1297eb5d C |
18 | let rateCounts = 0 |
19 | ||
5c6d985f | 20 | await Bluebird.map(ratesUrl, async rateUrl => { |
1297eb5d | 21 | try { |
5c6d985f C |
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 | ||
848f499d | 30 | const actorUrl = getAPId(body.actor) |
5c6d985f C |
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 | ||
1297eb5d | 39 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) |
5c6d985f | 40 | |
1297eb5d C |
41 | const [ , created ] = await AccountVideoRateModel |
42 | .findOrCreate({ | |
43 | where: { | |
44 | videoId: video.id, | |
45 | accountId: actor.Account.id | |
46 | }, | |
47 | defaults: { | |
48 | videoId: video.id, | |
49 | accountId: actor.Account.id, | |
5c6d985f C |
50 | type: rate, |
51 | url: body.id | |
1297eb5d C |
52 | } |
53 | }) | |
54 | ||
55 | if (created) rateCounts += 1 | |
56 | } catch (err) { | |
5c6d985f | 57 | logger.warn('Cannot add rate %s.', rateUrl, { err }) |
1297eb5d C |
58 | } |
59 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
60 | ||
61 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | |
62 | ||
63 | // This is "likes" and "dislikes" | |
64 | if (rateCounts !== 0) await video.increment(rate + 's', { by: rateCounts }) | |
65 | ||
66 | return | |
67 | } | |
2ccaeeb3 | 68 | |
07197db4 C |
69 | async function sendVideoRateChange (account: AccountModel, |
70 | video: VideoModel, | |
71 | likes: number, | |
72 | dislikes: number, | |
73 | t: Transaction) { | |
2ccaeeb3 C |
74 | const actor = account.Actor |
75 | ||
76 | // Keep the order: first we undo and then we create | |
77 | ||
78 | // Undo Like | |
07197db4 | 79 | if (likes < 0) await sendUndoLike(actor, video, t) |
2ccaeeb3 | 80 | // Undo Dislike |
07197db4 | 81 | if (dislikes < 0) await sendUndoDislike(actor, video, t) |
2ccaeeb3 C |
82 | |
83 | // Like | |
07197db4 | 84 | if (likes > 0) await sendLike(actor, video, t) |
2ccaeeb3 | 85 | // Dislike |
1e7eb25f | 86 | if (dislikes > 0) await sendDislike(actor, video, t) |
2ccaeeb3 C |
87 | } |
88 | ||
5c6d985f C |
89 | function getRateUrl (rateType: VideoRateType, actor: ActorModel, video: VideoModel) { |
90 | return rateType === 'like' ? getVideoLikeActivityPubUrl(actor, video) : getVideoDislikeActivityPubUrl(actor, video) | |
91 | } | |
92 | ||
2ccaeeb3 | 93 | export { |
5c6d985f | 94 | getRateUrl, |
1297eb5d | 95 | createRates, |
07197db4 | 96 | sendVideoRateChange |
2ccaeeb3 | 97 | } |