]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { ActivityCreate, VideoChannelObject } from '../../../../shared' |
63c93323 | 2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' |
54141398 | 3 | import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' |
40ff5707 | 4 | import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object' |
54141398 | 5 | import { logger, retryTransactionWrapper } from '../../../helpers' |
54141398 C |
6 | import { database as db } from '../../../initializers' |
7 | import { AccountInstance } from '../../../models/account/account-interface' | |
0f91ae62 | 8 | import { getOrCreateAccountAndServer } from '../account' |
63c93323 | 9 | import { forwardActivity } from '../send/misc' |
892211e8 | 10 | import { getVideoChannelActivityPubUrl } from '../url' |
20494f12 | 11 | import { videoChannelActivityObjectToDBAttributes } from './misc' |
e4f97bab | 12 | |
0d0e8dd0 | 13 | async function processCreateActivity (activity: ActivityCreate) { |
e4f97bab C |
14 | const activityObject = activity.object |
15 | const activityType = activityObject.type | |
0f91ae62 | 16 | const account = await getOrCreateAccountAndServer(activity.actor) |
e4f97bab | 17 | |
40ff5707 | 18 | if (activityType === 'View') { |
63c93323 | 19 | return processCreateView(account, activity) |
0032ebe9 | 20 | } else if (activityType === 'Dislike') { |
63c93323 | 21 | return processCreateDislike(account, activity) |
40ff5707 | 22 | } else if (activityType === 'VideoChannel') { |
0d0e8dd0 | 23 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) |
8e13fa7d C |
24 | } else if (activityType === 'Flag') { |
25 | return processCreateVideoAbuse(account, activityObject as VideoAbuseObject) | |
e4f97bab C |
26 | } |
27 | ||
28 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 29 | return Promise.resolve(undefined) |
e4f97bab C |
30 | } |
31 | ||
32 | // --------------------------------------------------------------------------- | |
33 | ||
34 | export { | |
35 | processCreateActivity | |
36 | } | |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
63c93323 | 40 | async function processCreateDislike (byAccount: AccountInstance, activity: ActivityCreate) { |
0032ebe9 | 41 | const options = { |
63c93323 | 42 | arguments: [ byAccount, activity ], |
0032ebe9 C |
43 | errorMessage: 'Cannot dislike the video with many retries.' |
44 | } | |
45 | ||
46 | return retryTransactionWrapper(createVideoDislike, options) | |
47 | } | |
48 | ||
63c93323 C |
49 | function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreate) { |
50 | const dislike = activity.object as DislikeObject | |
0032ebe9 | 51 | |
63c93323 C |
52 | return db.sequelize.transaction(async t => { |
53 | const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) | |
0032ebe9 C |
54 | if (!video) throw new Error('Unknown video ' + dislike.object) |
55 | ||
56 | const rate = { | |
57 | type: 'dislike' as 'dislike', | |
58 | videoId: video.id, | |
59 | accountId: byAccount.id | |
60 | } | |
61 | const [ , created ] = await db.AccountVideoRate.findOrCreate({ | |
62 | where: rate, | |
63c93323 C |
63 | defaults: rate, |
64 | transaction: t | |
0032ebe9 | 65 | }) |
63c93323 | 66 | await video.increment('dislikes', { transaction: t }) |
0032ebe9 | 67 | |
63c93323 C |
68 | if (video.isOwned() && created === true) { |
69 | // Don't resend the activity to the sender | |
70 | const exceptions = [ byAccount ] | |
71 | await forwardActivity(activity, t, exceptions) | |
72 | } | |
0032ebe9 C |
73 | }) |
74 | } | |
75 | ||
63c93323 C |
76 | async function processCreateView (byAccount: AccountInstance, activity: ActivityCreate) { |
77 | const view = activity.object as ViewObject | |
78 | ||
40ff5707 C |
79 | const video = await db.Video.loadByUrlAndPopulateAccount(view.object) |
80 | ||
81 | if (!video) throw new Error('Unknown video ' + view.object) | |
82 | ||
83 | const account = await db.Account.loadByUrl(view.actor) | |
84 | if (!account) throw new Error('Unknown account ' + view.actor) | |
85 | ||
86 | await video.increment('views') | |
87 | ||
63c93323 C |
88 | if (video.isOwned()) { |
89 | // Don't resend the activity to the sender | |
90 | const exceptions = [ byAccount ] | |
91 | await forwardActivity(activity, undefined, exceptions) | |
92 | } | |
40ff5707 C |
93 | } |
94 | ||
0d0e8dd0 | 95 | function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { |
e4f97bab | 96 | const options = { |
0d0e8dd0 C |
97 | arguments: [ account, videoChannelToCreateData ], |
98 | errorMessage: 'Cannot insert the remote video channel with many retries.' | |
e4f97bab C |
99 | } |
100 | ||
0d0e8dd0 | 101 | return retryTransactionWrapper(addRemoteVideoChannel, options) |
e4f97bab C |
102 | } |
103 | ||
20494f12 | 104 | function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { |
0d0e8dd0 | 105 | logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) |
e4f97bab | 106 | |
d8465018 | 107 | return db.sequelize.transaction(async t => { |
0d0e8dd0 | 108 | let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) |
63c93323 | 109 | if (videoChannel) return videoChannel |
0d0e8dd0 | 110 | |
20494f12 | 111 | const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) |
0d0e8dd0 | 112 | videoChannel = db.VideoChannel.build(videoChannelData) |
54141398 | 113 | videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) |
e4f97bab | 114 | |
d8465018 C |
115 | videoChannel = await videoChannel.save({ transaction: t }) |
116 | logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) | |
e4f97bab | 117 | |
d8465018 C |
118 | return videoChannel |
119 | }) | |
e4f97bab | 120 | } |
8e13fa7d C |
121 | |
122 | function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { | |
123 | const options = { | |
124 | arguments: [ account, videoAbuseToCreateData ], | |
125 | errorMessage: 'Cannot insert the remote video abuse with many retries.' | |
126 | } | |
127 | ||
128 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | |
129 | } | |
130 | ||
20494f12 | 131 | function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d C |
132 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
133 | ||
134 | return db.sequelize.transaction(async t => { | |
d7d5611c | 135 | const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) |
8e13fa7d C |
136 | if (!video) { |
137 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) | |
79d5caf9 | 138 | return undefined |
8e13fa7d C |
139 | } |
140 | ||
141 | const videoAbuseData = { | |
142 | reporterAccountId: account.id, | |
143 | reason: videoAbuseToCreateData.content, | |
144 | videoId: video.id | |
145 | } | |
146 | ||
147 | await db.VideoAbuse.create(videoAbuseData) | |
148 | ||
149 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
150 | }) | |
151 | } |