messages rebind now work properly and sends message correctly,

Message constructor no longer incorrectly check message type
This commit is contained in:
shockrah 2021-03-12 23:16:06 -08:00
parent c667f69d0e
commit c3d5c75cc0
3 changed files with 61 additions and 5 deletions

View File

@ -45,7 +45,7 @@
<div class="container" id="send-container">
<div class="input-group" id="message-area">
<textarea rows="1" class="form-control" placeholder="Message" aria-describedby="message-btn" id="message-box"></textarea>
<input type="text" class="form-control" placeholder="Message" aria-describedby="message-btn" id="message-box">
<div class="input-group-append">
<button class="btn btn-outline-secondary btn-nav-settings" type="button" id="message-btn">Send</button>
</div>

View File

@ -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 = $('<li>').text('Unsupported content')
content = $('<span>').text('Unsupported content')
}
let container = $('<p>').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) }
)
}

View File

@ -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