]>
Commit | Line | Data |
---|---|---|
50d6de9c C |
1 | import * as Bluebird from 'bluebird' |
2 | import { ActivityCreate, VideoTorrentObject } from '../../../../shared' | |
3fd3ab2d | 3 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' |
6d852470 | 4 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' |
50d6de9c | 5 | import { VideoRateType } from '../../../../shared/models/videos' |
da854ddd C |
6 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
7 | import { logger } from '../../../helpers/logger' | |
3fd3ab2d | 8 | import { sequelizeTypescript } from '../../../initializers' |
3fd3ab2d | 9 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
50d6de9c | 10 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d C |
11 | import { VideoModel } from '../../../models/video/video' |
12 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | |
6d852470 | 13 | import { VideoCommentModel } from '../../../models/video/video-comment' |
50d6de9c | 14 | import { getOrCreateActorAndServerAndModel } from '../actor' |
93ef8a9d | 15 | import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc' |
2ccaeeb3 C |
16 | import { addVideoComments, resolveThread } from '../video-comments' |
17 | import { addVideoShares, getOrCreateAccountAndVideoAndChannel } from '../videos' | |
e4f97bab | 18 | |
0d0e8dd0 | 19 | async function processCreateActivity (activity: ActivityCreate) { |
e4f97bab C |
20 | const activityObject = activity.object |
21 | const activityType = activityObject.type | |
50d6de9c | 22 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
e4f97bab | 23 | |
40ff5707 | 24 | if (activityType === 'View') { |
50d6de9c | 25 | return processCreateView(actor, activity) |
0032ebe9 | 26 | } else if (activityType === 'Dislike') { |
50d6de9c C |
27 | return processCreateDislike(actor, activity) |
28 | } else if (activityType === 'Video') { | |
29 | return processCreateVideo(actor, activity) | |
8e13fa7d | 30 | } else if (activityType === 'Flag') { |
50d6de9c | 31 | return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject) |
6d852470 C |
32 | } else if (activityType === 'Note') { |
33 | return processCreateVideoComment(actor, activity) | |
e4f97bab C |
34 | } |
35 | ||
36 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 37 | return Promise.resolve(undefined) |
e4f97bab C |
38 | } |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
42 | export { | |
43 | processCreateActivity | |
44 | } | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
50d6de9c C |
48 | async function processCreateVideo ( |
49 | actor: ActorModel, | |
50 | activity: ActivityCreate | |
51 | ) { | |
52 | const videoToCreateData = activity.object as VideoTorrentObject | |
53 | ||
2ccaeeb3 | 54 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor) |
50d6de9c C |
55 | |
56 | // Process outside the transaction because we could fetch remote data | |
57 | if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) { | |
da854ddd | 58 | logger.info('Adding likes of video %s.', video.uuid) |
50d6de9c C |
59 | await createRates(videoToCreateData.likes.orderedItems, video, 'like') |
60 | } | |
61 | ||
62 | if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) { | |
da854ddd | 63 | logger.info('Adding dislikes of video %s.', video.uuid) |
50d6de9c C |
64 | await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike') |
65 | } | |
66 | ||
67 | if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) { | |
da854ddd | 68 | logger.info('Adding shares of video %s.', video.uuid) |
50d6de9c C |
69 | await addVideoShares(video, videoToCreateData.shares.orderedItems) |
70 | } | |
71 | ||
da854ddd C |
72 | if (videoToCreateData.comments && Array.isArray(videoToCreateData.comments.orderedItems)) { |
73 | logger.info('Adding comments of video %s.', video.uuid) | |
74 | await addVideoComments(video, videoToCreateData.comments.orderedItems) | |
75 | } | |
76 | ||
50d6de9c C |
77 | return video |
78 | } | |
79 | ||
50d6de9c C |
80 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { |
81 | let rateCounts = 0 | |
1f7ab4f3 | 82 | const tasks: Bluebird<number>[] = [] |
50d6de9c C |
83 | |
84 | for (const actorUrl of actorUrls) { | |
85 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | |
86 | const p = AccountVideoRateModel | |
87 | .create({ | |
88 | videoId: video.id, | |
89 | accountId: actor.Account.id, | |
90 | type: rate | |
91 | }) | |
92 | .then(() => rateCounts += 1) | |
93 | ||
94 | tasks.push(p) | |
95 | } | |
96 | ||
97 | await Promise.all(tasks) | |
98 | ||
99 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | |
100 | ||
101 | // This is "likes" and "dislikes" | |
102 | await video.increment(rate + 's', { by: rateCounts }) | |
103 | ||
104 | return | |
105 | } | |
106 | ||
107 | async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { | |
0032ebe9 | 108 | const options = { |
50d6de9c | 109 | arguments: [ byActor, activity ], |
0032ebe9 C |
110 | errorMessage: 'Cannot dislike the video with many retries.' |
111 | } | |
112 | ||
113 | return retryTransactionWrapper(createVideoDislike, options) | |
114 | } | |
115 | ||
2ccaeeb3 | 116 | async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 | 117 | const dislike = activity.object as DislikeObject |
50d6de9c C |
118 | const byAccount = byActor.Account |
119 | ||
120 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
0032ebe9 | 121 | |
2ccaeeb3 | 122 | const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) |
0032ebe9 | 123 | |
2ccaeeb3 | 124 | return sequelizeTypescript.transaction(async t => { |
0032ebe9 C |
125 | const rate = { |
126 | type: 'dislike' as 'dislike', | |
127 | videoId: video.id, | |
128 | accountId: byAccount.id | |
129 | } | |
3fd3ab2d | 130 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 131 | where: rate, |
63c93323 C |
132 | defaults: rate, |
133 | transaction: t | |
0032ebe9 | 134 | }) |
f00984c0 | 135 | if (created === true) await video.increment('dislikes', { transaction: t }) |
0032ebe9 | 136 | |
63c93323 C |
137 | if (video.isOwned() && created === true) { |
138 | // Don't resend the activity to the sender | |
50d6de9c | 139 | const exceptions = [ byActor ] |
63c93323 C |
140 | await forwardActivity(activity, t, exceptions) |
141 | } | |
0032ebe9 C |
142 | }) |
143 | } | |
144 | ||
6d852470 | 145 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 C |
146 | const view = activity.object as ViewObject |
147 | ||
2ccaeeb3 | 148 | const { video } = await getOrCreateAccountAndVideoAndChannel(view.object) |
40ff5707 | 149 | |
7bc29171 C |
150 | const actor = await ActorModel.loadByUrl(view.actor) |
151 | if (!actor) throw new Error('Unknown actor ' + view.actor) | |
40ff5707 C |
152 | |
153 | await video.increment('views') | |
154 | ||
63c93323 C |
155 | if (video.isOwned()) { |
156 | // Don't resend the activity to the sender | |
6d852470 | 157 | const exceptions = [ byActor ] |
63c93323 C |
158 | await forwardActivity(activity, undefined, exceptions) |
159 | } | |
40ff5707 C |
160 | } |
161 | ||
50d6de9c | 162 | function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d | 163 | const options = { |
50d6de9c | 164 | arguments: [ actor, videoAbuseToCreateData ], |
8e13fa7d C |
165 | errorMessage: 'Cannot insert the remote video abuse with many retries.' |
166 | } | |
167 | ||
168 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | |
169 | } | |
170 | ||
2ccaeeb3 | 171 | async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d C |
172 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
173 | ||
50d6de9c C |
174 | const account = actor.Account |
175 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) | |
176 | ||
2ccaeeb3 | 177 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object) |
8e13fa7d | 178 | |
2ccaeeb3 | 179 | return sequelizeTypescript.transaction(async t => { |
8e13fa7d C |
180 | const videoAbuseData = { |
181 | reporterAccountId: account.id, | |
182 | reason: videoAbuseToCreateData.content, | |
183 | videoId: video.id | |
184 | } | |
185 | ||
3fd3ab2d | 186 | await VideoAbuseModel.create(videoAbuseData) |
8e13fa7d C |
187 | |
188 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
189 | }) | |
190 | } | |
6d852470 C |
191 | |
192 | function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { | |
193 | const options = { | |
194 | arguments: [ byActor, activity ], | |
195 | errorMessage: 'Cannot create video comment with many retries.' | |
196 | } | |
197 | ||
198 | return retryTransactionWrapper(createVideoComment, options) | |
199 | } | |
200 | ||
2ccaeeb3 | 201 | async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) { |
6d852470 C |
202 | const comment = activity.object as VideoCommentObject |
203 | const byAccount = byActor.Account | |
204 | ||
205 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
206 | ||
2ccaeeb3 C |
207 | const { video, parents } = await resolveThread(comment.inReplyTo) |
208 | ||
6d852470 | 209 | return sequelizeTypescript.transaction(async t => { |
2ccaeeb3 C |
210 | let originCommentId = null |
211 | let inReplyToCommentId = null | |
212 | ||
213 | if (parents.length !== 0) { | |
214 | const parent = parents[0] | |
215 | ||
216 | originCommentId = parent.getThreadId() | |
217 | inReplyToCommentId = parent.id | |
218 | } | |
6d852470 C |
219 | |
220 | // This is a new thread | |
2ccaeeb3 C |
221 | const objectToCreate = { |
222 | url: comment.id, | |
223 | text: comment.content, | |
224 | originCommentId, | |
225 | inReplyToCommentId, | |
226 | videoId: video.id, | |
227 | accountId: byAccount.id | |
ea44f375 C |
228 | } |
229 | ||
93ef8a9d C |
230 | const options = { |
231 | where: { | |
232 | url: objectToCreate.url | |
233 | }, | |
234 | defaults: objectToCreate, | |
235 | transaction: t | |
236 | } | |
237 | const [ ,created ] = await VideoCommentModel.findOrCreate(options) | |
238 | ||
239 | if (video.isOwned() && created === true) { | |
ea44f375 C |
240 | // Don't resend the activity to the sender |
241 | const exceptions = [ byActor ] | |
93ef8a9d C |
242 | |
243 | // Mastodon does not add our announces in audience, so we forward to them manually | |
244 | const additionalActors = await getActorsInvolvedInVideo(video, t) | |
245 | const additionalFollowerUrls = additionalActors.map(a => a.followersUrl) | |
246 | ||
247 | await forwardActivity(activity, t, exceptions, additionalFollowerUrls) | |
ea44f375 | 248 | } |
6d852470 C |
249 | }) |
250 | } |