-
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.
Extract Nip27 mentions from content (#31)
- Loading branch information
Showing
7 changed files
with
75 additions
and
18 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
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,23 @@ | ||
module ContentTests | ||
|
||
open Xunit | ||
open Xunit.Abstractions | ||
open FsUnit.Xunit | ||
open Nostra | ||
|
||
type ``Nip27 Mentions``(output:ITestOutputHelper) = | ||
|
||
[<Fact>] | ||
let ``Parse npub reference`` () = | ||
"Hello nostr:npub1nccwjspr3nv7h67xx2qhdh2dzzvpyy55gte2dsu8yl7xd7n74y9qydz7mj !" | ||
|> Content.extractNip27Mentions | ||
|> should equal [("p", ["9e30e940238cd9ebebc6328176dd4d109812129442f2a6c38727fc66fa7ea90a"])] | ||
|
||
[<Fact>] | ||
let ``Parse multiple npub references`` () = | ||
"Hello nostr:npub1nccwjspr3nv7h67xx2qhdh2dzzvpyy55gte2dsu8yl7xd7n74y9qydz7mj and nostr:npub14zln9kg0yx7qdn2kx8p2z9zdrz3ujfhyx6adeepmjsl98gjlfj5sr6fcn4!" | ||
|> Content.extractNip27Mentions | ||
|> should equal [ | ||
("p", ["9e30e940238cd9ebebc6328176dd4d109812129442f2a6c38727fc66fa7ea90a"]) | ||
("p", ["a8bf32d90f21bc06cd5631c2a1144d18a3c926e436badce43b943e53a25f4ca9"]) | ||
] |
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
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
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,23 @@ | ||
namespace Nostra | ||
|
||
module Content = | ||
let (|Nip27Mention|_|) = Regex.matches @"\bnostr:((?:note|npub|naddr|nevent|nprofile)1\w )\b" | ||
|
||
let extractNip27Mentions content = | ||
let rec parseMentions content (mentions : Tag list) = | ||
match content with | ||
| Nip27Mention (mention, endPos) -> | ||
Shareable.decode mention | ||
|> Option.map (function | ||
| NPub authorId -> Tag.authorTag authorId | ||
| Note eventId -> Tag.eventRefTag eventId | ||
| NProfile(AuthorId authorId, relays) -> Tag("p", (Utils.toHex (authorId.ToBytes()))::relays) | ||
| NEvent(EventId eventId, relays, _, _) -> Tag("e", (Utils.toHex eventId)::relays) | ||
| NRelay relay -> Tag.relayTag [relay] | ||
| NSec _ -> failwith "Are you crazy!?" ) | ||
|> function | ||
| Some mention -> parseMentions content[endPos..] (mention::mentions) | ||
| None -> parseMentions content[endPos..] mentions | ||
| _ -> mentions | ||
parseMentions content [] | ||
|> List.rev |
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
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