diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-02 09:35:01 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-02 16:57:53 +0200 |
commit | 69290ab37b8aead01477b9b98fdfad0e69b08582 (patch) | |
tree | 4b93d349dfce5014925e7f060b3b158ac9b3bbc2 /server/lib/activitypub/videos/shared/video-sync-attributes.ts | |
parent | 81628e5069e0168b11857f276fe8e03b93102dde (diff) | |
download | PeerTube-69290ab37b8aead01477b9b98fdfad0e69b08582.tar.gz PeerTube-69290ab37b8aead01477b9b98fdfad0e69b08582.tar.zst PeerTube-69290ab37b8aead01477b9b98fdfad0e69b08582.zip |
Refactor AP video update
Diffstat (limited to 'server/lib/activitypub/videos/shared/video-sync-attributes.ts')
-rw-r--r-- | server/lib/activitypub/videos/shared/video-sync-attributes.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/server/lib/activitypub/videos/shared/video-sync-attributes.ts b/server/lib/activitypub/videos/shared/video-sync-attributes.ts new file mode 100644 index 000000000..181893c68 --- /dev/null +++ b/server/lib/activitypub/videos/shared/video-sync-attributes.ts | |||
@@ -0,0 +1,75 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { JobQueue } from '@server/lib/job-queue' | ||
3 | import { AccountVideoRateModel } from '@server/models/account/account-video-rate' | ||
4 | import { VideoCommentModel } from '@server/models/video/video-comment' | ||
5 | import { VideoShareModel } from '@server/models/video/video-share' | ||
6 | import { MVideo } from '@server/types/models' | ||
7 | import { ActivitypubHttpFetcherPayload, VideoObject } from '@shared/models' | ||
8 | import { crawlCollectionPage } from '../../crawl' | ||
9 | import { addVideoShares } from '../../share' | ||
10 | import { addVideoComments } from '../../video-comments' | ||
11 | import { createRates } from '../../video-rates' | ||
12 | |||
13 | import Bluebird = require('bluebird') | ||
14 | |||
15 | type SyncParam = { | ||
16 | likes: boolean | ||
17 | dislikes: boolean | ||
18 | shares: boolean | ||
19 | comments: boolean | ||
20 | thumbnail: boolean | ||
21 | refreshVideo?: boolean | ||
22 | } | ||
23 | |||
24 | async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { | ||
25 | logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) | ||
26 | |||
27 | const jobPayloads: ActivitypubHttpFetcherPayload[] = [] | ||
28 | |||
29 | if (syncParam.likes === true) { | ||
30 | const handler = items => createRates(items, video, 'like') | ||
31 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate) | ||
32 | |||
33 | await crawlCollectionPage<string>(fetchedVideo.likes, handler, cleaner) | ||
34 | .catch(err => logger.error('Cannot add likes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.likes })) | ||
35 | } else { | ||
36 | jobPayloads.push({ uri: fetchedVideo.likes, videoId: video.id, type: 'video-likes' as 'video-likes' }) | ||
37 | } | ||
38 | |||
39 | if (syncParam.dislikes === true) { | ||
40 | const handler = items => createRates(items, video, 'dislike') | ||
41 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate) | ||
42 | |||
43 | await crawlCollectionPage<string>(fetchedVideo.dislikes, handler, cleaner) | ||
44 | .catch(err => logger.error('Cannot add dislikes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.dislikes })) | ||
45 | } else { | ||
46 | jobPayloads.push({ uri: fetchedVideo.dislikes, videoId: video.id, type: 'video-dislikes' as 'video-dislikes' }) | ||
47 | } | ||
48 | |||
49 | if (syncParam.shares === true) { | ||
50 | const handler = items => addVideoShares(items, video) | ||
51 | const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) | ||
52 | |||
53 | await crawlCollectionPage<string>(fetchedVideo.shares, handler, cleaner) | ||
54 | .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares })) | ||
55 | } else { | ||
56 | jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' }) | ||
57 | } | ||
58 | |||
59 | if (syncParam.comments === true) { | ||
60 | const handler = items => addVideoComments(items) | ||
61 | const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) | ||
62 | |||
63 | await crawlCollectionPage<string>(fetchedVideo.comments, handler, cleaner) | ||
64 | .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments })) | ||
65 | } else { | ||
66 | jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' }) | ||
67 | } | ||
68 | |||
69 | await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })) | ||
70 | } | ||
71 | |||
72 | export { | ||
73 | SyncParam, | ||
74 | syncVideoExternalAttributes | ||
75 | } | ||