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