Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bob2402 committed Apr 29, 2024
1 parent 635cae1 commit 2194864
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion graphql-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const typeDefs = gql`
type RelayInformation {
name: String
description: String
pubkey: PublicKey!
pubkey: PublicKey
contact: String
supported_nips: [Int!]
software: String
Expand Down
19 changes: 16 additions & 3 deletions main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,24 @@ export const supported_nips = [1, 2];
export const software = "https://github.com/BlowaterNostr/relayed";

const landing_handler = async (args: { relayInformationStore: RelayInformationStore }) => {
const storeInformation = await args.relayInformationStore.resolveRelayInformation();
if(storeInformation instanceof Error) {
return new Response(render(Error404()), { status: 404 });
}
const resp = new Response(
render(Landing(await args.relayInformationStore.resolveRelayInformation()), { status: 200 }),
render(Landing(storeInformation), { status: 200 }),
);
resp.headers.set("content-type", "html");
return resp;
};

const information_handler = async (args: { relayInformationStore: RelayInformationStore }) => {
const resp = new Response(JSON.stringify(await args.relayInformationStore.resolveRelayInformation()), {
const storeInformation = await args.relayInformationStore.resolveRelayInformation();
if(storeInformation instanceof Error) {
return new Response(render(Error404()), { status: 404 });
}
const information = { ...storeInformation, pubkey: storeInformation.pubkey.bech32()}
const resp = new Response(JSON.stringify(information), {
status: 200,
});
resp.headers.set("content-type", "application/json; charset=utf-8");
Expand All @@ -258,7 +267,11 @@ async function verifyToken(event: NostrEvent, relayInformationStore: RelayInform
if (pubkey instanceof Error) {
throw new Error("pubkey not valid");
}
if (pubkey.hex !== (await relayInformationStore.resolveRelayInformation()).pubkey.hex) {
const storeInformation = await relayInformationStore.resolveRelayInformation();
if(storeInformation instanceof Error) {
throw new Error("store pubkey not valid");
}
if (pubkey.hex !== storeInformation.pubkey.hex) {
throw new Error("not admin");
}
return {
Expand Down
5 changes: 4 additions & 1 deletion queries/getRelayInformation.gql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ query getRelayInformation {
contact
description
icon
pubkey
pubkey {
hex
bech32
}
software
supported_nips
version
Expand Down
5 changes: 4 additions & 1 deletion queries/setRelayInformation.gql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ mutation setRelayInformation($pubkey: String, $contact: String, $name: String, $
contact
description
icon
pubkey
pubkey {
hex
bech32
}
software
supported_nips
version
Expand Down
59 changes: 31 additions & 28 deletions resolvers/nip11.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { PublicKey } from "../_libs.ts";

export type RelayInformationSave = {
name?: string;
description?: string;
pubkey?: string;
contact?: string;
icon?: string;
}

export type RelayInformation = {
name?: string;
description?: string;
Expand Down Expand Up @@ -31,38 +39,33 @@ export class RelayInformationStore {
this.default_information = default_information;
}

resolveRelayInformation = async (): Promise<RelayInformation> => {
const get_relay_information = (await this.kv.get<RelayInformation>(["relay_information"])).value;
// if pubkey is set in default_information, it will be used as the pubkey
if (get_relay_information && this.default_information.pubkey) {
get_relay_information.pubkey = this.default_information.pubkey;
resolveRelayInformation = async (): Promise<RelayInformation | Error> => {
const store_information_i = (await this.kv.get<RelayInformationSave>(["relay_information"])).value;
if(!store_information_i) {
return { ...this.default_information, ...not_modifiable_information };
}
if (!store_information_i.pubkey) {
return { ...this.default_information, ...{ name: store_information_i.name, contact: store_information_i.contact, description: store_information_i.description, icon: store_information_i.icon }, ...not_modifiable_information };
}
const get_relay_information_pubkey = PublicKey.FromString(store_information_i.pubkey);
if (get_relay_information_pubkey instanceof Error) {
return get_relay_information_pubkey
}
return { ...this.default_information, ...get_relay_information, ...not_modifiable_information };
const store_infomation = {
...store_information_i,
pubkey: get_relay_information_pubkey,
}
return { ...this.default_information, ...store_infomation, ...not_modifiable_information };
};

set_relay_information = async (
args: {
name?: string;
description?: string;
pubkey?: string;
contact?: string;
icon?: string;
},
) => {
set_relay_information = async (args: RelayInformationSave) => {
const old_information = await this.resolveRelayInformation();
const new_information = {
...old_information,
name: args.name,
description: args.description,
contact: args.contact,
icon: args.icon,
};
if (args.pubkey) {
const pubkey = PublicKey.FromString(args.pubkey);
if (pubkey instanceof Error) {
throw new Error("Invalid pubkey");
}
new_information.pubkey = pubkey;
if (old_information instanceof Error) {
return old_information;
}
const new_information = {...old_information, ...args};
if (new_information.pubkey instanceof PublicKey) {
new_information.pubkey = new_information.pubkey.hex
}
await this.kv.set(["relay_information"], new_information);
return { ...new_information, ...not_modifiable_information };
Expand Down
2 changes: 1 addition & 1 deletion routes/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Landing(information?: RelayInformation) {
<span class="font-bold">Description:</span> {information?.description}
</li>
<li>
<span class="font-bold">Pubkey:</span> {information?.pubkey}
<span class="font-bold">Pubkey:</span> {information?.pubkey.bech32()}
</li>
<li>
<span class="font-bold">Contact:</span> {information?.contact}
Expand Down

0 comments on commit 2194864

Please sign in to comment.