* update_channels_list now literally updates the channel list with the correct hooks
! Callback hooks are not featured yet but they are registered in the DOM
This commit is contained in:
parent
25876f90e4
commit
080b2cc538
@ -1,65 +1,86 @@
|
||||
// Requesting channel data here
|
||||
const { ipcRenderer } = require('electron')
|
||||
const auth = require('./auth.js')
|
||||
const { Request } = require('./request.js')
|
||||
const { Request, BuildUrl } = require('./request.js')
|
||||
|
||||
const $ = require('jquery')
|
||||
const got = require('got')
|
||||
|
||||
const VOICE_KIND = 1
|
||||
const TEXT_KIND = 2
|
||||
class Channel {
|
||||
constructor(name, id, type, description) {
|
||||
this.name = name
|
||||
this.id = id
|
||||
this.type = type
|
||||
this.description = description
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @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 | basic object with jwt + id fields
|
||||
* @param {Object} params | object with jwt + id fields
|
||||
*
|
||||
* @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
|
||||
*
|
||||
*/
|
||||
function update_channels_list(domain, port, params) {
|
||||
function update_channels_list(proto, hostname, port, params) {
|
||||
const url = `${proto}://${hostname}:${port}/channels/list`
|
||||
|
||||
Request('get', domain, port, '/channels/list', params).then(
|
||||
got(url, {
|
||||
searchParams: { id: params['id'], jwt: params['jwt'] },
|
||||
responseType: 'json'
|
||||
})
|
||||
.then(
|
||||
response => {
|
||||
if(response.status_code != 200) {
|
||||
document.getElementById('channels-list').textContent = 'Could not fetch channels'
|
||||
}
|
||||
else {
|
||||
const channels = response.body
|
||||
const voice = channels.filter(chan => chan.kind == VOICE_KIND)
|
||||
const text = channels.filter(chan => chan.kind == TEXT_KIND)
|
||||
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)
|
||||
for(const channel of chans) {
|
||||
const name = channel.name
|
||||
let item = `<li> <p class="message"> <a href="#" class="btn btn-link channel-name">${name}</a> </p> </li>`
|
||||
document.getElementById('channels-list').innerHTML += item
|
||||
}
|
||||
const chans = voice.concat(text)
|
||||
for(const channel of chans) {
|
||||
// wack jquery shit
|
||||
$('#channels-list').append(
|
||||
$('<li>').append(
|
||||
$('<button>').attr({
|
||||
type: 'button',
|
||||
'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)
|
||||
console.log(channel)
|
||||
let ref = document.getElementById(channel['name'])
|
||||
ref.addEventListener('click', function() {
|
||||
$('#channel-name').text(`#${channel['name']}`)
|
||||
$('#channel-description').text(channel['description'])
|
||||
})
|
||||
}
|
||||
},
|
||||
reason => {
|
||||
console.log('Request for /channels/list rejected', 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
|
||||
* @param {Object} server_conf Should contain at least id/secret + domain/port
|
||||
* @param {Object} server Object containing server configuration
|
||||
* @param {Object} user Object containing user access data
|
||||
*
|
||||
*/
|
||||
exports.get_channels = function(server_conf) {
|
||||
// the first two checks are here to rpevent getting a 401 due to a bad/missing jwt
|
||||
console.log(server_conf)
|
||||
exports.list = function(server, user) {
|
||||
const params = {
|
||||
id: server_conf['id'],
|
||||
jwt: server_conf['jwt']
|
||||
id: user['id'],
|
||||
jwt: user['jwt']
|
||||
}
|
||||
|
||||
update_channels_list(server_conf['domain'], server_conf['port'], params)
|
||||
const proto = server['protocol']
|
||||
|
||||
$('#channels-list').html('')
|
||||
$('#server-name').text(server['name'])
|
||||
update_channels_list(proto, server['hostname'], server['port'], params)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user