mattermost-mobile-upstream/app/managers/integrations_manager.ts
Scott Bishel c05aea4a2b
Some checks are pending
ci / lint-typecheck (push) Waiting to run
ci / detox-lint-typecheck (push) Waiting to run
ci / test (push) Waiting to run
MM-69122: Add file element support to interactive dialogs (#9898)
* Implement Interactive Dialog - file upload

* update demo plugin version for e2etests

* fix and add e2etests

* review fixes
2026-10-07 14:43:26 -05:00

89 lines
2.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {fetchCommands} from '@actions/remote/command';
import {Screens} from '@constants';
import DatabaseManager from '@database/manager';
import {getCurrentChannelId} from '@queries/servers/system';
import {navigateToScreen} from '@screens/navigation';
import {logDebug} from '@utils/log';
const TIME_TO_REFETCH_COMMANDS = 60000; // 1 minute
class ServerIntegrationsManager {
private serverUrl: string;
private commandsLastFetched: {[teamId: string]: number | undefined} = {};
private commands: {[teamId: string]: Command[] | undefined} = {};
private triggerId = '';
private storedDialog?: InteractiveDialogConfig;
constructor(serverUrl: string) {
this.serverUrl = serverUrl;
}
public async fetchCommands(teamId: string) {
const lastFetched = this.commandsLastFetched[teamId] || 0;
const lastCommands = this.commands[teamId];
if (lastCommands && lastFetched + TIME_TO_REFETCH_COMMANDS > Date.now()) {
return lastCommands;
}
try {
const res = await fetchCommands(this.serverUrl, teamId);
if ('error' in res) {
return [];
}
this.commands[teamId] = res.commands;
this.commandsLastFetched[teamId] = Date.now();
return res.commands;
} catch {
return [];
}
}
public setTriggerId(id: string) {
this.triggerId = id;
if (this.storedDialog?.trigger_id === id) {
this.showDialog();
}
}
public setDialog(dialog: InteractiveDialogConfig) {
this.storedDialog = dialog;
if (this.triggerId === dialog.trigger_id) {
this.showDialog();
}
}
private async showDialog() {
const config = this.storedDialog;
if (!config) {
return;
}
let channelId = '';
try {
const {database} = DatabaseManager.getServerDatabaseAndOperator(this.serverUrl);
channelId = await getCurrentChannelId(database);
} catch (error) {
logDebug('integrations_manager.showDialog: could not read channelId', error);
}
navigateToScreen(Screens.DIALOG_ROUTER, {title: config.dialog.title, config, channelId});
}
}
class IntegrationsManagerSingleton {
private serverManagers: {[serverUrl: string]: ServerIntegrationsManager | undefined} = {};
public getManager(serverUrl: string): ServerIntegrationsManager {
if (!this.serverManagers[serverUrl]) {
this.serverManagers[serverUrl] = new ServerIntegrationsManager(serverUrl);
}
return this.serverManagers[serverUrl]!;
}
}
const IntegrationsManager = new IntegrationsManagerSingleton();
export default IntegrationsManager;