]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Correctly notify on auto blacklist
[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'
418d092a
C
12import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
13import { createOrUpdateVideoPlaylist } from '../playlist'
ee79b60e 14import { VideoModel } from '../../../models/video/video'
e4f97bab 15
e587e0ec 16async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
e4f97bab
C
17 const activityObject = activity.object
18 const activityType = activityObject.type
19
848f499d 20 if (activityType === 'Video') {
f6eebcb3 21 return processCreateVideo(activity)
848f499d
C
22 }
23
24 if (activityType === 'Note') {
25 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
26 }
27
28 if (activityType === 'CacheFile') {
418d092a
C
29 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
30 }
31
32 if (activityType === 'Playlist') {
33 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
e4f97bab
C
34 }
35
36 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 37 return Promise.resolve(undefined)
e4f97bab
C
38}
39
40// ---------------------------------------------------------------------------
41
42export {
43 processCreateActivity
44}
45
46// ---------------------------------------------------------------------------
47
f6eebcb3 48async function processCreateVideo (activity: ActivityCreate) {
50d6de9c
C
49 const videoToCreateData = activity.object as VideoTorrentObject
50
5b77537c 51 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
cef534ed 52
5b77537c 53 if (created) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
50d6de9c 54
50d6de9c
C
55 return video
56}
57
418d092a 58async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
c48e82b5
C
59 const cacheFile = activity.object as CacheFileObject
60
4157cdb1 61 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5 62
e5565833 63 await sequelizeTypescript.transaction(async t => {
b88a4596 64 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 65 })
c48e82b5
C
66
67 if (video.isOwned()) {
68 // Don't resend the activity to the sender
69 const exceptions = [ byActor ]
e5565833 70 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
71 }
72}
73
848f499d 74async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
83e6519b 75 const commentObject = activity.object as VideoCommentObject
6d852470
C
76 const byAccount = byActor.Account
77
78 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
79
ee79b60e
C
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 )
7d14d4d2 90 return
ee79b60e 91 }
2ccaeeb3 92
cef534ed 93 const { comment, created } = await addVideoComment(video, commentObject.id)
93ef8a9d 94
83e6519b
C
95 if (video.isOwned() && created === true) {
96 // Don't resend the activity to the sender
97 const exceptions = [ byActor ]
93ef8a9d 98
83e6519b
C
99 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
100 }
cef534ed
C
101
102 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
6d852470 103}
418d092a
C
104
105async 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}