]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-create.ts
Fix videos list user NSFW policy
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
... / ...
CommitLineData
1import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared'
2import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
3import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
6import { sequelizeTypescript } from '../../../initializers'
7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8import { ActorModel } from '../../../models/activitypub/actor'
9import { VideoAbuseModel } from '../../../models/video/video-abuse'
10import { addVideoComment, resolveThread } from '../video-comments'
11import { getOrCreateVideoAndAccountAndChannel } from '../videos'
12import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
13import { Redis } from '../../redis'
14import { createCacheFile } from '../cache-file'
15
16async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
19
20 if (activityType === 'View') {
21 return processCreateView(byActor, activity)
22 } else if (activityType === 'Dislike') {
23 return retryTransactionWrapper(processCreateDislike, byActor, activity)
24 } else if (activityType === 'Video') {
25 return processCreateVideo(activity)
26 } else if (activityType === 'Flag') {
27 return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject)
28 } else if (activityType === 'Note') {
29 return retryTransactionWrapper(processCreateVideoComment, byActor, activity)
30 } else if (activityType === 'CacheFile') {
31 return retryTransactionWrapper(processCacheFile, byActor, activity)
32 }
33
34 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
35 return Promise.resolve(undefined)
36}
37
38// ---------------------------------------------------------------------------
39
40export {
41 processCreateActivity
42}
43
44// ---------------------------------------------------------------------------
45
46async function processCreateVideo (activity: ActivityCreate) {
47 const videoToCreateData = activity.object as VideoTorrentObject
48
49 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
50
51 return video
52}
53
54async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
55 const dislike = activity.object as DislikeObject
56 const byAccount = byActor.Account
57
58 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
59
60 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
61
62 return sequelizeTypescript.transaction(async t => {
63 const rate = {
64 type: 'dislike' as 'dislike',
65 videoId: video.id,
66 accountId: byAccount.id
67 }
68 const [ , created ] = await AccountVideoRateModel.findOrCreate({
69 where: rate,
70 defaults: rate,
71 transaction: t
72 })
73 if (created === true) await video.increment('dislikes', { transaction: t })
74
75 if (video.isOwned() && created === true) {
76 // Don't resend the activity to the sender
77 const exceptions = [ byActor ]
78
79 await forwardVideoRelatedActivity(activity, t, exceptions, video)
80 }
81 })
82}
83
84async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
85 const view = activity.object as ViewObject
86
87 const options = {
88 videoObject: view.object,
89 fetchType: 'only-video' as 'only-video'
90 }
91 const { video } = await getOrCreateVideoAndAccountAndChannel(options)
92
93 await Redis.Instance.addVideoView(video.id)
94
95 if (video.isOwned()) {
96 // Don't resend the activity to the sender
97 const exceptions = [ byActor ]
98 await forwardActivity(activity, undefined, exceptions)
99 }
100}
101
102async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
103 const cacheFile = activity.object as CacheFileObject
104
105 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
106
107 await createCacheFile(cacheFile, video, byActor)
108
109 if (video.isOwned()) {
110 // Don't resend the activity to the sender
111 const exceptions = [ byActor ]
112 await forwardActivity(activity, undefined, exceptions)
113 }
114}
115
116async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
117 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
118
119 const account = byActor.Account
120 if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
121
122 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object })
123
124 return sequelizeTypescript.transaction(async t => {
125 const videoAbuseData = {
126 reporterAccountId: account.id,
127 reason: videoAbuseToCreateData.content,
128 videoId: video.id,
129 state: VideoAbuseState.PENDING
130 }
131
132 await VideoAbuseModel.create(videoAbuseData, { transaction: t })
133
134 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
135 })
136}
137
138async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
139 const commentObject = activity.object as VideoCommentObject
140 const byAccount = byActor.Account
141
142 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
143
144 const { video } = await resolveThread(commentObject.inReplyTo)
145
146 const { created } = await addVideoComment(video, commentObject.id)
147
148 if (video.isOwned() && created === true) {
149 // Don't resend the activity to the sender
150 const exceptions = [ byActor ]
151
152 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
153 }
154}