]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Remove unused actor uuid field
[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'
848f499d
C
12import { processViewActivity } from './process-view'
13import { processDislikeActivity } from './process-dislike'
14import { processFlagActivity } from './process-flag'
418d092a
C
15import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
16import { createOrUpdateVideoPlaylist } from '../playlist'
e4f97bab 17
e587e0ec 18async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
e4f97bab
C
19 const activityObject = activity.object
20 const activityType = activityObject.type
21
40ff5707 22 if (activityType === 'View') {
848f499d
C
23 return processViewActivity(activity, byActor)
24 }
25
26 if (activityType === 'Dislike') {
27 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
28 }
29
30 if (activityType === 'Flag') {
31 return retryTransactionWrapper(processFlagActivity, activity, byActor)
32 }
33
34 if (activityType === 'Video') {
f6eebcb3 35 return processCreateVideo(activity)
848f499d
C
36 }
37
38 if (activityType === 'Note') {
39 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
40 }
41
42 if (activityType === 'CacheFile') {
418d092a
C
43 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
44 }
45
46 if (activityType === 'Playlist') {
47 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
e4f97bab
C
48 }
49
50 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 51 return Promise.resolve(undefined)
e4f97bab
C
52}
53
54// ---------------------------------------------------------------------------
55
56export {
57 processCreateActivity
58}
59
60// ---------------------------------------------------------------------------
61
f6eebcb3 62async function processCreateVideo (activity: ActivityCreate) {
50d6de9c
C
63 const videoToCreateData = activity.object as VideoTorrentObject
64
cef534ed
C
65 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
66
67 if (created) Notifier.Instance.notifyOnNewVideo(video)
50d6de9c 68
50d6de9c
C
69 return video
70}
71
418d092a 72async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
c48e82b5
C
73 const cacheFile = activity.object as CacheFileObject
74
4157cdb1 75 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5 76
e5565833 77 await sequelizeTypescript.transaction(async t => {
b88a4596 78 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 79 })
c48e82b5
C
80
81 if (video.isOwned()) {
82 // Don't resend the activity to the sender
83 const exceptions = [ byActor ]
e5565833 84 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
85 }
86}
87
848f499d 88async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
83e6519b 89 const commentObject = activity.object as VideoCommentObject
6d852470
C
90 const byAccount = byActor.Account
91
92 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
93
83e6519b 94 const { video } = await resolveThread(commentObject.inReplyTo)
2ccaeeb3 95
cef534ed 96 const { comment, created } = await addVideoComment(video, commentObject.id)
93ef8a9d 97
83e6519b
C
98 if (video.isOwned() && created === true) {
99 // Don't resend the activity to the sender
100 const exceptions = [ byActor ]
93ef8a9d 101
83e6519b
C
102 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
103 }
cef534ed
C
104
105 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
6d852470 106}
418d092a
C
107
108async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) {
109 const playlistObject = activity.object as PlaylistObject
110 const byAccount = byActor.Account
111
112 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
113
114 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
115}