]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-create.ts
Fix update host script
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
1 import { ActivityCreate, VideoChannelObject } from '../../../../shared'
2 import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object'
3 import { logger, retryTransactionWrapper } from '../../../helpers'
4 import { getOrCreateAccount, getVideoChannelActivityPubUrl } from '../../../helpers/activitypub'
5 import { database as db } from '../../../initializers'
6 import { AccountInstance } from '../../../models/account/account-interface'
7 import { videoChannelActivityObjectToDBAttributes } from './misc'
8
9 async function processCreateActivity (activity: ActivityCreate) {
10 const activityObject = activity.object
11 const activityType = activityObject.type
12 const account = await getOrCreateAccount(activity.actor)
13
14 if (activityType === 'VideoChannel') {
15 return processCreateVideoChannel(account, activityObject as VideoChannelObject)
16 } else if (activityType === 'Flag') {
17 return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
18 }
19
20 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
21 return Promise.resolve(undefined)
22 }
23
24 // ---------------------------------------------------------------------------
25
26 export {
27 processCreateActivity
28 }
29
30 // ---------------------------------------------------------------------------
31
32 function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
33 const options = {
34 arguments: [ account, videoChannelToCreateData ],
35 errorMessage: 'Cannot insert the remote video channel with many retries.'
36 }
37
38 return retryTransactionWrapper(addRemoteVideoChannel, options)
39 }
40
41 function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
42 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
43
44 return db.sequelize.transaction(async t => {
45 let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
46 if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.')
47
48 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
49 videoChannel = db.VideoChannel.build(videoChannelData)
50 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
51
52 videoChannel = await videoChannel.save({ transaction: t })
53 logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
54
55 return videoChannel
56 })
57 }
58
59 function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
60 const options = {
61 arguments: [ account, videoAbuseToCreateData ],
62 errorMessage: 'Cannot insert the remote video abuse with many retries.'
63 }
64
65 return retryTransactionWrapper(addRemoteVideoAbuse, options)
66 }
67
68 function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
69 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
70
71 return db.sequelize.transaction(async t => {
72 const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
73 if (!video) {
74 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
75 return undefined
76 }
77
78 const videoAbuseData = {
79 reporterAccountId: account.id,
80 reason: videoAbuseToCreateData.content,
81 videoId: video.id
82 }
83
84 await db.VideoAbuse.create(videoAbuseData)
85
86 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
87 })
88 }