2021-07-21 18:52:45 +00:00
|
|
|
package counter
|
|
|
|
|
|
|
|
import (
|
2021-07-29 16:33:03 +00:00
|
|
|
"embed"
|
2021-07-21 18:52:45 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-11-28 19:12:38 +00:00
|
|
|
"io/ioutil"
|
2021-07-21 18:52:45 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
)
|
|
|
|
|
2021-07-29 16:33:03 +00:00
|
|
|
//go:embed *.html
|
|
|
|
var embeddedFS embed.FS
|
|
|
|
|
2021-07-21 18:52:45 +00:00
|
|
|
func (p *CounterPlugin) registerWeb() {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.HandleFunc("/api/users/{user}/items/{item}/increment", p.mkIncrementAPI(1))
|
|
|
|
r.HandleFunc("/api/users/{user}/items/{item}/decrement", p.mkIncrementAPI(-1))
|
|
|
|
r.HandleFunc("/api", p.handleCounterAPI)
|
|
|
|
r.HandleFunc("/", p.handleCounter)
|
2021-07-28 15:32:59 +00:00
|
|
|
p.b.RegisterWebName(r, "/counter", "Counter")
|
2021-07-21 18:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CounterPlugin) mkIncrementAPI(delta int) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-07-28 15:32:59 +00:00
|
|
|
userName := chi.URLParam(r, "user")
|
|
|
|
itemName := chi.URLParam(r, "item")
|
|
|
|
|
|
|
|
secret, pass, ok := r.BasicAuth()
|
|
|
|
if !ok || !p.b.CheckPassword(secret, pass) {
|
|
|
|
err := fmt.Errorf("unauthorized access")
|
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("error authenticating user")
|
|
|
|
w.WriteHeader(401)
|
|
|
|
j, _ := json.Marshal(struct {
|
|
|
|
Status bool
|
|
|
|
Error string
|
|
|
|
}{false, err.Error()})
|
|
|
|
fmt.Fprint(w, string(j))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find an ID if possible
|
|
|
|
u, err := p.b.DefaultConnector().Profile(userName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error finding user")
|
|
|
|
w.WriteHeader(400)
|
|
|
|
j, _ := json.Marshal(struct {
|
|
|
|
Status bool
|
|
|
|
Error error
|
|
|
|
}{false, err})
|
|
|
|
fmt.Fprint(w, string(j))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err := GetUserItem(p.db, userName, u.ID, itemName)
|
2021-07-21 18:52:45 +00:00
|
|
|
if err != nil {
|
2021-07-28 15:32:59 +00:00
|
|
|
log.Error().Err(err).Msg("error finding item")
|
|
|
|
w.WriteHeader(400)
|
|
|
|
j, _ := json.Marshal(struct {
|
|
|
|
Status bool
|
|
|
|
Error error
|
|
|
|
}{false, err})
|
|
|
|
fmt.Fprint(w, string(j))
|
2021-07-21 18:52:45 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-28 19:12:38 +00:00
|
|
|
|
|
|
|
body, _ := ioutil.ReadAll(r.Body)
|
|
|
|
postData := map[string]string{}
|
|
|
|
err = json.Unmarshal(body, &postData)
|
|
|
|
personalMsg := ""
|
|
|
|
if inputMsg, ok := postData["message"]; ok {
|
|
|
|
personalMsg = fmt.Sprintf("\nMessage: %s", inputMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
chs := p.cfg.GetArray("channels", []string{p.cfg.Get("channels", "none")})
|
2021-07-21 18:52:45 +00:00
|
|
|
req := &bot.Request{
|
|
|
|
Conn: p.b.DefaultConnector(),
|
|
|
|
Kind: bot.Message,
|
|
|
|
Msg: msg.Message{
|
2021-11-28 19:12:38 +00:00
|
|
|
User: &u,
|
|
|
|
// Noting here that we're only going to do goals in a "default"
|
|
|
|
// channel even if it should send updates to others.
|
|
|
|
Channel: chs[0],
|
|
|
|
Body: fmt.Sprintf("%s += %d", itemName, delta),
|
|
|
|
Time: time.Now(),
|
2021-07-21 18:52:45 +00:00
|
|
|
},
|
|
|
|
Values: nil,
|
|
|
|
Args: nil,
|
|
|
|
}
|
2021-11-28 19:12:38 +00:00
|
|
|
msg := fmt.Sprintf("%s changed their %s counter by %d for a total of %d via the amazing %s API. %s",
|
2021-12-04 21:01:11 +00:00
|
|
|
userName, itemName, delta, item.Count+delta, p.cfg.Get("nick", "catbase"), personalMsg)
|
2021-11-28 19:12:38 +00:00
|
|
|
for _, ch := range chs {
|
2021-07-21 18:52:45 +00:00
|
|
|
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
|
2021-11-18 22:18:17 +00:00
|
|
|
req.Msg.Channel = ch
|
2021-07-21 18:52:45 +00:00
|
|
|
}
|
2021-12-04 21:01:11 +00:00
|
|
|
item.UpdateDelta(req, delta)
|
2021-07-21 18:52:45 +00:00
|
|
|
j, _ := json.Marshal(struct{ Status bool }{true})
|
|
|
|
fmt.Fprint(w, string(j))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CounterPlugin) handleCounter(w http.ResponseWriter, r *http.Request) {
|
2021-07-29 16:33:03 +00:00
|
|
|
index, _ := embeddedFS.ReadFile("index.html")
|
|
|
|
w.Write(index)
|
2021-07-21 18:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
info := struct {
|
|
|
|
User string
|
|
|
|
Thing string
|
|
|
|
Action string
|
|
|
|
Password string
|
|
|
|
}{}
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&info)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug().
|
|
|
|
Interface("postbody", info).
|
|
|
|
Msg("Got a POST")
|
|
|
|
if p.b.CheckPassword("", info.Password) {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
j, _ := json.Marshal(struct{ Err string }{Err: "Invalid Password"})
|
|
|
|
w.Write(j)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nick, id := p.resolveUser(bot.Request{Conn: p.b.DefaultConnector()}, info.User)
|
|
|
|
item, err := GetUserItem(p.db, nick, id, info.Thing)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Str("subject", info.User).
|
|
|
|
Str("itemName", info.Thing).
|
|
|
|
Msg("error finding item")
|
|
|
|
w.WriteHeader(404)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.Action == "++" {
|
|
|
|
item.UpdateDelta(nil, 1)
|
|
|
|
} else if info.Action == "--" {
|
|
|
|
item.UpdateDelta(nil, -1)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
fmt.Fprint(w, "Invalid increment")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
all, err := GetAllItems(p.db)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(all)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, string(data))
|
|
|
|
}
|
|
|
|
|
2021-11-18 22:18:17 +00:00
|
|
|
// Update represents a change that gets sent off to other plugins such as goals
|
2021-07-21 18:52:45 +00:00
|
|
|
type Update struct {
|
2021-11-18 22:18:17 +00:00
|
|
|
// Username to be displayed/recorded
|
|
|
|
Who string `json:"who"`
|
|
|
|
// Counter Item
|
|
|
|
What string `json:"what"`
|
|
|
|
// Total counter amount
|
|
|
|
Amount int `json:"amount"`
|
2021-07-21 18:52:45 +00:00
|
|
|
}
|