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