1 import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
2 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { addVideoComment, resolveThread } from '../video-comments'
8 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9 import { forwardVideoRelatedActivity } from '../send/utils'
10 import { createOrUpdateCacheFile } from '../cache-file'
11 import { Notifier } from '../../notifier'
12 import { processViewActivity } from './process-view'
13 import { processDislikeActivity } from './process-dislike'
14 import { processFlagActivity } from './process-flag'
15 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
16 import { createOrUpdateVideoPlaylist } from '../playlist'
18 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
19 const activityObject = activity.object
20 const activityType = activityObject.type
22 if (activityType === 'View') {
23 return processViewActivity(activity, byActor)
26 if (activityType === 'Dislike') {
27 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
30 if (activityType === 'Flag') {
31 return retryTransactionWrapper(processFlagActivity, activity, byActor)
34 if (activityType === 'Video') {
35 return processCreateVideo(activity)
38 if (activityType === 'Note') {
39 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
42 if (activityType === 'CacheFile') {
43 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
46 if (activityType === 'Playlist') {
47 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
50 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
51 return Promise.resolve(undefined)
54 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 async function processCreateVideo (activity: ActivityCreate) {
63 const videoToCreateData = activity.object as VideoTorrentObject
65 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
67 if (created) Notifier.Instance.notifyOnNewVideo(video)
72 async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
73 const cacheFile = activity.object as CacheFileObject
75 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
77 await sequelizeTypescript.transaction(async t => {
78 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
81 if (video.isOwned()) {
82 // Don't resend the activity to the sender
83 const exceptions = [ byActor ]
84 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
88 async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
89 const commentObject = activity.object as VideoCommentObject
90 const byAccount = byActor.Account
92 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
94 const { video } = await resolveThread(commentObject.inReplyTo)
96 const { comment, created } = await addVideoComment(video, commentObject.id)
98 if (video.isOwned() && created === true) {
99 // Don't resend the activity to the sender
100 const exceptions = [ byActor ]
102 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
105 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
108 async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) {
109 const playlistObject = activity.object as PlaylistObject
110 const byAccount = byActor.Account
112 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
114 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)