]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-add.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-add.ts
CommitLineData
d7d5611c 1import * as Bluebird from 'bluebird'
54141398
C
2import { VideoTorrentObject } from '../../../../shared'
3import { ActivityAdd } from '../../../../shared/models/activitypub/activity'
16b90975 4import { VideoRateType } from '../../../../shared/models/videos/video-rate.type'
892211e8
C
5import { retryTransactionWrapper } from '../../../helpers/database-utils'
6import { logger } from '../../../helpers/logger'
54141398
C
7import { database as db } from '../../../initializers'
8import { AccountInstance } from '../../../models/account/account-interface'
9import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
16b90975 10import { VideoInstance } from '../../../models/video/video-interface'
0f91ae62 11import { getOrCreateAccountAndServer } from '../account'
892211e8
C
12import { getOrCreateVideoChannel } from '../video-channels'
13import { generateThumbnailFromUrl } from '../videos'
4e50b6a1 14import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
0d0e8dd0
C
15
16async function processAddActivity (activity: ActivityAdd) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
0f91ae62 19 const account = await getOrCreateAccountAndServer(activity.actor)
0d0e8dd0
C
20
21 if (activityType === 'Video') {
20494f12
C
22 const videoChannelUrl = activity.target
23 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
24
79d5caf9 25 return processAddVideo(account, activity, videoChannel, activityObject)
0d0e8dd0
C
26 }
27
28 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
29 return Promise.resolve(undefined)
30}
31
32// ---------------------------------------------------------------------------
33
34export {
35 processAddActivity
36}
37
38// ---------------------------------------------------------------------------
39
4e50b6a1
C
40async function processAddVideo (account: AccountInstance,
41 activity: ActivityAdd,
42 videoChannel: VideoChannelInstance,
43 videoToCreateData: VideoTorrentObject) {
0d0e8dd0 44 const options = {
16b90975 45 arguments: [ account, activity, videoChannel, videoToCreateData ],
0d0e8dd0
C
46 errorMessage: 'Cannot insert the remote video with many retries.'
47 }
48
16b90975
C
49 const video = await retryTransactionWrapper(addRemoteVideo, options)
50
51 // Process outside the transaction because we could fetch remote data
52 if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
53 await createRates(videoToCreateData.likes.orderedItems, video, 'like')
54 }
55
56 if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
57 await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
58 }
59
4e50b6a1
C
60 if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
61 await addVideoShares(video, videoToCreateData.shares.orderedItems)
62 }
63
16b90975 64 return video
0d0e8dd0
C
65}
66
892211e8
C
67function addRemoteVideo (account: AccountInstance,
68 activity: ActivityAdd,
69 videoChannel: VideoChannelInstance,
70 videoToCreateData: VideoTorrentObject) {
c46edbc2 71 logger.debug('Adding remote video %s.', videoToCreateData.id)
0d0e8dd0 72
d8465018 73 return db.sequelize.transaction(async t => {
0d0e8dd0
C
74 const sequelizeOptions = {
75 transaction: t
76 }
77
0d0e8dd0
C
78 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
79
d7d5611c 80 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
63c93323 81 if (videoFromDatabase) return videoFromDatabase
d7d5611c 82
9a27cdc2 83 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
0d0e8dd0
C
84 const video = db.Video.build(videoData)
85
86 // Don't block on request
87 generateThumbnailFromUrl(video, videoToCreateData.icon)
350e31d6 88 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
0d0e8dd0
C
89
90 const videoCreated = await video.save(sequelizeOptions)
91
79d5caf9 92 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
20494f12
C
93 if (videoFileAttributes.length === 0) {
94 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
95 }
0d0e8dd0 96
20494f12 97 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
0d0e8dd0
C
98 await Promise.all(tasks)
99
100 const tags = videoToCreateData.tag.map(t => t.name)
101 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
102 await videoCreated.setTags(tagInstances, sequelizeOptions)
d8465018
C
103
104 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
105
106 return videoCreated
0d0e8dd0 107 })
0d0e8dd0 108}
16b90975
C
109
110async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) {
111 let rateCounts = 0
112 const tasks: Bluebird<any>[] = []
113
114 for (const accountUrl of accountUrls) {
115 const account = await getOrCreateAccountAndServer(accountUrl)
116 const p = db.AccountVideoRate
117 .create({
118 videoId: video.id,
119 accountId: account.id,
120 type: rate
121 })
122 .then(() => rateCounts += 1)
123
124 tasks.push(p)
125 }
126
127 await Promise.all(tasks)
128
129 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
130
131 // This is "likes" and "dislikes"
132 await video.increment(rate + 's', { by: rateCounts })
133
134 return
135}