❗ 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:
parent
7fede3b4e1
commit
2fa1ec6f34
@ -46,7 +46,6 @@ exports.init = async function() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('after init', config)
|
|
||||||
|
|
||||||
await ipcRenderer.invoke('config-update', config)
|
await ipcRenderer.invoke('config-update', config)
|
||||||
return await ipcRenderer.invoke('config-request')
|
return await ipcRenderer.invoke('config-request')
|
||||||
|
@ -5,98 +5,82 @@ const got = require('got')
|
|||||||
const msg = require('./messages.js')
|
const msg = require('./messages.js')
|
||||||
|
|
||||||
|
|
||||||
|
const ANY_CHANNEL = 0
|
||||||
const VOICE_KIND = 1
|
const VOICE_KIND = 1
|
||||||
const TEXT_KIND = 2
|
const TEXT_KIND = 2
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Channel button id's have the format of ${hostname}:${channel['name']}
|
||||||
* @param {String} proto | http/https
|
* Helper function that literally just pushes out some bs to the DOM
|
||||||
* @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
|
|
||||||
*
|
*
|
||||||
* @note This function fails quietly for the sake of not destroying the UI instantly
|
* @param {String} server.hostname
|
||||||
* Errors get logged for now since those are in the debugger anyway but everything else
|
* @param {u16} server.port
|
||||||
* is basically really quiet
|
* @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) {
|
function push(server, user, channel) {
|
||||||
const url = `${proto}://${hostname}:${port}/channels/list`
|
$('#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, {
|
const now = Math.floor(new Date().getTime() / 1000)
|
||||||
searchParams: { id: params['id'], jwt: params['jwt'] },
|
const yesterday = now - (60 * 60 * 48)
|
||||||
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 chans = voice.concat(text)
|
msg.recent_messages(user, server, channel, yesterday , now, 1000)
|
||||||
for(const channel of chans) {
|
.then(()=>{}, reason => {
|
||||||
// wack jquery shit
|
console.log(reason)
|
||||||
$('#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)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listener callback for actually getting a list of channels
|
* Listener callback for actually getting a list of channels
|
||||||
* @param {Object} server Object containing server configuration
|
* @param {Object} server Object containing server configuration
|
||||||
* @param {Object} user Object containing user access data
|
* @param {Object} user Object containing user access data
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
exports.list = function(server, user) {
|
exports.list = async function(server, user) {
|
||||||
const params = {
|
|
||||||
id: user['id'],
|
|
||||||
jwt: user['jwt']
|
|
||||||
}
|
|
||||||
|
|
||||||
const proto = server['protocol']
|
|
||||||
|
|
||||||
|
// 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('')
|
$('#channels-list').html('')
|
||||||
$('#server-name').text(server['name'])
|
$('#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'])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user