From 03c79fc5f1af69fe2f3d5caa409dca0a820d94f7 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 17 Jul 2022 18:19:11 -0400 Subject: [PATCH] Don't send the same MSC more than once in the same message --- main.go | 7 +++++++ msc_test.go | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/main.go b/main.go index 480840c..852602c 100644 --- a/main.go +++ b/main.go @@ -158,9 +158,16 @@ func getMsgResponse(client *mautrix.Client, evt *event.Event) *event.MessageEven func getMSCs(body string) (mscs []uint) { bodyNoReplies := event.TrimReplyFallbackText(body) matches := MSC_REGEX.FindAllStringSubmatch(bodyNoReplies, -1) + mscSet := make(map[int]struct{}) for _, match := range matches { // error can never happen because of %d in regex msc, _ := strconv.Atoi(match[1]) + _, exists := mscSet[msc] + if exists { + // don't add the same MSC twice + continue + } + mscSet[msc] = struct{}{} mscs = append(mscs, uint(msc)) } return mscs diff --git a/msc_test.go b/msc_test.go index 68afee8..76588f4 100644 --- a/msc_test.go +++ b/msc_test.go @@ -23,3 +23,12 @@ func TestCapitalization(t *testing.T) { t.Fail() } } + +func TestNoRepeats(t *testing.T) { + mscs := getMSCs("That msc seems to be the last prerequisite for MSC2677: https://github.com/uhoreg/matrix-doc/blob/aggregations-reactions/proposals/2677-reactions.md#msc2677-annotations-and-reactions") + if len(mscs) != 1 { + t.Fail() + } else if mscs[0] != 2677 { + t.Fail() + } +} -- 2.38.4