mirror of
https://github.com/BlowaterNostr/blowater.git
synced 2024-10-18 15:43:20 +00:00
Naddr parsing (#178)
Co-authored-by: BlowaterNostr <127284497+BlowaterNostr@users.noreply.github.com>
This commit is contained in:
parent
01609b9916
commit
fd8f6800a4
@ -1,6 +1,8 @@
|
|||||||
import { assertEquals } from "https://deno.land/std@0.176.0/testing/asserts.ts";
|
import { assertEquals } from "https://deno.land/std@0.176.0/testing/asserts.ts";
|
||||||
import { ChatMessage, groupContinuousMessages, parseContent } from "./message.ts";
|
import { ChatMessage, groupContinuousMessages, parseContent } from "./message.ts";
|
||||||
import { PrivateKey, PublicKey } from "../lib/nostr-ts/key.ts";
|
import { PrivateKey, PublicKey } from "../lib/nostr-ts/key.ts";
|
||||||
|
import { NostrAddress } from "../lib/nostr-ts/nip19.ts";
|
||||||
|
import { NostrKind } from "../lib/nostr-ts/nostr.ts";
|
||||||
|
|
||||||
Deno.test("inline parse", async (t) => {
|
Deno.test("inline parse", async (t) => {
|
||||||
const data = [
|
const data = [
|
||||||
@ -169,6 +171,23 @@ Deno.test("inline parse", async (t) => {
|
|||||||
],
|
],
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input:
|
||||||
|
`naddr1qqxnzd3exsmnjvphxqunqv33qgsp7hwmlh5zccs55shzpfued50pznvypj0wwzn00dtyjzlqkr04w4grqsqqqa28vct2px`,
|
||||||
|
output: [{
|
||||||
|
type: "naddr",
|
||||||
|
start: 0,
|
||||||
|
end: 99,
|
||||||
|
addr: new NostrAddress({
|
||||||
|
pubkey: PublicKey.FromHex(
|
||||||
|
"1f5ddbfde82c6214a42e20a7996d1e114d840c9ee70a6f7b56490be0b0df5755",
|
||||||
|
) as PublicKey,
|
||||||
|
identifier: "1694790709021",
|
||||||
|
kind: NostrKind.Long_Form,
|
||||||
|
relays: [],
|
||||||
|
}),
|
||||||
|
}],
|
||||||
|
},
|
||||||
// {
|
// {
|
||||||
// input: `nostr:nevent1qqsz25j8nrppstgmyry8hgsg4fggtfa6xnym2n4c2xth7usxtydtgpcpp4mhxue69uhhjctzw5hx6egzyze7g05vclndlu36x0vjzw37jykcjkcu8ep9qfqwpjvahmlrq6947qcyqqqqqqgj5mjek`,
|
// input: `nostr:nevent1qqsz25j8nrppstgmyry8hgsg4fggtfa6xnym2n4c2xth7usxtydtgpcpp4mhxue69uhhjctzw5hx6egzyze7g05vclndlu36x0vjzw37jykcjkcu8ep9qfqwpjvahmlrq6947qcyqqqqqqgj5mjek`,
|
||||||
// output: [{
|
// output: [{
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
import { PublicKey } from "../lib/nostr-ts/key.ts";
|
import { PublicKey } from "../lib/nostr-ts/key.ts";
|
||||||
import { MessageThread } from "./dm.tsx";
|
import { MessageThread } from "./dm.tsx";
|
||||||
import { DirectedMessage_Event, Text_Note_Event } from "../nostr.ts";
|
import { DirectedMessage_Event, Text_Note_Event } from "../nostr.ts";
|
||||||
import { NostrProfile, NoteID } from "../lib/nostr-ts/nip19.ts";
|
import { NostrAddress, NostrProfile, NoteID } from "../lib/nostr-ts/nip19.ts";
|
||||||
|
import { Int } from "https://deno.land/x/automerge@2.1.0-alpha.12/types.ts";
|
||||||
|
import { NostrKind } from "../lib/nostr-ts/nostr.ts";
|
||||||
|
|
||||||
export function* parseContent(content: string) {
|
export function* parseContent(content: string) {
|
||||||
// URLs
|
// URLs
|
||||||
@ -13,6 +15,9 @@ export function* parseContent(content: string) {
|
|||||||
//nprofile
|
//nprofile
|
||||||
yield* match(/(nostr:)?nprofile[0-9a-z]+/g, content, "nprofile");
|
yield* match(/(nostr:)?nprofile[0-9a-z]+/g, content, "nprofile");
|
||||||
|
|
||||||
|
//naddr
|
||||||
|
yield* match(/(nostr:)?naddr[0-9a-z]+/g, content, "naddr");
|
||||||
|
|
||||||
// notes
|
// notes
|
||||||
yield* match(/note[0-9a-z]{59}/g, content, "note");
|
yield* match(/note[0-9a-z]{59}/g, content, "note");
|
||||||
|
|
||||||
@ -82,6 +87,24 @@ function* match(regex: RegExp, content: string, type: ItemType): Generator<Conte
|
|||||||
relays: decoded_nProfile.relays,
|
relays: decoded_nProfile.relays,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
} else if (type == "naddr") {
|
||||||
|
let bech32: string;
|
||||||
|
if (match[0].startsWith("nostr:")) {
|
||||||
|
bech32 = content.slice(urlStartPosition + 6, urlEndPosition + 1);
|
||||||
|
} else {
|
||||||
|
bech32 = content.slice(urlStartPosition, urlEndPosition + 1);
|
||||||
|
}
|
||||||
|
const decoded_nAddr = NostrAddress.decode(bech32);
|
||||||
|
if (decoded_nAddr instanceof Error) {
|
||||||
|
// ignore
|
||||||
|
} else {
|
||||||
|
yield {
|
||||||
|
type: "naddr",
|
||||||
|
start: urlStartPosition,
|
||||||
|
end: urlEndPosition,
|
||||||
|
addr: decoded_nAddr,
|
||||||
|
};
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
yield {
|
yield {
|
||||||
type: type,
|
type: type,
|
||||||
@ -93,7 +116,7 @@ function* match(regex: RegExp, content: string, type: ItemType): Generator<Conte
|
|||||||
}
|
}
|
||||||
|
|
||||||
type otherItemType = "url" | "tag";
|
type otherItemType = "url" | "tag";
|
||||||
type ItemType = otherItemType | "note" | "npub" | "nprofile";
|
type ItemType = otherItemType | "note" | "npub" | "nprofile" | "naddr";
|
||||||
export type ContentItem = {
|
export type ContentItem = {
|
||||||
type: otherItemType;
|
type: otherItemType;
|
||||||
start: number;
|
start: number;
|
||||||
@ -109,6 +132,11 @@ export type ContentItem = {
|
|||||||
noteID: NoteID;
|
noteID: NoteID;
|
||||||
start: number;
|
start: number;
|
||||||
end: number;
|
end: number;
|
||||||
|
} | {
|
||||||
|
type: "naddr";
|
||||||
|
start: number;
|
||||||
|
end: number;
|
||||||
|
addr: NostrAddress;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Think of ChatMessage as an materialized view of NostrEvent
|
// Think of ChatMessage as an materialized view of NostrEvent
|
||||||
|
Loading…
Reference in New Issue
Block a user