1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
import { runInReadCommittedTransaction } from '@server/helpers/database-utils'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { doJSONRequest } from '@server/helpers/requests'
import { JobQueue } from '@server/lib/job-queue'
import { VideoModel } from '@server/models/video/video'
import { VideoCommentModel } from '@server/models/video/video-comment'
import { VideoShareModel } from '@server/models/video/video-share'
import { MVideo } from '@server/types/models'
import { ActivitypubHttpFetcherPayload, ActivityPubOrderedCollection, VideoObject } from '@shared/models'
import { crawlCollectionPage } from '../../crawl'
import { addVideoShares } from '../../share'
import { addVideoComments } from '../../video-comments'
const lTags = loggerTagsFactory('ap', 'video')
type SyncParam = {
rates: boolean
shares: boolean
comments: boolean
thumbnail: boolean
refreshVideo?: boolean
}
async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) {
logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid)
const ratePromise = updateVideoRates(video, fetchedVideo)
if (syncParam.rates) await ratePromise
await syncShares(video, fetchedVideo, syncParam.shares)
await syncComments(video, fetchedVideo, syncParam.comments)
}
async function updateVideoRates (video: MVideo, fetchedVideo: VideoObject) {
const [ likes, dislikes ] = await Promise.all([
getRatesCount('like', video, fetchedVideo),
getRatesCount('dislike', video, fetchedVideo)
])
return runInReadCommittedTransaction(async t => {
await VideoModel.updateRatesOf(video.id, 'like', likes, t)
await VideoModel.updateRatesOf(video.id, 'dislike', dislikes, t)
})
}
// ---------------------------------------------------------------------------
export {
SyncParam,
syncVideoExternalAttributes,
updateVideoRates
}
// ---------------------------------------------------------------------------
async function getRatesCount (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject) {
const uri = type === 'like'
? fetchedVideo.likes
: fetchedVideo.dislikes
logger.info('Sync %s of video %s', type, video.url)
const options = { activityPub: true }
const response = await doJSONRequest<ActivityPubOrderedCollection<any>>(uri, options)
const totalItems = response.body.totalItems
if (isNaN(totalItems)) {
logger.error('Cannot sync %s of video %s, totalItems is not a number', type, video.url, { body: response.body })
return
}
return totalItems
}
function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
const uri = fetchedVideo.shares
if (!isSync) {
return createJob({ uri, videoId: video.id, type: 'video-shares' })
}
const handler = items => addVideoShares(items, video)
const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
return crawlCollectionPage<string>(uri, handler, cleaner)
.catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
}
function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
const uri = fetchedVideo.comments
if (!isSync) {
return createJob({ uri, videoId: video.id, type: 'video-comments' })
}
const handler = items => addVideoComments(items)
const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
return crawlCollectionPage<string>(uri, handler, cleaner)
.catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
}
function createJob (payload: ActivitypubHttpFetcherPayload) {
return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
}
|