mirror of https://github.com/velour/catbase.git
Compare commits
No commits in common. "9cecccfcdddf0a4c39288f907c48dd0b842c09c9" and "f2153bf0b4b2fa8a103b6f039d5318d5668be56b" have entirely different histories.
9cecccfcdd
...
f2153bf0b4
|
@ -30,8 +30,8 @@ func (p *CounterPlugin) registerWeb() {
|
||||||
subrouter.Use(httprate.LimitByIP(requests, dur))
|
subrouter.Use(httprate.LimitByIP(requests, dur))
|
||||||
subrouter.HandleFunc("/api/users/{user}/items/{item}/increment/{delta}", p.mkIncrementByNAPI(1))
|
subrouter.HandleFunc("/api/users/{user}/items/{item}/increment/{delta}", p.mkIncrementByNAPI(1))
|
||||||
subrouter.HandleFunc("/api/users/{user}/items/{item}/decrement/{delta}", p.mkIncrementByNAPI(-1))
|
subrouter.HandleFunc("/api/users/{user}/items/{item}/decrement/{delta}", p.mkIncrementByNAPI(-1))
|
||||||
subrouter.HandleFunc("/api/users/{user}/items/{item}/increment", p.mkIncrementByNAPI(1))
|
subrouter.HandleFunc("/api/users/{user}/items/{item}/increment", p.mkIncrementAPI(1))
|
||||||
subrouter.HandleFunc("/api/users/{user}/items/{item}/decrement", p.mkIncrementByNAPI(-1))
|
subrouter.HandleFunc("/api/users/{user}/items/{item}/decrement", p.mkIncrementAPI(-1))
|
||||||
r.Mount("/", subrouter)
|
r.Mount("/", subrouter)
|
||||||
r.HandleFunc("/api", p.handleCounterAPI)
|
r.HandleFunc("/api", p.handleCounterAPI)
|
||||||
r.HandleFunc("/", p.handleCounter)
|
r.HandleFunc("/", p.handleCounter)
|
||||||
|
@ -42,10 +42,7 @@ func (p *CounterPlugin) mkIncrementByNAPI(direction int) func(w http.ResponseWri
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userName, _ := url.QueryUnescape(chi.URLParam(r, "user"))
|
userName, _ := url.QueryUnescape(chi.URLParam(r, "user"))
|
||||||
itemName, _ := url.QueryUnescape(chi.URLParam(r, "item"))
|
itemName, _ := url.QueryUnescape(chi.URLParam(r, "item"))
|
||||||
delta, err := strconv.Atoi(chi.URLParam(r, "delta"))
|
delta, _ := strconv.Atoi(chi.URLParam(r, "delta"))
|
||||||
if err != nil || delta == 0 {
|
|
||||||
delta = direction
|
|
||||||
}
|
|
||||||
|
|
||||||
secret, pass, ok := r.BasicAuth()
|
secret, pass, ok := r.BasicAuth()
|
||||||
if !ok || !p.b.CheckPassword(secret, pass) {
|
if !ok || !p.b.CheckPassword(secret, pass) {
|
||||||
|
@ -120,6 +117,84 @@ func (p *CounterPlugin) mkIncrementByNAPI(direction int) func(w http.ResponseWri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *CounterPlugin) mkIncrementAPI(delta int) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userName, _ := url.QueryUnescape(chi.URLParam(r, "user"))
|
||||||
|
itemName, _ := url.QueryUnescape(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
|
||||||
|
id := ""
|
||||||
|
u, err := p.b.DefaultConnector().Profile(userName)
|
||||||
|
if err == nil {
|
||||||
|
id = u.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
item, err := GetUserItem(p.db, userName, id, itemName)
|
||||||
|
if err != nil {
|
||||||
|
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))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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.GetMap("counter.channelItems", map[string]string{})
|
||||||
|
ch, ok := chs[itemName]
|
||||||
|
if len(chs) == 0 || !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req := &bot.Request{
|
||||||
|
Conn: p.b.DefaultConnector(),
|
||||||
|
Kind: bot.Message,
|
||||||
|
Msg: msg.Message{
|
||||||
|
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: ch,
|
||||||
|
Body: fmt.Sprintf("%s += %d", itemName, delta),
|
||||||
|
Time: time.Now(),
|
||||||
|
},
|
||||||
|
Values: nil,
|
||||||
|
Args: nil,
|
||||||
|
}
|
||||||
|
msg := fmt.Sprintf("%s changed their %s counter by %d for a total of %d via the amazing %s API. %s",
|
||||||
|
userName, itemName, delta, item.Count+delta, p.cfg.Get("nick", "catbase"), personalMsg)
|
||||||
|
for _, ch := range chs {
|
||||||
|
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
|
||||||
|
req.Msg.Channel = ch
|
||||||
|
}
|
||||||
|
item.UpdateDelta(req, delta)
|
||||||
|
j, _ := json.Marshal(struct{ Status bool }{true})
|
||||||
|
fmt.Fprint(w, string(j))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *CounterPlugin) handleCounter(w http.ResponseWriter, r *http.Request) {
|
func (p *CounterPlugin) handleCounter(w http.ResponseWriter, r *http.Request) {
|
||||||
index, _ := embeddedFS.ReadFile("index.html")
|
index, _ := embeddedFS.ReadFile("index.html")
|
||||||
w.Write(index)
|
w.Write(index)
|
||||||
|
|
Loading…
Reference in New Issue