forked from Ivasoft/mattermost-mobile
[Gekidou] Unique servers (#5798)
* Disallow the same server from being added the twice * Add Default Server name config & EMM managed config * Update android/app/src/main/res/values/strings.xml Co-authored-by: Jason Frerich <jason.frerich@mattermost.com> Co-authored-by: Jason Frerich <jason.frerich@mattermost.com>
This commit is contained in:
@@ -174,7 +174,8 @@ class DatabaseHelper {
|
||||
|
||||
if (lastId == key) {
|
||||
earliest = post.get("create_at") as Double
|
||||
} else if (firstId == key) {
|
||||
}
|
||||
if (firstId == key) {
|
||||
latest = post.get("create_at") as Double
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
<string name="copyAndPasteProtection_title">Copy&Paste Protection</string>
|
||||
<string name="copyAndPasteProtection_description">Disable the ability to copy from or paste into any text inputs in the app.</string>
|
||||
<string name="serverUrl_title">Mattermost Server URL</string>
|
||||
<string name="serverUrl_description">Set a default Mattermost server URL.</string>
|
||||
<string name="serverUrl_description">Set a default Server URL for your Mattermost instance.</string>
|
||||
<string name="serverName_title">Mattermost Server Name</string>
|
||||
<string name="serverName_description">Set a default Server Name for your Mattermost instance.</string>
|
||||
<string name="allowOtherServers_title">Allow Other Servers</string>
|
||||
<string name="allowOtherServers_description">Allow the user to change the above server URL.</string>
|
||||
<string name="username_title">Default Username</string>
|
||||
|
||||
@@ -37,6 +37,12 @@
|
||||
android:description="@string/serverUrl_description"
|
||||
android:restrictionType="string"
|
||||
android:defaultValue="" />
|
||||
<restriction
|
||||
android:key="serverName"
|
||||
android:title="@string/serverName_title"
|
||||
android:description="@string/serverName_description"
|
||||
android:restrictionType="string"
|
||||
android:defaultValue="" />
|
||||
<restriction
|
||||
android:key="allowOtherServers"
|
||||
android:title="@string/allowOtherServers_title"
|
||||
|
||||
@@ -37,6 +37,12 @@ export const queryServerByIdentifier = async (appDatabase: Database, identifier:
|
||||
}
|
||||
};
|
||||
|
||||
export const queryServerByDisplayName = async (appDatabase: Database, displayName: string) => {
|
||||
const servers = await queryAllServers(appDatabase);
|
||||
const server = servers.find((s) => s.displayName.toLowerCase() === displayName.toLowerCase());
|
||||
return server;
|
||||
};
|
||||
|
||||
export const queryServerName = async (appDatabase: Database, serverUrl: string) => {
|
||||
try {
|
||||
const servers = (await appDatabase.get<ServerModel>(SERVERS).query(Q.where('url', serverUrl)).fetch());
|
||||
@@ -45,4 +51,3 @@ export const queryServerName = async (appDatabase: Database, serverUrl: string)
|
||||
return serverUrl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -191,12 +191,14 @@ const ServerForm = ({
|
||||
value={displayName}
|
||||
/>
|
||||
</View>
|
||||
{!displayNameError &&
|
||||
<FormattedText
|
||||
defaultMessage={'Choose a display name for your server'}
|
||||
id={'mobile.components.select_server_view.displayHelp'}
|
||||
style={styles.chooseText}
|
||||
testID={'mobile.components.select_server_view.displayHelp'}
|
||||
/>
|
||||
}
|
||||
<Button
|
||||
containerStyle={[styles.connectButton, styleButtonBackground]}
|
||||
disabled={buttonDisabled}
|
||||
|
||||
@@ -15,8 +15,10 @@ import LocalConfig from '@assets/config.json';
|
||||
import ClientError from '@client/rest/error';
|
||||
import AppVersion from '@components/app_version';
|
||||
import {Screens} from '@constants';
|
||||
import DatabaseManager from '@database/manager';
|
||||
import {t} from '@i18n';
|
||||
import NetworkManager from '@init/network_manager';
|
||||
import {queryServerByDisplayName, queryServerByIdentifier} from '@queries/app/servers';
|
||||
import {goToScreen} from '@screens/navigation';
|
||||
import {DeepLinkWithData, LaunchProps, LaunchType} from '@typings/launch';
|
||||
import {getErrorMessage} from '@utils/client_error';
|
||||
@@ -49,11 +51,13 @@ const Server = ({componentId, extra, launchType, launchError, theme}: ServerProp
|
||||
const [displayName, setDisplayName] = useState<string>('');
|
||||
const [buttonDisabled, setButtonDisabled] = useState(true);
|
||||
const [url, setUrl] = useState<string>('');
|
||||
const [urlError, setUrlError] = useState<string | undefined>(undefined);
|
||||
const [displayNameError, setDisplayNameError] = useState<string | undefined>();
|
||||
const [urlError, setUrlError] = useState<string | undefined>();
|
||||
const styles = getStyleSheet(theme);
|
||||
const {formatMessage} = intl;
|
||||
|
||||
useEffect(() => {
|
||||
const serverName = managedConfig?.serverName || LocalConfig.DefaultServerName;
|
||||
let serverUrl = managedConfig?.serverUrl || LocalConfig.DefaultServerUrl;
|
||||
let autoconnect = managedConfig?.allowOtherServers === 'false' || LocalConfig.AutoSelectServerUrl;
|
||||
|
||||
@@ -76,11 +80,15 @@ const Server = ({componentId, extra, launchType, launchError, theme}: ServerProp
|
||||
if (serverUrl) {
|
||||
// If a server Url is set by the managed or local configuration, use it.
|
||||
setUrl(serverUrl);
|
||||
}
|
||||
|
||||
if (autoconnect) {
|
||||
// If no other servers are allowed or the local config for AutoSelectServerUrl is set, attempt to connect
|
||||
handleConnect(managedConfig?.serverUrl || LocalConfig.DefaultServerUrl);
|
||||
}
|
||||
if (serverName) {
|
||||
setDisplayName(serverName);
|
||||
}
|
||||
|
||||
if (serverUrl && serverName && autoconnect) {
|
||||
// If no other servers are allowed or the local config for AutoSelectServerUrl is set, attempt to connect
|
||||
handleConnect(managedConfig?.serverUrl || LocalConfig.DefaultServerUrl);
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -151,7 +159,7 @@ const Server = ({componentId, extra, launchType, launchError, theme}: ServerProp
|
||||
};
|
||||
|
||||
const handleConnect = preventDoubleTap(async (manualUrl?: string) => {
|
||||
if (buttonDisabled) {
|
||||
if (buttonDisabled && !manualUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -170,15 +178,23 @@ const Server = ({componentId, extra, launchType, launchError, theme}: ServerProp
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Query to see if there is a server with the same display name
|
||||
// Set the error here and return
|
||||
// Elias to add this change in a Later PR. as soon as this one goes in
|
||||
const server = await queryServerByDisplayName(DatabaseManager.appDatabase!.database, displayName);
|
||||
if (server && server.lastActiveAt > 0) {
|
||||
setButtonDisabled(true);
|
||||
setDisplayNameError(formatMessage({
|
||||
id: 'mobile.server_name.exists',
|
||||
defaultMessage: 'You are using this name for another server.',
|
||||
}));
|
||||
setConnecting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
pingServer(serverUrl);
|
||||
});
|
||||
|
||||
const handleDisplayNameTextChanged = useCallback((text: string) => {
|
||||
setDisplayName(text);
|
||||
setDisplayNameError(undefined);
|
||||
}, []);
|
||||
|
||||
const handleUrlTextChanged = useCallback((text: string) => {
|
||||
@@ -235,6 +251,17 @@ const Server = ({componentId, extra, launchType, launchError, theme}: ServerProp
|
||||
return;
|
||||
}
|
||||
|
||||
const server = await queryServerByIdentifier(DatabaseManager.appDatabase!.database, data.config!.DiagnosticId);
|
||||
if (server && server.lastActiveAt > 0) {
|
||||
setButtonDisabled(true);
|
||||
setUrlError(formatMessage({
|
||||
id: 'mobile.server_identifier.exists',
|
||||
defaultMessage: 'You are already connected to this server.',
|
||||
}));
|
||||
setConnecting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
displayLogin(serverUrl, data.config!, data.license!);
|
||||
};
|
||||
|
||||
@@ -266,6 +293,7 @@ const Server = ({componentId, extra, launchType, launchError, theme}: ServerProp
|
||||
buttonDisabled={buttonDisabled}
|
||||
connecting={connecting}
|
||||
displayName={displayName}
|
||||
displayNameError={displayNameError}
|
||||
handleConnect={handleConnect}
|
||||
handleDisplayNameTextChanged={handleDisplayNameTextChanged}
|
||||
handleUrlTextChanged={handleUrlTextChanged}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"DefaultServerUrl": "",
|
||||
"DefaultServerName": "",
|
||||
"TestServerUrl": "http://localhost:8065",
|
||||
"DefaultTheme": "default",
|
||||
"ShowErrorsList": false,
|
||||
|
||||
Reference in New Issue
Block a user