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