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