Skip to content

Commit

Permalink
remove Guild Id from auth and add Guild Name lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkLightTuna committed Jan 30, 2024
1 parent 3cedfc8 commit b7e2ebf
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 92 deletions.
2 changes: 0 additions & 2 deletions languages/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"oronder.Auth-Token": "Auth Token",
"oronder.Discord-Server-Id": "Discord Server Id",
"oronder.Fetch-Discord-User-Ids": "Fetch Discord User Ids",
"oronder.Full-Sync": "Manually Sync All Actors",
"oronder.Fetching-Discord-Ids": "Fetching Discord Ids",
Expand All @@ -10,7 +9,6 @@
"oronder.Discord-Ids": "Discord Ids",
"oronder.Oronder-Bot-Config": "Oronder Bot Config",
"oronder.Invalid-Auth": "Invalid Server Id and/or Auth Token. Have you run \"/admin init\" in Discord?",
"oronder.Server-Id-NaN": "Discord Server Id must be a number.",
"oronder.Unexpected-Error": "Unexpected Error Occurred!",
"oronder.Could-Not-Be-Found": "could not be found in Discord server.",
"oronder.Discord-Server-Link-Title": "Oronder Discord",
Expand Down
7 changes: 4 additions & 3 deletions src/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ export const MODULE_DEBUG_TAG = [
`|`,
];

export const GUILD_ID = "guild_id"
export const GUILD_NAME = "guild_name"
export const AUTH = "auth"
export const ORONDER_CONFIGURATION_FORM = "oronder_options"
export const VALID_CONFIG = "valid_config"
export const ID_MAP = "id_map"

export const ACTORS = "actors"
export const ACTORS = `${MODULE_ID}.actors`

const common = window.location.host === 'localhost:65434' ? '://localhost:65435' : 's://api.oronder.com'
export const DEV_MODE = window.location.host === 'localhost:65434'
const common = DEV_MODE ? '://localhost:65435' : 's://api.oronder.com'
export const ORONDER_BASE_URL = `http${common}`
export const ORONDER_WS_URL = `ws${common}`
7 changes: 3 additions & 4 deletions src/module.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Logger} from "./util.mjs"
import {registerSettings} from "./settings.mjs"
import {AUTH, GUILD_ID, MODULE_ID, ORONDER_WS_URL} from "./constants.mjs"
import {AUTH, MODULE_ID, ORONDER_WS_URL} from "./constants.mjs"
import {del_actor, sync_actor} from "./sync.mjs"

let socket
Expand Down Expand Up @@ -69,13 +69,12 @@ export function open_socket_with_oronder(update = false) {
}
}

const guild_id = game.settings.get(MODULE_ID, GUILD_ID)
const authorization = game.settings.get(MODULE_ID, AUTH)
if (!guild_id || !authorization) return
if (!authorization) return

socket = io(ORONDER_WS_URL, {
transports: ["websocket"],
auth: {'Guild-Id': guild_id, 'Authorization': authorization}
auth: {'Authorization': authorization}
})

