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