]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-create.ts
Merge branch 'release/2.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
1 import { isRedundancyAccepted } from '@server/lib/redundancy'
2 import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
3 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
4 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
5 import { retryTransactionWrapper } from '../../../helpers/database-utils'
6 import { logger } from '../../../helpers/logger'
7 import { sequelizeTypescript } from '../../../initializers/database'
8 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
9 import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../typings/models'
10 import { Notifier } from '../../notifier'
11 import { createOrUpdateCacheFile } from '../cache-file'
12 import { createOrUpdateVideoPlaylist } from '../playlist'
13 import { forwardVideoRelatedActivity } from '../send/utils'
14 import { resolveThread } from '../video-comments'
15 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
16 import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
17
18 async 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
48 export {
49 processCreateActivity
50 }
51
52 // ---------------------------------------------------------------------------
53
54 async 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
64 async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
65 if (await isRedundancyAccepted(activity, byActor) !== true) return
66
67 const cacheFile = activity.object as CacheFileObject
68
69 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
70
71 await sequelizeTypescript.transaction(async t => {
72 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
73 })
74
75 if (video.isOwned()) {
76 // Don't resend the activity to the sender
77 const exceptions = [ byActor ]
78 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
79 }
80 }
81
82 async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
83 const commentObject = activity.object as VideoCommentObject
84 const byAccount = byActor.Account
85
86 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
87
88 let video: MVideoAccountLightBlacklistAllFiles
89 let created: boolean
90 let comment: MCommentOwnerVideo
91 try {
92 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
93 video = resolveThreadResult.video
94 created = resolveThreadResult.commentCreated
95 comment = resolveThreadResult.comment
96 } catch (err) {
97 logger.debug(
98 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
99 commentObject.inReplyTo,
100 { err }
101 )
102 return
103 }
104
105 // Try to not forward unwanted commments on our videos
106 if (video.isOwned() && await isBlockedByServerOrAccount(comment.Account, video.VideoChannel.Account)) {
107 logger.info('Skip comment forward from blocked account or server %s.', comment.Account.Actor.url)
108 return
109 }
110
111 if (video.isOwned() && created === true) {
112 // Don't resend the activity to the sender
113 const exceptions = [ byActor ]
114
115 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
116 }
117
118 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
119 }
120
121 async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
122 const playlistObject = activity.object as PlaylistObject
123 const byAccount = byActor.Account
124
125 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
126
127 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
128 }