]>
Commit | Line | Data |
---|---|---|
3fd3ab2d C |
1 | import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub' |
2 | import { logger, retryTransactionWrapper } from '../../../helpers' | |
3 | import { sequelizeTypescript } from '../../../initializers' | |
4 | import { AccountModel } from '../../../models/account/account' | |
5 | import { VideoModel } from '../../../models/video/video' | |
6 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
7 | import { VideoChannelShareModel } from '../../../models/video/video-channel-share' | |
8 | import { VideoShareModel } from '../../../models/video/video-share' | |
4e50b6a1 C |
9 | import { getOrCreateAccountAndServer } from '../account' |
10 | import { forwardActivity } from '../send/misc' | |
79d5caf9 C |
11 | import { processAddActivity } from './process-add' |
12 | import { processCreateActivity } from './process-create' | |
d8465018 C |
13 | |
14 | async function processAnnounceActivity (activity: ActivityAnnounce) { | |
20494f12 | 15 | const announcedActivity = activity.object |
0f91ae62 | 16 | const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor) |
d8465018 | 17 | |
20494f12 | 18 | if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') { |
4e50b6a1 | 19 | return processVideoChannelShare(accountAnnouncer, activity) |
20494f12 | 20 | } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') { |
4e50b6a1 | 21 | return processVideoShare(accountAnnouncer, activity) |
d8465018 C |
22 | } |
23 | ||
20494f12 C |
24 | logger.warn( |
25 | 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type, | |
26 | { activity: activity.id } | |
27 | ) | |
54141398 C |
28 | |
29 | return undefined | |
d8465018 C |
30 | } |
31 | ||
32 | // --------------------------------------------------------------------------- | |
33 | ||
34 | export { | |
35 | processAnnounceActivity | |
36 | } | |
4e50b6a1 C |
37 | |
38 | // --------------------------------------------------------------------------- | |
39 | ||
3fd3ab2d | 40 | function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
4e50b6a1 C |
41 | const options = { |
42 | arguments: [ accountAnnouncer, activity ], | |
43 | errorMessage: 'Cannot share the video channel with many retries.' | |
44 | } | |
45 | ||
46 | return retryTransactionWrapper(shareVideoChannel, options) | |
47 | } | |
48 | ||
3fd3ab2d | 49 | async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
4e50b6a1 C |
50 | const announcedActivity = activity.object as ActivityCreate |
51 | ||
3fd3ab2d | 52 | return sequelizeTypescript.transaction(async t => { |
4e50b6a1 | 53 | // Add share entry |
3fd3ab2d | 54 | const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity) |
4e50b6a1 C |
55 | const share = { |
56 | accountId: accountAnnouncer.id, | |
57 | videoChannelId: videoChannel.id | |
58 | } | |
59 | ||
3fd3ab2d | 60 | const [ , created ] = await VideoChannelShareModel.findOrCreate({ |
4e50b6a1 C |
61 | where: share, |
62 | defaults: share, | |
63 | transaction: t | |
64 | }) | |
65 | ||
66 | if (videoChannel.isOwned() && created === true) { | |
67 | // Don't resend the activity to the sender | |
68 | const exceptions = [ accountAnnouncer ] | |
69 | await forwardActivity(activity, t, exceptions) | |
70 | } | |
71 | ||
72 | return undefined | |
73 | }) | |
74 | } | |
75 | ||
3fd3ab2d | 76 | function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
4e50b6a1 C |
77 | const options = { |
78 | arguments: [ accountAnnouncer, activity ], | |
79 | errorMessage: 'Cannot share the video with many retries.' | |
80 | } | |
81 | ||
82 | return retryTransactionWrapper(shareVideo, options) | |
83 | } | |
84 | ||
3fd3ab2d | 85 | function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { |
4e50b6a1 C |
86 | const announcedActivity = activity.object as ActivityAdd |
87 | ||
3fd3ab2d | 88 | return sequelizeTypescript.transaction(async t => { |
4e50b6a1 | 89 | // Add share entry |
3fd3ab2d | 90 | const video: VideoModel = await processAddActivity(announcedActivity) |
4e50b6a1 C |
91 | |
92 | const share = { | |
93 | accountId: accountAnnouncer.id, | |
94 | videoId: video.id | |
95 | } | |
96 | ||
3fd3ab2d | 97 | const [ , created ] = await VideoShareModel.findOrCreate({ |
4e50b6a1 C |
98 | where: share, |
99 | defaults: share, | |
100 | transaction: t | |
101 | }) | |
102 | ||
103 | if (video.isOwned() && created === true) { | |
104 | // Don't resend the activity to the sender | |
105 | const exceptions = [ accountAnnouncer ] | |
106 | await forwardActivity(activity, t, exceptions) | |
107 | } | |
108 | ||
109 | return undefined | |
110 | }) | |
111 | } |