Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use jsr #24

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

28 changes: 14 additions & 14 deletions channel.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { ChannelCreation, ChannelEdition, Kind_V2 } from "./nostr.ts/nostr.ts";
import { v2 } from "@blowater/nostr-sdk";
import { DB, SqliteError } from "https://deno.land/x/[email protected]/mod.ts";

export type func_GetChannelByName = (
name: string,
) => Promise<{ create: ChannelCreation; edit?: ChannelEdition } | undefined>;
) => Promise<{ create: v2.ChannelCreation; edit?: v2.ChannelEdition } | undefined>;

export type func_GetChannelByID = (
id: string,
) => Promise<{ create: ChannelCreation; edit?: ChannelEdition } | undefined>;
) => Promise<{ create: v2.ChannelCreation; edit?: v2.ChannelEdition } | undefined>;

export type func_CreateChannel = (event: ChannelCreation) => Promise<boolean>;
export type func_EditChannel = (event: ChannelEdition) => Promise<void | Error>;
export type func_CreateChannel = (event: v2.ChannelCreation) => Promise<boolean>;
export type func_EditChannel = (event: v2.ChannelEdition) => Promise<void | Error>;

export function channel_creation_key(id: string) {
return ["event_v2", Kind_V2.ChannelCreation, id];
return ["event_v2", v2.Kind_V2.ChannelCreation, id];
}
export function channel_edition_key(id: string) {
return ["event_v2", Kind_V2.ChannelEdition, id];
return ["event_v2", v2.Kind_V2.ChannelEdition, id];
}

