Skip to content

Commit

Permalink
Add message que and load more button for groupmessages
Browse files Browse the repository at this point in the history
  • Loading branch information
n9lsjr committed Mar 1, 2024
1 parent ebdc647 commit a680a73
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 141 deletions.
9 changes: 6 additions & 3 deletions src/backend/database.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,11 @@ const getConversation = async (chat) => {
}

//Print a chosen group from the shared key.
const printGroup = async (group) => {
const printGroup = async (group, page) => {
const channels = await getChannels()

let limit = 10
let offset = 0
if (page !== 0) offset = page * limit
const thisGroup = []
return new Promise((resolve, reject) => {
const getGroup = `SELECT
Expand All @@ -664,7 +666,8 @@ const printGroup = async (group) => {
WHERE grp = ?
ORDER BY
time
DESC`
DESC
LIMIT ${offset}, ${limit}`
const stmt = database.prepare(getGroup)

for(const row of stmt.iterate(group)) {
Expand Down
6 changes: 3 additions & 3 deletions src/backend/electron.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const { expand_sdp_offer, parse_sdp } = require("./sdp.cjs")
const { loadHugin, loadAccount, loadWallet, createAccount } = require('./wallet.cjs')
const { new_beam } = require("./beam.cjs")
const { new_swarm, end_swarm} = require("./swarm.cjs")
const { sendMessage, startMessageSyncer } = require('./messages.cjs')
const { sendMessage, start_message_syncer } = require('./messages.cjs')
const { keychain } = require('./crypto.cjs')

let mainWindow
Expand Down Expand Up @@ -236,7 +236,7 @@ const startCheck = async () => {
//A misc database exits, probably we have an account
loadHugin(mainWindow)
ipcMain.on('login', async (event, data) => {
if (await loadAccount(data)) startMessageSyncer()
if (await loadAccount(data)) start_message_syncer()
})
} else {
//No wallet found, probably first start
Expand All @@ -261,7 +261,7 @@ const { desktopCapturer } = require('electron')
}

ipcMain.on('create-account', async (e, accountData) => {
if(await createAccount(accountData)) startMessageSyncer()
if(await createAccount(accountData)) start_message_syncer()
})

//BEAM
Expand Down
69 changes: 50 additions & 19 deletions src/backend/messages.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ ipcMain.handle('getGroups', async (e) => {
return groups.reverse()
})

ipcMain.handle('printGroup', async (e, grp) => {
return await printGroup(grp)
ipcMain.handle('printGroup', async (e, grp, page) => {
return await printGroup(grp, page)
})

ipcMain.handle('getGroupReply', async (e, data) => {
Expand Down Expand Up @@ -155,17 +155,17 @@ ipcMain.on('decrypt_rtc_group_message', async (e, message, key) => {
})


const startMessageSyncer = async () => {
const start_message_syncer = async () => {
//Load knownTxsIds to backgroundSyncMessages on startup
known_keys = Hugin.known_keys
block_list = Hugin.block_list
backgroundSyncMessages(await loadCheckedTxs())
background_sync_messages(await loadCheckedTxs())
while (true) {
try {
//Start syncing
await sleep(1000 * 10)
await sleep(1000 * 5)

backgroundSyncMessages()
await background_sync_messages()

const [walletBlockCount, localDaemonBlockCount, networkBlockCount] = await Hugin.wallet.getSyncStatus()

Expand Down Expand Up @@ -197,26 +197,56 @@ const startMessageSyncer = async () => {
}
}

async function backgroundSyncMessages(checkedTxs = false) {
let incoming_messages = []
const incoming = () => { return incoming_messages.length > 0 ? true : false}

async function background_sync_messages(checkedTxs = false) {
console.log('Background syncing...')

//First start, set known pool txs
if (checkedTxs) {
known_pool_txs = await setKnownPoolTxs(checkedTxs)
known_pool_txs = await set_known_pooltxs(checkedTxs)
}

let transactions = await fetchHuginMessages()
if (!transactions) return
decryptHuginMessages(transactions)
const transactions = await fetch_hugin_messages()
if (!transactions && !incoming) return
const large_batch = transactions.length > 99

if (large_batch || (large_batch && incoming)) {
//Add to que
console.log("Adding que:", transactions.length)
const known_hashes = transactions.map(a => {return a.transactionPrefixInfotxHash})
incoming_messages = transactions
known_pool_txs = [...known_pool_txs, ...known_hashes]
Hugin.send('incoming-que', true)
}

if(incoming) {
console.log("Checking incoming messages:", incoming_messages.length)
decrypt_hugin_messages(decrypt = update_que(), true)
return
}

Hugin.send('incoming-que', false)
console.log("Incoming transactions", transactions.length)
decrypt_hugin_messages(transactions, false)
}

function update_que() {
const decrypt = incoming_messages.slice(0,99)
const update = incoming_messages.slice(decrypt.length)
incoming_messages = update
return decrypt
}

async function decryptHuginMessages(transactions) {
async function decrypt_hugin_messages(transactions, que = false) {
console.log("Checking nr of txs:", transactions.length)
for (const transaction of transactions) {
try {
let thisExtra = transaction.transactionPrefixInfo.extra
let thisHash = transaction.transactionPrefixInfotxHash
if (!validateExtra(thisExtra, thisHash)) continue

if (!validateExtra(thisExtra, thisHash, que)) continue
if (thisExtra !== undefined && thisExtra.length > 200) {
if (!saveHash(thisHash)) continue
//Check for viewtag
Expand Down Expand Up @@ -285,14 +315,15 @@ async function checkForGroupMessage(thisExtra, thisHash) {
}

//Validate extradata, here we can add more conditions
function validateExtra(thisExtra, thisHash) {
function validateExtra(thisExtra, thisHash, que) {
//Extra too long
if (thisExtra.length > 7000) {
known_pool_txs.push(thisHash)
if (!saveHash(thisHash)) return false
return false;
}
//Check if known tx
//Check if known tx, if que is true we already know it but should check anyway
if (que) return true
if (known_pool_txs.indexOf(thisHash) === -1) {
known_pool_txs.push(thisHash)
return true
Expand Down Expand Up @@ -322,7 +353,7 @@ async function loadCheckedTxs() {


//Set known pool txs on start
function setKnownPoolTxs(checkedTxs) {
function set_known_pooltxs(checkedTxs) {
//Here we can adjust number of known we send to the node
known_pool_txs = checkedTxs
//Can't send undefined to node, it wont respond
Expand All @@ -331,7 +362,7 @@ function setKnownPoolTxs(checkedTxs) {
}


async function fetchHuginMessages() {
async function fetch_hugin_messages() {
const node = Hugin.node
try {
const resp = await fetch(
Expand Down Expand Up @@ -362,6 +393,7 @@ async function fetchHuginMessages() {

} catch (e) {
Hugin.send('sync', 'Error')
console.log("Sync error", e)
return false
}
}
Expand Down Expand Up @@ -780,7 +812,6 @@ async function syncGroupHistory(timeframe, recommended_api, key=false, page=1) {
fetch(`${recommended_api.url}/api/v1/posts-encrypted-group?from=${timeframe}&to=${Date.now() / 1000}&size=50&page=` + page)
.then((response) => response.json())
.then(async (json) => {
console.log(timeframe + " " + key)
const items = json.encrypted_group_posts;

for (message in items) {
Expand Down Expand Up @@ -1014,4 +1045,4 @@ ipcMain.on('fetchGroupHistory', async (e, settings) => {
await syncGroupHistory(timeframe, settings.recommended_api, settings.key)
})

module.exports = {checkHistory, saveMessage, startMessageSyncer, sendMessage, optimizeMessages}
module.exports = {checkHistory, saveMessage, start_message_syncer, sendMessage, optimizeMessages}
4 changes: 2 additions & 2 deletions src/backend/preload.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ const WINDOW_API = {
let resp = await ipcRenderer.invoke('getGroups')
return resp
},
printGroup: async (grp) => {
let resp = await ipcRenderer.invoke('printGroup', grp)
printGroup: async (grp, page) => {
let resp = await ipcRenderer.invoke('printGroup', grp, page)
return resp
},

Expand Down
1 change: 0 additions & 1 deletion src/backend/wallet.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const store = new Store()
const {hash, sleep} = require('./utils.cjs')
const { welcomeMessage, firstContact} = require("./database.cjs")
const { Hugin } = require('./account.cjs')
const { startMessageSyncer } = require('./messages.cjs')
const { keychain } = require('./crypto.cjs')

let js_wallet
Expand Down
3 changes: 0 additions & 3 deletions src/lib/components/chat/GroupHugins.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ const disconnect_from_swarm = async () => {
let firstConnect = false
$: console.log("$thisswarm", thisSwarm)
$: console.log("$thisswarm", $groups.thisGroup)
const connecto_to_swarm = () => {
if (!window.localStorage.getItem('swarm-info')) {
$swarm.showInfo = true
Expand Down
1 change: 1 addition & 0 deletions src/lib/stores/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const notify = writable({
success: [],
unread: [],
update: [],
notifications: []
})

export const transactions = writable({
Expand Down
33 changes: 14 additions & 19 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,17 @@
})
window.api.receive('newBoardMessage', (data) => {
if (data.board === $boards.thisBoard && $page.url.pathname === '/boards') return
if ($boards.thisBoard === 'Home') return
if ($page.url.pathname !== '/boards') {
data.type = 'board'
$notify.unread.push(data)
$notify.unread = $notify.unread
}
new_messages = true
board_message_sound.play()
$notify.new.push(data)
$notify.new = $notify.new
})
window.api.receive('newGroupMessage', (data) => {
if (data.address == $user.myAddress) return
if (data.group === $groups.thisGroup.key && $page.url.pathname === '/groups' && $swarm.showVideoGrid && data.channel === "Chat room") return
if (data.group === $groups.thisGroup.key && $page.url.pathname === '/groups' && data.channel !== "Chat room") return
new_messages = true
data.key = data.address
if ($notify.new.length < 2) {
//Future notifications page
$notify.notifications.push(data)
if ($notify.new.length < 2 && !$notify.que) {
board_message_sound.play()
$notify.new.push(data)
}
Expand Down Expand Up @@ -397,6 +387,11 @@
updateUploadProgress(data)
})
window.api.receive('incoming-que', (data) => {
$notify.que = data
})
window.api.receive('checked', (data) => {
console.log("Got p2p data", data)
if (data) {
Expand Down Expand Up @@ -541,10 +536,10 @@

{#if $user.loggedIn && $notify.new.length > 0 && new_messages}
<div class="notifs">
{#if $notify.new.length < 2}
{#each $notify.new as notif}
<Notification on:hide="{removeNotification}" message="{notif}" error="{false}"/>
{/each}
{#if $notify.new.length < 2 && !$notify.que}
{#each $notify.new as notif}
<Notification on:hide="{removeNotification}" message="{notif}" error="{false}"/>
{/each}
{/if}

</div>
Expand Down
Loading

0 comments on commit a680a73

Please sign in to comment.