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