]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-create.ts
Remove unused actor uuid field
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
1 import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
2 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { addVideoComment, resolveThread } from '../video-comments'
8 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9 import { forwardVideoRelatedActivity } from '../send/utils'
10 import { createOrUpdateCacheFile } from '../cache-file'
11 import { Notifier } from '../../notifier'
12 import { processViewActivity } from './process-view'
13 import { processDislikeActivity } from './process-dislike'
14 import { processFlagActivity } from './process-flag'
15 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
16 import { createOrUpdateVideoPlaylist } from '../playlist'
17
18 async 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
56 export {
57 processCreateActivity
58 }
59
60 // ---------------------------------------------------------------------------
61
62 async 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
72 async 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
88 async 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
108 async 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 }