+ Adding cache handler for adding lots of messages from one server call

+ Better error logging in cache

! There is still lots of debug logs which at some point need to be built _out_ from prod builds
Also I need some prod builds

+ Adding more return type annotations for clarity/docs sake

- Removed ancient docs in messages module as they were basically just straight up wrong
This commit is contained in:
shockrah 2021-04-15 19:57:13 -07:00
parent 68e22e1b38
commit 591e993329
3 changed files with 41 additions and 10 deletions

View File

@ -127,3 +127,14 @@ ipcMain.handle('cache-new-server', function(event, kwargs) {
cache.set_active(active_target) 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)
})

View File

@ -4,6 +4,8 @@ import { Event } from './events';
class ServerCache { 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> channels: Array<Channel>
// Channel-id => Array<Message> // Channel-id => Array<Message>
messages: Map<BigInt, Array<Message>> messages: Map<BigInt, Array<Message>>
@ -25,7 +27,9 @@ class ServerCache {
console.log('initial message cache: ', this.messages) 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) let ref = this.messages.get(message.cid)
if(ref) { if(ref) {
ref.push(message) // NOTE: this one here? ref.push(message) // NOTE: this one here?
@ -50,13 +54,13 @@ export class Cache extends EventEmitter {
this.active_host = null this.active_host = null
} }
add_server(url: String, servercfg: ServerConfig, channels: Array<Channel>, messages: Array<Message>) { add_server(url: String, servercfg: ServerConfig, channels: Array<Channel>, messages: Array<Message>) : void {
let new_entry = new ServerCache(servercfg, channels, messages) let new_entry = new ServerCache(servercfg, channels, messages)
this.servers.set(url, new_entry) this.servers.set(url, new_entry)
console.log(this.servers) console.log(this.servers)
} }
update_channels(hostname: String, channels: Array<Channel>) { update_channels(hostname: String, channels: Array<Channel>) : void {
this.servers.get(hostname).channels = channels this.servers.get(hostname).channels = channels
} }
@ -76,15 +80,30 @@ export class Cache extends EventEmitter {
let event = new Event(message) let event = new Event(message)
ref.push_message(event.message) ref.push_message(event.message)
} else { } else {
console.error(`No server with ${origin} as hostname`) console.error(`cache::new_message No server with ${origin} as hostname`)
console.error('Active host: ', this.active_host) console.error('cache::new_message Active host: ', this.active_host)
console.error('Servers: ', this.servers) console.error('cache::new_message Servers: ', this.servers)
} }
} }
set_active(url: String) { set_active(url: String) : void {
this.active_host = url this.active_host = url
} }
append_messages(origin: string, messages: Array<Message>) : 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)
}
}
} }

View File

@ -108,9 +108,8 @@ function push_message(message) {
* @param {String} auth.jwt | jwt for quick auth * @param {String} auth.jwt | jwt for quick auth
* @param {u64} auth.id user | id required by most/all endpoints * @param {u64} auth.id user | id required by most/all endpoints
* *
* @param {String} server.hostname | Hostname of target server * @param {String} server.url | HTTP URL
* @param {u16} server.port | Port to use for server * @param {String} server.wsurl | Websocket URL
* @param {String} server.protocol | http/https (no colon)
* *
* @param {String} channel.description | Description of channel * @param {String} channel.description | Description of channel
* @params {u64} channel.id | channel id to query * @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) console.log(message)
push_message(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 * @param {Number} channel_id
*/ */
exports.fetch_from_id = async function (server, auth, 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 url = `${server.protocol}://${server.hostname}:${server.port}/message/from_id`
const start_id = await ipcRenderer.invoke('cache-latest-msg', { const start_id = await ipcRenderer.invoke('cache-latest-msg', {
hostname: server.hostname, hostname: server.hostname,