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'
17 import { VideoModel } from '../../../models/video/video'
19 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
20 const activityObject = activity.object
21 const activityType = activityObject.type
23 if (activityType === 'View') {
24 return processViewActivity(activity, byActor)
27 if (activityType === 'Dislike') {
28 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
31 if (activityType === 'Flag') {
32 return retryTransactionWrapper(processFlagActivity, activity, byActor)
35 if (activityType === 'Video') {
36 return processCreateVideo(activity)
39 if (activityType === 'Note') {
40 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
43 if (activityType === 'CacheFile') {
44 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
47 if (activityType === 'Playlist') {
48 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
51 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
52 return Promise.resolve(undefined)
55 // ---------------------------------------------------------------------------
61 // ---------------------------------------------------------------------------
63 async function processCreateVideo (activity: ActivityCreate) {
64 const videoToCreateData = activity.object as VideoTorrentObject
66 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
68 if (created) Notifier.Instance.notifyOnNewVideo(video)
73 async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) {
74 const cacheFile = activity.object as CacheFileObject
76 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
78 await sequelizeTypescript.transaction(async t => {
79 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
82 if (video.isOwned()) {
83 // Don't resend the activity to the sender
84 const exceptions = [ byActor ]
85 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
89 async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
90 const commentObject = activity.object as VideoCommentObject
91 const byAccount = byActor.Account
93 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
97 const resolveThreadResult = await resolveThread(commentObject.inReplyTo)
98 video = resolveThreadResult.video
101 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
102 commentObject.inReplyTo,
107 const { comment, created } = await addVideoComment(video, commentObject.id)
109 if (video.isOwned() && created === true) {
110 // Don't resend the activity to the sender
111 const exceptions = [ byActor ]
113 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
116 if (created === true) Notifier.Instance.notifyOnNewComment(comment)
119 async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) {
120 const playlistObject = activity.object as PlaylistObject
121 const byAccount = byActor.Account
123 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
125 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)