]>
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' |
2ccaeeb3 | 10 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' |
79d5caf9 | 11 | import { processCreateActivity } from './process-create' |
d8465018 C |
12 | |
13 | async function processAnnounceActivity (activity: ActivityAnnounce) { | |
20494f12 | 14 | const announcedActivity = activity.object |
50d6de9c | 15 | const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor) |
d8465018 | 16 | |
d7a7c248 C |
17 | if (typeof announcedActivity === 'string') { |
18 | return processVideoShare(actorAnnouncer, activity) | |
19 | } else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') { | |
50d6de9c | 20 | return processVideoShare(actorAnnouncer, activity) |
d8465018 C |
21 | } |
22 | ||
20494f12 C |
23 | logger.warn( |
24 | 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type, | |
25 | { activity: activity.id } | |
26 | ) | |
54141398 C |
27 | |
28 | return undefined | |
d8465018 C |
29 | } |
30 | ||
31 | // --------------------------------------------------------------------------- | |
32 | ||
33 | export { | |
34 | processAnnounceActivity | |
35 | } | |
4e50b6a1 C |
36 | |
37 | // --------------------------------------------------------------------------- | |
38 | ||
50d6de9c | 39 | function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { |
4e50b6a1 | 40 | const options = { |
50d6de9c | 41 | arguments: [ actorAnnouncer, activity ], |
d7a7c248 | 42 | errorMessage: 'Cannot share the video activity with many retries.' |
4e50b6a1 C |
43 | } |
44 | ||
45 | return retryTransactionWrapper(shareVideo, options) | |
46 | } | |
47 | ||
2ccaeeb3 | 48 | async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { |
d7a7c248 | 49 | const announced = activity.object |
2ccaeeb3 C |
50 | let video: VideoModel |
51 | ||
52 | if (typeof announced === 'string') { | |
53 | const res = await getOrCreateAccountAndVideoAndChannel(announced) | |
54 | video = res.video | |
55 | } else { | |
56 | video = await processCreateActivity(announced) | |
57 | } | |
4e50b6a1 | 58 | |
3fd3ab2d | 59 | return sequelizeTypescript.transaction(async t => { |
4e50b6a1 | 60 | // Add share entry |
4e50b6a1 C |
61 | |
62 | const share = { | |
50d6de9c | 63 | actorId: actorAnnouncer.id, |
4e50b6a1 C |
64 | videoId: video.id |
65 | } | |
66 | ||
3fd3ab2d | 67 | const [ , created ] = await VideoShareModel.findOrCreate({ |
4e50b6a1 C |
68 | where: share, |
69 | defaults: share, | |
70 | transaction: t | |
71 | }) | |
72 | ||
73 | if (video.isOwned() && created === true) { | |
74 | // Don't resend the activity to the sender | |
50d6de9c | 75 | const exceptions = [ actorAnnouncer ] |
4e50b6a1 C |
76 | await forwardActivity(activity, t, exceptions) |
77 | } | |
78 | ||
79 | return undefined | |
80 | }) | |
81 | } |