From 231141221ba6fcd8896c3d6addc8d721a6fe4791 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 3 Mar 2021 21:49:40 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Adding=20hooks=20to=20reqest=20chan?= =?UTF-8?q?nel=20messages=20=E2=9C=A8=20DOM=20manipulation=20for=20channel?= =?UTF-8?q?=20buttons=20moved=20to=20new=20module=20=E2=9D=97=20Message=20?= =?UTF-8?q?hooks=20themselves=20still=20require=20actual=20implementation?= =?UTF-8?q?=20but=20all=20the=20required=20fallbacks=20are=20there?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- freechat-client/src/channels.js | 23 +++++-- freechat-client/src/messages.js | 104 ++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 freechat-client/src/messages.js diff --git a/freechat-client/src/channels.js b/freechat-client/src/channels.js index 619d9eb..087fc49 100644 --- a/freechat-client/src/channels.js +++ b/freechat-client/src/channels.js @@ -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) }) } }, diff --git a/freechat-client/src/messages.js b/freechat-client/src/messages.js new file mode 100644 index 0000000..70b3f47 --- /dev/null +++ b/freechat-client/src/messages.js @@ -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 + * + * @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') + }) +}