blowater/app/UI/relay-config.ts

113 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-01-17 08:37:29 +00:00
import { NostrAccountContext } from "../../libs/nostr.ts/nostr.ts";
import { ConnectionPool } from "../../libs/nostr.ts/relay-pool.ts";
import { parseJSON } from "../features/profile.ts";
2024-01-01 17:28:10 +00:00
import { SingleRelayConnection } from "../../libs/nostr.ts/relay-single.ts";
2023-07-09 07:06:13 +00:00
export const default_blowater_relay = "wss://blowater.nostr1.com";
export const recommendedRelays = [
default_blowater_relay,
2023-09-02 23:38:24 +00:00
"wss://nos.lol",
"wss://relay.damus.io",
"wss://relay.nostr.wirednet.jp",
"wss://yabu.me",
"wss://relay.nostr.band",
"wss://bevo.nostr1.com",
"wss://island.nostr1.com",
"wss://vitor.nostr1.com",
"wss://frens.nostr1.com",
"wss://shawn.nostr1.com",
"wss://fiatjaf.nostr1.com",
"wss://nostrdevs.nostr1.com",
2023-06-30 14:05:57 +00:00
];
export class RelayConfig {
2023-11-18 17:11:07 +00:00
private readonly ctx: NostrAccountContext;
2023-11-25 05:25:49 +00:00
private readonly relayPool: ConnectionPool;
2023-11-18 17:11:07 +00:00
2023-11-14 06:22:50 +00:00
private constructor(
2023-11-18 17:11:07 +00:00
args: {
ctx: NostrAccountContext;
2023-11-25 05:25:49 +00:00
relayPool: ConnectionPool;
2023-11-18 17:11:07 +00:00
},
) {
this.ctx = args.ctx;
this.relayPool = args.relayPool;
}
2023-11-25 05:25:49 +00:00
static Default(args: { ctx: NostrAccountContext; relayPool: ConnectionPool }) {
const config = new RelayConfig(args);
config.add(default_blowater_relay).then((res) => {
if (res instanceof Error) {
console.error(res);
}
});
return config;
}
// The the relay config of this account from local storage
2023-11-18 17:11:07 +00:00
static async FromLocalStorage(args: {
ctx: NostrAccountContext;
2023-11-25 05:25:49 +00:00
relayPool: ConnectionPool;
2023-11-18 17:11:07 +00:00
}) {
const encodedConfigStr = localStorage.getItem(this.localStorageKey(args.ctx));
if (encodedConfigStr == null) {
return RelayConfig.Default(args);
2023-11-15 13:21:45 +00:00
}
let relayArray = parseJSON<string[]>(encodedConfigStr);
if (relayArray instanceof Error) {
console.log(relayArray.message);
relayArray = [];
}
2023-11-18 17:11:07 +00:00
const relayConfig = new RelayConfig(args);
2023-11-15 13:21:45 +00:00
for (const relay of relayArray) {
2023-11-18 17:11:07 +00:00
const err = await relayConfig.add(relay);
if (err instanceof Error) {
console.error(err);
}
2023-11-14 06:22:50 +00:00
}
return relayConfig;
}
static localStorageKey(ctx: NostrAccountContext) {
return `${RelayConfig.name}-${ctx.publicKey.bech32()}`;
}
getRelayURLs() {
2023-11-25 05:25:49 +00:00
return new Set(Array.from(this.relayPool.getRelays()).map((r) => r.url));
}
2023-11-18 17:11:07 +00:00
saveToLocalStorage() {
console.log(RelayConfig.name, ":: saveToLocalStorage");
2023-11-25 05:25:49 +00:00
localStorage.setItem(
RelayConfig.localStorageKey(this.ctx),
JSON.stringify(Array.from(this.relayPool.getRelays()).map((r) => r.url)),
);
}
2023-11-15 13:21:45 +00:00
async add(url: string): Promise<Error | SingleRelayConnection> {
2023-11-18 17:11:07 +00:00
console.log(RelayConfig.name, ":: add relay config", url);
2023-11-15 13:21:45 +00:00
const relay = await this.relayPool.addRelayURL(url);
if (relay instanceof Error) {
return relay;
}
2023-11-18 17:11:07 +00:00
this.saveToLocalStorage();
2023-11-15 13:21:45 +00:00
return relay;
}
async remove(url: string) {
if (url == default_blowater_relay) {
return new RemoveBlowaterRelay();
}
2023-11-18 17:11:07 +00:00
await this.relayPool.removeRelay(url);
this.saveToLocalStorage();
}
}
export function applyPoolToRelayConfig(pool: ConnectionPool, relayConfig: RelayConfig) {
for (const relay of pool.getRelays()) {
relayConfig.add(relay.url);
}
2023-06-30 14:05:57 +00:00
}
export class RemoveBlowaterRelay extends Error {}