]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Automatically remove bad followings
[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'
6b9c966f 7import { 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'
1198edf4 15import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
6b9c966f 16import { VideoCommentModel } from '../../../models/video/video-comment'
e4f97bab 17
1198edf4
C
18async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) {
19 const { activity, byActor } = options
20
21 // Only notify if it is not from a fetcher job
22 const notify = options.fromFetch !== true
e4f97bab
C
23 const activityObject = activity.object
24 const activityType = activityObject.type
25
848f499d 26 if (activityType === 'Video') {
1198edf4 27 return processCreateVideo(activity, notify)
848f499d
C
28 }
29
30 if (activityType === 'Note') {
1198edf4 31 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
848f499d
C
32 }
33
34 if (activityType === 'CacheFile') {
418d092a
C
35 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
36 }
37
38 if (activityType === 'Playlist') {
39 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
e4f97bab
C
40 }
41
42 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 43 return Promise.resolve(undefined)
e4f97bab
C
44}
45
46// ---------------------------------------------------------------------------
47
48export {
49 processCreateActivity
50}
51
52// ---------------------------------------------------------------------------
53
1198edf4 54async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
50d6de9c
C
55 const videoToCreateData = activity.object as VideoTorrentObject
56
5b77537c 57 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
cef534ed 58
1198edf4 59 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
50d6de9c 60
50d6de9c
C
61 return video
62}
63
418d092a 64async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
c48e82b5
C
65 const cacheFile = activity.object as CacheFileObject
66
4157cdb1 67 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5 68
e5565833 69 await sequelizeTypescript.transaction(async t => {
b88a4596 70 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 71 })
c48e82b5
C
72
73 if (video.isOwned()) {
74 // Don't resend the activity to the sender
75 const exceptions = [ byActor ]
e5565833 76 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
77 }
78}
79
1198edf4 80async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel, notify: boolean) {
83e6519b 81 const commentObject = activity.object as VideoCommentObject
6d852470
C
82 const byAccount = byActor.Account
83
84 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
85
ee79b60e 86 let video: VideoModel
6b9c966f
C
87 let created: boolean
88 let comment: VideoCommentModel
ee79b60e 89 try {
6b9c966f 90 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
ee79b60e 91 video = resolveThreadResult.video
6b9c966f
C
92 created = resolveThreadResult.commentCreated
93 comment = resolveThreadResult.comment
ee79b60e
C
94 } catch (err) {
95 logger.debug(
96 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
97 commentObject.inReplyTo,
98 { err }
99 )
7d14d4d2 100 return
ee79b60e 101 }
2ccaeeb3 102
83e6519b
C
103 if (video.isOwned() && created === true) {
104 // Don't resend the activity to the sender
105 const exceptions = [ byActor ]
93ef8a9d 106
83e6519b
C
107 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
108 }
cef534ed 109
1198edf4 110 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
6d852470 111}
418d092a
C
112
113async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) {
114 const playlistObject = activity.object as PlaylistObject
115 const byAccount = byActor.Account
116
117 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
118
119 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
120}