]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-create.ts
e4c173e9981c6c371fc8e86eaf45111830fea749
[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 import { VideoModel } from '../../../models/video/video'
18
19 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
20 const activityObject = activity.object
21 const activityType = activityObject.type
22
23 if (activityType === 'View') {
24 return processViewActivity(activity, byActor)
25 }
26
27 if (activityType === 'Dislike') {
28 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
29 }
30
31 if (activityType === 'Flag') {
32 return retryTransactionWrapper(processFlagActivity, activity, byActor)
33 }
34
35 if (activityType === 'Video') {
36 return processCreateVideo(activity)
37 }
38
39 if (activityType === 'Note') {
40 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
41 }
42
43 if (activityType === 'CacheFile') {
44 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
45 }
46
47 if (activityType === 'Playlist') {
48 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
49 }
50
51 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
52 return Promise.resolve(undefined)
53 }
54
55 // ---------------------------------------------------------------------------
56
57 export {
58 processCreateActivity
59 }
60
61 // ---------------------------------------------------------------------------
62
63 async function processCreateVideo (activity: ActivityCreate) {
64 const videoToCreateData = activity.object as VideoTorrentObject
65
66 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
67
68 if (created) Notifier.Instance.notifyOnNewVideo(video)
69
70 return video
71 }
72
73 async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
74 const cacheFile = activity.object as CacheFileObject
75
76 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
77
78 await sequelizeTypescript.transaction(async t => {
79 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
80 })
81
82 if (video.isOwned()) {
83 // Don't resend the activity to the sender
84 const exceptions = [ byActor ]
85 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
86 }
87 }
88
89 async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
90 const commentObject = activity.object as VideoCommentObject
91 const byAccount = byActor.Account
92
93 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
94
95 let video: VideoModel
96 try {
97 const resolveThreadResult = await resolveThread(commentObject.inReplyTo)
98 video = resolveThreadResult.video
99 } catch (err) {
100 logger.debug(
101 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
102 commentObject.inReplyTo,
103 { err }
104 )
105 }
106
107 const { comment, created } = await addVideoComment(video, commentObject.id)
108
109 if (video.isOwned() && created === true) {
110 // Don't resend the activity to the sender
111 const exceptions = [ byActor ]
112
113 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
114 }
115
116 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
117 }
118
119 async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) {
120 const playlistObject = activity.object as PlaylistObject
121 const byAccount = byActor.Account
122
123 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
124
125 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
126 }