aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/videos/shared/video-sync-attributes.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-18 11:17:35 +0100
committerChocobozzz <me@florianbigard.com>2022-03-18 11:21:50 +0100
commit57e4e1c1a95c3a81a967f54ecc2a510d8b0e129c (patch)
treefcf12670d643ec4a3b5eccdfa834227c0417d988 /server/lib/activitypub/videos/shared/video-sync-attributes.ts
parent2e3f7a5a6fbae276d3ba1cb1b08289917ec7604b (diff)
downloadPeerTube-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/video-sync-attributes.ts')
-rw-r--r--server/lib/activitypub/videos/shared/video-sync-attributes.ts56
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 @@
1import { runInReadCommittedTransaction } from '@server/helpers/database-utils'
1import { logger, loggerTagsFactory } from '@server/helpers/logger' 2import { logger, loggerTagsFactory } from '@server/helpers/logger'
3import { doJSONRequest } from '@server/helpers/requests'
2import { JobQueue } from '@server/lib/job-queue' 4import { JobQueue } from '@server/lib/job-queue'
3import { AccountVideoRateModel } from '@server/models/account/account-video-rate' 5import { VideoModel } from '@server/models/video/video'
4import { VideoCommentModel } from '@server/models/video/video-comment' 6import { VideoCommentModel } from '@server/models/video/video-comment'
5import { VideoShareModel } from '@server/models/video/video-share' 7import { VideoShareModel } from '@server/models/video/video-share'
6import { MVideo } from '@server/types/models' 8import { MVideo } from '@server/types/models'
7import { ActivitypubHttpFetcherPayload, VideoObject } from '@shared/models' 9import { ActivitypubHttpFetcherPayload, ActivityPubOrderedCollection, VideoObject } from '@shared/models'
8import { crawlCollectionPage } from '../../crawl' 10import { crawlCollectionPage } from '../../crawl'
9import { addVideoShares } from '../../share' 11import { addVideoShares } from '../../share'
10import { addVideoComments } from '../../video-comments' 12import { addVideoComments } from '../../video-comments'
11import { createRates } from '../../video-rates'
12 13
13const lTags = loggerTagsFactory('ap', 'video') 14const lTags = loggerTagsFactory('ap', 'video')
14 15
15type SyncParam = { 16type 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 = {
24async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { 24async 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
35async 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
37export { 49export {
38 SyncParam, 50 SyncParam,
39 syncVideoExternalAttributes 51 syncVideoExternalAttributes,
52 updateVideoRates
40} 53}
41 54
42// --------------------------------------------------------------------------- 55// ---------------------------------------------------------------------------
43 56
44function createJob (payload: ActivitypubHttpFetcherPayload) { 57async function getRatesCount (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject) {
45 return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
46}
47
48function 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) 76function 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
68function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { 80function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {