Added history processing.

This commit is contained in:
2026-02-21 10:41:51 -05:00
parent 504c78feba
commit e81b5e5b48
2 changed files with 49 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
#include "include/chat.h" #include "include/chat.h"
#include "include/data.h"
#include "include/session.h" #include "include/session.h"
#include <ctype.h> #include <ctype.h>
@@ -8,6 +9,38 @@
#include <time.h> #include <time.h>
#include <yyjson.h> #include <yyjson.h>
MsgData* chat_history[CHAT_HISTORY_SZ] = {NULL};
size_t chat_history_head = 0;
MsgData* chat_history_msg_add(MsgData* msg) {
if (chat_history_head < CHAT_HISTORY_SZ - 1) {
chat_history[chat_history_head] = msg;
chat_history_head++;
} else if (chat_history_head == CHAT_HISTORY_SZ - 1) {
chat_history[chat_history_head] = msg;
chat_history_head = 0;
} else {
exit(132); // Bad.
}
return msg;
}
MsgData** chat_history_nice(void) {
MsgData** msgs = calloc(CHAT_HISTORY_SZ, sizeof(MsgData*));
size_t i = chat_history_head, j = 0;
while (true) {
if (!chat_history[i]) break;
msgs[j] = chat_history[i];
i = (i + 1) % CHAT_HISTORY_SZ;
j++;
if (chat_history_head == i) break;
}
return msgs;
}
static size_t next_msg_id = 1; static size_t next_msg_id = 1;
// History buffer for last 100 messages. // History buffer for last 100 messages.

View File

@@ -1,7 +1,23 @@
#ifndef CHAT_H #ifndef CHAT_H
#define CHAT_H #define CHAT_H
#include "data.h"
#include <libwebsockets.h> #include <libwebsockets.h>
#include <stddef.h>
#include <stdbool.h>
// Message history (ring).
#define CHAT_HISTORY_SZ 128
extern MsgData* chat_history[CHAT_HISTORY_SZ];
extern size_t chat_history_head; // Points to the oldest message, or NULL.
// Add message to history ring.
MsgData* chat_history_msg_add(MsgData* msg);
// Get a list of messages in order.
MsgData** chat_history_nice(void);
/** /**
* cb_chat - libwebsockets protocol callback for COMS chat. * cb_chat - libwebsockets protocol callback for COMS chat.