Skip to content

Commit

Permalink
fix: parse multi reply with same message for all
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 20, 2023
1 parent 0c290f9 commit 660503d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
21 changes: 20 additions & 1 deletion internal/fourchan/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ func explodePost(post Post) (posts []Post) {
}

// Add simple 1/1 reply/comment messages
if len(replies) == len(findings) && (len(replies) == 1 || !containsEmptyString(replies)) {
if len(replies) == len(findings) &&
(len(replies) == 1 || allEmptyButLast(replies) || !containsEmptyString(replies)) {

// squash many/1 reply/comment into 1/1
if allEmptyButLast(replies) {
replies = []string{replies[len(replies)-1]}
}

for i, reply := range replies {
parentId := getParentId(findings[i])
if parentId == 0 {
Expand Down Expand Up @@ -100,6 +107,18 @@ func explodePost(post Post) (posts []Post) {
return
}

func allEmptyButLast(replies []string) bool {
if len(replies) == 0 {
return false
}
for _, reply := range replies[:len(replies)-1] {
if reply != "" {
return false
}
}
return replies[len(replies)-1] != ""
}

func fixComments(comments []string) (ret []string) {
for _, comment := range comments {
ret = append(ret, plaintextComment(cleanComment(comment)))
Expand Down
27 changes: 26 additions & 1 deletion internal/fourchan/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestExplodeNPosts(t *testing.T) {
func TestNParents(t *testing.T) {
thread := toThread(testThread)
got := len(thread.posts)
expected := 8
expected := 7
if got != expected {
t.Errorf("got %d expected %d", got, expected)
}
Expand Down Expand Up @@ -190,3 +190,28 @@ func TestCleanComment(t *testing.T) {
}
}
}

func TestAllEmptyButLast(t *testing.T) {
testReplies := []struct {
replies []string
expected bool
}{
{[]string{"", "", "foo"}, true},
{[]string{"foo", "bar"}, false},
{[]string{"", "foo"}, true},
{[]string{}, false},
}
for _, reply := range testReplies {
got := allEmptyButLast(reply.replies)
expected := reply.expected
if expected != got {
t.Errorf(
"got %t expected %t - (%d)%+v",
got,
expected,
len(reply.replies),
reply.replies,
)
}
}
}

0 comments on commit 660503d

Please sign in to comment.