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/data.h"
#include "include/session.h"
#include <ctype.h>
@@ -8,6 +9,38 @@
#include <time.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;
// History buffer for last 100 messages.