socket.on('connect', () => {
Expand Down
60 changes: 37 additions & 23 deletions src/settings-form-application.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Logger} from "./util.mjs";
import {AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_BASE_URL, VALID_CONFIG} from "./constants.mjs";
import {AUTH, GUILD_NAME, ID_MAP, MODULE_ID, ORONDER_BASE_URL, VALID_CONFIG} from "./constants.mjs";
import {full_sync} from "./sync.mjs";
import {open_socket_with_oronder} from "./module.mjs";

Expand All @@ -8,7 +8,7 @@ export class OronderSettingsFormApplication extends FormApplication {
constructor(object = {}, options = {}) {
const id_map = game.settings.get(MODULE_ID, ID_MAP)
foundry.utils.mergeObject(object, {
guild_id: game.settings.get(MODULE_ID, GUILD_ID),
guild_name: game.settings.get(MODULE_ID, GUILD_NAME),
auth: game.settings.get(MODULE_ID, AUTH),
valid_config: game.settings.get(MODULE_ID, VALID_CONFIG),
fetch_button_icon: "fa-solid fa-rotate",
Expand Down Expand Up @@ -62,12 +62,11 @@ export class OronderSettingsFormApplication extends FormApplication {
}
}

_requestOptions(guild_id, auth) {
_getRequestOptions(auth) {
return {
method: 'GET',
headers: new Headers({
"Accept": "application/json",
'Guild-Id': guild_id,
'Authorization': auth
}),
redirect: 'follow'
Expand All @@ -76,7 +75,6 @@ export class OronderSettingsFormApplication extends FormApplication {

/** @override */
async _updateObject(event, formData) {
this.object.guild_id = this.form.elements.guild_id.value
this.object.auth = this.form.elements.auth.value

const id_map = {}
Expand All @@ -88,7 +86,7 @@ export class OronderSettingsFormApplication extends FormApplication {
id_map[p.foundry_id] = p.discord_id
}
})
const requestOptions = this._requestOptions(this.object.guild_id, this.object.auth)
const requestOptions = this._getRequestOptions(this.object.auth)
let valid_config = false
await fetch(`${ORONDER_BASE_URL}/validate_discord_ids?${queryParams}`, requestOptions)
.then(this._handle_json_response)
Expand All @@ -106,16 +104,17 @@ export class OronderSettingsFormApplication extends FormApplication {
})
.catch(Logger.error)

if (!this.object.guild_name) {
this._set_guild_name(requestOptions)
}

let updated = false

if (game.settings.get(MODULE_ID, GUILD_ID) !== this.object.guild_id) {
game.settings.set(MODULE_ID, GUILD_ID, this.object.guild_id)
updated = true
}
if (game.settings.get(MODULE_ID, AUTH) !== this.object.auth) {
game.settings.set(MODULE_ID, AUTH, this.object.auth)
updated = true
}
game.settings.set(MODULE_ID, GUILD_NAME, this.object.guild_name)
game.settings.set(MODULE_ID, VALID_CONFIG, valid_config)
game.settings.set(MODULE_ID, ID_MAP, id_map)

Expand All @@ -137,7 +136,6 @@ export class OronderSettingsFormApplication extends FormApplication {
}

async _fetch_discord_ids() {
this.object.guild_id = this.form.elements.guild_id.value
this.object.auth = this.form.elements.auth.value
this.object.players.forEach(p =>
p.discord_id = this.form.elements[p.foundry_id].value
Expand All @@ -148,10 +146,6 @@ export class OronderSettingsFormApplication extends FormApplication {
)
let err = false

if (!Number.fromString(this.object.guild_id)) {
err = true
Logger.error(game.i18n.localize("oronder.Server-Id-NaN"))
}
if (!this.object.auth) {
err = true
Logger.error(game.i18n.localize("oronder.Auth-Token-Empty"))
Expand All @@ -174,9 +168,10 @@ export class OronderSettingsFormApplication extends FormApplication {
players_without_discord_ids.forEach(p =>
queryParams.append('p', p.foundry_name)
)
const requestOptions = this._requestOptions(this.object.guild_id, this.object.auth)
const requestOptions = this._getRequestOptions(this.object.auth)

await fetch(`${ORONDER_BASE_URL}/discord_id?${queryParams}`, requestOptions)
const p1 = this._set_guild_name(requestOptions)
const p2 = fetch(`${ORONDER_BASE_URL}/discord_id?${queryParams}`, requestOptions)
.then(this._handle_json_response)
.then(result => {
for (const [foundry_name, discord_user_id] of Object.entries(result)) {
Expand All @@ -185,21 +180,40 @@ export class OronderSettingsFormApplication extends FormApplication {
})
.catch(Logger.error)

await Promise.all([p1, p2])

this.object.fetch_button_icon = "fa-solid fa-rotate"
this.object.fetch_sync_disabled = false
this.render()
}

_handle_json_response(response) {
async _handle_json_response(response) {
if (!response.ok) {
const errorMessage = await response.text();
const errorDetails = `Status: ${response.status}, Message: ${errorMessage}`

if (response.status === 401) {
throw new Error(game.i18n.localize("oronder.Invalid-Auth"))
} else if (response.status === 400) {
throw new Error(game.i18n.localize("oronder.Server-Id-NaN"))
throw new Error(game.i18n.localize("oronder.Invalid-Auth") + ". " + errorDetails)
} else {
throw new Error(game.i18n.localize("oronder.Unexpected-Error"))
throw new Error(game.i18n.localize("oronder.Unexpected-Error") + ". " + errorDetails)
}
}
return response.json()

try {
return await response.json()
} catch (error) {
throw new Error(`Failed to parse JSON response. Error: ${error.message}`)
}
}

async _set_guild_name(requestOptions) {
try {
const response = await fetch(`${ORONDER_BASE_URL}/guild`, requestOptions)
const guild = await this._handle_json_response(response)
this.object.guild_name = guild.name
} catch (error) {
this.object.guild_name = undefined
Logger.error(`Error setting guild name: ${error.message}`)
}
}
}
4 changes: 2 additions & 2 deletions src/settings.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_CONFIGURATION_FORM, VALID_CONFIG} from "./constants.mjs";
import {AUTH, GUILD_NAME, ID_MAP, MODULE_ID, ORONDER_CONFIGURATION_FORM, VALID_CONFIG} from "./constants.mjs";
import {Logger} from "./util.mjs";
import {OronderSettingsFormApplication} from "./settings-form-application.mjs";

export const registerSettings = async () => {
game.settings.register(MODULE_ID, GUILD_ID, {
game.settings.register(MODULE_ID, GUILD_NAME, {
scope: "world",
type: String,
config: false
Expand Down
4 changes: 1 addition & 3 deletions src/sync.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ACTORS, AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_BASE_URL} from "./constants.mjs";
import {ACTORS, AUTH, ID_MAP, MODULE_ID, ORONDER_BASE_URL} from "./constants.mjs";
import {hash, Logger} from "./util.mjs";

function prune_roll_data(
Expand Down Expand Up @@ -152,12 +152,10 @@ export function enrich_actor(actor) {
}

function headers() {
const guild_id = game.settings.get(MODULE_ID, GUILD_ID)
const authorization = game.settings.get(MODULE_ID, AUTH)
return new Headers({
"Accept": "application/json",
"Content-Type": "application/json",
'Guild-Id': guild_id,
'Authorization': authorization
})
}
Expand Down
107 changes: 52 additions & 55 deletions templates/settings-form-application.hbs
Original file line number Diff line number Diff line change
@@ -1,63 +1,60 @@
<form autocomplete="off">
<div>
{{#unless valid_config}}
<h2>Installation Instructions</h2>
<ol>
<li>Invite the <a
href="https://discord.com/oauth2/authorize?client_id=1064553830810923048&scope=bot+guilds.members.read&permissions=403761728512">
{{localize "oronder.Discord-Bot-Link-Title"}}</a> to your Discord.
</li>
<li>Subscribe to the <a href="https://discord.gg/27npDAXaCA\">
{{localize "oronder.Discord-Server-Link-Title"}}
</a> to gain access to advanced features.
</li>
<li>From your Discord, run "<code>/admin init</code>"</li>
<li>Copy your Discord server id, and the generated token into the fields below.</li>
<li>Clicking "<code>{{localize "oronder.Fetch-Discord-User-Ids"}}</code>" will populate Discord user ids for
players whose Discord name
matches their Foundry name. For everyone else, you will need to manually add their user id.
</li>
</ol>
<hr>
{{/unless}}
<div>
<h1>{{#if guild_name}}{{guild_name}}{{else}}No Discord Server Configured{{/if}}</h1>
{{#unless valid_config}}
<h2>Installation Instructions</h2>
<ol>
<li>Invite the <a
href="https://discord.com/oauth2/authorize?client_id=1064553830810923048&scope=bot+guilds.members.read&permissions=403761728512">
{{localize "oronder.Discord-Bot-Link-Title"}}</a> to your Discord.
</li>
<li>Subscribe to the <a href="https://discord.gg/27npDAXaCA\">
{{localize "oronder.Discord-Server-Link-Title"}}
</a> to gain access to advanced features.
</li>
<li>From your Discord, run "<code>/admin init</code>"</li>
<li>Copy the generated token into the fields below.</li>
<li>Clicking "<code>{{localize "oronder.Fetch-Discord-User-Ids"}}</code>" will populate Discord user ids
for
players whose Discord name
matches their Foundry name. For everyone else, you will need to manually add their user id.
</li>
</ol>
<hr>
{{/unless}}
</div>
<div class="form-group">
<label for="auth">{{localize "oronder.Auth-Token"}}</label>
<input type="password" autocomplete="off" name="auth" id="auth" value="{{auth}}" required>
</div>

</div>
<div class="form-group">
<label for="guild_id">{{localize "oronder.Discord-Server-Id"}}</label>
<input type="text" name="guild_id" value="{{guild_id}}" required inputmode="numeric">
</div>
<div class="form-group">
<button type="button" class='control' data-action='fetch' {{#if fetch_disabled}}disabled{{/if}}>
<i class="{{fetch_button_icon}}"></i> {{fetch_button_msg}}
</button>
</div>
<div class="form-group">
<button type="button" class='control' data-action='sync-all' {{#if full_sync_disabled}}disabled{{/if}}>
<i class="{{full_sync_button_icon}}"></i> {{full_sync_button_msg}}
</button>
</div>

<div class="form-group">
<label for="auth">{{localize "oronder.Auth-Token"}}</label>
<input type="password" name="auth" value="{{auth}}" required>
</div>
{{#if players}}
<hr>
<h3 class="border">{{localize "oronder.Discord-Ids"}}</h3>
{{/if}}

<div class="form-group">
<button type="button" class='control' data-action='fetch' {{#if fetch_disabled}}disabled{{/if}}>
<i class="{{fetch_button_icon}}"></i> {{fetch_button_msg}}
</button>
</div>
<div class="form-group">
<button type="button" class='control' data-action='sync-all' {{#if full_sync_disabled}}disabled{{/if}}>
<i class="{{full_sync_button_icon}}"></i> {{full_sync_button_msg}}
</button>
</div>
{{#each players}}
<div class="form-group">
<label for="{{this.foundry_id}}">{{this.foundry_name}}</label>
<input type="text" name="{{this.foundry_id}}" id="{{this.foundry_id}}" autocomplete="off"
inputmode="numeric" value="{{this.discord_id}}">
</div>
{{/each}}

{{#if players}}
<hr>
<h3 class="border">{{localize "oronder.Discord-Ids"}}</h3>
{{/if}}

{{#each players}}
<div class="form-group">
<label for="{{this.foundry_id}}">{{this.foundry_name}}</label>
<input type="text" name="{{this.foundry_id}}" inputmode="numeric" value="{{this.discord_id}}">
</div>
{{/each}}

<hr>
<button type="submit">
<i class="far fa-save"></i> {{ localize "Save Changes" }}
</button>
<button type="submit">
<i class="far fa-save"></i> {{ localize "Save Changes" }}
</button>

</form>

0 comments on commit b7e2ebf

Please sign in to comment.