Skip to content

Commit

Permalink
Fetch historical message data (#114)
Browse files Browse the repository at this point in the history
Fetch historical message data in Groups
  • Loading branch information
TechyGuy17 authored Dec 27, 2023
1 parent 54c4d32 commit f3c84a9
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 160 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ being awesome contributors to this project. **We'd like to take a moment to reco
[<img src="https://avatars.githubusercontent.com/u/33717111?v=4?size=72" alt="mjovanc" width="72">](https://github.com/mjovanc)
[<img src="https://avatars.githubusercontent.com/u/3246908?v=4?size=72" alt="f-r00t" width="72">](https://github.com/f-r00t)
[<img src="https://avatars.githubusercontent.com/u/24655747?v=4size=72" alt="appelskrutt34" width="72">](https://github.com/appelskrutt34)
[<img src="https://github.com/TechyGuy17.png?size=72" alt="TechyGuy17" width="72">](https://github.com/TechyGuy17)

# License

Expand Down
283 changes: 132 additions & 151 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion src/backend/electron.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const {
CryptoNote,
} = require('kryptokrona-utils')


const { newBeam, endBeam, sendBeamMessage, addLocalFile, requestDownload, removeLocalFile } = require("./beam.cjs")
const { newSwarm, sendSwarmMessage, endSwarm} = require("./swarm.cjs")
const Store = require('electron-store');
Expand All @@ -98,6 +99,7 @@ const dev = !app.isPackaged

const DHT = require('@hyperswarm/dht')


let mainWindow
let daemon
let nodeUrl
Expand Down Expand Up @@ -1653,7 +1655,6 @@ async function load_file(path) {
stream.on('data', (data) => {
imgArray.push(data)
})

stream.on('error', (data) => {
return "File not found"
})
Expand All @@ -1671,6 +1672,31 @@ async function load_file(path) {
}
}

async function syncGroupHistory(timeframe, recommended_api = undefined, 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) {
try {
let tx = {}
tx.sb = items[message].tx_sb
tx.t = items[message].tx_timestamp
await decryptGroupMessage(tx, items[message].tx_hash, key)

}
catch {
}
}
if(json.current_page != json.total_pages) {
syncGroupHistory(timeframe, recommended_api, key, page+1)
}
})
}


function get_sdp(data)
{
if (data.type == 'offer')
Expand Down Expand Up @@ -1795,6 +1821,11 @@ ipcMain.on('removeGroup', async (e, grp) => {
removeGroup(grp)
})

ipcMain.on('fetchHistory', async (e, settings) => {
let timeframe = Date.now() / 1000 - settings.timeframe * 86400
await syncGroupHistory(timeframe, settings.recommended_api)
})


//BOARDS

Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/popups/NodeSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
let node_requests = [];
let ssl_nodes =[];
if (ssl) {
ssl_nodes = $nodelist.filter(node => {return node.ssl});
ssl_nodes = $nodelist.nodes.filter(node => {return node.ssl});
} else {
ssl_nodes = $nodelist.filter(node => {return !node.ssl});
ssl_nodes = $nodelist.nodes.filter(node => {return !node.ssl});
}
ssl_nodes = ssl_nodes.sort((a, b) => 0.5 - Math.random());
Expand Down Expand Up @@ -88,7 +88,7 @@ if (recommended_node == undefined) {
<h1>Pick a node</h1>
<input spellcheck="false" type="text" placeholder="Enter url & port" bind:value="{nodeInput}"/>
<div class="node-list">
{#each $nodelist as node, i}
{#each $nodelist.nodes as node, i}
<div
class="node-card"
class:selected="{selectedNode === i}"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stores/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const fetchNodes = async () => {
if (result.nodes.length === 0) {
nodelist.set(standard.nodes)
}
nodelist.set(result.nodes)
nodelist.set(result)
} catch(e) {
nodelist.set(standard.nodes)
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/stores/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const groups = writable({
thisGroup: {key: "SEKReYU57DLLvUjNzmjVhaK7jqc8SdZZ3cyKJS5f4gWXK4NQQYChzKUUwzCGhgqUPkWQypeR94rqpgMPjXWG9ijnZKNw2LWXnZU1", chat: false},
groupArray: [],
blockList: [],
activeHugins: []
activeHugins: [],
historyTimeframe: 0
})

export const rtc_groups = writable({
Expand Down
31 changes: 31 additions & 0 deletions src/routes/groups/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import BlockContact from '$lib/components/chat/BlockContact.svelte'
import { containsOnlyEmojis, sleep } from '$lib/utils/utils'
import Loader from '$lib/components/popups/Loader.svelte'
import GroupHugins from '$lib/components/chat/GroupHugins.svelte'
import { nodelist } from '$lib/stores/nodes.js'
let replyto = ''
let reply_exit_icon = 'x'
Expand Down Expand Up @@ -248,6 +249,11 @@ const addNewGroup = async (e) => {
}
$groupMessages = []
window.api.addGroup(add)
await sleep(100)
let settings = {}
settings.timeframe = 14
settings.recommended_api = await getBestApi()
window.api.send('fetchHistory', settings)
await sleep(200)
printGroup(group)
}
Expand Down Expand Up @@ -408,6 +414,31 @@ function addHash(data) {
console.log("Active channel messages", $swarm.activeChannelMessages)
//await checkReactions(channel)
}
async function getBestApi() {
let apis = $nodelist.apis
let recommended_api = undefined
apis = apis.sort((a, b) => 0.5 - Math.random())
for(const item in apis) {
let this_api = apis[item]
let apiURL = `${this_api.url}/api/v1/posts-encrypted-group`
try {
const resp = await fetch(apiURL, {
method: 'GET'
}, 1000);
if (resp.ok) {
console.log("selecting " + this_api )
recommended_api = this_api;
return(this_api);
}
} catch (e) {
console.log(e);
}
}
}
</script>

Expand Down
81 changes: 78 additions & 3 deletions src/routes/settings/messagesettings/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
<script>
import { fade } from "svelte/transition"
import { messageWallet } from '$lib/stores/user.js'
import { messageWallet, groups } from '$lib/stores/user.js'
import { nodelist } from '$lib/stores/nodes.js'
import Button from "$lib/components/buttons/Button.svelte"
const optimizeMessages = () => {
window.api.send('optimize')
}
let timeframeDays;
async function getBestApi() {
let apis = $nodelist.apis
let recommended_api = undefined
apis = apis.sort((a, b) => 0.5 - Math.random())
for(const item in apis) {
let this_api = apis[item]
let apiURL = `${this_api.url}/api/v1/posts-encrypted-group`
try {
const resp = await fetch(apiURL, {
method: 'GET'
}, 1000);
if (resp.ok) {
console.log("selecting " + this_api )
recommended_api = this_api;
return(this_api);
}
} catch (e) {
console.log(e);
}
}
}
const fetchHistory = async () => {
let settings = {}
settings.timeframe = timeframeDays
settings.recommended_api = await getBestApi()
window.api.send('fetchHistory', settings)
}
</script>

<h2>Messages</h2>
Expand All @@ -28,7 +66,23 @@
Press the button <b>Optimize inputs</b> to create more.</p>
<br>
</div>

<h2>Message History</h2>
<div class="history">
<input spellcheck="false" type="number" placeholder="Enter amount of days" bind:value="{timeframeDays}"/>
<Button
text="Fetch history"
disabled="{false}"
on:click="{fetchHistory}"
/>
</div>
<div class="settings" in:fade>
<p>Here you can set the timeframe of message history you want to fetch.
<br>
This will fetch the data from a Hugin API.</p>
<br>
<p>This might cause added bandwith and storage usage.
<br>
</div>
<style>
.settings {
Expand All @@ -43,5 +97,26 @@
.optimize {
margin-left: -15px;
}
.history {
margin-left: -15px;
margin-top: 2rem;
}
input {
/* margin: 0 auto; */
margin-left: 15px;
max-width: 300px;
width: 100%;
height: 48px;
padding: 0 15px;
border-radius: 0.5rem;
transition: 200ms ease-in-out;
background-color: var(--card-background);
border: 1px solid var(--card-border);
color: var(--text-color);
font-size: 1.1rem;
</style>
/* &:focus {
outline: none;
} */
}
</style>

0 comments on commit f3c84a9

Please sign in to comment.