]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-create.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
... / ...
CommitLineData
1import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
2import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
5import { sequelizeTypescript } from '../../../initializers'
6import { ActorModel } from '../../../models/activitypub/actor'
7import { addVideoComment, resolveThread } from '../video-comments'
8import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9import { forwardVideoRelatedActivity } from '../send/utils'
10import { createOrUpdateCacheFile } from '../cache-file'
11import { Notifier } from '../../notifier'
12import { processViewActivity } from './process-view'
13import { processDislikeActivity } from './process-dislike'
14import { processFlagActivity } from './process-flag'
15
16async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
19
20 if (activityType === 'View') {
21 return processViewActivity(activity, byActor)
22 }
23
24 if (activityType === 'Dislike') {
25 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
26 }
27
28 if (activityType === 'Flag') {
29 return retryTransactionWrapper(processFlagActivity, activity, byActor)
30 }
31
32 if (activityType === 'Video') {
33 return processCreateVideo(activity)
34 }
35
36 if (activityType === 'Note') {
37 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
38 }
39
40 if (activityType === 'CacheFile') {
41 return retryTransactionWrapper(processCacheFile, activity, byActor)
42 }
43
44 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
45 return Promise.resolve(undefined)
46}
47
48// ---------------------------------------------------------------------------
49
50export {
51 processCreateActivity
52}
53
54// ---------------------------------------------------------------------------
55
56async function processCreateVideo (activity: ActivityCreate) {
57 const videoToCreateData = activity.object as VideoTorrentObject
58
59 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
60
61 if (created) Notifier.Instance.notifyOnNewVideo(video)
62
63 return video
64}
65
66async function processCacheFile (activity: ActivityCreate, byActor: ActorModel) {
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
82async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
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 const { video } = await resolveThread(commentObject.inReplyTo)
89
90 const { comment, created } = await addVideoComment(video, commentObject.id)
91
92 if (video.isOwned() && created === true) {
93 // Don't resend the activity to the sender
94 const exceptions = [ byActor ]
95
96 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
97 }
98
99 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
100}