diff --git a/freechat-client/main.js b/freechat-client/main.js index 3a77b5d..dfa38c2 100644 --- a/freechat-client/main.js +++ b/freechat-client/main.js @@ -127,3 +127,14 @@ ipcMain.handle('cache-new-server', function(event, kwargs) { cache.set_active(active_target) } }) + +ipcMain.handle('cache-append-messages', function cache_append_message(event, ...args) { + // Used heavily by methods that call for messages en-masse + // The whole point of this is to make it easier to dump messages into the + // cache without worrying so much about "how do i do that" + const server = args[0] + const messages = args[1] + + const wsurl = server.wsurl + cache.append_messages(wsurl, messages) +}) diff --git a/freechat-client/src/cache.ts b/freechat-client/src/cache.ts index 41bab01..c08ddea 100644 --- a/freechat-client/src/cache.ts +++ b/freechat-client/src/cache.ts @@ -4,6 +4,8 @@ import { Event } from './events'; class ServerCache { + // NOTE: at some point we should opt for making the messages field private and provide + // some safer methods for data access channels: Array // Channel-id => Array messages: Map> @@ -25,7 +27,9 @@ class ServerCache { console.log('initial message cache: ', this.messages) } - push_message(message: Message) { + push_message(message: Message) : void { + // Safe way of pushing messages into the message cache + // This will append messages one at a time let ref = this.messages.get(message.cid) if(ref) { ref.push(message) // NOTE: this one here? @@ -50,13 +54,13 @@ export class Cache extends EventEmitter { this.active_host = null } - add_server(url: String, servercfg: ServerConfig, channels: Array, messages: Array) { + add_server(url: String, servercfg: ServerConfig, channels: Array, messages: Array) : void { let new_entry = new ServerCache(servercfg, channels, messages) this.servers.set(url, new_entry) console.log(this.servers) } - update_channels(hostname: String, channels: Array) { + update_channels(hostname: String, channels: Array) : void { this.servers.get(hostname).channels = channels } @@ -76,15 +80,30 @@ export class Cache extends EventEmitter { let event = new Event(message) ref.push_message(event.message) } else { - console.error(`No server with ${origin} as hostname`) - console.error('Active host: ', this.active_host) - console.error('Servers: ', this.servers) + console.error(`cache::new_message No server with ${origin} as hostname`) + console.error('cache::new_message Active host: ', this.active_host) + console.error('cache::new_message Servers: ', this.servers) } } - set_active(url: String) { + set_active(url: String) : void { this.active_host = url } + + append_messages(origin: string, messages: Array) : void { + // Takes a list of messages and appends them to the relevant origin cache + let ref = this.servers.get(origin) + // only if that server cache is setup should we push anything to it + if(ref) { + for(const msg of messages) { + ref.push_message(msg) + } + } else { + console.error(`cache::append_messages No server with ${origin}`) + console.error(`cache::append_messages Active host: ${this.active_host}`) + console.error('cache::append_messages Servers: ', this.servers) + } + } } diff --git a/freechat-client/src/messages.js b/freechat-client/src/messages.js index 284ca5d..38c235c 100644 --- a/freechat-client/src/messages.js +++ b/freechat-client/src/messages.js @@ -108,9 +108,8 @@ function push_message(message) { * @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} server.url | HTTP URL + * @param {String} server.wsurl | Websocket URL * * @param {String} channel.description | Description of channel * @params {u64} channel.id | channel id to query @@ -125,6 +124,7 @@ exports.recent_messages = async function (auth, server, channel, start_time, end console.log(message) push_message(message) } + await ipcRenderer.invoke('cache-append-messages', server, messages) } /** @@ -170,6 +170,7 @@ async function send(server, auth, channel_id, body) { * @param {Number} channel_id */ exports.fetch_from_id = async function (server, auth, channel_id) { + // TODO: refactor to use updated methodoly const url = `${server.protocol}://${server.hostname}:${server.port}/message/from_id` const start_id = await ipcRenderer.invoke('cache-latest-msg', { hostname: server.hostname,