Customize Bootstrap Relays (#205)

* feat: add bootstrap relays file

* feat: add save bootstrap relays command

* feat: add customize bootstrap relays screen
This commit is contained in:
雨宮蓮
2024-06-10 10:48:39 +07:00
committed by GitHub
parent b396c8a695
commit 90342c552f
9 changed files with 902 additions and 448 deletions

View File

@@ -1,8 +1,13 @@
use std::{
fs,
io::{self, BufRead, Write},
};
use crate::Nostr;
use nostr_sdk::prelude::*;
use serde::Serialize;
use specta::Type;
use tauri::State;
use tauri::{path::BaseDirectory, Manager, State};
#[derive(Serialize, Type)]
pub struct Relays {
@@ -103,3 +108,42 @@ pub async fn remove_relay(relay: &str, state: State<'_, Nostr>) -> Result<bool,
Ok(false)
}
}
#[tauri::command]
#[specta::specta]
pub fn get_bootstrap_relays(app: tauri::AppHandle) -> Result<Vec<String>, ()> {
let relays_path = app
.path()
.resolve("resources/relays.txt", BaseDirectory::Resource)
.expect("Bootstrap relays not found.");
let file = std::fs::File::open(&relays_path).unwrap();
let lines = io::BufReader::new(file).lines();
let mut relays = Vec::new();
for line in lines.flatten() {
relays.push(line.to_string())
}
Ok(relays)
}
#[tauri::command]
#[specta::specta]
pub fn save_bootstrap_relays(relays: &str, app: tauri::AppHandle) -> Result<(), String> {
let relays_path = app
.path()
.resolve("resources/relays.txt", BaseDirectory::Resource)
.expect("Bootstrap relays not found.");
let mut file = fs::OpenOptions::new()
.write(true)
.open(&relays_path)
.unwrap();
match file.write_all(relays.as_bytes()) {
Ok(_) => Ok(()),
Err(_) => Err("Cannot save bootstrap relays, please try again later.".into()),
}
}