Browse Source

Improve typing

pull/358/head
Michael Telatynski 3 years ago
parent
commit
13d63a93c0
  1. 23
      clients/clients.go
  2. 5
      services/github/github.go
  3. 7
      types/service.go

23
clients/clients.go

@ -2,6 +2,7 @@ package clients
import ( import (
"database/sql" "database/sql"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"reflect" "reflect"
@ -287,11 +288,22 @@ func (c *Clients) onBotOptionsEvent(client *mautrix.Client, event *mevt.Event) {
return return
} }
// these options fully clobber what was there previously. // 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{ opts := types.BotOptions{
UserID: client.UserID, UserID: client.UserID,
RoomID: event.RoomID, RoomID: event.RoomID,
SetByUserID: event.Sender, SetByUserID: event.Sender,
Options: event.Content.Raw,
Options: options,
} }
if _, err := c.db.StoreBotOptions(opts); err != nil { if _, err := c.db.StoreBotOptions(opts); err != nil {
log.WithFields(log.Fields{ 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 { func (c *Clients) initClient(botClient *BotClient) error {
config := botClient.config config := botClient.config
client, err := mautrix.NewClient(config.HomeserverURL, config.UserID, config.AccessToken) 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 // 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} 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{ nebStore := &matrix.NEBStore{
InMemoryStore: *mautrix.NewInMemoryStore(), InMemoryStore: *mautrix.NewInMemoryStore(),

5
services/github/github.go

@ -76,6 +76,11 @@ type Service struct {
RealmID string 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) { func (s *Service) requireGithubClientFor(userID id.UserID) (cli *gogithub.Client, resp interface{}, err error) {
cli = s.githubClientFor(userID, false) cli = s.githubClientFor(userID, false)
if cli == nil { if cli == nil {

7
types/service.go

@ -4,6 +4,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/matrix-org/go-neb/services/github"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -13,12 +14,16 @@ import (
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
) )
type BotOptionsContent struct {
Github github.Options `json:"github"`
}
// BotOptions for a given bot user in a given room // BotOptions for a given bot user in a given room
type BotOptions struct { type BotOptions struct {
RoomID id.RoomID RoomID id.RoomID
UserID id.UserID UserID id.UserID
SetByUserID 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. // Poller represents a thing which can poll. Services should implement this method signature to support polling.

Loading…
Cancel
Save