]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityLike } from '../../../../shared/models/activitypub' | |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
3 | import { sequelizeTypescript } from '../../../initializers' | |
4 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
5 | import { ActorModel } from '../../../models/activitypub/actor' | |
6 | import { forwardVideoRelatedActivity } from '../send/utils' | |
7 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | |
8 | import { getVideoLikeActivityPubUrl } from '../url' | |
9 | import { getAPId } from '../../../helpers/activitypub' | |
10 | ||
11 | async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) { | |
12 | return retryTransactionWrapper(processLikeVideo, byActor, activity) | |
13 | } | |
14 | ||
15 | // --------------------------------------------------------------------------- | |
16 | ||
17 | export { | |
18 | processLikeActivity | |
19 | } | |
20 | ||
21 | // --------------------------------------------------------------------------- | |
22 | ||
23 | async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) { | |
24 | const videoUrl = getAPId(activity.object) | |
25 | ||
26 | const byAccount = byActor.Account | |
27 | if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url) | |
28 | ||
29 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl }) | |
30 | ||
31 | return sequelizeTypescript.transaction(async t => { | |
32 | const rate = { | |
33 | type: 'like' as 'like', | |
34 | videoId: video.id, | |
35 | accountId: byAccount.id | |
36 | } | |
37 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ | |
38 | where: rate, | |
39 | defaults: Object.assign({}, rate, { url: getVideoLikeActivityPubUrl(byActor, video) }), | |
40 | transaction: t | |
41 | }) | |
42 | if (created === true) await video.increment('likes', { transaction: t }) | |
43 | ||
44 | if (video.isOwned() && created === true) { | |
45 | // Don't resend the activity to the sender | |
46 | const exceptions = [ byActor ] | |
47 | ||
48 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
49 | } | |
50 | }) | |
51 | } |