➕ Adding hooks to reqest channel messages

✨ DOM manipulation for channel buttons moved to new module
❗ Message hooks themselves still require actual implementation but
all the required fallbacks are there
This commit is contained in:
shockrah
2021-03-03 21:49:40 -08:00
parent 33716e5b7b
commit 231141221b
2 changed files with 122 additions and 5 deletions

View File

@@ -1,11 +1,13 @@
// Requesting channel data here
const { ipcRenderer } = require('electron')
const auth = require('./auth.js')
const { Request, BuildUrl } = require('./request.js')
const $ = require('jquery')
const got = require('got')
const auth = require('./auth.js')
const { Request, BuildUrl } = require('./request.js')
const msg = require('./messages.js')
const VOICE_KIND = 1
const TEXT_KIND = 2
@@ -52,8 +54,19 @@ function update_channels_list(proto, hostname, port, params) {
console.log(channel)
let ref = document.getElementById(channel['name'])
ref.addEventListener('click', function() {
$('#channel-name').text(`#${channel['name']}`)
$('#channel-description').text(channel['description'])
// 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
msg.recent_messages(user, server, channel)
})
}
},

View File

@@ -0,0 +1,104 @@
const got = require('got')
const $ = require('jquery')
exports.Message = class _Message {
static TYPES = ['text', 'jpeg', 'png', 'webm', 'mp4']
constructor(id, time, content, type, uid, cid) {
this.id = id
this.time = time
this.uid = uid
this.cid = cid
this.type = type
// basically throw away the content since its not valid and we don't
// bother dealing with invalid data
if(Message.TYPES.indexOf(type) == -1) {
this.content = null
} else {
this.content = content
}
}
}
/**
* @param {String} auth.jwt | jwt for quick auth
* @param {u64} auth.id user | id required by most/all endpoints
*
* @param {String} server.hostname | Hostname of target server
* @param {u16} server.port | Port to use for server
* @param {String} server.protocol | http/https (no colon)
*
* @param {u64} channel_id | Channel id to query
* @param {i64} start_time | Unix time to start fetching messages from
* @param {i64} end_time | Unix time to stop fetching messages from
*
* @returns Array<Message>
*
* @throws HTTPResponseError
*/
async function get_range(auth, server, channel_id, start_time, end_time, limit) {
const url = `${server.protocol}://${server.hostname}:${server.port}/message/get_range`
const response = await got(url, {
searchParams: {
id: auth.id,
jwt: auth.jwt,
channel_id: channel_id,
start_time: start_time,
end_time: end_time,
limit: limit // backend allows for fucked up values
},
responseType: 'json'
});
// assming 200 from this point out
let messages = []
for(const msg of response.body['messages']) {
messages.push(new Message(
msg['id'],
msg['time'],
msg['content'],
msg['type'],
msg['author_id'],
msg['channel_id']
))
}
return messages
}
/**
* @param {String} auth.jwt | jwt for quick auth
* @param {u64} auth.id user | id required by most/all endpoints
*
* @param {String} server.hostname | Hostname of target server
* @param {u16} server.port | Port to use for server
* @param {String} server.protocol | http/https (no colon)
*
* @param {String} channel.description | Description of channel
* @params {u64} channel.id | channel id to query
* @returns void
*/
exports.recent_messages = function (auth, server, channel, start_time, end_time, limit) {
$('#channel-name').text('#' + channel['id'])
$('#channel-description').text(channel['description'])
const now = Math.floor(new Date().getTime() / 1000)
const yesterday = now - (60 * 60 * 24)
get_range(auth, server, channel['id'], yesterday, now, 1000)
.then(messages => {
console.log(messages)
},
http_err => {
console.log('Couldn\'t fetch data from server', http_err)
console.log(http_err.request)
})
.catch(err => {
console.log('Hanlding built in error')
})
}