]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
848f499d 1import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
6d852470 2import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
da854ddd
C
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
3fd3ab2d 5import { sequelizeTypescript } from '../../../initializers'
50d6de9c 6import { ActorModel } from '../../../models/activitypub/actor'
83e6519b 7import { addVideoComment, resolveThread } from '../video-comments'
1297eb5d 8import { getOrCreateVideoAndAccountAndChannel } from '../videos'
cfaf819c 9import { forwardVideoRelatedActivity } from '../send/utils'
b88a4596 10import { createOrUpdateCacheFile } from '../cache-file'
cef534ed 11import { Notifier } from '../../notifier'
848f499d
C
12import { processViewActivity } from './process-view'
13import { processDislikeActivity } from './process-dislike'
14import { processFlagActivity } from './process-flag'
e4f97bab 15
e587e0ec 16async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
e4f97bab
C
17 const activityObject = activity.object
18 const activityType = activityObject.type
19
40ff5707 20 if (activityType === 'View') {
848f499d
C
21 return processViewActivity(activity, byActor)
22 }
23
24 if (activityType === 'Dislike') {
25 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
26 }
27
28 if (activityType === 'Flag') {
29 return retryTransactionWrapper(processFlagActivity, activity, byActor)
30 }
31
32 if (activityType === 'Video') {
f6eebcb3 33 return processCreateVideo(activity)
848f499d
C
34 }
35
36 if (activityType === 'Note') {
37 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
38 }
39
40 if (activityType === 'CacheFile') {
41 return retryTransactionWrapper(processCacheFile, activity, byActor)
e4f97bab
C
42 }
43
44 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 45 return Promise.resolve(undefined)
e4f97bab
C
46}
47
48// ---------------------------------------------------------------------------
49
50export {
51 processCreateActivity
52}
53
54// ---------------------------------------------------------------------------
55
f6eebcb3 56async function processCreateVideo (activity: ActivityCreate) {
50d6de9c
C
57 const videoToCreateData = activity.object as VideoTorrentObject
58
cef534ed
C
59 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
60
61 if (created) Notifier.Instance.notifyOnNewVideo(video)
50d6de9c 62
50d6de9c
C
63 return video
64}
65
848f499d 66async function processCacheFile (activity: ActivityCreate, byActor: ActorModel) {
c48e82b5
C
67 const cacheFile = activity.object as CacheFileObject
68
4157cdb1 69 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5 70
e5565833 71 await sequelizeTypescript.transaction(async t => {
b88a4596 72 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 73 })
c48e82b5
C
74
75 if (video.isOwned()) {
76 // Don't resend the activity to the sender
77 const exceptions = [ byActor ]
e5565833 78 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
79 }
80}
81
848f499d 82async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
83e6519b 83 const commentObject = activity.object as VideoCommentObject
6d852470
C
84 const byAccount = byActor.Account
85
86 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
87
83e6519b 88 const { video } = await resolveThread(commentObject.inReplyTo)
2ccaeeb3 89
cef534ed 90 const { comment, created } = await addVideoComment(video, commentObject.id)
93ef8a9d 91
83e6519b
C
92 if (video.isOwned() && created === true) {
93 // Don't resend the activity to the sender
94 const exceptions = [ byActor ]
93ef8a9d 95
83e6519b
C
96 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
97 }
cef534ed
C
98
99 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
6d852470 100}