]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-create.ts
Automatically remove bad followings
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
... / ...
CommitLineData
1import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
2import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
5import { sequelizeTypescript } from '../../../initializers'
6import { ActorModel } from '../../../models/activitypub/actor'
7import { resolveThread } from '../video-comments'
8import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9import { forwardVideoRelatedActivity } from '../send/utils'
10import { createOrUpdateCacheFile } from '../cache-file'
11import { Notifier } from '../../notifier'
12import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
13import { createOrUpdateVideoPlaylist } from '../playlist'
14import { VideoModel } from '../../../models/video/video'
15import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
16import { VideoCommentModel } from '../../../models/video/video-comment'
17
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
23 const activityObject = activity.object
24 const activityType = activityObject.type
25
26 if (activityType === 'Video') {
27 return processCreateVideo(activity, notify)
28 }
29
30 if (activityType === 'Note') {
31 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
32 }
33
34 if (activityType === 'CacheFile') {
35 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
36 }
37
38 if (activityType === 'Playlist') {
39 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
40 }
41
42 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
43 return Promise.resolve(undefined)
44}
45
46// ---------------------------------------------------------------------------
47
48export {
49 processCreateActivity
50}
51
52// ---------------------------------------------------------------------------
53
54async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
55 const videoToCreateData = activity.object as VideoTorrentObject
56
57 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
58
59 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
60
61 return video
62}
63
64async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
65 const cacheFile = activity.object as CacheFileObject
66
67 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
68
69 await sequelizeTypescript.transaction(async t => {
70 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
71 })
72
73 if (video.isOwned()) {
74 // Don't resend the activity to the sender
75 const exceptions = [ byActor ]
76 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
77 }
78}
79
80async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel, notify: boolean) {
81 const commentObject = activity.object as VideoCommentObject
82 const byAccount = byActor.Account
83
84 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
85
86 let video: VideoModel
87 let created: boolean
88 let comment: VideoCommentModel
89 try {
90 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
91 video = resolveThreadResult.video
92 created = resolveThreadResult.commentCreated
93 comment = resolveThreadResult.comment
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 )
100 return
101 }
102
103 if (video.isOwned() && created === true) {
104 // Don't resend the activity to the sender
105 const exceptions = [ byActor ]
106
107 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
108 }
109
110 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
111}
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}