|
1 | | -import { api } from '@rocket.chat/core-services'; |
2 | | -import { FederationMatrix } from '@rocket.chat/federation-matrix'; |
| 1 | +import { api, FederationMatrix as FederationMatrixService } from '@rocket.chat/core-services'; |
| 2 | +import { FederationMatrix, setupFederationMatrix } from '@rocket.chat/federation-matrix'; |
3 | 3 | import { InstanceStatus } from '@rocket.chat/instance-status'; |
4 | | -import { License } from '@rocket.chat/license'; |
5 | 4 | import { Logger } from '@rocket.chat/logger'; |
6 | 5 |
|
7 | | -import { settings } from '../../../app/settings/server'; |
8 | 6 | import { StreamerCentral } from '../../../server/modules/streamer/streamer.module'; |
9 | 7 | import { registerFederationRoutes } from '../api/federation'; |
10 | 8 |
|
11 | 9 | const logger = new Logger('Federation'); |
12 | 10 |
|
13 | | -// TODO: should validate if the domain is resolving to us or not correctly |
14 | | -// should use homeserver.getFinalSomethingSomething and validate final Host header to have siteUrl |
15 | | -// this is a minimum sanity check to avoid full urls instead of the expected domain part |
16 | | -function validateDomain(domain: string): boolean { |
17 | | - const value = domain.trim(); |
18 | | - |
19 | | - if (!value) { |
20 | | - logger.error('The Federation domain is not set'); |
21 | | - return false; |
22 | | - } |
23 | | - |
24 | | - if (value.toLowerCase() !== value) { |
25 | | - logger.error(`The Federation domain "${value}" cannot have uppercase letters`); |
26 | | - return false; |
27 | | - } |
28 | | - |
| 11 | +export const startFederationService = async (): Promise<void> => { |
29 | 12 | try { |
30 | | - const valid = new URL(`https://${value}`).hostname === value; |
31 | | - |
32 | | - if (!valid) { |
33 | | - throw new Error(); |
34 | | - } |
35 | | - } catch { |
36 | | - logger.error(`The configured Federation domain "${value}" is not valid`); |
37 | | - return false; |
38 | | - } |
39 | | - |
40 | | - return true; |
41 | | -} |
| 13 | + const isEnabled = await setupFederationMatrix(InstanceStatus.id()); |
42 | 14 |
|
43 | | -export const startFederationService = async (): Promise<void> => { |
44 | | - let federationMatrixService: FederationMatrix | undefined; |
| 15 | + api.registerService(new FederationMatrix()); |
45 | 16 |
|
46 | | - const shouldStartService = (): boolean => { |
47 | | - const hasLicense = License.hasModule('federation'); |
48 | | - const isEnabled = settings.get('Federation_Service_Enabled') === true; |
49 | | - const domain = settings.get<string>('Federation_Service_Domain'); |
50 | | - const hasDomain = validateDomain(domain); |
51 | | - return hasLicense && isEnabled && hasDomain; |
52 | | - }; |
| 17 | + await registerFederationRoutes(); |
53 | 18 |
|
54 | | - const startService = async (): Promise<void> => { |
55 | | - if (federationMatrixService) { |
56 | | - logger.debug('Federation-matrix service already started... skipping'); |
| 19 | + // only registers the typing listener if the service is enabled |
| 20 | + if (!isEnabled) { |
57 | 21 | return; |
58 | 22 | } |
59 | 23 |
|
60 | | - logger.debug('Starting federation-matrix service'); |
61 | | - federationMatrixService = await FederationMatrix.create(InstanceStatus.id()); |
62 | | - |
| 24 | + // TODO move to service/setup? |
63 | 25 | StreamerCentral.on('broadcast', (name, eventName, args) => { |
64 | | - if (!federationMatrixService) { |
65 | | - return; |
66 | | - } |
67 | 26 | if (name === 'notify-room' && eventName.endsWith('user-activity')) { |
68 | 27 | const [rid] = eventName.split('/'); |
69 | 28 | const [user, activity] = args; |
70 | | - void federationMatrixService.notifyUserTyping(rid, user, activity.includes('user-typing')); |
| 29 | + void FederationMatrixService.notifyUserTyping(rid, user, activity.includes('user-typing')); |
71 | 30 | } |
72 | 31 | }); |
73 | | - |
74 | | - try { |
75 | | - api.registerService(federationMatrixService); |
76 | | - await registerFederationRoutes(federationMatrixService); |
77 | | - } catch (error) { |
78 | | - logger.error('Failed to start federation-matrix service:', error); |
79 | | - } |
80 | | - }; |
81 | | - |
82 | | - const stopService = async (): Promise<void> => { |
83 | | - if (!federationMatrixService) { |
84 | | - logger.debug('Federation-matrix service not registered... skipping'); |
85 | | - return; |
86 | | - } |
87 | | - |
88 | | - logger.debug('Stopping federation-matrix service'); |
89 | | - |
90 | | - // TODO: Unregister routes |
91 | | - // await unregisterFederationRoutes(federationMatrixService); |
92 | | - |
93 | | - await api.destroyService(federationMatrixService); |
94 | | - federationMatrixService = undefined; |
95 | | - }; |
96 | | - |
97 | | - if (shouldStartService()) { |
98 | | - await startService(); |
| 32 | + } catch (error) { |
| 33 | + logger.error('Failed to start federation-matrix service:', error); |
99 | 34 | } |
100 | | - |
101 | | - void License.onLicense('federation', async () => { |
102 | | - logger.debug('Federation license became available'); |
103 | | - if (shouldStartService()) { |
104 | | - await startService(); |
105 | | - } |
106 | | - }); |
107 | | - |
108 | | - License.onInvalidateLicense(async () => { |
109 | | - logger.debug('License invalidated, checking federation module'); |
110 | | - if (!shouldStartService()) { |
111 | | - await stopService(); |
112 | | - } |
113 | | - }); |
114 | | - |
115 | | - settings.watch('Federation_Service_Enabled', async (enabled) => { |
116 | | - logger.debug('Federation_Service_Enabled setting changed:', enabled); |
117 | | - if (shouldStartService()) { |
118 | | - await startService(); |
119 | | - } else { |
120 | | - await stopService(); |
121 | | - } |
122 | | - }); |
123 | | - |
124 | | - settings.watch<string>('Federation_Service_Domain', async (domain) => { |
125 | | - logger.debug('Federation_Service_Domain setting changed:', domain); |
126 | | - if (shouldStartService()) { |
127 | | - if (domain.toLowerCase() !== federationMatrixService?.getServerName().toLowerCase()) { |
128 | | - await stopService(); |
129 | | - } |
130 | | - await startService(); |
131 | | - } else { |
132 | | - await stopService(); |
133 | | - } |
134 | | - }); |
135 | 35 | }; |
0 commit comments