]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityAnnounce } from '../../../../shared/models/activitypub' | |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
3 | import { sequelizeTypescript } from '../../../initializers' | |
4 | import { ActorModel } from '../../../models/activitypub/actor' | |
5 | import { VideoModel } from '../../../models/video/video' | |
6 | import { VideoShareModel } from '../../../models/video/video-share' | |
7 | import { getOrCreateActorAndServerAndModel } from '../actor' | |
8 | import { forwardVideoRelatedActivity } from '../send/utils' | |
9 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' | |
10 | ||
11 | async function processAnnounceActivity (activity: ActivityAnnounce) { | |
12 | const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor) | |
13 | ||
14 | return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity) | |
15 | } | |
16 | ||
17 | // --------------------------------------------------------------------------- | |
18 | ||
19 | export { | |
20 | processAnnounceActivity | |
21 | } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { | |
26 | const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id | |
27 | let video: VideoModel | |
28 | ||
29 | const res = await getOrCreateAccountAndVideoAndChannel(objectUri) | |
30 | video = res.video | |
31 | ||
32 | return sequelizeTypescript.transaction(async t => { | |
33 | // Add share entry | |
34 | ||
35 | const share = { | |
36 | actorId: actorAnnouncer.id, | |
37 | videoId: video.id, | |
38 | url: activity.id | |
39 | } | |
40 | ||
41 | const [ , created ] = await VideoShareModel.findOrCreate({ | |
42 | where: { | |
43 | url: activity.id | |
44 | }, | |
45 | defaults: share, | |
46 | transaction: t | |
47 | }) | |
48 | ||
49 | if (video.isOwned() && created === true) { | |
50 | // Don't resend the activity to the sender | |
51 | const exceptions = [ actorAnnouncer ] | |
52 | ||
53 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
54 | } | |
55 | ||
56 | return undefined | |
57 | }) | |
58 | } |