Message constructor now takes in username as a parameter and binds it to uname

 Removed some fluff
 New get_member endpoint handler for later
This commit is contained in:
shockrah 2021-03-07 13:50:48 -08:00
parent 784043fc89
commit fd1bf41fec
4 changed files with 70 additions and 38 deletions

View File

@ -1,10 +1,7 @@
// Requesting channel data here
const { ipcRenderer } = require('electron')
const $ = require('jquery')
const got = require('got')
const auth = require('./auth.js')
const { Request, BuildUrl } = require('./request.js')
const msg = require('./messages.js')
@ -52,7 +49,6 @@ function update_channels_list(proto, hostname, port, params) {
)
// why not jquery? Because the callback doesn't register
// (I have no idea why)
console.log(channel)
let ref = document.getElementById(channel['name'])
ref.addEventListener('click', function() {
// Collect parameters to pass into the actual handler
@ -67,7 +63,13 @@ function update_channels_list(proto, hostname, port, params) {
}
// now for the actual handler where we deal with clicks
msg.recent_messages(user, server, channel)
console.log('fetching', channel)
const now = Math.floor(new Date().getTime() / 1000)
const yesterday = now - (60 * 60 * 48)
msg.recent_messages(user, server, channel, yesterday , now, 1000)
.then(()=>{},reason => {
console.log(reason)
})
})
}
},

View File

@ -0,0 +1,29 @@
const got = require('got')
/**
* @param {String} auth.jwt
* @param {u64} auth.id
*
* @param {String server.protocol
* @param {String} server.hostname
* @param {u16} server.port
*
* @param {u64} id
*
* @return {Sucess} {id: u64, name: String, joindate: i64, permissions: u64}
* @returns {Failure} null
*/
exports.get_member = async function(auth, server, id) {
try {
const url = `${server.protocol}://${server.hostname}:${server.port}/members/single`
const response = await got(url, {
searchParams: { id: auth.id, jwt: auth.jwt, member_id: id },
responseType: 'json',
})
return response.body['member']
} catch (err) {
return null
}
}

View File

@ -1,3 +1,4 @@
const members = require('./members.js')
const got = require('got')
const $ = require('jquery')
@ -5,10 +6,11 @@ class Message {
static TYPES = ['text', 'jpeg', 'png', 'webm', 'mp4']
constructor(id, time, content, type, uid, cid) {
constructor(id, time, content, type, uid, uname, cid) {
this.id = id
this.time = time
this.uid = uid
this.uname = uname
this.cid = cid
this.type = type
@ -19,6 +21,7 @@ class Message {
this.content = null
} else {
this.content = content
}
}
@ -65,6 +68,7 @@ async function get_range(auth, server, channel_id, start_time, end_time, limit)
msg['content'],
msg['type'],
msg['author_id'],
msg['name'],
msg['channel_id']
))
}
@ -72,6 +76,31 @@ async function get_range(auth, server, channel_id, start_time, end_time, limit)
return messages
}
/**
* @param {Message} message
*
* @description Basically pushes the required html to the dom
* @returns void
*/
function push_message(message) {
let a_tag = $('<a>').attr({
id: `message-${message.uid}`,
href: '#',
'class': 'btn btn-link author-name'
}).text(message.uname)
// next the content itself
let content
if(message.content) {
content = message.content
} else {
content = $('<li>').text('Unsupported content')
}
let container = $('<p>').append(a_tag, content).attr('class', 'message')
$('#messages-list').append( $('<li>').append(container) )
}
/**
* @param {String} auth.jwt | jwt for quick auth
* @param {u64} auth.id user | id required by most/all endpoints
@ -84,39 +113,12 @@ async function get_range(auth, server, channel_id, start_time, end_time, limit)
* @params {u64} channel.id | channel id to query
* @returns void
*/
exports.recent_messages = function (auth, server, channel, start_time, end_time, limit) {
exports.recent_messages = async function (auth, server, channel, start_time, end_time, limit) {
$('#channel-name').text('#' + channel['id'])
$('#channel-description').text(channel['description'])
$('#messages-list').html('')
const now = Math.floor(new Date().getTime() / 1000)
const yesterday = now - (60 * 60 * 48)
const messages = await get_range(auth, server, channel['id'], start_time, end_time, limit)
get_range(auth, server, channel['id'], yesterday, now, 1000)
.then(messages => {
// TODO: this shit right here brother here we go html farming wew lad
//<li id="m-123"> <p class="message"> <a href="#" class="btn btn-link author-name">resident tweeker</a> some bs content </p> </li>
for(const message of messages) {
$('#messages-list').append(
$('<li>').append(
$('<p>').append(
$('<a>').attr({
id: message.uid,
href: '#',
'class': 'btn btn-link author-name'
}).text(message.uid),
message.content
).attr('class', 'message')
)
)
console.log(message)
}
},
http_err => {
console.log('Couldn\'t fetch data from server', http_err)
console.log(http_err.request)
})
.catch(err => {
console.log('Hanlding built in error')
})
for(const message of messages) { push_message(message) }
}

View File

@ -1,6 +1,5 @@
const got = require('got')
const { Response } = require('./response.js')
const fs = require('fs')
/**
*