]>
Commit | Line | Data |
---|---|---|
cb9244de | 1 | import { ActivityAnnounce } from '../../../../shared/models/activitypub' |
da854ddd | 2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
80fdaf06 | 3 | import { sequelizeTypescript } from '../../../initializers/database' |
3fd3ab2d | 4 | import { VideoShareModel } from '../../../models/video/video-share' |
9588d4f4 | 5 | import { forwardVideoRelatedActivity } from '../send/utils' |
1297eb5d | 6 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
cef534ed | 7 | import { Notifier } from '../../notifier' |
ee79b60e | 8 | import { logger } from '../../../helpers/logger' |
26d6bf65 C |
9 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
10 | import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models' | |
d8465018 | 11 | |
1198edf4 C |
12 | async function processAnnounceActivity (options: APProcessorOptions<ActivityAnnounce>) { |
13 | const { activity, byActor: actorAnnouncer } = options | |
14 | // Only notify if it is not from a fetcher job | |
15 | const notify = options.fromFetch !== true | |
16 | ||
17 | return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify) | |
d8465018 C |
18 | } |
19 | ||
20 | // --------------------------------------------------------------------------- | |
21 | ||
22 | export { | |
23 | processAnnounceActivity | |
24 | } | |
4e50b6a1 C |
25 | |
26 | // --------------------------------------------------------------------------- | |
27 | ||
453e83ea | 28 | async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) { |
7acee6f1 | 29 | const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id |
2ccaeeb3 | 30 | |
0283eaac | 31 | let video: MVideoAccountLightBlacklistAllFiles |
ee79b60e C |
32 | let videoCreated: boolean |
33 | ||
34 | try { | |
35 | const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri }) | |
36 | video = result.video | |
37 | videoCreated = result.created | |
38 | } catch (err) { | |
39 | logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err }) | |
40 | return | |
41 | } | |
4e50b6a1 | 42 | |
cef534ed | 43 | await sequelizeTypescript.transaction(async t => { |
4e50b6a1 | 44 | // Add share entry |
4e50b6a1 C |
45 | |
46 | const share = { | |
50d6de9c | 47 | actorId: actorAnnouncer.id, |
4ba3b8ea C |
48 | videoId: video.id, |
49 | url: activity.id | |
4e50b6a1 C |
50 | } |
51 | ||
3fd3ab2d | 52 | const [ , created ] = await VideoShareModel.findOrCreate({ |
4ba3b8ea C |
53 | where: { |
54 | url: activity.id | |
55 | }, | |
4e50b6a1 C |
56 | defaults: share, |
57 | transaction: t | |
58 | }) | |
59 | ||
60 | if (video.isOwned() && created === true) { | |
61 | // Don't resend the activity to the sender | |
50d6de9c | 62 | const exceptions = [ actorAnnouncer ] |
9588d4f4 C |
63 | |
64 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
4e50b6a1 C |
65 | } |
66 | ||
67 | return undefined | |
68 | }) | |
cef534ed | 69 | |
1198edf4 | 70 | if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video) |
4e50b6a1 | 71 | } |