export const create_channel_kv = (kv: Deno.Kv): func_CreateChannel => async (event: ChannelCreation) => {
export const create_channel_kv = (kv: Deno.Kv): func_CreateChannel => async (event: v2.ChannelCreation) => {
const result = await kv.set(channel_creation_key(event.id), event);
return result.ok;
};

export const create_channel_sqlite = (db: DB): func_CreateChannel => async (event: ChannelCreation) => {
export const create_channel_sqlite = (db: DB): func_CreateChannel => async (event: v2.ChannelCreation) => {
const rows = db.query(
`INSERT INTO channels (channel_id, name, creation_event) VALUES (?, ?, ?);`,
[event.id, event.name, JSON.stringify(event)],
);
return true;
};

export const edit_channel_kv = (kv: Deno.Kv): func_EditChannel => async (event: ChannelEdition) => {
const chan = await kv.get<ChannelCreation>(channel_creation_key(event.channel_id));
export const edit_channel_kv = (kv: Deno.Kv): func_EditChannel => async (event: v2.ChannelEdition) => {
const chan = await kv.get<v2.ChannelCreation>(channel_creation_key(event.channel_id));
if (chan.value == null) {
return new Error(`channel ${event.channel_id} does not exist`);
}
Expand All @@ -49,7 +49,7 @@ export const edit_channel_kv = (kv: Deno.Kv): func_EditChannel => async (event:
}
};

export const edit_channel_sqlite = (db: DB): func_EditChannel => async (event: ChannelEdition) => {
export const edit_channel_sqlite = (db: DB): func_EditChannel => async (event: v2.ChannelEdition) => {
const channel = await get_channel_by_id_sqlite(db)(event.channel_id);
if (channel == undefined) {
return new Error(`channel ${event.channel_id} does not exist`);
Expand All @@ -71,11 +71,11 @@ export const edit_channel_sqlite = (db: DB): func_EditChannel => async (event: C
};

export const get_channel_by_id_kv = (kv: Deno.Kv): func_GetChannelByID => async (id: string) => {
const chan = await kv.get<ChannelCreation>(channel_creation_key(id));
const chan = await kv.get<v2.ChannelCreation>(channel_creation_key(id));
if (chan.value == null) {
return undefined;
}
const chan_edit = await kv.get<ChannelEdition>(channel_edition_key(id));
const chan_edit = await kv.get<v2.ChannelEdition>(channel_edition_key(id));
return {
create: chan.value,
edit: chan_edit.value || undefined,
Expand Down
2 changes: 2 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"**/_fresh/*"
],
"imports": {
"@blowater/nostr-sdk": "jsr:@blowater/nostr-sdk@^0.0.13",
"@std/datetime": "jsr:@std/datetime@^0.224.5",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
"tailwindcss": "npm:[email protected]",
Expand Down
297 changes: 297 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import Landing from "./routes/landing.tsx";
import Error404 from "./routes/_404.tsx";
import { RelayInfomationBase, RelayInformation, RelayInformationStore } from "./resolvers/nip11.ts";
import { Cookie, getCookies, setCookie } from "https://deno.land/[email protected]/http/cookie.ts";
import { Event_V2, Kind_V2, NostrEvent, NostrKind, verify_event_v2, verifyEvent } from "./nostr.ts/nostr.ts";
import {
create_channel_sqlite,
edit_channel_sqlite,
Expand All @@ -47,8 +46,8 @@ import {
func_DeleteEventsFromPubkey,
func_GetDeletedEventIDs,
} from "./resolvers/event_deletion.ts";
import { PublicKey } from "./nostr.ts/key.ts";
import { parseJSON } from "./nostr.ts/_helper.ts";

import { NostrEvent, NostrKind, parseJSON, PublicKey, v2, verifyEvent } from "@blowater/nostr-sdk";

const schema = gql.buildSchema(gql.print(typeDefs));

Expand Down Expand Up @@ -475,31 +474,31 @@ const event_v2_handler = (args: {
}) =>
async (req: Request) => {
const text = await req.text();
const event = parseJSON<Event_V2>(text);
const event = parseJSON<v2.Event_V2>(text);
if (event instanceof Error) {
return new Response(event.message, {
status: 400,
});
}
const ok = await verify_event_v2(event);
const ok = await v2.verify_event_v2(event);
if (!ok) {
console.error("event", event);
return new Response("event is not valid", { status: 400 });
}
if (event.kind == Kind_V2.ChannelCreation) {
if (event.kind == v2.Kind_V2.ChannelCreation) {
const ok = await args.create_channel(event);
if (ok) {
return new Response();
} else {
return new Response("failed to write event", { status: 400 });
}
} else if (event.kind == Kind_V2.ChannelEdition) {
} else if (event.kind == v2.Kind_V2.ChannelEdition) {
const res = await args.edit_channel(event);
if (res instanceof Error) {
return new Response(res.message, { status: 400 });
}
return new Response();
} else if (event.kind == Kind_V2.SpaceMember) {
} else if (event.kind == v2.Kind_V2.SpaceMember) {
const res = await args.add_space_member(event);
if (res instanceof Error) {
console.error(res);
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fmt:
deno fmt

test: fmt
deno test --lock deno.test.lock \
deno test --lock --lock-write deno.test.lock \
--trace-leaks --unstable-kv \
--allow-read=queries,test.sqlite,relayed.db,relayed.db-journal \
--allow-net --allow-write --allow-ffi \
Expand Down
1 change: 0 additions & 1 deletion nostr.ts
Submodule nostr.ts deleted from 7534e6
2 changes: 1 addition & 1 deletion resolvers/event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DB, SqliteError } from "https://deno.land/x/[email protected]/mod.ts";
import { NostrEvent, NostrFilter, NostrKind } from "../nostr.ts/nostr.ts";
import { NostrEvent, NostrFilter, NostrKind } from "@blowater/nostr-sdk";

export type func_GetEventsByIDs = (ids: Set<string>) => AsyncIterable<NostrEvent>;
export type interface_GetEventsByIDs = {
Expand Down
2 changes: 1 addition & 1 deletion resolvers/event_deletion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DB } from "https://deno.land/x/[email protected]/mod.ts";
import { PublicKey } from "../nostr.ts/key.ts";
import { PublicKey } from "@blowater/nostr-sdk";

export type func_DeleteEvent = (event_id: string) => Promise<boolean>;
export type func_DeleteEventsFromPubkey = (pubkey: string | PublicKey) => Promise<string[]>;
Expand Down
2 changes: 1 addition & 1 deletion resolvers/nip11.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PublicKey } from "@blowater/nostr-sdk";
import { software, supported_nips } from "../main.ts";
import { PublicKey } from "../nostr.ts/key.ts";

export type RelayInfomationBase = {
name?: string;
Expand Down
20 changes: 9 additions & 11 deletions resolvers/policy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { DB } from "https://deno.land/x/[email protected]/mod.ts";
import { DefaultPolicy } from "../main.ts";
import { Kind_V2, SpaceMember, verify_event_v2 } from "../nostr.ts/nostr.ts";
import { parseJSON } from "../nostr.ts/_helper.ts";
import { PublicKey } from "../nostr.ts/key.ts";
import { NostrKind } from "../nostr.ts/nostr.ts";

import { NostrKind, parseJSON, PublicKey, v2 } from "@blowater/nostr-sdk";

export const Policies = (kv: Deno.Kv) =>
async function () {
Expand Down Expand Up @@ -111,18 +109,18 @@ export class PolicyStore {
};
}

export type func_GetSpaceMembers = () => Promise<SpaceMember[] | Error>;
export type func_AddSpaceMember = (event: SpaceMember) => Promise<void | Error>;
export type func_GetSpaceMembers = () => Promise<v2.SpaceMember[] | Error>;
export type func_AddSpaceMember = (event: v2.SpaceMember) => Promise<void | Error>;
export type func_IsSpaceMember = (pubkey: string) => Promise<boolean | Error>;

export const get_space_members = (db: DB): func_GetSpaceMembers => async () => {
const rows = db.query<[string]>(
"select event from events_v2 where kind = (?)",
[Kind_V2.SpaceMember],
[v2.Kind_V2.SpaceMember],
);
const events = [] as SpaceMember[];
const events = [] as v2.SpaceMember[];
for (const row of rows) {
const space_member_event = parseJSON<SpaceMember>(row[0]);
const space_member_event = parseJSON<v2.SpaceMember>(row[0]);
if (space_member_event instanceof Error) {
return space_member_event;
}
Expand All @@ -132,7 +130,7 @@ export const get_space_members = (db: DB): func_GetSpaceMembers => async () => {
};

export const add_space_member =
(args: { admin: PublicKey; db: DB }): func_AddSpaceMember => async (event: SpaceMember) => {
(args: { admin: PublicKey; db: DB }): func_AddSpaceMember => async (event: v2.SpaceMember) => {
if (event.pubkey != args.admin.hex) {
return new Error("Only administrators can add members to the space.");
}
Expand All @@ -155,7 +153,7 @@ export const is_space_member =
try {
const rows = args.db.query<[string]>(
"select event from events_v2 where kind = (?) and json_extract(event, '$.member') = (?)",
[Kind_V2.SpaceMember, pubkey],
[v2.Kind_V2.SpaceMember, pubkey],
);
return rows.length > 0;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion resolvers/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Policies } from "./policy.ts";
import { RelayInformationStore } from "./nip11.ts";
import { func_GetEventCount } from "./event.ts";
import { func_DeleteEvent, func_DeleteEventsFromPubkey, func_GetDeletedEventIDs } from "./event_deletion.ts";
import { NostrKind } from "../nostr.ts/nostr.ts";
import { NostrKind } from "@blowater/nostr-sdk";

export function RootResolver({ deps }: {
deps: {
Expand Down
10 changes: 5 additions & 5 deletions tests/space-member-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Relay, run } from "../main.ts";
import { InMemoryAccountContext } from "../nostr.ts/nostr.ts";

import { fail } from "https://deno.land/[email protected]/assert/fail.ts";
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import { prepareSpaceMember } from "../nostr.ts/space-member.ts";
import { InMemoryAccountContext, v2 } from "@blowater/nostr-sdk";

const test_ctx = InMemoryAccountContext.Generate();
const test_kv = async () => {
Expand All @@ -24,7 +24,7 @@ Deno.test("Space Member", async (t) => {
const new_member = InMemoryAccountContext.Generate();

await t.step("admin can add member", async () => {
const add_member_event = await prepareSpaceMember(test_ctx, new_member.publicKey.hex);
const add_member_event = await v2.prepareSpaceMember(test_ctx, new_member.publicKey.hex);
if (add_member_event instanceof Error) fail(add_member_event.message);
const r = await fetch(`${relay.http_url}`, {
method: "POST",
Expand All @@ -39,7 +39,7 @@ Deno.test("Space Member", async (t) => {

await t.step("other can not add member", async () => {
const other = InMemoryAccountContext.Generate();
const add_member_event = await prepareSpaceMember(other, new_member.publicKey.hex);
const add_member_event = await v2.prepareSpaceMember(other, new_member.publicKey.hex);
if (add_member_event instanceof Error) fail(add_member_event.message);
const r = await fetch(`${relay.http_url}`, {
method: "POST",
Expand All @@ -53,7 +53,7 @@ Deno.test("Space Member", async (t) => {
});

await t.step("it it already a member", async () => {
const redo = await prepareSpaceMember(test_ctx, new_member.publicKey.hex);
const redo = await v2.prepareSpaceMember(test_ctx, new_member.publicKey.hex);
if (redo instanceof Error) fail(redo.message);
const r = await fetch(`${relay.http_url}`, {
method: "POST",
Expand Down
Loading
Loading