]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-add.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-add.ts
1 import * as Bluebird from 'bluebird'
2 import { VideoTorrentObject } from '../../../../shared'
3 import { ActivityAdd } from '../../../../shared/models/activitypub/activity'
4 import { VideoRateType } from '../../../../shared/models/videos/video-rate.type'
5 import { retryTransactionWrapper } from '../../../helpers/database-utils'
6 import { logger } from '../../../helpers/logger'
7 import { database as db } from '../../../initializers'
8 import { AccountInstance } from '../../../models/account/account-interface'
9 import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
10 import { VideoInstance } from '../../../models/video/video-interface'
11 import { getOrCreateAccountAndServer } from '../account'
12 import { getOrCreateVideoChannel } from '../video-channels'
13 import { generateThumbnailFromUrl } from '../videos'
14 import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
15
16 async function processAddActivity (activity: ActivityAdd) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
19 const account = await getOrCreateAccountAndServer(activity.actor)
20
21 if (activityType === 'Video') {
22 const videoChannelUrl = activity.target
23 const videoChannel = await getOrCreateVideoChannel(account, videoChannelUrl)
24
25 return processAddVideo(account, activity, videoChannel, activityObject)
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
34 export {
35 processAddActivity
36 }
37
38 // ---------------------------------------------------------------------------
39
40 async function processAddVideo (account: AccountInstance,
41 activity: ActivityAdd,
42 videoChannel: VideoChannelInstance,
43 videoToCreateData: VideoTorrentObject) {
44 const options = {
45 arguments: [ account, activity, videoChannel, videoToCreateData ],
46 errorMessage: 'Cannot insert the remote video with many retries.'
47 }
48
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
60 if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
61 await addVideoShares(video, videoToCreateData.shares.orderedItems)
62 }
63
64 return video
65 }
66
67 function addRemoteVideo (account: AccountInstance,
68 activity: ActivityAdd,
69 videoChannel: VideoChannelInstance,
70 videoToCreateData: VideoTorrentObject) {
71 logger.debug('Adding remote video %s.', videoToCreateData.id)
72
73 return db.sequelize.transaction(async t => {
74 const sequelizeOptions = {
75 transaction: t
76 }
77
78 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
79
80 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
81 if (videoFromDatabase) return videoFromDatabase
82
83 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
84 const video = db.Video.build(videoData)
85
86 // Don't block on request
87 generateThumbnailFromUrl(video, videoToCreateData.icon)
88 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
89
90 const videoCreated = await video.save(sequelizeOptions)
91
92 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
93 if (videoFileAttributes.length === 0) {
94 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
95 }
96
97 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
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)
103
104 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
105
106 return videoCreated
107 })
108 }
109
110 async 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 }