diff --git a/nostr.ts b/nostr.ts index 8a2ae19..618f4d6 160000 --- a/nostr.ts +++ b/nostr.ts @@ -1 +1 @@ -Subproject commit 8a2ae19117276ee1c4402dca5550e75abe023778 +Subproject commit 618f4d6baa9e913d6a2674e324d53a219114ea32 diff --git a/resolvers/policy.ts b/resolvers/policy.ts index 5fb0abc..1a0e705 100644 --- a/resolvers/policy.ts +++ b/resolvers/policy.ts @@ -138,6 +138,9 @@ export const add_space_member = (db?: DB): func_AddSpaceMember => async (event: if (!db) { return new Error("add_space_member is not supported"); } + if (await is_space_member({ db })(event.member)) { + return new Error(`${event.member} is already a member of the space.`); + } db.query( `INSERT INTO events_v2(id, pubkey, kind, event) VALUES (?, ?, ?, ?)`, [event.id, event.pubkey, event.kind, JSON.stringify(event)], @@ -145,8 +148,8 @@ export const add_space_member = (db?: DB): func_AddSpaceMember => async (event: }; export const is_space_member = - (args: { admin: PublicKey; db?: DB }): func_IsSpaceMember => async (pubkey: string) => { - if (args.admin.hex === pubkey) return true; + (args: { admin?: PublicKey; db?: DB }): func_IsSpaceMember => async (pubkey: string) => { + if (args.admin?.hex === pubkey) return true; if (!args.db) { return new Error("is_space_member is not supported"); diff --git a/tests/space-member-test.ts b/tests/space-member-test.ts index 3f1918f..3ef53a4 100644 --- a/tests/space-member-test.ts +++ b/tests/space-member-test.ts @@ -3,7 +3,6 @@ import { InMemoryAccountContext } from "../nostr.ts/nostr.ts"; import { fail } from "https://deno.land/std@0.202.0/assert/fail.ts"; import { assertEquals } from "https://deno.land/std@0.220.1/assert/assert_equals.ts"; import { prepareAddMember } from "../nostr.ts/space-member.ts"; -import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts"; const test_ctx = InMemoryAccountContext.Generate(); const test_kv = async () => { @@ -38,6 +37,18 @@ Deno.test("Space Member", async (t) => { assertEquals(space_members.map((event) => event.id).includes(add_member_event.id), true); }); + await t.step("it it already a member", async () => { + const redo = await prepareAddMember(test_ctx, new_member.publicKey.hex); + if (redo instanceof Error) fail(redo.message); + const r = await fetch(`${relay.http_url}`, { + method: "POST", + body: JSON.stringify(redo), + }); + const message = await r.text(); + assertEquals(r.status, 400); + assertEquals(message, `${new_member.publicKey.hex} is already a member of the space.`); + }); + await t.step("admin is member", async () => { const res = await relay.is_space_member(test_ctx.publicKey.hex); if (res instanceof Error) fail(res.message); diff --git a/tests/test.ts b/tests/test.ts index 3b234be..c678af3 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -19,6 +19,7 @@ import { prepareNormalNostrEvent } from "../nostr.ts/event.ts"; import { RelayRejectedEvent, SingleRelayConnection, SubscriptionStream } from "../nostr.ts/relay-single.ts"; import { PrivateKey, PublicKey } from "../nostr.ts/key.ts"; import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts"; +import { nowRFC3339 } from "../nostr.ts/_helper.ts"; const test_kv = async () => { try { @@ -222,6 +223,7 @@ Deno.test({ channel_id: ChannelCreation_event.id, name: "test2", scope: "server", + created_at: nowRFC3339(), }); const r2 = await fetch(`${relay.http_url}`, { method: "POST",