diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-20 09:43:39 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | 54141398354e6e7b94aa3065a705a1251390111c (patch) | |
tree | 8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/lib/activitypub/process/process-create.ts | |
parent | eb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff) | |
download | PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip |
Refractor activity pub lib/helpers
Diffstat (limited to 'server/lib/activitypub/process/process-create.ts')
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts new file mode 100644 index 000000000..aac941a6c --- /dev/null +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -0,0 +1,88 @@ | |||
1 | import { ActivityCreate, VideoChannelObject } from '../../../../shared' | ||
2 | import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' | ||
3 | import { logger, retryTransactionWrapper } from '../../../helpers' | ||
4 | import { getOrCreateAccount, getVideoChannelActivityPubUrl } from '../../../helpers/activitypub' | ||
5 | import { database as db } from '../../../initializers' | ||
6 | import { AccountInstance } from '../../../models/account/account-interface' | ||
7 | import { videoChannelActivityObjectToDBAttributes } from './misc' | ||
8 | |||
9 | async function processCreateActivity (activity: ActivityCreate) { | ||
10 | const activityObject = activity.object | ||
11 | const activityType = activityObject.type | ||
12 | const account = await getOrCreateAccount(activity.actor) | ||
13 | |||
14 | if (activityType === 'VideoChannel') { | ||
15 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) | ||
16 | } else if (activityType === 'Flag') { | ||
17 | return processCreateVideoAbuse(account, activityObject as VideoAbuseObject) | ||
18 | } | ||
19 | |||
20 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | ||
21 | return Promise.resolve(undefined) | ||
22 | } | ||
23 | |||
24 | // --------------------------------------------------------------------------- | ||
25 | |||
26 | export { | ||
27 | processCreateActivity | ||
28 | } | ||
29 | |||
30 | // --------------------------------------------------------------------------- | ||
31 | |||
32 | function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { | ||
33 | const options = { | ||
34 | arguments: [ account, videoChannelToCreateData ], | ||
35 | errorMessage: 'Cannot insert the remote video channel with many retries.' | ||
36 | } | ||
37 | |||
38 | return retryTransactionWrapper(addRemoteVideoChannel, options) | ||
39 | } | ||
40 | |||
41 | function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { | ||
42 | logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) | ||
43 | |||
44 | return db.sequelize.transaction(async t => { | ||
45 | let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) | ||
46 | if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.') | ||
47 | |||
48 | const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) | ||
49 | videoChannel = db.VideoChannel.build(videoChannelData) | ||
50 | videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) | ||
51 | |||
52 | videoChannel = await videoChannel.save({ transaction: t }) | ||
53 | logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) | ||
54 | |||
55 | return videoChannel | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { | ||
60 | const options = { | ||
61 | arguments: [ account, videoAbuseToCreateData ], | ||
62 | errorMessage: 'Cannot insert the remote video abuse with many retries.' | ||
63 | } | ||
64 | |||
65 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | ||
66 | } | ||
67 | |||
68 | function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { | ||
69 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | ||
70 | |||
71 | return db.sequelize.transaction(async t => { | ||
72 | const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) | ||
73 | if (!video) { | ||
74 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) | ||
75 | return undefined | ||
76 | } | ||
77 | |||
78 | const videoAbuseData = { | ||
79 | reporterAccountId: account.id, | ||
80 | reason: videoAbuseToCreateData.content, | ||
81 | videoId: video.id | ||
82 | } | ||
83 | |||
84 | await db.VideoAbuse.create(videoAbuseData) | ||
85 | |||
86 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | ||
87 | }) | ||
88 | } | ||