Skip to content

Commit

Permalink
feat: add negentropy sync to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed Nov 11, 2024
1 parent c93edde commit c5d06a2
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 142 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod account;
pub mod event;
pub mod metadata;
pub mod relay;
pub mod sync;
pub mod window;
71 changes: 71 additions & 0 deletions src-tauri/src/commands/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use nostr_sdk::prelude::*;
use std::collections::HashSet;
use tauri::State;

use crate::Nostr;

#[tauri::command]
#[specta::specta]
pub async fn sync_all(
state: State<'_, Nostr>,
reader: tauri::ipc::Channel<f64>,
) -> Result<(), String> {
let client = &state.client;

// Create a filter for get all public keys
let filter = Filter::new().kinds(vec![
Kind::TextNote,
Kind::Repost,
Kind::FollowSet,
Kind::ContactList,
Kind::MuteList,
]);

let events = client
.database()
.query(vec![filter])
.await
.map_err(|err| err.to_string())?;

let public_keys: Vec<PublicKey> = events
.iter()
.flat_map(|ev| ev.tags.public_keys().copied())
.collect::<HashSet<_>>()
.into_iter()
.collect();

let (tx, mut rx) = SyncProgress::channel();
let opts = SyncOptions::default().progress(tx);

tauri::async_runtime::spawn(async move {
while rx.changed().await.is_ok() {
let progress = *rx.borrow_and_update();

if progress.total > 0 {
reader.send(progress.percentage() * 100.0).unwrap();
}
}
});

for chunk in public_keys.chunks(200) {
let authors = chunk.to_owned();
let filter = Filter::new().authors(authors).kinds(vec![
Kind::Metadata,
Kind::ContactList,
Kind::FollowSet,
Kind::Interests,
Kind::InterestSet,
Kind::EventDeletion,
Kind::TextNote,
Kind::Repost,
Kind::Comment,
]);

let _ = client
.sync(filter, &opts)
.await
.map_err(|err| err.to_string())?;
}

Ok(())
}
5 changes: 4 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#[cfg(target_os = "macos")]
use border::WebviewWindowExt as BorderWebviewWindowExt;
use commands::{account::*, event::*, metadata::*, relay::*, window::*};
use commands::{account::*, event::*, metadata::*, relay::*, sync::*, window::*};
use common::{get_all_accounts, parse_event};
use nostr_sdk::prelude::{Profile as DatabaseProfile, *};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -76,6 +76,7 @@ fn main() {
tracing_subscriber::fmt::init();

let builder = Builder::<tauri::Wry>::new().commands(collect_commands![
sync_all,
get_all_relays,
get_all_relay_lists,
is_relay_connected,
Expand Down Expand Up @@ -365,6 +366,8 @@ fn main() {

// Set interval
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(600));
// Skip the first tick
interval.tick().await;

loop {
interval.tick().await;
Expand Down
9 changes: 9 additions & 0 deletions src/commands.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@


export const commands = {
async syncAll(reader: TAURI_CHANNEL<number>) : Promise<Result<null, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("sync_all", { reader }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getAllRelays() : Promise<Result<string[], string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("get_all_relays") };
Expand Down Expand Up @@ -554,6 +562,7 @@ export type Meta = { content: string; images: string[]; events: string[]; mentio
export type NewWindow = { label: string; title: string; url: string; width: number; height: number; maximizable: boolean; minimizable: boolean; hidden_title: boolean; closable: boolean }
export type RichEvent = { raw: string; parsed: Meta | null }
export type Settings = { resize_service: boolean; content_warning: boolean; display_avatar: boolean; display_zap_button: boolean; display_repost_button: boolean; display_media: boolean }
export type TAURI_CHANNEL<TSend> = null

/** tauri-specta globals **/

Expand Down
87 changes: 57 additions & 30 deletions src/routes.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Route as AppIndexImport } from './routes/_app/index'
import { Route as ZapIdImport } from './routes/zap.$id'
import { Route as SettingsWalletImport } from './routes/settings/wallet'
import { Route as SettingsRelaysImport } from './routes/settings/relays'
import { Route as SettingsGeneralImport } from './routes/settings/general'
import { Route as ColumnsLayoutImport } from './routes/columns/_layout'
import { Route as IdSetProfileImport } from './routes/$id.set-profile'
import { Route as IdSetInterestImport } from './routes/$id.set-interest'
Expand All @@ -39,6 +38,8 @@ import { Route as ColumnsLayoutCreateNewsfeedF2fImport } from './routes/columns/
const ColumnsImport = createFileRoute('/columns')()
const SettingsLazyImport = createFileRoute('/settings')()
const NewLazyImport = createFileRoute('/new')()
const SettingsSyncLazyImport = createFileRoute('/settings/sync')()
const SettingsGeneralLazyImport = createFileRoute('/settings/general')()
const NewAccountWatchLazyImport = createFileRoute('/new-account/watch')()
const NewAccountImportLazyImport = createFileRoute('/new-account/import')()
const NewAccountConnectLazyImport = createFileRoute('/new-account/connect')()
Expand Down Expand Up @@ -118,6 +119,20 @@ const AppIndexRoute = AppIndexImport.update({
getParentRoute: () => AppRoute,
} as any).lazy(() => import('./routes/_app/index.lazy').then((d) => d.Route))

const SettingsSyncLazyRoute = SettingsSyncLazyImport.update({
id: '/sync',
path: '/sync',
getParentRoute: () => SettingsLazyRoute,
} as any).lazy(() => import('./routes/settings/sync.lazy').then((d) => d.Route))

const SettingsGeneralLazyRoute = SettingsGeneralLazyImport.update({
id: '/general',
path: '/general',
getParentRoute: () => SettingsLazyRoute,
} as any).lazy(() =>
import('./routes/settings/general.lazy').then((d) => d.Route),
)

const NewAccountWatchLazyRoute = NewAccountWatchLazyImport.update({
id: '/new-account/watch',
path: '/new-account/watch',
Expand Down Expand Up @@ -164,14 +179,6 @@ const SettingsRelaysRoute = SettingsRelaysImport.update({
import('./routes/settings/relays.lazy').then((d) => d.Route),
)

const SettingsGeneralRoute = SettingsGeneralImport.update({
id: '/general',
path: '/general',
getParentRoute: () => SettingsLazyRoute,
} as any).lazy(() =>
import('./routes/settings/general.lazy').then((d) => d.Route),
)

const ColumnsLayoutRoute = ColumnsLayoutImport.update({
id: '/_layout',
getParentRoute: () => ColumnsRoute,
Expand Down Expand Up @@ -440,13 +447,6 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof ColumnsLayoutImport
parentRoute: typeof ColumnsRoute
}
'/settings/general': {
id: '/settings/general'
path: '/general'
fullPath: '/settings/general'
preLoaderRoute: typeof SettingsGeneralImport
parentRoute: typeof SettingsLazyImport
}
'/settings/relays': {
id: '/settings/relays'
path: '/relays'
Expand Down Expand Up @@ -489,6 +489,20 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof NewAccountWatchLazyImport
parentRoute: typeof rootRoute
}
'/settings/general': {
id: '/settings/general'
path: '/general'
fullPath: '/settings/general'
preLoaderRoute: typeof SettingsGeneralLazyImport
parentRoute: typeof SettingsLazyImport
}
'/settings/sync': {
id: '/settings/sync'
path: '/sync'
fullPath: '/settings/sync'
preLoaderRoute: typeof SettingsSyncLazyImport
parentRoute: typeof SettingsLazyImport
}
'/_app/': {
id: '/_app/'
path: '/'
Expand Down Expand Up @@ -666,15 +680,17 @@ const AppRouteChildren: AppRouteChildren = {
const AppRouteWithChildren = AppRoute._addFileChildren(AppRouteChildren)

interface SettingsLazyRouteChildren {
SettingsGeneralRoute: typeof SettingsGeneralRoute
SettingsRelaysRoute: typeof SettingsRelaysRoute
SettingsWalletRoute: typeof SettingsWalletRoute
SettingsGeneralLazyRoute: typeof SettingsGeneralLazyRoute
SettingsSyncLazyRoute: typeof SettingsSyncLazyRoute
}

const SettingsLazyRouteChildren: SettingsLazyRouteChildren = {
SettingsGeneralRoute: SettingsGeneralRoute,
SettingsRelaysRoute: SettingsRelaysRoute,
SettingsWalletRoute: SettingsWalletRoute,
SettingsGeneralLazyRoute: SettingsGeneralLazyRoute,
SettingsSyncLazyRoute: SettingsSyncLazyRoute,
}

const SettingsLazyRouteWithChildren = SettingsLazyRoute._addFileChildren(
Expand Down Expand Up @@ -768,13 +784,14 @@ export interface FileRoutesByFullPath {
'/$id/set-interest': typeof IdSetInterestRoute
'/$id/set-profile': typeof IdSetProfileRoute
'/columns': typeof ColumnsLayoutRouteWithChildren
'/settings/general': typeof SettingsGeneralRoute
'/settings/relays': typeof SettingsRelaysRoute
'/settings/wallet': typeof SettingsWalletRoute
'/zap/$id': typeof ZapIdRoute
'/new-account/connect': typeof NewAccountConnectLazyRoute
'/new-account/import': typeof NewAccountImportLazyRoute
'/new-account/watch': typeof NewAccountWatchLazyRoute
'/settings/general': typeof SettingsGeneralLazyRoute
'/settings/sync': typeof SettingsSyncLazyRoute
'/': typeof AppIndexRoute
'/new-post': typeof NewPostIndexRoute
'/columns/create-newsfeed': typeof ColumnsLayoutCreateNewsfeedRouteWithChildren
Expand Down Expand Up @@ -807,13 +824,14 @@ export interface FileRoutesByTo {
'/$id/set-interest': typeof IdSetInterestRoute
'/$id/set-profile': typeof IdSetProfileRoute
'/columns': typeof ColumnsLayoutRouteWithChildren
'/settings/general': typeof SettingsGeneralRoute
'/settings/relays': typeof SettingsRelaysRoute
'/settings/wallet': typeof SettingsWalletRoute
'/zap/$id': typeof ZapIdRoute
'/new-account/connect': typeof NewAccountConnectLazyRoute
'/new-account/import': typeof NewAccountImportLazyRoute
'/new-account/watch': typeof NewAccountWatchLazyRoute
'/settings/general': typeof SettingsGeneralLazyRoute
'/settings/sync': typeof SettingsSyncLazyRoute
'/': typeof AppIndexRoute
'/new-post': typeof NewPostIndexRoute
'/columns/create-newsfeed': typeof ColumnsLayoutCreateNewsfeedRouteWithChildren
Expand Down Expand Up @@ -849,13 +867,14 @@ export interface FileRoutesById {
'/$id/set-profile': typeof IdSetProfileRoute
'/columns': typeof ColumnsRouteWithChildren
'/columns/_layout': typeof ColumnsLayoutRouteWithChildren
'/settings/general': typeof SettingsGeneralRoute
'/settings/relays': typeof SettingsRelaysRoute
'/settings/wallet': typeof SettingsWalletRoute
'/zap/$id': typeof ZapIdRoute
'/new-account/connect': typeof NewAccountConnectLazyRoute
'/new-account/import': typeof NewAccountImportLazyRoute
'/new-account/watch': typeof NewAccountWatchLazyRoute
'/settings/general': typeof SettingsGeneralLazyRoute
'/settings/sync': typeof SettingsSyncLazyRoute
'/_app/': typeof AppIndexRoute
'/new-post/': typeof NewPostIndexRoute
'/columns/_layout/create-newsfeed': typeof ColumnsLayoutCreateNewsfeedRouteWithChildren
Expand Down Expand Up @@ -891,13 +910,14 @@ export interface FileRouteTypes {
| '/$id/set-interest'
| '/$id/set-profile'
| '/columns'
| '/settings/general'
| '/settings/relays'
| '/settings/wallet'
| '/zap/$id'
| '/new-account/connect'
| '/new-account/import'
| '/new-account/watch'
| '/settings/general'
| '/settings/sync'
| '/'
| '/new-post'
| '/columns/create-newsfeed'
Expand Down Expand Up @@ -929,13 +949,14 @@ export interface FileRouteTypes {
| '/$id/set-interest'
| '/$id/set-profile'
| '/columns'
| '/settings/general'
| '/settings/relays'
| '/settings/wallet'
| '/zap/$id'
| '/new-account/connect'
| '/new-account/import'
| '/new-account/watch'
| '/settings/general'
| '/settings/sync'
| '/'
| '/new-post'
| '/columns/create-newsfeed'
Expand Down Expand Up @@ -969,13 +990,14 @@ export interface FileRouteTypes {
| '/$id/set-profile'
| '/columns'
| '/columns/_layout'
| '/settings/general'
| '/settings/relays'
| '/settings/wallet'
| '/zap/$id'
| '/new-account/connect'
| '/new-account/import'
| '/new-account/watch'
| '/settings/general'
| '/settings/sync'
| '/_app/'
| '/new-post/'
| '/columns/_layout/create-newsfeed'
Expand Down Expand Up @@ -1068,9 +1090,10 @@ export const routeTree = rootRoute
"/settings": {
"filePath": "settings.lazy.tsx",
"children": [
"/settings/general",
"/settings/relays",
"/settings/wallet"
"/settings/wallet",
"/settings/general",
"/settings/sync"
]
},
"/$id/set-group": {
Expand Down Expand Up @@ -1113,10 +1136,6 @@ export const routeTree = rootRoute
"/columns/_layout/users/$id"
]
},
"/settings/general": {
"filePath": "settings/general.tsx",
"parent": "/settings"
},
"/settings/relays": {
"filePath": "settings/relays.tsx",
"parent": "/settings"
Expand All @@ -1137,6 +1156,14 @@ export const routeTree = rootRoute
"/new-account/watch": {
"filePath": "new-account/watch.lazy.tsx"
},
"/settings/general": {
"filePath": "settings/general.lazy.tsx",
"parent": "/settings"
},
"/settings/sync": {
"filePath": "settings/sync.lazy.tsx",
"parent": "/settings"
},
"/_app/": {
"filePath": "_app/index.tsx",
"parent": "/_app"
Expand Down
4 changes: 2 additions & 2 deletions src/routes/_app.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function Account({ pubkey }: { pubkey: string }) {
const items = await Promise.all([
MenuItem.new({
text: "Unlock",
enabled: !isActive || true,
enabled: !isActive,
action: async () => await commands.setSigner(pubkey),
}),
PredefinedMenuItem.new({ item: "Separator" }),
Expand Down Expand Up @@ -183,7 +183,7 @@ function Account({ pubkey }: { pubkey: string }) {

await menu.popup().catch((e) => console.error(e));
},
[pubkey],
[isActive, pubkey],
);

useEffect(() => {
Expand Down
Loading

0 comments on commit c5d06a2

Please sign in to comment.