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