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