]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-create.ts
Add concept of video state, and add ability to wait transcoding before
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
1 import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
2 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
3 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { ActorModel } from '../../../models/activitypub/actor'
9 import { VideoAbuseModel } from '../../../models/video/video-abuse'
10 import { VideoCommentModel } from '../../../models/video/video-comment'
11 import { getOrCreateActorAndServerAndModel } from '../actor'
12 import { resolveThread } from '../video-comments'
13 import { getOrCreateAccountAndVideoAndChannel } from '../videos'
14 import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
15
16 async function processCreateActivity (activity: ActivityCreate) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
19 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
20
21 if (activityType === 'View') {
22 return processCreateView(actor, activity)
23 } else if (activityType === 'Dislike') {
24 return processCreateDislike(actor, activity)
25 } else if (activityType === 'Video') {
26 return processCreateVideo(actor, activity)
27 } else if (activityType === 'Flag') {
28 return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
29 } else if (activityType === 'Note') {
30 return processCreateVideoComment(actor, activity)
31 }
32
33 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
34 return Promise.resolve(undefined)
35 }
36
37 // ---------------------------------------------------------------------------
38
39 export {
40 processCreateActivity
41 }
42
43 // ---------------------------------------------------------------------------
44
45 async function processCreateVideo (
46 actor: ActorModel,
47 activity: ActivityCreate
48 ) {
49 const videoToCreateData = activity.object as VideoTorrentObject
50
51 const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor)
52
53 return video
54 }
55
56 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
57 const options = {
58 arguments: [ byActor, activity ],
59 errorMessage: 'Cannot dislike the video with many retries.'
60 }
61
62 return retryTransactionWrapper(createVideoDislike, options)
63 }
64
65 async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
66 const dislike = activity.object as DislikeObject
67 const byAccount = byActor.Account
68
69 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
70
71 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
72
73 return sequelizeTypescript.transaction(async t => {
74 const rate = {
75 type: 'dislike' as 'dislike',
76 videoId: video.id,
77 accountId: byAccount.id
78 }
79 const [ , created ] = await AccountVideoRateModel.findOrCreate({
80 where: rate,
81 defaults: rate,
82 transaction: t
83 })
84 if (created === true) await video.increment('dislikes', { transaction: t })
85
86 if (video.isOwned() && created === true) {
87 // Don't resend the activity to the sender
88 const exceptions = [ byActor ]
89
90 await forwardVideoRelatedActivity(activity, t, exceptions, video)
91 }
92 })
93 }
94
95 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
96 const view = activity.object as ViewObject
97
98 const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
99
100 const actor = await ActorModel.loadByUrl(view.actor)
101 if (!actor) throw new Error('Unknown actor ' + view.actor)
102
103 await video.increment('views')
104
105 if (video.isOwned()) {
106 // Don't resend the activity to the sender
107 const exceptions = [ byActor ]
108 await forwardActivity(activity, undefined, exceptions)
109 }
110 }
111
112 function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
113 const options = {
114 arguments: [ actor, videoAbuseToCreateData ],
115 errorMessage: 'Cannot insert the remote video abuse with many retries.'
116 }
117
118 return retryTransactionWrapper(addRemoteVideoAbuse, options)
119 }
120
121 async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
122 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
123
124 const account = actor.Account
125 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
126
127 const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object)
128
129 return sequelizeTypescript.transaction(async t => {
130 const videoAbuseData = {
131 reporterAccountId: account.id,
132 reason: videoAbuseToCreateData.content,
133 videoId: video.id
134 }
135
136 await VideoAbuseModel.create(videoAbuseData)
137
138 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
139 })
140 }
141
142 function 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
151 async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) {
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
157 const { video, parents } = await resolveThread(comment.inReplyTo)
158
159 return sequelizeTypescript.transaction(async t => {
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 }
169
170 // This is a new thread
171 const objectToCreate = {
172 url: comment.id,
173 text: comment.content,
174 originCommentId,
175 inReplyToCommentId,
176 videoId: video.id,
177 accountId: byAccount.id
178 }
179
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) {
190 // Don't resend the activity to the sender
191 const exceptions = [ byActor ]
192
193 await forwardVideoRelatedActivity(activity, t, exceptions, video)
194 }
195 })
196 }