]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-create.ts
Move config in its own file
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
... / ...
CommitLineData
1import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
2import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
5import { sequelizeTypescript } from '../../../initializers'
6import { ActorModel } from '../../../models/activitypub/actor'
7import { addVideoComment, resolveThread } from '../video-comments'
8import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9import { forwardVideoRelatedActivity } from '../send/utils'
10import { createOrUpdateCacheFile } from '../cache-file'
11import { Notifier } from '../../notifier'
12import { processViewActivity } from './process-view'
13import { processDislikeActivity } from './process-dislike'
14import { processFlagActivity } from './process-flag'
15import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
16import { createOrUpdateVideoPlaylist } from '../playlist'
17
18async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
19 const activityObject = activity.object
20 const activityType = activityObject.type
21
22 if (activityType === 'View') {
23 return processViewActivity(activity, byActor)
24 }
25
26 if (activityType === 'Dislike') {
27 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
28 }
29
30 if (activityType === 'Flag') {
31 return retryTransactionWrapper(processFlagActivity, activity, byActor)
32 }
33
34 if (activityType === 'Video') {
35 return processCreateVideo(activity)
36 }
37
38 if (activityType === 'Note') {
39 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
40 }
41
42 if (activityType === 'CacheFile') {
43 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
44 }
45
46 if (activityType === 'Playlist') {
47 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
48 }
49
50 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
51 return Promise.resolve(undefined)
52}
53
54// ---------------------------------------------------------------------------
55
56export {
57 processCreateActivity
58}
59
60// ---------------------------------------------------------------------------
61
62async function processCreateVideo (activity: ActivityCreate) {
63 const videoToCreateData = activity.object as VideoTorrentObject
64
65 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
66
67 if (created) Notifier.Instance.notifyOnNewVideo(video)
68
69 return video
70}
71
72async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
73 const cacheFile = activity.object as CacheFileObject
74
75 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
76
77 await sequelizeTypescript.transaction(async t => {
78 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
79 })
80
81 if (video.isOwned()) {
82 // Don't resend the activity to the sender
83 const exceptions = [ byActor ]
84 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
85 }
86}
87
88async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
89 const commentObject = activity.object as VideoCommentObject
90 const byAccount = byActor.Account
91
92 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
93
94 const { video } = await resolveThread(commentObject.inReplyTo)
95
96 const { comment, created } = await addVideoComment(video, commentObject.id)
97
98 if (video.isOwned() && created === true) {
99 // Don't resend the activity to the sender
100 const exceptions = [ byActor ]
101
102 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
103 }
104
105 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
106}
107
108async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) {
109 const playlistObject = activity.object as PlaylistObject
110 const byAccount = byActor.Account
111
112 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
113
114 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
115}