damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 20299615ba9389ca4458d9c67e457e0e2fc0ca2e
parent 972a183ed876643c822e5867c7d9c1d6eac0e775
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 17 Nov 2023 08:09:28 -0800

parser: always convert damus.io links to bech32 mentions

Update our note parse to always interpret damus.io links as bech32
mentions (nostr:npub...)

This means links will get converted to nostr: on post composition, and
if we ever see a link it will also get converted to nostr: visually

Changelog-Added: Always convert damus.io links to inline mentions
Fixes: https://github.com/damus-io/damus/issues/690

Diffstat:
Mdamus-c/damus.c | 49++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 44 insertions(+), 5 deletions(-)

diff --git a/damus-c/damus.c b/damus-c/damus.c @@ -169,6 +169,10 @@ static int consume_url_host(struct cursor *cur) static int parse_url(struct cursor *cur, struct note_block *block) { u8 *start = cur->p; + u8 *host; + int host_len; + struct cursor path_cur; + struct nostr_bech32 bech32; if (!parse_str(cur, "http")) return 0; @@ -185,14 +189,36 @@ static int parse_url(struct cursor *cur, struct note_block *block) { } } - if (!(consume_url_host(cur) && - consume_url_path(cur) && - consume_url_fragment(cur))) - { - cur->p = start; + // make sure to save the hostname. We will use this to detect damus.io links + host = cur->p; + + if (!consume_url_host(cur)) { + cur->p = start; + return 0; + } + + // get the length of the host string + host_len = cur->p - host; + + // save the current parse state so that we can continue from here when + // parsing the bech32 in the damus.io link if we have it + copy_cursor(cur, &path_cur); + + // skip leading / + if (!cursor_skip(&path_cur, 1)) { return 0; } + if (!consume_url_path(cur)) { + cur->p = start; + return 0; + } + + if (!consume_url_fragment(cur)) { + cur->p = start; + return 0; + } + // smart parens if (start - 1 >= 0 && start < cur->end && @@ -203,6 +229,19 @@ static int parse_url(struct cursor *cur, struct note_block *block) { cur->p--; } + // save the bech32 string pos in case we hit a damus.io link + block->block.str.start = path_cur.p; + + // if we have a damus link, make it a mention + if (host_len == 8 + && !strncmp(host, "damus.io", 8) + && parse_nostr_bech32(&path_cur, &block->block.mention_bech32.bech32)) + { + block->block.str.end = path_cur.p; + block->type = BLOCK_MENTION_BECH32; + return 1; + } + block->type = BLOCK_URL; block->block.str.start = (const char *)start; block->block.str.end = (const char *)cur->p;