From c3d5c75cc0686173c174ccd6cc860bbef35ef73b Mon Sep 17 00:00:00 2001 From: shockrah Date: Fri, 12 Mar 2021 23:16:06 -0800 Subject: [PATCH] messages rebind now work properly and sends message correctly, Message constructor no longer incorrectly check message type --- freechat-client/pages/index.html | 2 +- freechat-client/src/messages.js | 62 ++++++++++++++++++++++++++++++-- freechat-client/src/types.ts | 2 +- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/freechat-client/pages/index.html b/freechat-client/pages/index.html index d11ec73..3e76d32 100644 --- a/freechat-client/pages/index.html +++ b/freechat-client/pages/index.html @@ -45,7 +45,7 @@
- +
diff --git a/freechat-client/src/messages.js b/freechat-client/src/messages.js index 9ad9f5d..0d05191 100644 --- a/freechat-client/src/messages.js +++ b/freechat-client/src/messages.js @@ -1,8 +1,35 @@ +const { ipcRenderer } = require('electron') const { Message } = require('./types') const got = require('got') const $ = require('jquery') +/** + * @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} auth.jwt | jwt for quick auth + * @param {u64} auth.id user | id required by most/all endpoints + * + */ +function rebind_message_box(server, auth, channel_id) { + + $('#message-btn').click( async () => { + const content = $.trim($('#message-box').val()) + await send(server, auth, channel_id, content) + $('#message-box').val('') + }) + + $('#message-box').on('keypress', async (event) => { + if(event.keyCode == 13 && !event.shiftKey ) { + const content = $.trim($('#message-box').val()) + await send(server, auth, channel_id, content) + $('#message-box').val('') + } + }) +} + /** * @param {String} auth.jwt | jwt for quick auth * @param {u64} auth.id user | id required by most/all endpoints @@ -37,6 +64,7 @@ async function get_range(auth, server, channel_id, start_time, end_time, limit) // assming 200 from this point out let messages = [] for(const msg of response.body['messages']) { + console.log('raw message', msg) messages.push(new Message( msg['id'], msg['time'], @@ -48,6 +76,9 @@ async function get_range(auth, server, channel_id, start_time, end_time, limit) )) } + console.log(messages) + + rebind_message_box(server, auth, channel_id) return messages } @@ -69,7 +100,7 @@ function push_message(message) { if(message.content) { content = message.content } else { - content = $('
  • ').text('Unsupported content') + content = $('').text('Unsupported content') } let container = $('

    ').append(a_tag, content).attr('class', 'message') @@ -99,6 +130,31 @@ exports.recent_messages = async function (auth, server, channel, start_time, end } } -exports.send = async function() { - console.log('todo') +/** + * @param {String} server.protocol + * @param {String} server.hostname + * @param {String} server.port + * + * @param {String} auth.jwt + * @param {Number} auth.id + * + * @param {Number} channel_id + */ +async function send(server, auth, channel_id, body) { + const url = `${server.protocol}://${server.hostname}:${server.port}/message/send` + console.log('sending: ', url) + // using callbacks for finer grain control of error messages + got.post(url, { + searchParams: { + id: auth.id, jwt: auth.jwt, + channel_id: channel_id, + type: 'text' + }, + body: body + }) + .then( + response => console.log('Sucess: ', response), // all good + failure => { console.log('Failed to send: ', server, auth, channel_id, failure.options) } + ) + } diff --git a/freechat-client/src/types.ts b/freechat-client/src/types.ts index 4668322..9e82d2b 100644 --- a/freechat-client/src/types.ts +++ b/freechat-client/src/types.ts @@ -20,7 +20,7 @@ export class Message { this.type = type // throw away the content if its not of a valid type - if(this.TYPES.indexOf(content) == -1) { + if(this.TYPES.indexOf(type) < 0) { this.content = null } else { this.content = content