]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityAnnounce } from '../../../../shared/models/activitypub' | |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
3 | import { logger } from '../../../helpers/logger' | |
4 | import { sequelizeTypescript } from '../../../initializers' | |
5 | import { ActorModel } from '../../../models/activitypub/actor' | |
6 | import { VideoModel } from '../../../models/video/video' | |
7 | import { VideoShareModel } from '../../../models/video/video-share' | |
8 | import { getOrCreateActorAndServerAndModel } from '../actor' | |
9 | import { forwardActivity } from '../send/misc' | |
10 | import { processCreateActivity } from './process-create' | |
11 | ||
12 | async function processAnnounceActivity (activity: ActivityAnnounce) { | |
13 | const announcedActivity = activity.object | |
14 | const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor) | |
15 | ||
16 | if (typeof announcedActivity === 'string') { | |
17 | return processVideoShare(actorAnnouncer, activity) | |
18 | } else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') { | |
19 | return processVideoShare(actorAnnouncer, activity) | |
20 | } | |
21 | ||
22 | logger.warn( | |
23 | 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type, | |
24 | { activity: activity.id } | |
25 | ) | |
26 | ||
27 | return undefined | |
28 | } | |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | ||
32 | export { | |
33 | processAnnounceActivity | |
34 | } | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { | |
39 | const options = { | |
40 | arguments: [ actorAnnouncer, activity ], | |
41 | errorMessage: 'Cannot share the video activity with many retries.' | |
42 | } | |
43 | ||
44 | return retryTransactionWrapper(shareVideo, options) | |
45 | } | |
46 | ||
47 | function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { | |
48 | const announced = activity.object | |
49 | ||
50 | return sequelizeTypescript.transaction(async t => { | |
51 | // Add share entry | |
52 | let video: VideoModel | |
53 | ||
54 | if (typeof announced === 'string') { | |
55 | video = await VideoModel.loadByUrlAndPopulateAccount(announced) | |
56 | if (!video) throw new Error('Unknown video to share ' + announced) | |
57 | } else { | |
58 | video = await processCreateActivity(announced) | |
59 | } | |
60 | ||
61 | const share = { | |
62 | actorId: actorAnnouncer.id, | |
63 | videoId: video.id | |
64 | } | |
65 | ||
66 | const [ , created ] = await VideoShareModel.findOrCreate({ | |
67 | where: share, | |
68 | defaults: share, | |
69 | transaction: t | |
70 | }) | |
71 | ||
72 | if (video.isOwned() && created === true) { | |
73 | // Don't resend the activity to the sender | |
74 | const exceptions = [ actorAnnouncer ] | |
75 | await forwardActivity(activity, t, exceptions) | |
76 | } | |
77 | ||
78 | return undefined | |
79 | }) | |
80 | } |