Refactor homeserver lookup code #12

Merged
bad merged 10 commits from refactor-homeserver-lookup into princess 2020-10-23 08:57:01 +00:00
Showing only changes of commit 2ff43ea801 - Show all commits

View file

@ -71,7 +71,6 @@ class Form extends ElemJS {
this.on("submit", this.submit.bind(this))
}
async submit() {
if (this.processing) return
this.processing = true
@ -114,12 +113,11 @@ 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`)
if (maxDepth <= 0) throw new Error(`Failed to look up homeserver, maximum search depth reached`)
//Preprocess the address
// Normalise the address
if (!address.match(/^https?:\/\//)) {
console.warn(`${address} doesn't specify the protocol, assuming https`)
address = "https://" + address
@ -131,7 +129,7 @@ class Form extends ElemJS {
// Check if we found the actual matrix server
try {
const versionsReq = await fetch(`${address}/_matrix/client/versions`)
if(versionsReq.ok) {
if (versionsReq.ok) {
const versions = await versionsReq.json()
if (Array.isArray(versions.versions)) {
return address
@ -139,7 +137,7 @@ class Form extends ElemJS {
}
} catch(e) {}
// find the next matrix server in the chain
// Find the next matrix server in the chain
const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => {
bad marked this conversation as resolved Outdated

Why chain a promise catch here? I don't have a problem with promise chaining, it's just weird considering that we used the try/catch syntax 3 lines up. IMO they should be made consistent?

Why chain a promise catch here? I don't have a problem with promise chaining, it's just weird considering that we used the try/catch syntax 3 lines up. IMO they should be made consistent?
Outdated
Review

It was shorter. I actually wanted to write the code like it's written now but jshint errored on the optional chain. It's fixed now

It was shorter. I actually wanted to write the code like it's written now but jshint errored on the optional chain. It's fixed now
console.error(e)
throw new Error(`Failed to look up server ${address}`)
@ -155,7 +153,6 @@ class Form extends ElemJS {
return this.findHomeserver(nextAddress, maxDepth)
}
status(message) {
feedback.setLoading(true)
feedback.message(message)
@ -169,6 +166,3 @@ class Form extends ElemJS {
}
const form = new Form()