diff --git a/freechat-client/main.js b/freechat-client/main.js index d8ef867..41f1712 100644 --- a/freechat-client/main.js +++ b/freechat-client/main.js @@ -4,6 +4,7 @@ const { ArgumentParser } = require('argparse') const path = require('path') const { Config, from_cli_parser } = require('./src/config.js') +const { Cache } = require('./src/cache') const arguments = (() => { const parser = new ArgumentParser({ @@ -16,6 +17,7 @@ const arguments = (() => { let win let config = from_cli_parser(arguments) +let cache = new Cache() // NOTE: this line is only here for demonstration as the current make script exposes this var // so that we can test against self signed certificates but still use ssl diff --git a/freechat-client/make b/freechat-client/make index 837d274..3018d7c 100755 --- a/freechat-client/make +++ b/freechat-client/make @@ -11,7 +11,9 @@ run() { build() { sh scripts/build-sass.sh build + echo tsc src/types.ts tsc src/types.ts + echo tsc src/cache.ts tsc src/cache.ts } diff --git a/freechat-client/pages/index.html b/freechat-client/pages/index.html index 3e76d32..e2acd28 100644 --- a/freechat-client/pages/index.html +++ b/freechat-client/pages/index.html @@ -35,14 +35,12 @@ -
+

- -
@@ -50,7 +48,6 @@
-
diff --git a/freechat-client/sass/scroll.scss b/freechat-client/sass/scroll.scss index 8418935..389cf9c 100644 --- a/freechat-client/sass/scroll.scss +++ b/freechat-client/sass/scroll.scss @@ -1,6 +1,6 @@ @import 'general'; -::-webkit-scrollbar { +::-webkit-scrollbar-corner, ::-webkit-scrollbar { background-color: $back-grey; width: .8em } @@ -12,6 +12,8 @@ } .scrollarea { - max-height: 100vh; + max-height: inherit; + max-width: inherit; overflow: scroll; + word-break: break-all; } diff --git a/freechat-client/sass/style.scss b/freechat-client/sass/style.scss index fcaff0c..52c615d 100644 --- a/freechat-client/sass/style.scss +++ b/freechat-client/sass/style.scss @@ -92,6 +92,7 @@ ul ul a { display: block; text-decoration: none; color: $text; + margin: 0; } .message a:hover { @@ -114,4 +115,6 @@ ul ul a { max-height: 1005px; // 1080 - $send-container-height } - +#content-container { + max-height: calc(100% - 100px); +} diff --git a/freechat-client/src/cache.ts b/freechat-client/src/cache.ts index ddf71a6..882d41c 100644 --- a/freechat-client/src/cache.ts +++ b/freechat-client/src/cache.ts @@ -1,6 +1,6 @@ import { ServerConfig, Message, Channel } from './types'; -export class ServerCache { +class ServerCache { channels: Array // Channel id's are used to key into the list of messages for that given channel messages: Map> @@ -44,13 +44,15 @@ export class Cache { // hostname -> Cache servers: Map - constructor(servers: Array) { - for(const server of servers) { - const hostname = server.config.hostname - this.servers.set(hostname, server) - } + constructor() { + this.servers = new Map() } + add_server(hostname: String, servercfg: ServerConfig, channels: Array, messages: Array) { + let new_entry = new ServerCache(servercfg, channels, messages) + this.servers.set(servercfg.hostname, new_entry) + } + add_message(hostname: String, message: Message) { let ref = this.servers.get(hostname) if(!ref) { @@ -59,5 +61,18 @@ export class Cache { ref.push_message(message) } + + update_channels(hostname: String, channels: Array) { + this.servers.get(hostname).channels = channels + } + + last_id(hostname: String, channel_id: Number) : Number|null { + try { + let ref = this.servers.get(hostname).messages.get(channel_id) + return ref[ref.length - 1].cid + } catch { + return null + } + } } diff --git a/freechat-client/src/channels.js b/freechat-client/src/channels.js index 31d153b..3f1bff7 100644 --- a/freechat-client/src/channels.js +++ b/freechat-client/src/channels.js @@ -39,13 +39,14 @@ function push(server, user, channel) { ) // Not using jquery for this because it breaks with weird id names really easily let ref = document.getElementById(`${server.hostname}:${channel['name']}`) - ref.addEventListener('click', function() { + ref.addEventListener('click', () => { // now for the actual handler where we deal with clicks $('#channel-name').text('#' + channel['name']) $('#channel-description').text(channel['description']) - const now = Math.floor(new Date().getTime() / 1000) - const yesterday = now - (60 * 60 * 48) + const now = Math.floor(new Date().getTime()) // message times are stored in ms + const yesterday = now - (60 * 60 * 48 * 1000) // adjust for ms + console.log(now); msg.recent_messages(user, server, channel, yesterday , now, 1000) .then(()=>{}, reason => { diff --git a/freechat-client/src/messages.js b/freechat-client/src/messages.js index 0d05191..ca298b4 100644 --- a/freechat-client/src/messages.js +++ b/freechat-client/src/messages.js @@ -139,22 +139,69 @@ exports.recent_messages = async function (auth, server, channel, start_time, end * @param {Number} auth.id * * @param {Number} channel_id + * + * @param {Any} body */ 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 + headers: { 'content-type': 'text/plain' }, + body: body, }) .then( - response => console.log('Sucess: ', response), // all good + response => { + console.log(response); + //await ipcRenderer.invoke('cache-sent-message', {host: server.hostname, message: response.body } + }, failure => { console.log('Failed to send: ', server, auth, channel_id, failure.options) } ) } + +/** + * @param {String} server.protocol + * @param {String} server.hostname + * @param {String} server.port + * + * @param {String} auth.jwt + * @param {Number} auth.id + * + * @param {Number} channel_id + */ +exports.fetch_from_id = async function (server, auth, channel_id) { + const url = `${server.protocol}://${server.hostname}:${server.port}/message/from_id` + const start_id = await ipcRenderer.invoke('cache-latest-msg', { + hostname: server.hostname, + channel: channel_id + }) + + got(url, { + searchParams: { + id: auth.id, + jwt: auth.jwt, + + channel_id: channel_id, + start: start_id + 1 // skip the last message because we already have it + }, + responseType: 'json' + }) + .then( + // proper values + messages => { + for(const message of messages) { + push_message(message) + } + }, + // Some kind of HTTP failure + reason => { + cosole.log('http error') + }) + .catch(err => { + console.error('couldn\'t handle: ', err) + }) +} diff --git a/freechat-client/src/types.ts b/freechat-client/src/types.ts index 9e82d2b..0b4cdc8 100644 --- a/freechat-client/src/types.ts +++ b/freechat-client/src/types.ts @@ -1,7 +1,12 @@ // Basically here we define various types that are used pretty much everywhere +const MESSAGE_TYPES: Array = [ + 'text/plain', + 'image/png', 'image/jpeg', 'image/jpg', + 'application/webm', 'application/mp4', + 'application/mp3' +] export class Message { - private TYPES: Array = ['text', 'jpeg', 'png', 'webm', 'mp4'] id: Number time: Number type: String @@ -20,7 +25,7 @@ export class Message { this.type = type // throw away the content if its not of a valid type - if(this.TYPES.indexOf(type) < 0) { + if(MESSAGE_TYPES.indexOf(type) < 0) { this.content = null } else { this.content = content