From 13d63a93c07ef690368e7d457822d9c1d106db95 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 11 Apr 2022 14:41:00 +0100 Subject: [PATCH] Improve typing --- clients/clients.go | 23 ++++++++++++++--------- services/github/github.go | 5 +++++ types/service.go | 7 ++++++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/clients/clients.go b/clients/clients.go index e11d10b..46b5bbf 100644 --- a/clients/clients.go +++ b/clients/clients.go @@ -2,6 +2,7 @@ package clients import ( "database/sql" + "encoding/json" "fmt" "net/http" "reflect" @@ -287,11 +288,22 @@ func (c *Clients) onBotOptionsEvent(client *mautrix.Client, event *mevt.Event) { return } // these options fully clobber what was there previously. + + var options types.BotOptionsContent + if err := json.Unmarshal(event.Content.VeryRaw, &options); err != nil { + log.WithFields(log.Fields{ + log.ErrorKey: err, + "room_id": event.RoomID, + "bot_user_id": client.UserID, + "set_by_user_id": event.Sender, + }).Error("Failed to parse bot options") + } + opts := types.BotOptions{ UserID: client.UserID, RoomID: event.RoomID, SetByUserID: event.Sender, - Options: event.Content.Raw, + Options: options, } if _, err := c.db.StoreBotOptions(opts); err != nil { log.WithFields(log.Fields{ @@ -328,13 +340,6 @@ func (c *Clients) onRoomMemberEvent(client *mautrix.Client, event *mevt.Event) { } } -type BotOptionsContent struct { - Github struct { - DefaultRepo string `json:"default_repo,omitempty"` - NewIssueLabels []string `json:"new_issue_labels,omitempty"` - } `json:"github"` -} - func (c *Clients) initClient(botClient *BotClient) error { config := botClient.config client, err := mautrix.NewClient(config.HomeserverURL, config.UserID, config.AccessToken) @@ -354,7 +359,7 @@ func (c *Clients) initClient(botClient *BotClient) error { // Add m.room.bot.options to mautrix's TypeMap so that it parses it as a valid event var StateBotOptions = mevt.Type{Type: "m.room.bot.options", Class: mevt.StateEventType} - mevt.TypeMap[StateBotOptions] = reflect.TypeOf(&BotOptionsContent{}) + mevt.TypeMap[StateBotOptions] = reflect.TypeOf(&types.BotOptionsContent{}) nebStore := &matrix.NEBStore{ InMemoryStore: *mautrix.NewInMemoryStore(), diff --git a/services/github/github.go b/services/github/github.go index 85f4c2a..ae71aac 100644 --- a/services/github/github.go +++ b/services/github/github.go @@ -76,6 +76,11 @@ type Service struct { RealmID string } +type Options struct { + DefaultRepo string `json:"default_repo,omitempty"` + NewIssueLabels []string `json:"new_issue_labels,omitempty"` +} + func (s *Service) requireGithubClientFor(userID id.UserID) (cli *gogithub.Client, resp interface{}, err error) { cli = s.githubClientFor(userID, false) if cli == nil { diff --git a/types/service.go b/types/service.go index 5f208d0..f567e20 100644 --- a/types/service.go +++ b/types/service.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "encoding/json" "errors" + "github.com/matrix-org/go-neb/services/github" "net/http" "strings" "time" @@ -13,12 +14,16 @@ import ( "maunium.net/go/mautrix/id" ) +type BotOptionsContent struct { + Github github.Options `json:"github"` +} + // BotOptions for a given bot user in a given room type BotOptions struct { RoomID id.RoomID UserID id.UserID SetByUserID id.UserID - Options map[string]interface{} + Options BotOptionsContent } // Poller represents a thing which can poll. Services should implement this method signature to support polling.