-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
119 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ coverage | |
test.sqlite | ||
*.db | ||
node_modules | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { ChannelCreation, ChannelEdition, Kind_V2 } from "./events.ts"; | ||
import { ChannelCreation, ChannelEdition, Kind_V2 } from "./nostr.ts/nostr.ts"; | ||
import { DB, SqliteError } from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
export type func_GetChannelByName = ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DB, SqliteError } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { InviteToSpace } from "./nostr.ts/nostr.ts"; | ||
|
||
export type func_InviteToSpace = (event: InviteToSpace) => Promise<undefined | Error>; | ||
|
||
export const invite_to_space_sqlite = (db: DB): func_InviteToSpace => async (event: InviteToSpace) => { | ||
try { | ||
db.query( | ||
`INSERT INTO events_v2(id, pubkey, kind, event) VALUES (?, ?, ?, ?)`, | ||
[event.id, event.pubkey, event.kind, JSON.stringify(event)], | ||
); | ||
} catch (e) { | ||
return e as SqliteError; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,46 +3,57 @@ import { func_IsMember, SubscriptionMap, ws_handler } from "./ws.ts"; | |
import { render } from "https://esm.sh/[email protected]"; | ||
import { RootResolver } from "./resolvers/root.ts"; | ||
import * as gql from "https://esm.sh/[email protected]"; | ||
import { func_GetRelayMembers, Policy } from "./resolvers/policy.ts"; | ||
import { func_ResolvePolicyByKind } from "./resolvers/policy.ts"; | ||
import { PolicyStore } from "./resolvers/policy.ts"; | ||
import { Policies } from "./resolvers/policy.ts"; | ||
import { | ||
func_GetRelayMembers, | ||
func_ResolvePolicyByKind, | ||
get_relay_members, | ||
Policies, | ||
Policy, | ||
PolicyStore, | ||
} from "./resolvers/policy.ts"; | ||
import { | ||
event_schema_sqlite, | ||
func_GetEventCount, | ||
func_GetEventsByFilter, | ||
func_WriteRegularEvent, | ||
func_WriteReplaceableEvent, | ||
get_event_count_sqlite, | ||
get_events_by_filter_sqlite, | ||
write_regular_event_sqlite, | ||
write_replaceable_event_sqlite, | ||
} from "./resolvers/event.ts"; | ||
import Landing from "./routes/landing.tsx"; | ||
import Error404 from "./routes/_404.tsx"; | ||
import { RelayInfomationBase, RelayInformation, RelayInformationStore } from "./resolvers/nip11.ts"; | ||
import { func_GetEventsByFilter, func_WriteRegularEvent } from "./resolvers/event.ts"; | ||
import { Cookie, getCookies, setCookie } from "https://deno.land/[email protected]/http/cookie.ts"; | ||
import { Event_V2, Kind_V2 } from "./events.ts"; | ||
import { | ||
Event_V2, | ||
InviteToSpace, | ||
Kind_V2, | ||
NostrEvent, | ||
NostrKind, | ||
verify_event_v2, | ||
verifyEvent, | ||
} from "./nostr.ts/nostr.ts"; | ||
import { | ||
create_channel_sqlite, | ||
edit_channel_sqlite, | ||
func_CreateChannel, | ||
func_EditChannel, | ||
func_GetChannelByID, | ||
get_channel_by_id_sqlite, | ||
sqlite_schema, | ||
} from "./channel.ts"; | ||
import { func_GetChannelByID } from "./channel.ts"; | ||
import { DB } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { get_relay_members } from "./resolvers/policy.ts"; | ||
import { get_events_by_filter_sqlite } from "./resolvers/event.ts"; | ||
import { get_event_count_sqlite } from "./resolvers/event.ts"; | ||
import { | ||
delete_event_sqlite, | ||
func_DeleteEvent, | ||
func_DeleteEventsFromPubkey, | ||
func_GetDeletedEventIDs, | ||
} from "./resolvers/event_deletion.ts"; | ||
import { delete_event_sqlite } from "./resolvers/event_deletion.ts"; | ||
|
||
import { NostrEvent, NostrKind, verify_event_v2, verifyEvent } from "./nostr.ts/nostr.ts"; | ||
import { PublicKey } from "./nostr.ts/key.ts"; | ||
import { InvalidKey, PublicKey } from "./nostr.ts/key.ts"; | ||
import { parseJSON } from "./nostr.ts/_helper.ts"; | ||
import { func_InviteToSpace, invite_to_space_sqlite } from "./invitation.ts"; | ||
|
||
const schema = gql.buildSchema(gql.print(typeDefs)); | ||
|
||
|
@@ -83,7 +94,7 @@ export const ENV_relayed_pubkey = "relayed_pubkey"; | |
|
||
export async function run(args: { | ||
port?: number; | ||
admin?: PublicKey; | ||
admin?: PublicKey | string; | ||
auth_required: boolean; | ||
default_policy: DefaultPolicy; | ||
default_information?: RelayInfomationBase; | ||
|
@@ -122,6 +133,14 @@ export async function run(args: { | |
return p; | ||
} | ||
args.admin = p; | ||
} else { | ||
if (typeof args.admin == "string") { | ||
const p = PublicKey.FromString(args.admin); | ||
if (p instanceof Error) { | ||
return new Error("invalid admin public key"); | ||
} | ||
args.admin = p; | ||
} | ||
} | ||
// Relay Key | ||
// let system_key: string | PrivateKey | Error = args.system_key; | ||
|
@@ -190,6 +209,24 @@ export async function run(args: { | |
create_channel: create_channel_sqlite(db), | ||
edit_channel: edit_channel_sqlite(db), | ||
kv: kv, | ||
// invitation | ||
invite_to_space: async (event: InviteToSpace) => { | ||
const policy = await policyStore.resolvePolicyByKind(NostrKind.TEXT_NOTE); | ||
if (policy instanceof Error) { | ||
return policy; | ||
} | ||
const appended = await policyStore.set_policy({ | ||
...policy, | ||
allow: policy.allow.add(event.invitee), | ||
}); | ||
if (appended instanceof Error) { | ||
return appended; | ||
} | ||
const done = invite_to_space_sqlite(db)(event); | ||
if (done instanceof Error) { | ||
return done; | ||
} | ||
}, | ||
_debug: args._debug ? true : false, | ||
}), | ||
); | ||
|
@@ -259,6 +296,8 @@ const root_handler = ( | |
get_relay_members: func_GetRelayMembers; | ||
// config | ||
auth_required: boolean; | ||
// invitation | ||
invite_to_space: func_InviteToSpace; | ||
kv: Deno.Kv; | ||
_debug: boolean; | ||
}, | ||
|
@@ -324,6 +363,7 @@ async (req: Request, info: Deno.ServeHandlerInfo) => { | |
} | ||
const ok = await 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) { | ||
|
@@ -339,6 +379,13 @@ async (req: Request, info: Deno.ServeHandlerInfo) => { | |
return new Response(res.message, { status: 400 }); | ||
} | ||
return new Response(); | ||
} else if (event.kind == Kind_V2.InviteToSpace) { | ||
const res = await args.invite_to_space(event); | ||
if (res instanceof Error) { | ||
console.error(res); | ||
return new Response(res.message, { status: 400 }); | ||
} | ||
return new Response(); | ||
} else { | ||
return new Response(`not a recognizable event`, { status: 400 }); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { DB } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { DefaultPolicy } from "../main.ts"; | ||
import { EventRelayMembers, Kind_V2 } from "../events.ts"; | ||
import { EventRelayMembers, Kind_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"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,11 @@ import { assertIsError, assertNotInstanceOf } from "https://deno.land/[email protected]. | |
import { fail } from "https://deno.land/[email protected]/assert/fail.ts"; | ||
|
||
import * as client_test from "../nostr.ts/relay-single-test.ts"; | ||
import { ChannelCreation, ChannelEdition } from "../events.ts"; | ||
import { Kind_V2 } from "../events.ts"; | ||
import { | ||
ChannelCreation, | ||
ChannelEdition, | ||
InMemoryAccountContext, | ||
Kind_V2, | ||
NostrKind, | ||
RelayResponse_Event, | ||
sign_event_v2, | ||
|
@@ -18,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 { prepareInvitation } from "../nostr.ts/invitation.ts"; | ||
|
||
const test_kv = async () => { | ||
try { | ||
|
@@ -412,6 +414,40 @@ Deno.test({ | |
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "Invitationn", | ||
// ignore: ture, | ||
fn: async () => { | ||
const admin = InMemoryAccountContext.Generate() as Signer; | ||
const invitee = InMemoryAccountContext.Generate(); | ||
const relay = await run({ | ||
admin: admin.publicKey.hex, | ||
kv: await test_kv(), | ||
default_policy: { | ||
allowed_kinds: "all", | ||
}, | ||
auth_required: true, | ||
}); | ||
if (relay instanceof Error) fail(relay.message); | ||
{ | ||
const event = await prepareInvitation(admin, invitee.publicKey.hex); | ||
if (event instanceof Error) fail(event.message); | ||
const r = await fetch(`${relay.http_url}`, { | ||
method: "POST", | ||
body: JSON.stringify(event), | ||
}); | ||
await r.text(); | ||
assertEquals(r.status, 200); | ||
const policy = await relay.get_policy(NostrKind.TEXT_NOTE); | ||
if (policy instanceof Error) fail(policy.message); | ||
console.log(policy.allow); | ||
assertEquals(policy.allow.has(invitee.publicKey.hex), true); | ||
} | ||
|
||
await relay.shutdown(); | ||
}, | ||
}); | ||
|
||
async function randomEvent(ctx: InMemoryAccountContext, kind?: NostrKind, content?: string) { | ||
const event = await prepareNormalNostrEvent(ctx, { | ||
kind: kind || NostrKind.TEXT_NOTE, | ||
|