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