Compare commits
2 commits
dce4fa6303
...
0113024be6
Author | SHA1 | Date | |
---|---|---|---|
0113024be6 | |||
1b97351ca0 |
1 changed files with 23 additions and 17 deletions
|
@ -71,6 +71,7 @@ class Form extends ElemJS {
|
|||
this.on("submit", this.submit.bind(this))
|
||||
}
|
||||
|
||||
|
||||
async submit() {
|
||||
if (this.processing) return
|
||||
this.processing = true
|
||||
|
@ -113,31 +114,32 @@ class Form extends ElemJS {
|
|||
}
|
||||
|
||||
async findHomeserver(address, maxDepth = 5) {
|
||||
// Protects us from servers sending us on a redirect loop
|
||||
|
||||
//Protects us from servers sending us on a redirect loop
|
||||
maxDepth--
|
||||
if (maxDepth <= 0) throw new Error(`Failed to look up homeserver, maximum search depth reached`)
|
||||
|
||||
// Normalise the address
|
||||
if(maxDepth<=0) throw new Error(`Failed to look up homeserver, maximum search depth reached`)
|
||||
|
||||
//Preprocess the address
|
||||
if (!address.match(/^https?:\/\//)) {
|
||||
console.warn(`${address} doesn't specify the protocol, assuming https`)
|
||||
address = "https://" + address
|
||||
}
|
||||
address = address.replace(/\/*$/, "")
|
||||
|
||||
|
||||
this.status(`Looking up homeserver... trying ${address}`)
|
||||
|
||||
|
||||
// Check if we found the actual matrix server
|
||||
try {
|
||||
const versionsReq = await fetch(`${address}/_matrix/client/versions`)
|
||||
if (versionsReq.ok) {
|
||||
const versions = await versionsReq.json()
|
||||
if (Array.isArray(versions.versions)) {
|
||||
return address
|
||||
}
|
||||
}
|
||||
} catch(e) {}
|
||||
|
||||
// Find the next matrix server in the chain
|
||||
const versionsReq = await fetch(`${address}/_matrix/client/versions`).catch(()=>{})
|
||||
/* jshint ignore:start */
|
||||
//JsHint doesn't support optional chaining
|
||||
// https://github.com/jshint/jshint/issues/3448
|
||||
if(versionsReq?.ok) {
|
||||
const versions = await versionsReq.json().catch(()=>{})
|
||||
if (Array.isArray(versions?.versions)) return address
|
||||
}
|
||||
/* jshint ignore:end */
|
||||
|
||||
// find the next matrix server in the chain
|
||||
const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => {
|
||||
console.error(e)
|
||||
throw new Error(`Failed to look up server ${address}`)
|
||||
|
@ -153,6 +155,7 @@ class Form extends ElemJS {
|
|||
return this.findHomeserver(nextAddress, maxDepth)
|
||||
}
|
||||
|
||||
|
||||
status(message) {
|
||||
feedback.setLoading(true)
|
||||
feedback.message(message)
|
||||
|
@ -166,3 +169,6 @@ class Form extends ElemJS {
|
|||
}
|
||||
|
||||
const form = new Form()
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue