Skip to content

Commit

Permalink
existing member
Browse files Browse the repository at this point in the history
  • Loading branch information
bob2402 committed Jun 14, 2024
1 parent b848b25 commit f3d5874
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nostr.ts
7 changes: 5 additions & 2 deletions resolvers/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,18 @@ 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)],
);
};

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");
Expand Down
13 changes: 12 additions & 1 deletion tests/space-member-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 { 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 () => {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit f3d5874

Please sign in to comment.