Async rewrite of entire module !

 Channels list now fully asynchronous
 html pusher is now its own function for cleanness
 New ANY_CHANNEL integer is supported by the backend for requesting all channels in one go instead of voice|text
 Removed more callback hell code
 JSDocs updated to reflect their respective function signatures
This commit is contained in:
shockrah 2021-03-09 00:10:59 -08:00
parent 7fede3b4e1
commit 2fa1ec6f34
2 changed files with 57 additions and 74 deletions

View File

@ -46,7 +46,6 @@ exports.init = async function() {
continue
}
}
console.log('after init', config)
await ipcRenderer.invoke('config-update', config)
return await ipcRenderer.invoke('config-request')

View File

@ -5,98 +5,82 @@ const got = require('got')
const msg = require('./messages.js')
const ANY_CHANNEL = 0
const VOICE_KIND = 1
const TEXT_KIND = 2
/**
*
* @param {String} proto | http/https
* @param {String} domain | domain of the instance we're looking at
* @param {Number} port | Should always be the default value of 4536 but other owner
* @param {Object} params | object with jwt + id fields
* Channel button id's have the format of ${hostname}:${channel['name']}
* Helper function that literally just pushes out some bs to the DOM
*
* @note This function fails quietly for the sake of not destroying the UI instantly
* Errors get logged for now since those are in the debugger anyway but everything else
* is basically really quiet
* @param {String} server.hostname
* @param {u16} server.port
* @param {String} server.protocol
*
* @param {u64} user.id
* @param {String} user.jwt
*
* @param {u64} channel.id
* @param {String} channel.name
* @param {String} channel.description
* @param {i32} channel.kind
*/
function update_channels_list(proto, hostname, port, params) {
const url = `${proto}://${hostname}:${port}/channels/list`
function push(server, user, channel) {
$('#channels-list').append(
$('<li>').append(
$('<p>').append(
$('<button>').attr({
'class': 'btn btn-outline-secondary btn-nav-settings channel-btn',
id: `${server.hostname}:${channel['name']}`,
}).text(channel['name'])
)
)
)
let ref = document.getElementById(`${server.hostname}:${channel['name']}`)
ref.addEventListener('click', function() {
// now for the actual handler where we deal with clicks
$('#channel-name').text('#' + channel['name'])
$('#channel-description').text(channel['description'])
got(url, {
searchParams: { id: params['id'], jwt: params['jwt'] },
responseType: 'json'
})
.then(
response => {
const channels_json = response.body['channels']
const voice = channels_json.filter(chan => chan.kind == VOICE_KIND)
const text = channels_json.filter(chan => chan.kind == TEXT_KIND)
const now = Math.floor(new Date().getTime() / 1000)
const yesterday = now - (60 * 60 * 48)
const chans = voice.concat(text)
for(const channel of chans) {
// wack jquery shit
$('#channels-list').append(
$('<li>').append(
$('<p>').append(
$('<button>').attr({
'class': 'btn btn-outline-secondary btn-nav-settings',
id: channel['name'],
}).text(channel['name'])
)
)
)
// why not jquery? Because the callback doesn't register
// (I have no idea why)
let ref = document.getElementById(channel['name'])
ref.addEventListener('click', function() {
// Collect parameters to pass into the actual handler
const server = {
hostname: hostname,
port: port,
protocol: proto
}
const user = {
id: params['id'],
jwt: params['jwt']
}
// now for the actual handler where we deal with clicks
console.log('fetching', channel)
const now = Math.floor(new Date().getTime() / 1000)
const yesterday = now - (60 * 60 * 48)
msg.recent_messages(user, server, channel, yesterday , now, 1000)
.then(()=>{},reason => {
console.log(reason)
})
})
}
},
fetch_reason => {
console.log(fetch_reason)
$('#channels-list').text('Could not fetch channels')
}
).catch(reason => {
$('#channels-list').text(reason)
msg.recent_messages(user, server, channel, yesterday , now, 1000)
.then(()=>{}, reason => {
console.log(reason)
})
})
}
/**
* Listener callback for actually getting a list of channels
* @param {Object} server Object containing server configuration
* @param {Object} user Object containing user access data
*
*/
exports.list = function(server, user) {
const params = {
id: user['id'],
jwt: user['jwt']
}
const proto = server['protocol']
exports.list = async function(server, user) {
// TODO: add some kind of caching system here: should probably check for some kind of cache here but hey fuck it right?
$('#channels-list').html('')
$('#server-name').text(server['name'])
update_channels_list(proto, server['hostname'], server['port'], params)
try {
const url = `${server['protocol']}://${server['hostname']}:${server['port']}/channels/list`
const response = await got(url, {
searchParams: { id: user['id'], jwt: user['jwt'], kind: ANY_CHANNEL },
responseType: 'json'
})
const channels = response.body['channels']
for(const channel of channels) {
console.log(server, user, channel)
push(server, user, channel)
}
} catch (err) {
console.log(err)
console.log(err['name'])
}
}