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