From 09557e4b160f95ca341caaa1877bcf46d6587177 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 31 Oct 2021 13:43:04 -0400 Subject: [PATCH] Add more shared code to shared function --- main.go | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index f33289f..84aa7e9 100644 --- a/main.go +++ b/main.go @@ -61,14 +61,11 @@ func main() { }) syncer.OnEvent(store.UpdateState) syncer.OnEventType(event.EventMessage, func(_ mautrix.EventSource, evt *event.Event) { - ret := getMsgResponse(client, evt) - if ret == "" { + retContent := getMsgResponse(client, evt) + if retContent == nil { return } - resp, err := client.SendMessageEvent(evt.RoomID, event.EventMessage, event.MessageEventContent{ - MsgType: event.MsgText, - Body: ret, - }) + resp, err := client.SendMessageEvent(evt.RoomID, event.EventMessage, retContent) if err != nil { log.Errorf("couldn't send event: %v", err) return @@ -84,15 +81,11 @@ func main() { if evt.Type != event.EventMessage { return } - ret := getMsgResponse(client, evt) - if ret == "" { + retContent := getMsgResponse(client, evt) + if retContent == nil { return } - content := event.MessageEventContent{ - MsgType: event.MsgText, - Body: ret, - } - encrypted, err := olmMachine.EncryptMegolmEvent(evt.RoomID, evt.Type, content) + encrypted, err := olmMachine.EncryptMegolmEvent(evt.RoomID, evt.Type, retContent) if err != nil { if isBadEncryptError(err) { log.Errorf("couldn't encrypt event: %v", err) @@ -104,7 +97,7 @@ func main() { log.Errorf("couldn't share group session: %v", err) return } - encrypted, err = olmMachine.EncryptMegolmEvent(evt.RoomID, evt.Type, content) + encrypted, err = olmMachine.EncryptMegolmEvent(evt.RoomID, evt.Type, retContent) if err != nil { log.Errorf("couldn't encrypt event(2): %v", err) return @@ -135,26 +128,29 @@ func isBadEncryptError(err error) bool { } // this function assumes evt.Type is EventMessage -// return value is the body of the message to send back, if any -func getMsgResponse(client *mautrix.Client, evt *event.Event) string { +// return value is the message content to send back, if any +func getMsgResponse(client *mautrix.Client, evt *event.Event) *event.MessageEventContent { // only respond to messages that were sent in the last five minutes if time.Unix(evt.Timestamp / 1000, evt.Timestamp % 1000).Before(time.Now().Add(time.Minute * -5)) { - return "" + return nil } content := evt.Content.AsMessage() if content.MsgType != event.MsgText { - return "" + return nil } mscs := getMSCs(content.Body) - ret := "" + retBody := "" for i, msc := range mscs { log.Infof("MSC: %v %v\n", evt.ID, msc) if i > 0 { - ret += "\n" + retBody += "\n" } - ret += fmt.Sprintf("https://github.com/matrix-org/matrix-doc/pull/%v", msc) + retBody += fmt.Sprintf("https://github.com/matrix-org/matrix-doc/pull/%v", msc) + } + return &event.MessageEventContent{ + MsgType: event.MsgText, + Body: retBody, } - return ret } func getMSCs(body string) (mscs []string) { -- 2.38.4