]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Fix user notifications on new follow
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
848f499d 1import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
6d852470 2import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
da854ddd
C
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
3fd3ab2d 5import { sequelizeTypescript } from '../../../initializers'
50d6de9c 6import { ActorModel } from '../../../models/activitypub/actor'
83e6519b 7import { addVideoComment, resolveThread } from '../video-comments'
1297eb5d 8import { getOrCreateVideoAndAccountAndChannel } from '../videos'
cfaf819c 9import { forwardVideoRelatedActivity } from '../send/utils'
b88a4596 10import { createOrUpdateCacheFile } from '../cache-file'
cef534ed 11import { Notifier } from '../../notifier'
418d092a
C
12import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
13import { createOrUpdateVideoPlaylist } from '../playlist'
ee79b60e 14import { VideoModel } from '../../../models/video/video'
1198edf4 15import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
e4f97bab 16
1198edf4
C
17async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) {
18 const { activity, byActor } = options
19
20 // Only notify if it is not from a fetcher job
21 const notify = options.fromFetch !== true
e4f97bab
C
22 const activityObject = activity.object
23 const activityType = activityObject.type
24
848f499d 25 if (activityType === 'Video') {
1198edf4 26 return processCreateVideo(activity, notify)
848f499d
C
27 }
28
29 if (activityType === 'Note') {
1198edf4 30 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
848f499d
C
31 }
32
33 if (activityType === 'CacheFile') {
418d092a
C
34 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
35 }
36
37 if (activityType === 'Playlist') {
38 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
e4f97bab
C
39 }
40
41 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 42 return Promise.resolve(undefined)
e4f97bab
C
43}
44
45// ---------------------------------------------------------------------------
46
47export {
48 processCreateActivity
49}
50
51// ---------------------------------------------------------------------------
52
1198edf4 53async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
50d6de9c
C
54 const videoToCreateData = activity.object as VideoTorrentObject
55
5b77537c 56 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
cef534ed 57
1198edf4 58 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
50d6de9c 59
50d6de9c
C
60 return video
61}
62
418d092a 63async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
c48e82b5
C
64 const cacheFile = activity.object as CacheFileObject
65
4157cdb1 66 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5 67
e5565833 68 await sequelizeTypescript.transaction(async t => {
b88a4596 69 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 70 })
c48e82b5
C
71
72 if (video.isOwned()) {
73 // Don't resend the activity to the sender
74 const exceptions = [ byActor ]
e5565833 75 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
76 }
77}
78
1198edf4 79async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel, notify: boolean) {
83e6519b 80 const commentObject = activity.object as VideoCommentObject
6d852470
C
81 const byAccount = byActor.Account
82
83 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
84
ee79b60e
C
85 let video: VideoModel
86 try {
87 const resolveThreadResult = await resolveThread(commentObject.inReplyTo)
88 video = resolveThreadResult.video
89 } catch (err) {
90 logger.debug(
91 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
92 commentObject.inReplyTo,
93 { err }
94 )
7d14d4d2 95 return
ee79b60e 96 }
2ccaeeb3 97
cef534ed 98 const { comment, created } = await addVideoComment(video, commentObject.id)
93ef8a9d 99
83e6519b
C
100 if (video.isOwned() && created === true) {
101 // Don't resend the activity to the sender
102 const exceptions = [ byActor ]
93ef8a9d 103
83e6519b
C
104 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
105 }
cef534ed 106
1198edf4 107 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
6d852470 108}
418d092a
C
109
110async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) {
111 const playlistObject = activity.object as PlaylistObject
112 const byAccount = byActor.Account
113
114 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
115
116 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
117}