]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process-create.ts
Add video abuse to activity pub
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / 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 { getActivityPubUrl, getOrCreateAccount } from '../../helpers/activitypub'
5 import { database as db } from '../../initializers'
6 import { AccountInstance } from '../../models/account/account-interface'
7
8 async function processCreateActivity (activity: ActivityCreate) {
9 const activityObject = activity.object
10 const activityType = activityObject.type
11 const account = await getOrCreateAccount(activity.actor)
12
13 if (activityType === 'VideoChannel') {
14 return processCreateVideoChannel(account, activityObject as VideoChannelObject)
15 } else if (activityType === 'Flag') {
16 return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
17 }
18
19 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
20 return Promise.resolve(undefined)
21 }
22
23 // ---------------------------------------------------------------------------
24
25 export {
26 processCreateActivity
27 }
28
29 // ---------------------------------------------------------------------------
30
31 function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
32 const options = {
33 arguments: [ account, videoChannelToCreateData ],
34 errorMessage: 'Cannot insert the remote video channel with many retries.'
35 }
36
37 return retryTransactionWrapper(addRemoteVideoChannel, options)
38 }
39
40 async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
41 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
42
43 await db.sequelize.transaction(async t => {
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,
53 remote: true,
54 accountId: account.id
55 }
56
57 videoChannel = db.VideoChannel.build(videoChannelData)
58 videoChannel.url = getActivityPubUrl('videoChannel', videoChannel.uuid)
59
60 await videoChannel.save({ transaction: t })
61 })
62
63 logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
64 }
65
66 function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
67 const options = {
68 arguments: [ account, videoAbuseToCreateData ],
69 errorMessage: 'Cannot insert the remote video abuse with many retries.'
70 }
71
72 return retryTransactionWrapper(addRemoteVideoAbuse, options)
73 }
74
75 async function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
76 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
77
78 return db.sequelize.transaction(async t => {
79 const video = await db.Video.loadByUrl(videoAbuseToCreateData.object, t)
80 if (!video) {
81 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
82 return
83 }
84
85 const videoAbuseData = {
86 reporterAccountId: account.id,
87 reason: videoAbuseToCreateData.content,
88 videoId: video.id
89 }
90
91 await db.VideoAbuse.create(videoAbuseData)
92
93 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
94 })
95 }