c1eb2a8ab2dbdf4240c124ba32d5eeb1e198ed1b
[oweals/peertube.git] / server / lib / activitypub / process / process-create.ts
1 import { ActivityCreate, VideoChannelObject } from '../../../../shared'
2 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
3 import { logger, retryTransactionWrapper } from '../../../helpers'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { AccountModel } from '../../../models/account/account'
6 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7 import { VideoModel } from '../../../models/video/video'
8 import { VideoAbuseModel } from '../../../models/video/video-abuse'
9 import { VideoChannelModel } from '../../../models/video/video-channel'
10 import { getOrCreateAccountAndServer } from '../account'
11 import { forwardActivity } from '../send/misc'
12 import { getVideoChannelActivityPubUrl } from '../url'
13 import { addVideoChannelShares, videoChannelActivityObjectToDBAttributes } from './misc'
14
15 async function processCreateActivity (activity: ActivityCreate) {
16   const activityObject = activity.object
17   const activityType = activityObject.type
18   const account = await getOrCreateAccountAndServer(activity.actor)
19
20   if (activityType === 'View') {
21     return processCreateView(account, activity)
22   } else if (activityType === 'Dislike') {
23     return processCreateDislike(account, activity)
24   } else if (activityType === 'VideoChannel') {
25     return processCreateVideoChannel(account, activityObject as VideoChannelObject)
26   } else if (activityType === 'Flag') {
27     return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
28   }
29
30   logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
31   return Promise.resolve(undefined)
32 }
33
34 // ---------------------------------------------------------------------------
35
36 export {
37   processCreateActivity
38 }
39
40 // ---------------------------------------------------------------------------
41
42 async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) {
43   const options = {
44     arguments: [ byAccount, activity ],
45     errorMessage: 'Cannot dislike the video with many retries.'
46   }
47
48   return retryTransactionWrapper(createVideoDislike, options)
49 }
50
51 function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) {
52   const dislike = activity.object as DislikeObject
53
54   return sequelizeTypescript.transaction(async t => {
55     const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
56     if (!video) throw new Error('Unknown video ' + dislike.object)
57
58     const rate = {
59       type: 'dislike' as 'dislike',
60       videoId: video.id,
61       accountId: byAccount.id
62     }
63     const [ , created ] = await AccountVideoRateModel.findOrCreate({
64       where: rate,
65       defaults: rate,
66       transaction: t
67     })
68     if (created === true) await video.increment('dislikes', { transaction: t })
69
70     if (video.isOwned() && created === true) {
71       // Don't resend the activity to the sender
72       const exceptions = [ byAccount ]
73       await forwardActivity(activity, t, exceptions)
74     }
75   })
76 }
77
78 async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) {
79   const view = activity.object as ViewObject
80
81   const video = await VideoModel.loadByUrlAndPopulateAccount(view.object)
82
83   if (!video) throw new Error('Unknown video ' + view.object)
84
85   const account = await AccountModel.loadByUrl(view.actor)
86   if (!account) throw new Error('Unknown account ' + view.actor)
87
88   await video.increment('views')
89
90   if (video.isOwned()) {
91     // Don't resend the activity to the sender
92     const exceptions = [ byAccount ]
93     await forwardActivity(activity, undefined, exceptions)
94   }
95 }
96
97 async function processCreateVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) {
98   const options = {
99     arguments: [ account, videoChannelToCreateData ],
100     errorMessage: 'Cannot insert the remote video channel with many retries.'
101   }
102
103   const videoChannel = await retryTransactionWrapper(addRemoteVideoChannel, options)
104
105   if (videoChannelToCreateData.shares && Array.isArray(videoChannelToCreateData.shares.orderedItems)) {
106     await addVideoChannelShares(videoChannel, videoChannelToCreateData.shares.orderedItems)
107   }
108
109   return videoChannel
110 }
111
112 function addRemoteVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) {
113   logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
114
115   return sequelizeTypescript.transaction(async t => {
116     let videoChannel = await VideoChannelModel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
117     if (videoChannel) return videoChannel
118
119     const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
120     videoChannel = new VideoChannelModel(videoChannelData)
121     videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
122
123     videoChannel = await videoChannel.save({ transaction: t })
124     logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
125
126     return videoChannel
127   })
128 }
129
130 function processCreateVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) {
131   const options = {
132     arguments: [ account, videoAbuseToCreateData ],
133     errorMessage: 'Cannot insert the remote video abuse with many retries.'
134   }
135
136   return retryTransactionWrapper(addRemoteVideoAbuse, options)
137 }
138
139 function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) {
140   logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
141
142   return sequelizeTypescript.transaction(async t => {
143     const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
144     if (!video) {
145       logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
146       return undefined
147     }
148
149     const videoAbuseData = {
150       reporterAccountId: account.id,
151       reason: videoAbuseToCreateData.content,
152       videoId: video.id
153     }
154
155     await VideoAbuseModel.create(videoAbuseData)
156
157     logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
158   })
159 }