Wait for ping to work during setup
This commit is contained in:
parent
37f3a59d8e
commit
7d42a530e7
3 changed files with 92 additions and 35 deletions
|
@ -11,6 +11,7 @@ const mreq = sync.require("./mreq")
|
||||||
const file = sync.require("./file")
|
const file = sync.require("./file")
|
||||||
/** @type {import("./txnid")} */
|
/** @type {import("./txnid")} */
|
||||||
const makeTxnId = sync.require("./txnid")
|
const makeTxnId = sync.require("./txnid")
|
||||||
|
const {reg} = require("./read-registration.js")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} p endpoint to access
|
* @param {string} p endpoint to access
|
||||||
|
@ -295,6 +296,22 @@ async function setUserPowerCascade(roomID, mxid, power) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function ping() {
|
||||||
|
const res = await fetch(`${mreq.baseUrl}/client/v1/appservice/${reg.id}/ping`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${reg.as_token}`
|
||||||
|
},
|
||||||
|
body: "{}"
|
||||||
|
})
|
||||||
|
const root = await res.json()
|
||||||
|
return {
|
||||||
|
ok: res.ok,
|
||||||
|
status: res.status,
|
||||||
|
root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.path = path
|
module.exports.path = path
|
||||||
module.exports.register = register
|
module.exports.register = register
|
||||||
module.exports.createRoom = createRoom
|
module.exports.createRoom = createRoom
|
||||||
|
@ -318,3 +335,4 @@ module.exports.profileSetDisplayname = profileSetDisplayname
|
||||||
module.exports.profileSetAvatarUrl = profileSetAvatarUrl
|
module.exports.profileSetAvatarUrl = profileSetAvatarUrl
|
||||||
module.exports.setUserPower = setUserPower
|
module.exports.setUserPower = setUserPower
|
||||||
module.exports.setUserPowerCascade = setUserPowerCascade
|
module.exports.setUserPowerCascade = setUserPowerCascade
|
||||||
|
module.exports.ping = ping
|
||||||
|
|
|
@ -78,5 +78,6 @@ async function withAccessToken(token, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.MatrixServerError = MatrixServerError
|
module.exports.MatrixServerError = MatrixServerError
|
||||||
|
module.exports.baseUrl = baseUrl
|
||||||
module.exports.mreq = mreq
|
module.exports.mreq = mreq
|
||||||
module.exports.withAccessToken = withAccessToken
|
module.exports.withAccessToken = withAccessToken
|
||||||
|
|
|
@ -3,11 +3,14 @@
|
||||||
const assert = require("assert").strict
|
const assert = require("assert").strict
|
||||||
const fs = require("fs")
|
const fs = require("fs")
|
||||||
const sqlite = require("better-sqlite3")
|
const sqlite = require("better-sqlite3")
|
||||||
const HeatSync = require("heatsync")
|
const {scheduler: {wait}} = require("timers/promises")
|
||||||
const {prompt, Prompt} = require("enquirer")
|
const {isDeepStrictEqual} = require("util")
|
||||||
|
|
||||||
|
const {prompt} = require("enquirer")
|
||||||
const Input = require("enquirer/lib/prompts/input")
|
const Input = require("enquirer/lib/prompts/input")
|
||||||
const fetch = require("node-fetch")
|
const fetch = require("node-fetch")
|
||||||
const {magenta, bold} = require("ansi-colors")
|
const {magenta, bold, cyan} = require("ansi-colors")
|
||||||
|
const HeatSync = require("heatsync")
|
||||||
|
|
||||||
const args = require("minimist")(process.argv.slice(2), {string: ["emoji-guild"]})
|
const args = require("minimist")(process.argv.slice(2), {string: ["emoji-guild"]})
|
||||||
|
|
||||||
|
@ -29,7 +32,7 @@ const discord = new DiscordClient(config.discordToken, "no")
|
||||||
passthrough.discord = discord
|
passthrough.discord = discord
|
||||||
|
|
||||||
let registration = require("../matrix/read-registration")
|
let registration = require("../matrix/read-registration")
|
||||||
let {reg, getTemplateRegistration, writeRegistration, readRegistration} = registration
|
let {reg, getTemplateRegistration, writeRegistration, readRegistration, registrationFilePath} = registration
|
||||||
|
|
||||||
function die(message) {
|
function die(message) {
|
||||||
console.error(message)
|
console.error(message)
|
||||||
|
@ -49,23 +52,7 @@ async function uploadAutoEmoji(guild, name, filename) {
|
||||||
return emoji
|
return emoji
|
||||||
}
|
}
|
||||||
|
|
||||||
;(async () => {
|
async function validateHomeserverOrigin(serverUrlPrompt, url) {
|
||||||
// create registration file with prompts...
|
|
||||||
if (!reg) {
|
|
||||||
console.log("What is the name of your homeserver? This is the part after : in your username.")
|
|
||||||
/** @type {{server_name: string}} */
|
|
||||||
const serverNameResponse = await prompt({
|
|
||||||
type: "input",
|
|
||||||
name: "server_name",
|
|
||||||
message: "Homeserver name"
|
|
||||||
})
|
|
||||||
console.log("What is the URL of your homeserver?")
|
|
||||||
const serverUrlPrompt = new Input({
|
|
||||||
type: "input",
|
|
||||||
name: "server_origin",
|
|
||||||
message: "Homeserver URL",
|
|
||||||
initial: () => `https://${serverNameResponse.server_name}`,
|
|
||||||
validate: async url => {
|
|
||||||
if (!url.match(/^https?:\/\//)) return "Must be a URL"
|
if (!url.match(/^https?:\/\//)) return "Must be a URL"
|
||||||
if (url.match(/\/$/)) return "Must not end with a slash"
|
if (url.match(/\/$/)) return "Must not end with a slash"
|
||||||
process.stdout.write(magenta(" checking, please wait..."))
|
process.stdout.write(magenta(" checking, please wait..."))
|
||||||
|
@ -90,6 +77,24 @@ async function uploadAutoEmoji(guild, name, filename) {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
// create registration file with prompts...
|
||||||
|
if (!reg) {
|
||||||
|
console.log("What is the name of your homeserver? This is the part after : in your username.")
|
||||||
|
/** @type {{server_name: string}} */
|
||||||
|
const serverNameResponse = await prompt({
|
||||||
|
type: "input",
|
||||||
|
name: "server_name",
|
||||||
|
message: "Homeserver name"
|
||||||
|
})
|
||||||
|
console.log("What is the URL of your homeserver?")
|
||||||
|
const serverUrlPrompt = new Input({
|
||||||
|
type: "input",
|
||||||
|
name: "server_origin",
|
||||||
|
message: "Homeserver URL",
|
||||||
|
initial: () => `https://${serverNameResponse.server_name}`,
|
||||||
|
validate: url => validateHomeserverOrigin(serverUrlPrompt, url)
|
||||||
})
|
})
|
||||||
/** @type {{server_origin: string}} */ // @ts-ignore
|
/** @type {{server_origin: string}} */ // @ts-ignore
|
||||||
const serverUrlResponse = await serverUrlPrompt.run()
|
const serverUrlResponse = await serverUrlPrompt.run()
|
||||||
|
@ -105,18 +110,53 @@ async function uploadAutoEmoji(guild, name, filename) {
|
||||||
validate: url => !!url.match(/^https?:\/\//)
|
validate: url => !!url.match(/^https?:\/\//)
|
||||||
})
|
})
|
||||||
const template = getTemplateRegistration()
|
const template = getTemplateRegistration()
|
||||||
reg = Object.assign(template, urlResponse, {ooye: {...template.ooye, ...serverNameResponse, ...serverUrlResponse}})
|
reg = {...template, ...urlResponse, ooye: {...template.ooye, ...serverNameResponse, ...serverUrlResponse}}
|
||||||
registration.reg = reg
|
registration.reg = reg
|
||||||
writeRegistration(reg)
|
writeRegistration(reg)
|
||||||
}
|
}
|
||||||
// done with user prompts, reg is now guaranteed to be valid
|
|
||||||
|
|
||||||
console.log("Processing. This could take up to 30 seconds. Please be patient...")
|
|
||||||
|
|
||||||
|
// Done with user prompts, reg is now guaranteed to be valid
|
||||||
const api = require("../matrix/api")
|
const api = require("../matrix/api")
|
||||||
const file = require("../matrix/file")
|
const file = require("../matrix/file")
|
||||||
const utils = require("../m2d/converters/utils")
|
const utils = require("../m2d/converters/utils")
|
||||||
|
|
||||||
|
console.log(`✅ Registration file saved as ${registrationFilePath}`)
|
||||||
|
console.log(` In ${cyan("Synapse")}, you need to add it to homeserver.yaml and ${cyan("restart Synapse")}.`)
|
||||||
|
console.log(" https://element-hq.github.io/synapse/latest/application_services.html")
|
||||||
|
console.log(` In ${cyan("Conduit")}, you need to send the file contents to the #admins room.`)
|
||||||
|
console.log(" https://docs.conduit.rs/appservices.html")
|
||||||
|
console.log()
|
||||||
|
|
||||||
|
const {as} = require("../matrix/appservice")
|
||||||
|
console.log("⏳ Waiting until homeserver registration works... (Ctrl+C to cancel)")
|
||||||
|
|
||||||
|
let itWorks = false
|
||||||
|
let lastError = null
|
||||||
|
do {
|
||||||
|
const result = await api.ping()
|
||||||
|
// If it didn't work, log details and retry after some time
|
||||||
|
itWorks = result.ok
|
||||||
|
if (!itWorks) {
|
||||||
|
// Log the full error data if the error is different to last time
|
||||||
|
if (!isDeepStrictEqual(lastError, result.root)) {
|
||||||
|
if (result.root.error) {
|
||||||
|
console.log(`\nHomeserver said: [${result.status}] ${result.root.error}`)
|
||||||
|
} else {
|
||||||
|
console.log(`\nHomeserver said: [${result.status}] ${JSON.stringify(result.root)}`)
|
||||||
|
}
|
||||||
|
lastError = result.root
|
||||||
|
} else {
|
||||||
|
process.stderr.write(".")
|
||||||
|
}
|
||||||
|
await wait(5000)
|
||||||
|
}
|
||||||
|
} while (!itWorks)
|
||||||
|
console.log("")
|
||||||
|
|
||||||
|
as.close().catch(() => {})
|
||||||
|
|
||||||
|
console.log("⏩ Processing. This could take up to 30 seconds. Please be patient...")
|
||||||
|
|
||||||
const mxid = `@${reg.sender_localpart}:${reg.ooye.server_name}`
|
const mxid = `@${reg.sender_localpart}:${reg.ooye.server_name}`
|
||||||
|
|
||||||
// ensure registration is correctly set...
|
// ensure registration is correctly set...
|
||||||
|
@ -128,8 +168,6 @@ async function uploadAutoEmoji(guild, name, filename) {
|
||||||
assert(botID.match(/^[0-9]{10,}$/), "discord token must follow the correct format")
|
assert(botID.match(/^[0-9]{10,}$/), "discord token must follow the correct format")
|
||||||
assert.match(reg.url, /^https?:/, "url must start with http:// or https://")
|
assert.match(reg.url, /^https?:/, "url must start with http:// or https://")
|
||||||
|
|
||||||
// TODO: appservice ping until it works
|
|
||||||
|
|
||||||
console.log("✅ Configuration looks good...")
|
console.log("✅ Configuration looks good...")
|
||||||
|
|
||||||
// database ddl...
|
// database ddl...
|
||||||
|
|
Loading…
Reference in a new issue