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