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