]>
Commit | Line | Data |
---|---|---|
0d0e8dd0 C |
1 | import { ActivityCreate, VideoChannelObject, VideoTorrentObject } from '../../../shared' |
2 | import { ActivityAdd } from '../../../shared/models/activitypub/activity' | |
3 | import { generateThumbnailFromUrl, logger, retryTransactionWrapper } from '../../helpers' | |
e4f97bab | 4 | import { database as db } from '../../initializers' |
0d0e8dd0 C |
5 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
6 | import Bluebird = require('bluebird') | |
7 | import { AccountInstance } from '../../models/account/account-interface' | |
8 | import { getActivityPubUrl, getOrCreateAccount } from '../../helpers/activitypub' | |
e4f97bab | 9 | |
0d0e8dd0 | 10 | async function processCreateActivity (activity: ActivityCreate) { |
e4f97bab C |
11 | const activityObject = activity.object |
12 | const activityType = activityObject.type | |
0d0e8dd0 | 13 | const account = await getOrCreateAccount(activity.actor) |
e4f97bab | 14 | |
0d0e8dd0 C |
15 | if (activityType === 'VideoChannel') { |
16 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) | |
e4f97bab C |
17 | } |
18 | ||
19 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 20 | return Promise.resolve(undefined) |
e4f97bab C |
21 | } |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | export { | |
26 | processCreateActivity | |
27 | } | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
0d0e8dd0 | 31 | function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { |
e4f97bab | 32 | const options = { |
0d0e8dd0 C |
33 | arguments: [ account, videoChannelToCreateData ], |
34 | errorMessage: 'Cannot insert the remote video channel with many retries.' | |
e4f97bab C |
35 | } |
36 | ||
0d0e8dd0 | 37 | return retryTransactionWrapper(addRemoteVideoChannel, options) |
e4f97bab C |
38 | } |
39 | ||
0d0e8dd0 C |
40 | async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { |
41 | logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) | |
e4f97bab C |
42 | |
43 | await db.sequelize.transaction(async t => { | |
0d0e8dd0 C |
44 | let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) |
45 | if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.') | |
46 | ||
47 | const videoChannelData = { | |
48 | name: videoChannelToCreateData.name, | |
49 | description: videoChannelToCreateData.content, | |
50 | uuid: videoChannelToCreateData.uuid, | |
51 | createdAt: videoChannelToCreateData.published, | |
52 | updatedAt: videoChannelToCreateData.updated, | |
e4f97bab | 53 | remote: true, |
0d0e8dd0 | 54 | accountId: account.id |
e4f97bab C |
55 | } |
56 | ||
0d0e8dd0 C |
57 | videoChannel = db.VideoChannel.build(videoChannelData) |
58 | videoChannel.url = getActivityPubUrl('videoChannel', videoChannel.uuid) | |
e4f97bab | 59 | |
0d0e8dd0 | 60 | await videoChannel.save({ transaction: t }) |
e4f97bab C |
61 | }) |
62 | ||
0d0e8dd0 | 63 | logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) |
e4f97bab | 64 | } |