]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-create.ts
Merge branch 'release/5.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
... / ...
CommitLineData
1import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
2import { isRedundancyAccepted } from '@server/lib/redundancy'
3import { VideoModel } from '@server/models/video/video'
4import { ActivityCreate, CacheFileObject, PlaylistObject, VideoCommentObject, VideoObject, WatchActionObject } from '@shared/models'
5import { retryTransactionWrapper } from '../../../helpers/database-utils'
6import { logger } from '../../../helpers/logger'
7import { sequelizeTypescript } from '../../../initializers/database'
8import { APProcessorOptions } from '../../../types/activitypub-processor.model'
9import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
10import { Notifier } from '../../notifier'
11import { createOrUpdateCacheFile } from '../cache-file'
12import { createOrUpdateLocalVideoViewer } from '../local-video-viewer'
13import { createOrUpdateVideoPlaylist } from '../playlists'
14import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
15import { resolveThread } from '../video-comments'
16import { getOrCreateAPVideo } from '../videos'
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 // Comments will be fetched from videos
32 if (options.fromFetch) return
33
34 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
35 }
36
37 if (activityType === 'WatchAction') {
38 return retryTransactionWrapper(processCreateWatchAction, activity)
39 }
40
41 if (activityType === 'CacheFile') {
42 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
43 }
44
45 if (activityType === 'Playlist') {
46 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
47 }
48
49 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
50 return Promise.resolve(undefined)
51}
52
53// ---------------------------------------------------------------------------
54
55export {
56 processCreateActivity
57}
58
59// ---------------------------------------------------------------------------
60
61async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
62 const videoToCreateData = activity.object as VideoObject
63
64 const syncParam = { rates: false, shares: false, comments: false, thumbnail: true, refreshVideo: false }
65 const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam })
66
67 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
68
69 return video
70}
71
72async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
73 if (await isRedundancyAccepted(activity, byActor) !== true) return
74
75 const cacheFile = activity.object as CacheFileObject
76
77 const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object })
78
79 await sequelizeTypescript.transaction(async t => {
80 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
81 })
82
83 if (video.isOwned()) {
84 // Don't resend the activity to the sender
85 const exceptions = [ byActor ]
86 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
87 }
88}
89
90async function processCreateWatchAction (activity: ActivityCreate) {
91 const watchAction = activity.object as WatchActionObject
92
93 if (watchAction.actionStatus !== 'CompletedActionStatus') return
94
95 const video = await VideoModel.loadByUrl(watchAction.object)
96 if (video.remote) return
97
98 await sequelizeTypescript.transaction(async t => {
99 return createOrUpdateLocalVideoViewer(watchAction, video, t)
100 })
101}
102
103async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
104 const commentObject = activity.object as VideoCommentObject
105 const byAccount = byActor.Account
106
107 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
108
109 let video: MVideoAccountLightBlacklistAllFiles
110 let created: boolean
111 let comment: MCommentOwnerVideo
112
113 try {
114 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
115 if (!resolveThreadResult) return // Comment not accepted
116
117 video = resolveThreadResult.video
118 created = resolveThreadResult.commentCreated
119 comment = resolveThreadResult.comment
120 } catch (err) {
121 logger.debug(
122 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
123 commentObject.inReplyTo,
124 { err }
125 )
126 return
127 }
128
129 // Try to not forward unwanted comments on our videos
130 if (video.isOwned()) {
131 if (await isBlockedByServerOrAccount(comment.Account, video.VideoChannel.Account)) {
132 logger.info('Skip comment forward from blocked account or server %s.', comment.Account.Actor.url)
133 return
134 }
135
136 if (created === true) {
137 // Don't resend the activity to the sender
138 const exceptions = [ byActor ]
139
140 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
141 }
142 }
143
144 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
145}
146
147async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
148 const playlistObject = activity.object as PlaylistObject
149 const byAccount = byActor.Account
150
151 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
152
153 await createOrUpdateVideoPlaylist(playlistObject, activity.to)
154}