diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-18 11:17:35 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-18 11:21:50 +0100 |
commit | 57e4e1c1a95c3a81a967f54ecc2a510d8b0e129c (patch) | |
tree | fcf12670d643ec4a3b5eccdfa834227c0417d988 /server/lib/activitypub/video-rates.ts | |
parent | 2e3f7a5a6fbae276d3ba1cb1b08289917ec7604b (diff) | |
download | PeerTube-57e4e1c1a95c3a81a967f54ecc2a510d8b0e129c.tar.gz PeerTube-57e4e1c1a95c3a81a967f54ecc2a510d8b0e129c.tar.zst PeerTube-57e4e1c1a95c3a81a967f54ecc2a510d8b0e129c.zip |
Don't store remote rates of remote videos
In the future we'll stop to expose all available rates to improve users
privacy
Diffstat (limited to 'server/lib/activitypub/video-rates.ts')
-rw-r--r-- | server/lib/activitypub/video-rates.ts | 79 |
1 files changed, 24 insertions, 55 deletions
diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts index 04aa5eae9..2e7920f4e 100644 --- a/server/lib/activitypub/video-rates.ts +++ b/server/lib/activitypub/video-rates.ts | |||
@@ -1,49 +1,21 @@ | |||
1 | import { map } from 'bluebird' | ||
2 | import { Transaction } from 'sequelize' | 1 | import { Transaction } from 'sequelize' |
3 | import { doJSONRequest } from '@server/helpers/requests' | ||
4 | import { VideoRateType } from '../../../shared/models/videos' | 2 | import { VideoRateType } from '../../../shared/models/videos' |
5 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' | 3 | import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoFullLight, MVideoId } from '../../types/models' |
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' | 4 | import { sendLike, sendUndoDislike, sendUndoLike } from './send' |
12 | import { sendDislike } from './send/send-dislike' | 5 | import { sendDislike } from './send/send-dislike' |
13 | import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url' | 6 | import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url' |
14 | 7 | import { federateVideoIfNeeded } from './videos' | |
15 | const lTags = loggerTagsFactory('ap', 'video-rate', 'create') | ||
16 | |||
17 | async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) { | ||
18 | await 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 | 8 | ||
27 | async function sendVideoRateChange ( | 9 | async function sendVideoRateChange ( |
28 | account: MAccountActor, | 10 | account: MAccountActor, |
29 | video: MVideoAccountLight, | 11 | video: MVideoFullLight, |
30 | likes: number, | 12 | likes: number, |
31 | dislikes: number, | 13 | dislikes: number, |
32 | t: Transaction | 14 | t: Transaction |
33 | ) { | 15 | ) { |
34 | const actor = account.Actor | 16 | if (video.isOwned()) return federateVideoIfNeeded(video, false, t) |
35 | 17 | ||
36 | // Keep the order: first we undo and then we create | 18 | return sendVideoRateChangeToOrigin(account, video, likes, dislikes, t) |
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 | } | 19 | } |
48 | 20 | ||
49 | function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) { | 21 | function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) { |
@@ -56,35 +28,32 @@ function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVid | |||
56 | 28 | ||
57 | export { | 29 | export { |
58 | getLocalRateUrl, | 30 | getLocalRateUrl, |
59 | createRates, | ||
60 | sendVideoRateChange | 31 | sendVideoRateChange |
61 | } | 32 | } |
62 | 33 | ||
63 | // --------------------------------------------------------------------------- | 34 | // --------------------------------------------------------------------------- |
64 | 35 | ||
65 | async function createRate (rateUrl: string, video: MVideo, rate: VideoRateType) { | 36 | async function sendVideoRateChangeToOrigin ( |
66 | // Fetch url | 37 | account: MAccountActor, |
67 | const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true }) | 38 | video: MVideoAccountLight, |
68 | if (!body || !body.actor) throw new Error('Body or body actor is invalid') | 39 | likes: number, |
69 | 40 | dislikes: number, | |
70 | const actorUrl = getAPId(body.actor) | 41 | t: Transaction |
71 | if (checkUrlsSameHost(actorUrl, rateUrl) !== true) { | 42 | ) { |
72 | throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`) | 43 | // Local video, we don't need to send like |
73 | } | 44 | if (video.isOwned()) return |
74 | 45 | ||
75 | if (checkUrlsSameHost(body.id, rateUrl) !== true) { | 46 | const actor = account.Actor |
76 | throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`) | ||
77 | } | ||
78 | 47 | ||
79 | const actor = await getOrCreateAPActor(actorUrl) | 48 | // Keep the order: first we undo and then we create |
80 | 49 | ||
81 | const entry = { | 50 | // Undo Like |
82 | videoId: video.id, | 51 | if (likes < 0) await sendUndoLike(actor, video, t) |
83 | accountId: actor.Account.id, | 52 | // Undo Dislike |
84 | type: rate, | 53 | if (dislikes < 0) await sendUndoDislike(actor, video, t) |
85 | url: body.id | ||
86 | } | ||
87 | 54 | ||
88 | // Video "likes"/"dislikes" will be updated by the caller | 55 | // Like |
89 | await AccountVideoRateModel.upsert(entry) | 56 | if (likes > 0) await sendLike(actor, video, t) |
57 | // Dislike | ||
58 | if (dislikes > 0) await sendDislike(actor, video, t) | ||
90 | } | 59 | } |