]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-add.ts
Add activitypub migration script
[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'
d7d5611c 14import { 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
16b90975
C
40async function processAddVideo (
41 account: AccountInstance,
42 activity: ActivityAdd,
43 videoChannel: VideoChannelInstance,
44 videoToCreateData: VideoTorrentObject
45) {
0d0e8dd0 46 const options = {
16b90975 47 arguments: [ account, activity, videoChannel, videoToCreateData ],
0d0e8dd0
C
48 errorMessage: 'Cannot insert the remote video with many retries.'
49 }
50
16b90975
C
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 return video
0d0e8dd0
C
63}
64
892211e8
C
65function addRemoteVideo (account: AccountInstance,
66 activity: ActivityAdd,
67 videoChannel: VideoChannelInstance,
68 videoToCreateData: VideoTorrentObject) {
c46edbc2 69 logger.debug('Adding remote video %s.', videoToCreateData.id)
0d0e8dd0 70
d8465018 71 return db.sequelize.transaction(async t => {
0d0e8dd0
C
72 const sequelizeOptions = {
73 transaction: t
74 }
75
0d0e8dd0
C
76 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
77
d7d5611c 78 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
63c93323 79 if (videoFromDatabase) return videoFromDatabase
d7d5611c 80
9a27cdc2 81 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
0d0e8dd0
C
82 const video = db.Video.build(videoData)
83
84 // Don't block on request
85 generateThumbnailFromUrl(video, videoToCreateData.icon)
350e31d6 86 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
0d0e8dd0
C
87
88 const videoCreated = await video.save(sequelizeOptions)
89
79d5caf9 90 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
20494f12
C
91 if (videoFileAttributes.length === 0) {
92 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
93 }
0d0e8dd0 94
20494f12 95 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t }))
0d0e8dd0
C
96 await Promise.all(tasks)
97
98 const tags = videoToCreateData.tag.map(t => t.name)
99 const tagInstances = await db.Tag.findOrCreateTags(tags, t)
100 await videoCreated.setTags(tagInstances, sequelizeOptions)
d8465018
C
101
102 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
103
104 return videoCreated
0d0e8dd0 105 })
0d0e8dd0 106}
16b90975
C
107
108async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) {
109 let rateCounts = 0
110 const tasks: Bluebird<any>[] = []
111
112 for (const accountUrl of accountUrls) {
113 const account = await getOrCreateAccountAndServer(accountUrl)
114 const p = db.AccountVideoRate
115 .create({
116 videoId: video.id,
117 accountId: account.id,
118 type: rate
119 })
120 .then(() => rateCounts += 1)
121
122 tasks.push(p)
123 }
124
125 await Promise.all(tasks)
126
127 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
128
129 // This is "likes" and "dislikes"
130 await video.increment(rate + 's', { by: rateCounts })
131
132 return
133}