From 53767f27cc2ef0fe8127b153440b9edddcdd9aa9 Mon Sep 17 00:00:00 2001 From: n9lsjr Date: Thu, 28 Nov 2024 04:43:37 +0100 Subject: [PATCH] Add get latest room hashes and room message exists to db --- src/backend/database.cjs | 43 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/backend/database.cjs b/src/backend/database.cjs index 1fa9191b..c8c0b5a0 100644 --- a/src/backend/database.cjs +++ b/src/backend/database.cjs @@ -850,6 +850,47 @@ const groupMessageExists = async (time) => { } resolve(exists) }) +} + +const roomMessageExists = async (hash) => { + let exists = false + return new Promise((resolve, reject) => { + const hashExists = + `SELECT * + FROM groupmessages + WHERE hash = '${hash}' + ` + const row = database.prepare(hashExists).get() + if(row) { + exists = true + } + resolve(exists) + }) +} + +const getLatestRoomHashes = async (key) => { + const offset = 0 + const limit = 25 + const hashes = [] + return new Promise((resolve, reject) => { + const latestRoomHashes = + `SELECT * + FROM groupmessages + WHERE grp = ? + ORDER BY + time + DESC + LIMIT ${offset}, ${limit}` + + const hashList = database.prepare(latestRoomHashes) + for(const row of hashList.iterate(key)) { + if(row) { + if (row.message === "Joined room") continue + hashes.push(row.hash) + } + } + resolve(hashes) + }) } @@ -881,4 +922,4 @@ process.on('SIGINT', async () => process.exit(128 + 2)); process.on('SIGTERM', async () => process.exit(128 + 15)); -module.exports = {saveHash, loadRoomKeys, removeRoom, getRooms ,addRoomKeys, firstContact, welcomeMessage, loadDB, loadGroups, loadRooms, loadKeys, getGroups, saveGroupMsg, unBlockContact, blockContact, removeMessages, removeContact, removeGroup, addGroup, loadBlockList, getConversation, getConversations, loadKnownTxs, getMessages, getGroupReply, printGroup, saveMsg, saveThisContact, groupMessageExists, messageExists, getContacts, getChannels, deleteMessage, addRoom} +module.exports = {saveHash, roomMessageExists, getLatestRoomHashes, loadRoomKeys, removeRoom, getRooms ,addRoomKeys, firstContact, welcomeMessage, loadDB, loadGroups, loadRooms, loadKeys, getGroups, saveGroupMsg, unBlockContact, blockContact, removeMessages, removeContact, removeGroup, addGroup, loadBlockList, getConversation, getConversations, loadKnownTxs, getMessages, getGroupReply, printGroup, saveMsg, saveThisContact, groupMessageExists, messageExists, getContacts, getChannels, deleteMessage, addRoom}