+ text/plain Message sending works now

+ More skeleton code for the caching system

! Ready for rtc on text based message listening which is where heavy caching comes into play
This commit is contained in:
shockrah 2021-03-20 19:35:38 -07:00
parent 530d287431
commit 8812ff7198
9 changed files with 96 additions and 22 deletions

View File

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

View File

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

View File

@ -35,14 +35,12 @@
</ul>
</nav>
<div class="container" style="max-width: 100%;">
<div class="container" id="content-container" style="max-width: 100%;">
<div class="channel-list-header">
<h4 id="channel-name"><h4>
<p id="channel-description"></p>
</div>
<ul class="scrollarea list-unstyled components" id="messages-list"></ul>
<div class="container" id="send-container">
<div class="input-group" id="message-area">
<input type="text" class="form-control" placeholder="Message" aria-describedby="message-btn" id="message-box">
@ -50,7 +48,6 @@
<button class="btn btn-outline-secondary btn-nav-settings" type="button" id="message-btn">Send</button>
</div>
</div>
</div>
</div>
</div>

View File

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

View File

@ -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);
}

View File

@ -1,6 +1,6 @@
import { ServerConfig, Message, Channel } from './types';
export class ServerCache {
class ServerCache {
channels: Array<Channel>
// Channel id's are used to key into the list of messages for that given channel
messages: Map<Number, Array<Message>>
@ -44,13 +44,15 @@ export class Cache {
// hostname -> Cache
servers: Map<String, ServerCache>
constructor(servers: Array<ServerCache>) {
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<Channel>, messages: Array<Message>) {
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<Channel>) {
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
}
}
}

View File

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

View File

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

View File

@ -1,7 +1,12 @@
// Basically here we define various types that are used pretty much everywhere
const MESSAGE_TYPES: Array<String> = [
'text/plain',
'image/png', 'image/jpeg', 'image/jpg',
'application/webm', 'application/mp4',
'application/mp3'
]
export class Message {
private TYPES: Array<String> = ['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