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