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