]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Split files in activitypub server
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
50d6de9c 1import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
3fd3ab2d 2import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
6d852470 3import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
da854ddd
C
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
3fd3ab2d 6import { sequelizeTypescript } from '../../../initializers'
3fd3ab2d 7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
50d6de9c 8import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d 9import { VideoAbuseModel } from '../../../models/video/video-abuse'
6d852470 10import { VideoCommentModel } from '../../../models/video/video-comment'
50d6de9c 11import { getOrCreateActorAndServerAndModel } from '../actor'
e251f170 12import { getActorsInvolvedInVideo } from '../audience'
7acee6f1
C
13import { resolveThread } from '../video-comments'
14import { getOrCreateAccountAndVideoAndChannel } from '../videos'
e251f170 15import { forwardActivity } from '../send/utils'
e4f97bab 16
0d0e8dd0 17async function processCreateActivity (activity: ActivityCreate) {
e4f97bab
C
18 const activityObject = activity.object
19 const activityType = activityObject.type
50d6de9c 20 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
e4f97bab 21
40ff5707 22 if (activityType === 'View') {
50d6de9c 23 return processCreateView(actor, activity)
0032ebe9 24 } else if (activityType === 'Dislike') {
50d6de9c
C
25 return processCreateDislike(actor, activity)
26 } else if (activityType === 'Video') {
27 return processCreateVideo(actor, activity)
8e13fa7d 28 } else if (activityType === 'Flag') {
50d6de9c 29 return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
6d852470
C
30 } else if (activityType === 'Note') {
31 return processCreateVideoComment(actor, activity)
e4f97bab
C
32 }
33
34 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 35 return Promise.resolve(undefined)
e4f97bab
C
36}
37
38// ---------------------------------------------------------------------------
39
40export {
41 processCreateActivity
42}
43
44// ---------------------------------------------------------------------------
45
50d6de9c
C
46async function processCreateVideo (
47 actor: ActorModel,
48 activity: ActivityCreate
49) {
50 const videoToCreateData = activity.object as VideoTorrentObject
51
2ccaeeb3 52 const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor)
50d6de9c 53
50d6de9c
C
54 return video
55}
56
50d6de9c 57async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
0032ebe9 58 const options = {
50d6de9c 59 arguments: [ byActor, activity ],
0032ebe9
C
60 errorMessage: 'Cannot dislike the video with many retries.'
61 }
62
63 return retryTransactionWrapper(createVideoDislike, options)
64}
65
2ccaeeb3 66async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
63c93323 67 const dislike = activity.object as DislikeObject
50d6de9c
C
68 const byAccount = byActor.Account
69
70 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
0032ebe9 71
2ccaeeb3 72 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
0032ebe9 73
2ccaeeb3 74 return sequelizeTypescript.transaction(async t => {
0032ebe9
C
75 const rate = {
76 type: 'dislike' as 'dislike',
77 videoId: video.id,
78 accountId: byAccount.id
79 }
3fd3ab2d 80 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 81 where: rate,
63c93323
C
82 defaults: rate,
83 transaction: t
0032ebe9 84 })
f00984c0 85 if (created === true) await video.increment('dislikes', { transaction: t })
0032ebe9 86
63c93323
C
87 if (video.isOwned() && created === true) {
88 // Don't resend the activity to the sender
50d6de9c 89 const exceptions = [ byActor ]
63c93323
C
90 await forwardActivity(activity, t, exceptions)
91 }
0032ebe9
C
92 })
93}
94
6d852470 95async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
63c93323
C
96 const view = activity.object as ViewObject
97
2ccaeeb3 98 const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
40ff5707 99
7bc29171
C
100 const actor = await ActorModel.loadByUrl(view.actor)
101 if (!actor) throw new Error('Unknown actor ' + view.actor)
40ff5707
C
102
103 await video.increment('views')
104
63c93323
C
105 if (video.isOwned()) {
106 // Don't resend the activity to the sender
6d852470 107 const exceptions = [ byActor ]
63c93323
C
108 await forwardActivity(activity, undefined, exceptions)
109 }
40ff5707
C
110}
111
50d6de9c 112function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
8e13fa7d 113 const options = {
50d6de9c 114 arguments: [ actor, videoAbuseToCreateData ],
8e13fa7d
C
115 errorMessage: 'Cannot insert the remote video abuse with many retries.'
116 }
117
118 return retryTransactionWrapper(addRemoteVideoAbuse, options)
119}
120
2ccaeeb3 121async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
8e13fa7d
C
122 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
123
50d6de9c
C
124 const account = actor.Account
125 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
126
2ccaeeb3 127 const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object)
8e13fa7d 128
2ccaeeb3 129 return sequelizeTypescript.transaction(async t => {
8e13fa7d
C
130 const videoAbuseData = {
131 reporterAccountId: account.id,
132 reason: videoAbuseToCreateData.content,
133 videoId: video.id
134 }
135
3fd3ab2d 136 await VideoAbuseModel.create(videoAbuseData)
8e13fa7d
C
137
138 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
139 })
140}
6d852470
C
141
142function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
143 const options = {
144 arguments: [ byActor, activity ],
145 errorMessage: 'Cannot create video comment with many retries.'
146 }
147
148 return retryTransactionWrapper(createVideoComment, options)
149}
150
2ccaeeb3 151async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) {
6d852470
C
152 const comment = activity.object as VideoCommentObject
153 const byAccount = byActor.Account
154
155 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
156
2ccaeeb3
C
157 const { video, parents } = await resolveThread(comment.inReplyTo)
158
6d852470 159 return sequelizeTypescript.transaction(async t => {
2ccaeeb3
C
160 let originCommentId = null
161 let inReplyToCommentId = null
162
163 if (parents.length !== 0) {
164 const parent = parents[0]
165
166 originCommentId = parent.getThreadId()
167 inReplyToCommentId = parent.id
168 }
6d852470
C
169
170 // This is a new thread
2ccaeeb3
C
171 const objectToCreate = {
172 url: comment.id,
173 text: comment.content,
174 originCommentId,
175 inReplyToCommentId,
176 videoId: video.id,
177 accountId: byAccount.id
ea44f375
C
178 }
179
93ef8a9d
C
180 const options = {
181 where: {
182 url: objectToCreate.url
183 },
184 defaults: objectToCreate,
185 transaction: t
186 }
187 const [ ,created ] = await VideoCommentModel.findOrCreate(options)
188
189 if (video.isOwned() && created === true) {
ea44f375
C
190 // Don't resend the activity to the sender
191 const exceptions = [ byActor ]
93ef8a9d
C
192
193 // Mastodon does not add our announces in audience, so we forward to them manually
194 const additionalActors = await getActorsInvolvedInVideo(video, t)
195 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
196
197 await forwardActivity(activity, t, exceptions, additionalFollowerUrls)
ea44f375 198 }
6d852470
C
199 })
200}