]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Add local user subscriptions
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
268eebed 1import { ActivityCreate, VideoAbuseState, 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'
7acee6f1
C
12import { resolveThread } from '../video-comments'
13import { getOrCreateAccountAndVideoAndChannel } from '../videos'
9588d4f4 14import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
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') {
90d4bb81 24 return retryTransactionWrapper(processCreateDislike, actor, activity)
50d6de9c
C
25 } else if (activityType === 'Video') {
26 return processCreateVideo(actor, activity)
8e13fa7d 27 } else if (activityType === 'Flag') {
90d4bb81 28 return retryTransactionWrapper(processCreateVideoAbuse, actor, activityObject as VideoAbuseObject)
6d852470 29 } else if (activityType === 'Note') {
90d4bb81 30 return retryTransactionWrapper(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) {
63c93323 57 const dislike = activity.object as DislikeObject
50d6de9c
C
58 const byAccount = byActor.Account
59
60 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
0032ebe9 61
2ccaeeb3 62 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
0032ebe9 63
2ccaeeb3 64 return sequelizeTypescript.transaction(async t => {
0032ebe9
C
65 const rate = {
66 type: 'dislike' as 'dislike',
67 videoId: video.id,
68 accountId: byAccount.id
69 }
3fd3ab2d 70 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 71 where: rate,
63c93323
C
72 defaults: rate,
73 transaction: t
0032ebe9 74 })
f00984c0 75 if (created === true) await video.increment('dislikes', { transaction: t })
0032ebe9 76
63c93323
C
77 if (video.isOwned() && created === true) {
78 // Don't resend the activity to the sender
50d6de9c 79 const exceptions = [ byActor ]
9588d4f4
C
80
81 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 82 }
0032ebe9
C
83 })
84}
85
6d852470 86async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
63c93323
C
87 const view = activity.object as ViewObject
88
2ccaeeb3 89 const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
40ff5707 90
7bc29171
C
91 const actor = await ActorModel.loadByUrl(view.actor)
92 if (!actor) throw new Error('Unknown actor ' + view.actor)
40ff5707
C
93
94 await video.increment('views')
95
63c93323
C
96 if (video.isOwned()) {
97 // Don't resend the activity to the sender
6d852470 98 const exceptions = [ byActor ]
63c93323
C
99 await forwardActivity(activity, undefined, exceptions)
100 }
40ff5707
C
101}
102
90d4bb81 103async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
8e13fa7d
C
104 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
105
50d6de9c
C
106 const account = actor.Account
107 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
108
2ccaeeb3 109 const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object)
8e13fa7d 110
2ccaeeb3 111 return sequelizeTypescript.transaction(async t => {
8e13fa7d
C
112 const videoAbuseData = {
113 reporterAccountId: account.id,
114 reason: videoAbuseToCreateData.content,
268eebed
C
115 videoId: video.id,
116 state: VideoAbuseState.PENDING
8e13fa7d
C
117 }
118
3fd3ab2d 119 await VideoAbuseModel.create(videoAbuseData)
8e13fa7d
C
120
121 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
122 })
123}
6d852470 124
90d4bb81 125async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
6d852470
C
126 const comment = activity.object as VideoCommentObject
127 const byAccount = byActor.Account
128
129 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
130
2ccaeeb3
C
131 const { video, parents } = await resolveThread(comment.inReplyTo)
132
6d852470 133 return sequelizeTypescript.transaction(async t => {
2ccaeeb3
C
134 let originCommentId = null
135 let inReplyToCommentId = null
136
137 if (parents.length !== 0) {
138 const parent = parents[0]
139
140 originCommentId = parent.getThreadId()
141 inReplyToCommentId = parent.id
142 }
6d852470
C
143
144 // This is a new thread
2ccaeeb3
C
145 const objectToCreate = {
146 url: comment.id,
147 text: comment.content,
148 originCommentId,
149 inReplyToCommentId,
150 videoId: video.id,
151 accountId: byAccount.id
ea44f375
C
152 }
153
93ef8a9d
C
154 const options = {
155 where: {
156 url: objectToCreate.url
157 },
158 defaults: objectToCreate,
159 transaction: t
160 }
161 const [ ,created ] = await VideoCommentModel.findOrCreate(options)
162
163 if (video.isOwned() && created === true) {
ea44f375
C
164 // Don't resend the activity to the sender
165 const exceptions = [ byActor ]
93ef8a9d 166
9588d4f4 167 await forwardVideoRelatedActivity(activity, t, exceptions, video)
ea44f375 168 }
6d852470
C
169 })
170}