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