]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import { ActivityCreate, VideoTorrentObject } from '../../../../shared' | |
3 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' | |
4 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | |
5 | import { VideoRateType } from '../../../../shared/models/videos' | |
6 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
7 | import { logger } from '../../../helpers/logger' | |
8 | import { sequelizeTypescript } from '../../../initializers' | |
9 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
10 | import { ActorModel } from '../../../models/activitypub/actor' | |
11 | import { VideoModel } from '../../../models/video/video' | |
12 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | |
13 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
14 | import { getOrCreateActorAndServerAndModel } from '../actor' | |
15 | import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc' | |
16 | import { addVideoComments, resolveThread } from '../video-comments' | |
17 | import { addVideoShares, getOrCreateAccountAndVideoAndChannel } from '../videos' | |
18 | ||
19 | async function processCreateActivity (activity: ActivityCreate) { | |
20 | const activityObject = activity.object | |
21 | const activityType = activityObject.type | |
22 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) | |
23 | ||
24 | if (activityType === 'View') { | |
25 | return processCreateView(actor, activity) | |
26 | } else if (activityType === 'Dislike') { | |
27 | return processCreateDislike(actor, activity) | |
28 | } else if (activityType === 'Video') { | |
29 | return processCreateVideo(actor, activity) | |
30 | } else if (activityType === 'Flag') { | |
31 | return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject) | |
32 | } else if (activityType === 'Note') { | |
33 | return processCreateVideoComment(actor, activity) | |
34 | } | |
35 | ||
36 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
37 | return Promise.resolve(undefined) | |
38 | } | |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
42 | export { | |
43 | processCreateActivity | |
44 | } | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | async function processCreateVideo ( | |
49 | actor: ActorModel, | |
50 | activity: ActivityCreate | |
51 | ) { | |
52 | const videoToCreateData = activity.object as VideoTorrentObject | |
53 | ||
54 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor) | |
55 | ||
56 | // Process outside the transaction because we could fetch remote data | |
57 | if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) { | |
58 | logger.info('Adding likes of video %s.', video.uuid) | |
59 | await createRates(videoToCreateData.likes.orderedItems, video, 'like') | |
60 | } | |
61 | ||
62 | if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) { | |
63 | logger.info('Adding dislikes of video %s.', video.uuid) | |
64 | await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike') | |
65 | } | |
66 | ||
67 | if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) { | |
68 | logger.info('Adding shares of video %s.', video.uuid) | |
69 | await addVideoShares(video, videoToCreateData.shares.orderedItems) | |
70 | } | |
71 | ||
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 | ||
77 | return video | |
78 | } | |
79 | ||
80 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { | |
81 | let rateCounts = 0 | |
82 | const tasks: Bluebird<any>[] = [] | |
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) { | |
108 | const options = { | |
109 | arguments: [ byActor, activity ], | |
110 | errorMessage: 'Cannot dislike the video with many retries.' | |
111 | } | |
112 | ||
113 | return retryTransactionWrapper(createVideoDislike, options) | |
114 | } | |
115 | ||
116 | async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { | |
117 | const dislike = activity.object as DislikeObject | |
118 | const byAccount = byActor.Account | |
119 | ||
120 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
121 | ||
122 | const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) | |
123 | ||
124 | return sequelizeTypescript.transaction(async t => { | |
125 | const rate = { | |
126 | type: 'dislike' as 'dislike', | |
127 | videoId: video.id, | |
128 | accountId: byAccount.id | |
129 | } | |
130 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ | |
131 | where: rate, | |
132 | defaults: rate, | |
133 | transaction: t | |
134 | }) | |
135 | if (created === true) await video.increment('dislikes', { transaction: t }) | |
136 | ||
137 | if (video.isOwned() && created === true) { | |
138 | // Don't resend the activity to the sender | |
139 | const exceptions = [ byActor ] | |
140 | await forwardActivity(activity, t, exceptions) | |
141 | } | |
142 | }) | |
143 | } | |
144 | ||
145 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { | |
146 | const view = activity.object as ViewObject | |
147 | ||
148 | const { video } = await getOrCreateAccountAndVideoAndChannel(view.object) | |
149 | ||
150 | const account = await ActorModel.loadByUrl(view.actor) | |
151 | if (!account) throw new Error('Unknown account ' + view.actor) | |
152 | ||
153 | await video.increment('views') | |
154 | ||
155 | if (video.isOwned()) { | |
156 | // Don't resend the activity to the sender | |
157 | const exceptions = [ byActor ] | |
158 | await forwardActivity(activity, undefined, exceptions) | |
159 | } | |
160 | } | |
161 | ||
162 | function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { | |
163 | const options = { | |
164 | arguments: [ actor, videoAbuseToCreateData ], | |
165 | errorMessage: 'Cannot insert the remote video abuse with many retries.' | |
166 | } | |
167 | ||
168 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | |
169 | } | |
170 | ||
171 | async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { | |
172 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | |
173 | ||
174 | const account = actor.Account | |
175 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) | |
176 | ||
177 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object) | |
178 | ||
179 | return sequelizeTypescript.transaction(async t => { | |
180 | const videoAbuseData = { | |
181 | reporterAccountId: account.id, | |
182 | reason: videoAbuseToCreateData.content, | |
183 | videoId: video.id | |
184 | } | |
185 | ||
186 | await VideoAbuseModel.create(videoAbuseData) | |
187 | ||
188 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
189 | }) | |
190 | } | |
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 | ||
201 | async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) { | |
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 | ||
207 | const { video, parents } = await resolveThread(comment.inReplyTo) | |
208 | ||
209 | return sequelizeTypescript.transaction(async t => { | |
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 | } | |
219 | ||
220 | // This is a new thread | |
221 | const objectToCreate = { | |
222 | url: comment.id, | |
223 | text: comment.content, | |
224 | originCommentId, | |
225 | inReplyToCommentId, | |
226 | videoId: video.id, | |
227 | accountId: byAccount.id | |
228 | } | |
229 | ||
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) { | |
240 | // Don't resend the activity to the sender | |
241 | const exceptions = [ byActor ] | |
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) | |
248 | } | |
249 | }) | |
250 | } |