2018-08-16 15:31:17 +03:00
/ *
2021-06-22 19:22:38 +03:00
Copyright 2018 - 2021 The Matrix . org Foundation C . I . C .
2018-08-16 15:31:17 +03:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2021-06-22 19:22:38 +03:00
import React , { ReactNode } from "react" ;
2023-08-09 18:10:54 +03:00
import { MatrixError , ConnectionError } from "matrix-js-sdk/src/matrix" ;
2021-06-22 19:22:38 +03:00
2023-08-22 18:32:05 +03:00
import { _t , _td , Tags , TranslatedString , TranslationKey } from "../languageHandler" ;
2023-04-27 11:05:31 +03:00
import SdkConfig from "../SdkConfig" ;
import { ValidatedServerConfig } from "./ValidatedServerConfig" ;
2023-05-04 00:26:26 +03:00
import ExternalLink from "../components/views/elements/ExternalLink" ;
2023-04-27 11:05:31 +03:00
export const resourceLimitStrings = {
"monthly_active_user" : _td ( "This homeserver has hit its Monthly Active User limit." ) ,
"hs_blocked" : _td ( "This homeserver has been blocked by its administrator." ) ,
"" : _td ( "This homeserver has exceeded one of its resource limits." ) ,
} ;
export const adminContactStrings = {
"" : _td ( "Please <a>contact your service administrator</a> to continue using this service." ) ,
} ;
2018-08-16 15:31:17 +03:00
/ * *
* Produce a translated error message for a
* M_RESOURCE_LIMIT_EXCEEDED error
*
* @param { string } limitType The limit_type from the error
* @param { string } adminContact The admin_contact from the error
2022-05-10 01:52:05 +03:00
* @param { Object } strings Translatable string for different
2018-08-16 15:31:17 +03:00
* limit_type . Must include at least the empty string key
* which is the default . Strings may include an 'a' tag
* for the admin contact link .
* @param { Object } extraTranslations Extra translation substitution functions
* for any tags in the strings apart from 'a'
* @returns { * } Translated string or react component
* /
2021-06-22 19:22:38 +03:00
export function messageForResourceLimitError (
2023-03-07 13:45:55 +03:00
limitType : string | undefined ,
2023-02-15 16:36:22 +03:00
adminContact : string | undefined ,
2023-08-22 18:32:05 +03:00
strings : Record < string , TranslationKey > ,
2021-06-22 19:22:38 +03:00
extraTranslations? : Tags ,
) : TranslatedString {
2023-03-07 13:45:55 +03:00
let errString = limitType ? strings [ limitType ] : undefined ;
2018-08-16 15:31:17 +03:00
if ( errString === undefined ) errString = strings [ "" ] ;
2023-01-12 16:25:14 +03:00
const linkSub = ( sub : string ) : ReactNode = > {
2018-08-16 15:31:17 +03:00
if ( adminContact ) {
2021-07-20 00:43:11 +03:00
return (
< a href = { adminContact } target = "_blank" rel = "noreferrer noopener" >
{ sub }
< / a >
) ;
2018-08-16 15:31:17 +03:00
} else {
return sub ;
}
} ;
if ( errString . includes ( "<a>" ) ) {
return _t ( errString , { } , Object . assign ( { a : linkSub } , extraTranslations ) ) ;
} else {
2023-02-15 16:36:22 +03:00
return _t ( errString , { } , extraTranslations ! ) ;
2018-08-16 15:31:17 +03:00
}
}
2018-09-07 14:18:25 +03:00
2022-10-21 13:44:33 +03:00
export function messageForSyncError ( err : Error ) : ReactNode {
if ( err instanceof MatrixError && err . errcode === "M_RESOURCE_LIMIT_EXCEEDED" ) {
2023-04-27 11:05:31 +03:00
const limitError = messageForResourceLimitError (
err . data . limit_type ,
err . data . admin_contact ,
resourceLimitStrings ,
) ;
const adminContact = messageForResourceLimitError (
err . data . limit_type ,
err . data . admin_contact ,
adminContactStrings ,
) ;
2022-12-12 14:24:14 +03:00
return (
< div >
2021-07-20 00:43:11 +03:00
< div > { limitError } < / div >
< div > { adminContact } < / div >
2022-12-12 14:24:14 +03:00
< / div >
2018-09-07 14:18:25 +03:00
) ;
} else {
2023-02-14 12:05:01 +03:00
return < div > { _t ( "Unable to connect to Homeserver. Retrying…" ) } < / div > ;
2018-09-07 14:18:25 +03:00
}
}
2023-04-27 11:05:31 +03:00
export function messageForLoginError (
err : MatrixError ,
serverConfig : Pick < ValidatedServerConfig , " hsName " | " hsUrl " > ,
) : ReactNode {
if ( err . errcode === "M_RESOURCE_LIMIT_EXCEEDED" ) {
const errorTop = messageForResourceLimitError (
err . data . limit_type ,
err . data . admin_contact ,
resourceLimitStrings ,
) ;
const errorDetail = messageForResourceLimitError (
err . data . limit_type ,
err . data . admin_contact ,
adminContactStrings ,
) ;
return (
< div >
< div > { errorTop } < / div >
< div className = "mx_Login_smallError" > { errorDetail } < / div >
< / div >
) ;
} else if ( err . httpStatus === 401 || err . httpStatus === 403 ) {
if ( err . errcode === "M_USER_DEACTIVATED" ) {
return _t ( "This account has been deactivated." ) ;
} else if ( SdkConfig . get ( "disable_custom_urls" ) ) {
return (
< div >
< div > { _t ( "Incorrect username and/or password." ) } < / div >
< div className = "mx_Login_smallError" >
{ _t ( "Please note you are logging into the %(hs)s server, not matrix.org." , {
hs : serverConfig.hsName ,
} ) }
< / div >
< / div >
) ;
} else {
return _t ( "Incorrect username and/or password." ) ;
}
} else {
return messageForConnectionError ( err , serverConfig ) ;
}
}
export function messageForConnectionError (
err : Error ,
serverConfig : Pick < ValidatedServerConfig , " hsName " | " hsUrl " > ,
) : ReactNode {
let errorText = _t ( "There was a problem communicating with the homeserver, please try again later." ) ;
if ( err instanceof ConnectionError ) {
if (
window . location . protocol === "https:" &&
( serverConfig . hsUrl . startsWith ( "http:" ) || ! serverConfig . hsUrl . startsWith ( "http" ) )
) {
return (
< span >
{ _t (
2023-08-22 18:32:05 +03:00
"Can't connect to homeserver via HTTP when an HTTPS URL is in your browser bar. Either use HTTPS or <a>enable unsafe scripts</a>." ,
2023-04-27 11:05:31 +03:00
{ } ,
{
a : ( sub ) = > {
return (
< a
target = "_blank"
rel = "noreferrer noopener"
href = "https://www.google.com/search?&q=enable%20unsafe%20scripts"
>
{ sub }
< / a >
) ;
} ,
} ,
) }
< / span >
) ;
}
return (
< span >
{ _t (
2023-08-22 18:32:05 +03:00
"Can't connect to homeserver - please check your connectivity, ensure your <a>homeserver's SSL certificate</a> is trusted, and that a browser extension is not blocking requests." ,
2023-04-27 11:05:31 +03:00
{ } ,
{
a : ( sub ) = > (
2023-05-04 00:26:26 +03:00
< ExternalLink target = "_blank" rel = "noreferrer noopener" href = { serverConfig . hsUrl } >
2023-04-27 11:05:31 +03:00
{ sub }
2023-05-04 00:26:26 +03:00
< / ExternalLink >
2023-04-27 11:05:31 +03:00
) ,
} ,
) }
< / span >
) ;
} else if ( err instanceof MatrixError ) {
if ( err . errcode ) {
errorText += ` ( ${ err . errcode } ) ` ;
} else if ( err . httpStatus ) {
errorText += ` (HTTP ${ err . httpStatus } ) ` ;
}
}
return errorText ;
}