]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
49af5ac8 1import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
696d83fd 2import { isRedundancyAccepted } from '@server/lib/redundancy'
b2111066
C
3import { VideoModel } from '@server/models/video/video'
4import { ActivityCreate, CacheFileObject, PlaylistObject, VideoCommentObject, VideoObject, WatchActionObject } from '@shared/models'
da854ddd
C
5import { retryTransactionWrapper } from '../../../helpers/database-utils'
6import { logger } from '../../../helpers/logger'
80fdaf06 7import { sequelizeTypescript } from '../../../initializers/database'
26d6bf65
C
8import { APProcessorOptions } from '../../../types/activitypub-processor.model'
9import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
696d83fd
C
10import { Notifier } from '../../notifier'
11import { createOrUpdateCacheFile } from '../cache-file'
b2111066 12import { createOrUpdateLocalVideoViewer } from '../local-video-viewer'
49af5ac8 13import { createOrUpdateVideoPlaylist } from '../playlists'
57e4e1c1 14import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
696d83fd 15import { resolveThread } from '../video-comments'
304a84d5 16import { getOrCreateAPVideo } from '../videos'
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') {
a2f99b54
C
31 // Comments will be fetched from videos
32 if (options.fromFetch) return
33
1198edf4 34 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
848f499d
C
35 }
36
b2111066
C
37 if (activityType === 'WatchAction') {
38 return retryTransactionWrapper(processCreateWatchAction, activity)
39 }
40
848f499d 41 if (activityType === 'CacheFile') {
418d092a
C
42 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
43 }
44
45 if (activityType === 'Playlist') {
46 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
e4f97bab
C
47 }
48
49 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 50 return Promise.resolve(undefined)
e4f97bab
C
51}
52
53// ---------------------------------------------------------------------------
54
55export {
56 processCreateActivity
57}
58
59// ---------------------------------------------------------------------------
60
1198edf4 61async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
de6310b2 62 const videoToCreateData = activity.object as VideoObject
50d6de9c 63
57e4e1c1 64 const syncParam = { rates: false, shares: false, comments: false, thumbnail: true, refreshVideo: false }
304a84d5 65 const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam })
cef534ed 66
1198edf4 67 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
50d6de9c 68
50d6de9c
C
69 return video
70}
71
453e83ea 72async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
8c9e7875
C
73 if (await isRedundancyAccepted(activity, byActor) !== true) return
74
c48e82b5
C
75 const cacheFile = activity.object as CacheFileObject
76
304a84d5 77 const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object })
c48e82b5 78
e5565833 79 await sequelizeTypescript.transaction(async t => {
b88a4596 80 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 81 })
c48e82b5
C
82
83 if (video.isOwned()) {
84 // Don't resend the activity to the sender
85 const exceptions = [ byActor ]
e5565833 86 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
87 }
88}
89
b2111066
C
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
453e83ea 103async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
83e6519b 104 const commentObject = activity.object as VideoCommentObject
6d852470
C
105 const byAccount = byActor.Account
106
107 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
108
0283eaac 109 let video: MVideoAccountLightBlacklistAllFiles
6b9c966f 110 let created: boolean
453e83ea 111 let comment: MCommentOwnerVideo
b2a70e3c 112
ee79b60e 113 try {
6b9c966f 114 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
b2a70e3c 115 if (!resolveThreadResult) return // Comment not accepted
403c69c5 116
ee79b60e 117 video = resolveThreadResult.video
6b9c966f
C
118 created = resolveThreadResult.commentCreated
119 comment = resolveThreadResult.comment
ee79b60e
C
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 )
7d14d4d2 126 return
ee79b60e 127 }
2ccaeeb3 128
7a4fd56c 129 // Try to not forward unwanted comments on our videos
403c69c5
C
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 }
696d83fd 135
403c69c5
C
136 if (created === true) {
137 // Don't resend the activity to the sender
138 const exceptions = [ byActor ]
93ef8a9d 139
403c69c5
C
140 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
141 }
83e6519b 142 }
cef534ed 143
1198edf4 144 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
6d852470 145}
418d092a 146
453e83ea 147async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
418d092a
C
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
37a44fc9 153 await createOrUpdateVideoPlaylist(playlistObject, activity.to)
418d092a 154}