From 2fc6e7ef221c01faa13c8c3e7ed75f677ebcebef Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 6 Jun 2022 08:55:32 -0700 Subject: [PATCH] fix parsing mention indices > 9 Signed-off-by: William Casarin --- damus/Models/Mentions.swift | 17 ++++++++++----- damus/Util/Parser.swift | 2 +- damusTests/ReplyTests.swift | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/damus/Models/Mentions.swift b/damus/Models/Mentions.swift index 016a8e0..b60bea6 100644 --- a/damus/Models/Mentions.swift +++ b/damus/Models/Mentions.swift @@ -159,30 +159,37 @@ func parse_mention(_ p: Parser, tags: [[String]]) -> Mention? { return nil } + var ind = digit + + if let d2 = parse_digit(p) { + ind = digit * 10 + ind += d2 + } + if !parse_char(p, "]") { return nil } var kind: MentionType = .pubkey - if digit > tags.count - 1 { + if ind > tags.count - 1 { return nil } - if tags[digit].count == 0 { + if tags[ind].count == 0 { return nil } - switch tags[digit][0] { + switch tags[ind][0] { case "e": kind = .event case "p": kind = .pubkey default: return nil } - guard let ref = tag_to_refid(tags[digit]) else { + guard let ref = tag_to_refid(tags[ind]) else { return nil } - return Mention(index: digit, type: kind, ref: ref) + return Mention(index: ind, type: kind, ref: ref) } func find_tag_ref(type: String, id: String, tags: [[String]]) -> Int? { diff --git a/damus/Util/Parser.swift b/damus/Util/Parser.swift index 0886b0f..682daaf 100644 --- a/damus/Util/Parser.swift +++ b/damus/Util/Parser.swift @@ -76,7 +76,7 @@ func parse_digit(_ p: Parser) -> Int? { } } - return 0 + return nil } diff --git a/damusTests/ReplyTests.swift b/damusTests/ReplyTests.swift index e4a4203..f310132 100644 --- a/damusTests/ReplyTests.swift +++ b/damusTests/ReplyTests.swift @@ -53,6 +53,48 @@ class ReplyTests: XCTestCase { XCTAssertEqual(event_refs[1].is_mention!.ref.ref_id, "mentioned_id") } + func testEmptyMention() throws { + let content = "this is some & content" + let tags: [[String]] = [] + let blocks = parse_mentions(content: content, tags: tags) + let post_blocks = parse_post_blocks(content: content) + let post_tags = make_post_tags(post_blocks: post_blocks, tags: tags) + let event_refs = interpret_event_refs(blocks: blocks, tags: tags) + + XCTAssertEqual(event_refs.count, 0) + XCTAssertEqual(post_tags.blocks.count, 1) + XCTAssertEqual(post_tags.tags.count, 0) + XCTAssertEqual(post_blocks.count, 1) + } + + func testManyPostMentions() throws { + let content = """ +@38bc54a8f675564058b987056fc27fe3d40ca34404586933a115d9e0baeaccb9 +@774734fad6c318799149c35008c356352b8bfc1791d9e41c803bd412b23143be +@d64266d4bbf3cbcb773d074ee5ffe9ae557425cce0521e102dfde88a7223fb4c +@9f936cfb57374c95c4b8f2d5e640d978e4c59ccbe7783d434f434a8cc69bfa07 +@29080a53a6cef22b28dd8c9a25684cb9c2691f8f0c98651d20c65e1a2cd5cef1 +@dcdc52ec631c4034b0766a49865ec2e7fc0cdb2ba071aff4050eba343e7ba0fe +@136f15a6e4c5f046a71ddaf014bbca51408041d5d0ec2a0154be4b089e6f0249 +@5d994e704a4d3edf0163a708f69cb821f5a9caefeb79c17c1507e11e8a238f36 +@d76951e648f1b00715fe55003fcfb6fe91a7bf73fca5b6fd3e5bbe6845a5a0b1 +@3e999f94e2cb34ef44a64b351141ac4e51b5121b2d31aed4a6c84602a1144692 +""" + //let tags: [[String]] = [] + let blocks = parse_post_blocks(content: content) + + let mentions = blocks.filter { $0.is_ref } + XCTAssertEqual(mentions.count, 10) + } + + func testManyMentions() throws { + let content = "#[10]" + let tags: [[String]] = [[],[],[],[],[],[],[],[],[],[],["p", "3e999f94e2cb34ef44a64b351141ac4e51b5121b2d31aed4a6c84602a1144692"]] + let blocks = parse_mentions(content: content, tags: tags) + let mentions = blocks.filter { $0.is_mention } + XCTAssertEqual(mentions.count, 1) + } + func testThreadedReply() throws { let content = "this is some content" let tags = [["e", "thread_id"], ["e", "reply_id"]]