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/videos/shared | |
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/videos/shared')
-rw-r--r-- | server/lib/activitypub/videos/shared/video-sync-attributes.ts | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/server/lib/activitypub/videos/shared/video-sync-attributes.ts b/server/lib/activitypub/videos/shared/video-sync-attributes.ts index c4e101005..8cf0c87a6 100644 --- a/server/lib/activitypub/videos/shared/video-sync-attributes.ts +++ b/server/lib/activitypub/videos/shared/video-sync-attributes.ts | |||
@@ -1,20 +1,20 @@ | |||
1 | import { runInReadCommittedTransaction } from '@server/helpers/database-utils' | ||
1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | 2 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
3 | import { doJSONRequest } from '@server/helpers/requests' | ||
2 | import { JobQueue } from '@server/lib/job-queue' | 4 | import { JobQueue } from '@server/lib/job-queue' |
3 | import { AccountVideoRateModel } from '@server/models/account/account-video-rate' | 5 | import { VideoModel } from '@server/models/video/video' |
4 | import { VideoCommentModel } from '@server/models/video/video-comment' | 6 | import { VideoCommentModel } from '@server/models/video/video-comment' |
5 | import { VideoShareModel } from '@server/models/video/video-share' | 7 | import { VideoShareModel } from '@server/models/video/video-share' |
6 | import { MVideo } from '@server/types/models' | 8 | import { MVideo } from '@server/types/models' |
7 | import { ActivitypubHttpFetcherPayload, VideoObject } from '@shared/models' | 9 | import { ActivitypubHttpFetcherPayload, ActivityPubOrderedCollection, VideoObject } from '@shared/models' |
8 | import { crawlCollectionPage } from '../../crawl' | 10 | import { crawlCollectionPage } from '../../crawl' |
9 | import { addVideoShares } from '../../share' | 11 | import { addVideoShares } from '../../share' |
10 | import { addVideoComments } from '../../video-comments' | 12 | import { addVideoComments } from '../../video-comments' |
11 | import { createRates } from '../../video-rates' | ||
12 | 13 | ||
13 | const lTags = loggerTagsFactory('ap', 'video') | 14 | const lTags = loggerTagsFactory('ap', 'video') |
14 | 15 | ||
15 | type SyncParam = { | 16 | type SyncParam = { |
16 | likes: boolean | 17 | rates: boolean |
17 | dislikes: boolean | ||
18 | shares: boolean | 18 | shares: boolean |
19 | comments: boolean | 19 | comments: boolean |
20 | thumbnail: boolean | 20 | thumbnail: boolean |
@@ -24,45 +24,57 @@ type SyncParam = { | |||
24 | async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { | 24 | async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { |
25 | logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) | 25 | logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) |
26 | 26 | ||
27 | await syncRates('like', video, fetchedVideo, syncParam.likes) | 27 | const ratePromise = updateVideoRates(video, fetchedVideo) |
28 | await syncRates('dislike', video, fetchedVideo, syncParam.dislikes) | 28 | if (syncParam.rates) await ratePromise |
29 | 29 | ||
30 | await syncShares(video, fetchedVideo, syncParam.shares) | 30 | await syncShares(video, fetchedVideo, syncParam.shares) |
31 | 31 | ||
32 | await syncComments(video, fetchedVideo, syncParam.comments) | 32 | await syncComments(video, fetchedVideo, syncParam.comments) |
33 | } | 33 | } |
34 | 34 | ||
35 | async function updateVideoRates (video: MVideo, fetchedVideo: VideoObject) { | ||
36 | const [ likes, dislikes ] = await Promise.all([ | ||
37 | getRatesCount('like', video, fetchedVideo), | ||
38 | getRatesCount('dislike', video, fetchedVideo) | ||
39 | ]) | ||
40 | |||
41 | return runInReadCommittedTransaction(async t => { | ||
42 | await VideoModel.updateRatesOf(video.id, 'like', likes, t) | ||
43 | await VideoModel.updateRatesOf(video.id, 'dislike', dislikes, t) | ||
44 | }) | ||
45 | } | ||
46 | |||
35 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
36 | 48 | ||
37 | export { | 49 | export { |
38 | SyncParam, | 50 | SyncParam, |
39 | syncVideoExternalAttributes | 51 | syncVideoExternalAttributes, |
52 | updateVideoRates | ||
40 | } | 53 | } |
41 | 54 | ||
42 | // --------------------------------------------------------------------------- | 55 | // --------------------------------------------------------------------------- |
43 | 56 | ||
44 | function createJob (payload: ActivitypubHttpFetcherPayload) { | 57 | async function getRatesCount (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject) { |
45 | return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) | ||
46 | } | ||
47 | |||
48 | function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { | ||
49 | const uri = type === 'like' | 58 | const uri = type === 'like' |
50 | ? fetchedVideo.likes | 59 | ? fetchedVideo.likes |
51 | : fetchedVideo.dislikes | 60 | : fetchedVideo.dislikes |
52 | 61 | ||
53 | if (!isSync) { | 62 | logger.info('Sync %s of video %s', type, video.url) |
54 | const jobType = type === 'like' | 63 | const options = { activityPub: true } |
55 | ? 'video-likes' | 64 | |
56 | : 'video-dislikes' | 65 | const response = await doJSONRequest<ActivityPubOrderedCollection<any>>(uri, options) |
66 | const totalItems = response.body.totalItems | ||
57 | 67 | ||
58 | return createJob({ uri, videoId: video.id, type: jobType }) | 68 | if (isNaN(totalItems)) { |
69 | logger.error('Cannot sync %s of video %s, totalItems is not a number', type, video.url, { body: response.body }) | ||
70 | return | ||
59 | } | 71 | } |
60 | 72 | ||
61 | const handler = items => createRates(items, video, type) | 73 | return totalItems |
62 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate) | 74 | } |
63 | 75 | ||
64 | return crawlCollectionPage<string>(uri, handler, cleaner) | 76 | function createJob (payload: ActivitypubHttpFetcherPayload) { |
65 | .catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) })) | 77 | return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) |
66 | } | 78 | } |
67 | 79 | ||
68 | function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { | 80 | function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { |