~edwargix/msc-link-bot

eecc65c89bdd056feb74583528c5d828cf1ed5fc — David Florness 2 years ago a703ceb
Don't include MSCs that are in quoted text

Otherwise the bot will likely post the same link multiple times in a discussion.
2 files changed, 37 insertions(+), 5 deletions(-)

M main.go
A msc_test.go
M main.go => main.go +12 -5
@@ 9,6 9,7 @@ import (
	"os"
	"regexp"
	"strconv"
	"strings"
	"time"

	_ "github.com/mattn/go-sqlite3"


@@ 155,11 156,17 @@ func getMsgResponse(client *mautrix.Client, evt *event.Event) *event.MessageEven
}

func getMSCs(body string) (mscs []uint) {
	matches := MSC_REGEX.FindAllStringSubmatch(body, -1)
	for _, match := range matches {
		// error can never happen because of %d in regex
		msc, _ := strconv.Atoi(match[1])
		mscs = append(mscs, uint(msc))
	lines := strings.Split(body, "\n")
	for _, line := range lines {
		if len(line) == 0 || line[0] == '>' {
			continue
		}
		matches := MSC_REGEX.FindAllStringSubmatch(line, -1)
		for _, match := range matches {
			// error can never happen because of %d in regex
			msc, _ := strconv.Atoi(match[1])
			mscs = append(mscs, uint(msc))
		}
	}
	return mscs
}

A msc_test.go => msc_test.go +25 -0
@@ 0,0 1,25 @@
package main

import "testing"

func TestQuote(t *testing.T) {
	mscs := getMSCs("> <@foo:matrix.org> MSC2444 just got merged\n\nwew lad; msc2444 has been a long time coming")
	if len(mscs) != 1 {
		t.Fail()
	} else if mscs[0] != 2444 {
		t.Fail()
	}
}

func TestCapitalization(t *testing.T) {
	mscs := getMSCs("msc123 foo bar baz MSC234\nfoo bar MsC345 MSC456")
	if len(mscs) != 3 {
		t.Fail()
	} else if mscs[0] != 123 {
		t.Fail()
	} else if mscs[1] != 234 {
		t.Fail()
	} else if mscs[2] != 456 {
		t.Fail()
	}
}