Add command spec parsing/handler execution

This commit is contained in:
Tulir Asokan 2018-06-20 22:25:33 +03:00
parent 307a32e0c0
commit 4536fcfe62
9 changed files with 282 additions and 37 deletions

View file

@ -20,6 +20,7 @@ import (
"maubot.xyz"
log "maunium.net/go/maulogger"
"database/sql"
"sort"
)
type MatrixClient struct {
@ -37,7 +38,7 @@ type MatrixClient struct {
DisplayName string `json:"display_name"`
AvatarURL string `json:"avatar_url"`
Commands map[string]*CommandSpec `json:"commandspecs"`
CommandSpecs map[string]*CommandSpec `json:"command_specs"`
}
type MatrixClientStatic struct {
@ -91,32 +92,63 @@ func (mcs *MatrixClientStatic) New() *MatrixClient {
func (mxc *MatrixClient) Scan(row Scannable) *MatrixClient {
err := row.Scan(&mxc.UserID, &mxc.Homeserver, &mxc.AccessToken, &mxc.NextBatch, &mxc.FilterID, &mxc.Sync, &mxc.AutoJoinRooms, &mxc.DisplayName, &mxc.AvatarURL)
if err != nil {
log.Fatalln("Database scan failed:", err)
log.Errorln("MatrixClient scan failed:", err)
return mxc
}
mxc.LoadCommandSpecs()
return mxc
}
func (mxc *MatrixClient) SetCommandSpec(owner string, newSpec *maubot.CommandSpec) bool {
spec := mxc.db.CommandSpec.GetOrCreate(owner, mxc.UserID)
if newSpec.Equals(spec.CommandSpec) {
spec, ok := mxc.CommandSpecs[owner]
if ok && newSpec.Equals(spec.CommandSpec) {
return false
}
spec.CommandSpec = newSpec
spec.Update()
mxc.Commands[owner] = spec
if spec == nil {
spec = mxc.db.CommandSpec.New()
spec.CommandSpec = newSpec
spec.Insert()
} else {
spec.CommandSpec = newSpec
spec.Update()
}
mxc.CommandSpecs[owner] = spec
return true
}
func (mxc *MatrixClient) LoadCommandSpecs() *MatrixClient {
specs := mxc.db.CommandSpec.GetAllByClient(mxc.UserID)
mxc.Commands = make(map[string]*CommandSpec)
mxc.CommandSpecs = make(map[string]*CommandSpec)
for _, spec := range specs {
mxc.Commands[spec.Owner] = spec
mxc.CommandSpecs[spec.Owner] = spec
}
log.Debugln("Loaded command specs:", mxc.CommandSpecs)
return mxc
}
func (mxc *MatrixClient) CommandSpecIDs() []string {
keys := make([]string, len(mxc.CommandSpecs))
i := 0
for key := range mxc.CommandSpecs {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
func (mxc *MatrixClient) Commands() *maubot.CommandSpec {
if len(mxc.CommandSpecs) == 0 {
return &maubot.CommandSpec{}
}
specIDs := mxc.CommandSpecIDs()
spec := mxc.CommandSpecs[specIDs[0]].Clone()
for _, specID := range specIDs[1:] {
spec.Merge(mxc.CommandSpecs[specID].CommandSpec)
}
return spec
}
func (mxc *MatrixClient) Insert() error {
_, err := mxc.sql.Exec("INSERT INTO matrix_client (user_id, homeserver, access_token, next_batch, filter_id, sync, autojoin, display_name, avatar_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
mxc.UserID, mxc.Homeserver, mxc.AccessToken, mxc.NextBatch, mxc.FilterID, mxc.Sync, mxc.AutoJoinRooms, mxc.DisplayName, mxc.AvatarURL)

View file

@ -54,11 +54,11 @@ func (css *CommandSpecStatic) CreateTable() error {
}
func (css *CommandSpecStatic) Get(owner, client string) *CommandSpec {
row := css.sql.QueryRow("SELECT * FROM command_spec WHERE owner=? AND client=?", owner, client)
if row != nil {
return css.New().Scan(row)
rows, err := css.sql.Query("SELECT * FROM command_spec WHERE owner=? AND client=?", owner, client)
if err != nil {
log.Errorf("Failed to Get(%s, %s): %v\n", owner, client, err)
}
return nil
return css.New().Scan(rows)
}
func (css *CommandSpecStatic) GetOrCreate(owner, client string) (spec *CommandSpec) {
@ -74,13 +74,15 @@ func (css *CommandSpecStatic) GetOrCreate(owner, client string) (spec *CommandSp
func (css *CommandSpecStatic) getAllByQuery(query string, args ...interface{}) (specs []*CommandSpec) {
rows, err := css.sql.Query(query, args...)
if err != nil || rows == nil {
if err != nil {
log.Errorf("Failed to getAllByQuery(%s): %v\n", query, err)
return nil
}
defer rows.Close()
for rows.Next() {
specs = append(specs, css.New().Scan(rows))
}
log.Debugln("getAllByQuery() =", specs)
return
}
@ -103,9 +105,14 @@ func (cs *CommandSpec) Scan(row Scannable) *CommandSpec {
var spec string
err := row.Scan(&cs.Owner, &cs.Client, &spec)
if err != nil {
log.Fatalln("Database scan failed:", err)
log.Errorln("CommandSpec scan failed:", err)
return cs
}
cs.CommandSpec = &maubot.CommandSpec{}
err = json.Unmarshal([]byte(spec), cs.CommandSpec)
if err != nil {
log.Errorln("CommandSpec parse failed:", err)
}
json.Unmarshal([]byte(spec), &cs.CommandSpec)
return cs
}

View file

@ -62,6 +62,11 @@ func (db *Database) CreateTables() {
if err != nil {
log.Errorln("Failed to create plugin table:", err)
}
err = db.CommandSpec.CreateTable()
if err != nil {
log.Errorln("Failed to create command_spec table:", err)
}
}
func (db *Database) SQL() *sql.DB {

View file

@ -87,7 +87,7 @@ func (ps *PluginStatic) New() *Plugin {
func (p *Plugin) Scan(row Scannable) *Plugin {
err := row.Scan(&p.ID, &p.Type, &p.Enabled, &p.UserID)
if err != nil {
log.Fatalln("Database scan failed:", err)
log.Errorln("Plugin scan failed:", err)
}
return p
}