~edwargix/msc-link-bot

03c79fc5f1af69fe2f3d5caa409dca0a820d94f7 — David Florness 1 year, 9 months ago 8bd53cd
Don't send the same MSC more than once in the same message
2 files changed, 16 insertions(+), 0 deletions(-)

M main.go
M msc_test.go
M main.go => main.go +7 -0
@@ 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

M msc_test.go => msc_test.go +9 -0
@@ 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()
	}
}