Skip to content

Commit

Permalink
separate bot and chat messages in the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jmicko committed Jul 17, 2024
1 parent 9c3f7b2 commit 9527d12
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
49 changes: 28 additions & 21 deletions server/modules/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { devLog } from "./utilities.js";
import { Coinbase } from "./coinbaseClient.js";
import { getAllErrorMessages, getAllMessages, saveMessage } from "./database/messages.js";
import { getAllErrorMessages, getAllMessages, getBotMessages, getChatMessages, saveMessage } from "./database/messages.js";
import { databaseClient } from "./databaseClient.js";

const botSettings = new class BotSettings {
Expand Down Expand Up @@ -191,6 +191,7 @@ class Messenger {
this.errors = new Array();
this.messages = new Array();
this.messageCount = Number(1);
this.chatMessages = new Array();
this.chatMessageCount = Number(1);
this.errorCount = Number(1);
}
Expand Down Expand Up @@ -226,10 +227,15 @@ class Messenger {
async saturateMessages() {
// get the messages from the database
const messages = await getAllMessages(this.userID);
const botMessages = await getBotMessages(this.userID);
const chatMessages = await getChatMessages(this.userID);
console.log(messages, 'all messages from database', this.userID);
// console.log(messages, 'all messages from database', this.userID);
// clear the messages array and add the messages from the database
this.messages.length = 0;
this.messages.push(...messages);
this.messages.push(...botMessages);
this.chatMessages.length = 0;
this.chatMessages.push(...chatMessages);
// set the message count to the length of the messages array
this.messageCount = this.messages.length;
// set the chat message count to the length of the chat messages array
Expand Down Expand Up @@ -258,22 +264,24 @@ class Messenger {
if (message.text) {
const saved = await saveMessage(this.userID, newMessage);
// console.log(saved, 'saved and returned from saveMessage');
this.messages.unshift(saved);
if (message.type === 'chat') {
this.chatMessages.unshift(saved);
this.chatMessageCount++;
// check and limit the number of stored messages
if (this.chatMessages.length > 1000) {
this.chatMessages.length = 1000;
}
} else {
this.messages.unshift(saved);
this.messageCount++;
// check and limit the number of stored messages
if (this.messages.length > 1000) {
this.messages.length = 1000;
}
}
fullMessage = saved;
// save the message to the database
}
// increase the counts
this.messageCount++;
if (message.type === 'chat') {
// only increase chat count if it is a chat type message
this.chatMessageCount++;
}


// check and limit the number of stored messages
// if (this.messages.length > 1000) {
// this.messages.length = 1000;
// }
// tell user to update messages
const jsonMsg = JSON.stringify(message);
console.log(jsonMsg, 'jsonMsg');
Expand All @@ -286,16 +294,15 @@ class Messenger {
// message will already be formatted as a message object
// add message to messages array if there is text to store
if (message.text) {
this.messages.unshift(message);
this.chatMessages.unshift(message);
// message is already in the database
}
// increase the counts
this.messageCount++;
this.chatMessageCount++;
// check and limit the number of stored messages
// if (this.messages.length > 1000) {
// this.messages.length = 1000;
// }
if (this.chatMessages.length > 1000) {
this.chatMessages.length = 1000;
}
// tell user to update messages
const jsonMsg = JSON.stringify(message);
this.sockets.forEach(socket => {
Expand All @@ -319,7 +326,7 @@ class Messenger {
// const messages = structuredClone(this.messages);
// extract the chats

this.messages.forEach(message => {
this.chatMessages.forEach(message => {
// console.log(message, 'message');
if (message.type === 'chat') {
chats.push(message);
Expand Down
17 changes: 17 additions & 0 deletions server/modules/database/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ export async function getAllMessages(userID) {
})
}

// get only the bot messages
export async function getBotMessages(userID) {
return new Promise(async (resolve, reject) => {
try {
const sqlText = `
SELECT *
FROM "messages"
WHERE ("user_id" = $1 OR ("to" = $2 OR "to" = 'all')) AND "type" != 'error' AND "type" != 'chat'
ORDER BY "timestamp" DESC LIMIT 1000;`;
const result = await pool.query(sqlText, [userID, userID.toString()]);
resolve(result.rows);
} catch (err) {
reject(err);
}
})
}

export async function getAllErrorMessages(userID) {
return new Promise(async (resolve, reject) => {
try {
Expand Down

0 comments on commit 9527d12

Please sign in to comment.