const path = require('path')
const {
ipcRenderer
} = require('electron')
const { v4: uuidv4 } = require('uuid');
const utf8 = require('utf8');
// https://stackoverflow.com/a/26227660
var appDataFolder = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.config")
var configFolder = path.join(appDataFolder, "FreeDATA");
var configPath = path.join(configFolder, 'config.json')
const config = require(configPath);
// set date format
const dateFormat = new Intl.DateTimeFormat('en-GB', {
timeStyle: 'long',
dateStyle: 'full'
});
// split character
const split_char = '\0;'
var chatDB = path.join(configFolder, 'chatDB')
// ---- MessageDB
var PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-find'));
var db = new PouchDB(chatDB);
// get all messages from database
//var messages = db.get("messages").value()
// get all dxcallsigns in database
var dxcallsigns = new Set();
db.createIndex({
index: {
fields: ['timestamp', 'uuid', 'dxcallsign', 'dxgrid', 'msg', 'checksum', 'type', 'command', 'status']
}
}).then(function (result) {
// handle result
console.log(result)
}).catch(function (err) {
console.log(err);
});
db.find({
selector: {
timestamp: {$exists: true}},
sort: [{'timestamp': 'asc'}]
}).then(function (result) {
// handle result
console.log(result);
console.log(typeof(result));
if(typeof(result) !== 'undefined'){
result.docs.forEach(function(item) {
update_chat(item);
});
}
}).catch(function (err) {
console.log(err);
});
// WINDOW LISTENER
window.addEventListener('DOMContentLoaded', () => {
document.querySelector('emoji-picker').addEventListener("emoji-click", (event) => {
document.getElementById('chatModuleMessage').setRangeText(event.detail.emoji.unicode)
console.log(event.detail);
})
document.getElementById("emojipickerbutton").addEventListener("click", () => {
var element = document.getElementById("emojipickercontainer")
console.log(element.style.display);
if (element.style.display === "none") {
element.style.display = "block";
} else {
element.style.display = "none";
}
})
// SEND MSG
document.getElementById("sendMessage").addEventListener("click", () => {
document.getElementById('emojipickercontainer').style.display = "none";
var dxcallsign = document.getElementById('chatModuleDxCall').value;
dxcallsign = dxcallsign.toUpperCase();
var chatmessage = document.getElementById('chatModuleMessage').value;
//chatmessage = Buffer.from(chatmessage, 'utf-8').toString();
var uuid = uuidv4();
console.log(chatmessage)
let Data = {
command: "send_message",
dxcallsign : dxcallsign,
mode : 255,
frames : 1,
data : chatmessage,
checksum : '123',
uuid : uuid
};
ipcRenderer.send('run-tnc-command', Data);
db.post({
_id: uuid,
timestamp: Math.floor(Date.now() / 1000),
dxcallsign: dxcallsign,
dxgrid: 'NULL',
msg: chatmessage,
checksum: 'NULL',
type: "transmit",
status: 'transmit',
uuid: uuid
}).then(function (response) {
// handle response
console.log("new database entry");
console.log(response);
}).catch(function (err) {
console.log(err);
});
db.get(uuid).then(function (doc) {
// handle doc
update_chat(doc)
}).catch(function (err) {
console.log(err);
});
// scroll to bottom
var element = document.getElementById("message-container");
element.scrollTo(0,element.scrollHeight);
// clear input
document.getElementById('chatModuleMessage').value = ''
})
});
ipcRenderer.on('action-update-transmission-status', (event, arg) => {
console.log(arg.status);
console.log(arg.uuid);
db.get(arg.uuid).then(function(doc) {
return db.put({
_id: arg.uuid,
_rev: doc._rev,
timestamp: doc.timestamp,
dxcallsign: doc.dxcallsign,
dxgrid: doc.dxgrid,
msg: doc.msg,
checksum: doc.checksum,
type: "transmit",
status: arg.status,
uuid: doc.uuid
});
}).then(function(response) {
// handle response
db.get(arg.uuid).then(function (doc) {
// handle doc
update_chat(doc);
}).catch(function (err) {
console.log(err);
});
}).catch(function (err) {
console.log(err);
});
});
ipcRenderer.on('action-new-msg-received', (event, arg) => {
console.log(arg);
console.log(arg.data);
var new_msg = arg.data;
new_msg.forEach(function(item) {
console.log(item);
//for (i = 0; i < arg.data.length; i++) {
let obj = new Object();
var encoded_data = atob(item.data);
var splitted_data = encoded_data.split(split_char);
console.log(utf8.decode(splitted_data[3]));
//obj.uuid = item.uuid;
item.command = splitted_data[1];
item.checksum = splitted_data[2];
// convert message to unicode from utf8 because of emojis
item.uuid = utf8.decode(splitted_data[3]);
item.msg = utf8.decode(splitted_data[4]);
//obj.dxcallsign = item.dxcallsign;
//obj.dxgrid = item.dxgrid;
//obj.timestamp = item.timestamp;
item.status = 'null';
// check if message not exists in database.
// this might cause big cpu load of file is getting too big
/*
if(!JSON.stringify(db.get("messages")).includes(item.uuid)){
console.log("new message: " + item);
db.get("messages").push(item).save();
}
*/
db.put({
_id: item.uuid,
timestamp: item.timestamp,
uuid: item.uuid,
dxcallsign: item.dxcallsign,
dxgrid: item.dxgrid,
msg: item.msg,
checksum: item.checksum,
type : "received",
command : item.command,
status : item.status
}).then(function (response) {
// handle response
console.log("new database entry");
console.log(response);
}).catch(function (err) {
console.log(err);
});
db.get(item.uuid).then(function (doc) {
// handle doc
// timestamp
update_chat(doc);
}).catch(function (err) {
console.log(err);
});
console.log("...................................")
return
});
});
// Update chat list
update_chat = function(obj) {
console.log(obj);
var dxcallsign = obj.dxcallsign;
var timestamp = dateFormat.format(obj.timestamp * 1000);
var dxgrid = obj.dxgrid;
// CALLSIGN LIST
if(!(document.getElementById('chat-' + dxcallsign + '-list'))){
var new_callsign = `
${dxcallsign}
${dxgrid}
${timestamp}
${obj.msg}
${timestamp}