Send server announce when users upload a video
[oweals/peertube.git] / server / lib / activitypub / process-announce.ts
1 import { ActivityAnnounce } from '../../../shared/models/activitypub/activity'
2 import { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object'
3 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object'
4 import { logger } from '../../helpers/logger'
5 import { processAddActivity } from './process-add'
6 import { processCreateActivity } from './process-create'
7 import { database as db } from '../../initializers/index'
8 import { getOrCreateAccount } from '../../helpers/activitypub'
9 import { VideoChannelInstance } from '../../models/video/video-channel-interface'
10 import { VideoInstance } from '../../models/index'
11
12 async function processAnnounceActivity (activity: ActivityAnnounce) {
13   const activityType = activity.object.type
14   const accountAnnouncer = await getOrCreateAccount(activity.actor)
15
16   if (activityType === 'VideoChannel') {
17     const activityCreate = Object.assign(activity, {
18       type: 'Create' as 'Create',
19       actor: activity.object.actor,
20       object: activity.object as VideoChannelObject
21     })
22
23     // Add share entry
24     const videoChannel: VideoChannelInstance = await processCreateActivity(activityCreate)
25     await db.VideoChannelShare.create({
26       accountId: accountAnnouncer.id,
27       videoChannelId: videoChannel.id
28     })
29   } else if (activityType === 'Video') {
30     const activityAdd = Object.assign(activity, {
31       type: 'Add' as 'Add',
32       actor: activity.object.actor,
33       object: activity.object as VideoTorrentObject
34     })
35
36     // Add share entry
37     const video: VideoInstance = await processAddActivity(activityAdd)
38     await db.VideoShare.create({
39       accountId: accountAnnouncer.id,
40       videoId: video.id
41     })
42   }
43
44   logger.warn('Unknown activity object type %s when announcing activity.', activityType, { activity: activity.id })
45   return Promise.resolve(undefined)
46 }
47
48 // ---------------------------------------------------------------------------
49
50 export {
51   processAnnounceActivity
52 }