2020-10-21 14:02:59 +00:00
|
|
|
package meme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-03-13 16:34:27 +00:00
|
|
|
"github.com/ggicci/httpin"
|
2020-10-21 14:02:59 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sort"
|
|
|
|
|
2021-07-21 18:52:45 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-10-21 14:02:59 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
)
|
|
|
|
|
2020-10-21 19:55:28 +00:00
|
|
|
func (p *MemePlugin) registerWeb(c bot.Connector) {
|
2021-07-21 18:52:45 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.HandleFunc("/slash", p.slashMeme(c))
|
2024-02-27 22:16:45 +00:00
|
|
|
r.Get("/img", p.img)
|
2024-03-13 16:34:27 +00:00
|
|
|
r.With(httpin.NewInput(SaveReq{})).
|
|
|
|
Put("/save/{name}", p.saveMeme)
|
|
|
|
r.With(httpin.NewInput(SaveReq{})).
|
|
|
|
Post("/add", p.saveMeme)
|
2024-02-27 22:16:45 +00:00
|
|
|
r.Delete("/rm/{name}", p.rmMeme)
|
|
|
|
r.Get("/edit/{name}", p.editMeme)
|
|
|
|
r.Get("/", p.webRoot)
|
2024-02-27 19:29:54 +00:00
|
|
|
p.bot.GetWeb().RegisterWebName(r, "/meme", "Memes")
|
2020-10-21 19:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type webResp struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Config string `json:"config"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type webResps []webResp
|
|
|
|
|
|
|
|
func (w webResps) Len() int { return len(w) }
|
|
|
|
func (w webResps) Swap(i, j int) { w[i], w[j] = w[j], w[i] }
|
|
|
|
|
|
|
|
type ByName struct{ webResps }
|
|
|
|
|
|
|
|
func (s ByName) Less(i, j int) bool { return s.webResps[i].Name < s.webResps[j].Name }
|
|
|
|
|
2024-02-27 22:16:45 +00:00
|
|
|
func (p *MemePlugin) all() webResps {
|
2020-10-21 14:02:59 +00:00
|
|
|
memes := p.c.GetMap("meme.memes", defaultFormats)
|
2020-10-21 19:55:28 +00:00
|
|
|
configs := p.c.GetMap("meme.memeconfigs", map[string]string{})
|
|
|
|
|
2020-10-21 14:02:59 +00:00
|
|
|
values := webResps{}
|
|
|
|
for n, u := range memes {
|
2020-10-21 19:55:28 +00:00
|
|
|
config, ok := configs[n]
|
|
|
|
if !ok {
|
2024-02-27 22:16:45 +00:00
|
|
|
b, _ := json.MarshalIndent(p.defaultFormatConfig(), " ", " ")
|
2020-10-21 19:55:28 +00:00
|
|
|
config = string(b)
|
|
|
|
}
|
2020-10-21 14:02:59 +00:00
|
|
|
realURL, err := url.Parse(u)
|
|
|
|
if err != nil || realURL.Scheme == "" {
|
2021-02-18 22:37:24 +00:00
|
|
|
values = append(values, webResp{n, "404.png", config})
|
|
|
|
log.Error().Err(err).Msgf("invalid URL")
|
|
|
|
continue
|
2020-10-21 14:02:59 +00:00
|
|
|
}
|
2020-10-21 19:55:28 +00:00
|
|
|
values = append(values, webResp{n, realURL.String(), config})
|
2020-10-21 14:02:59 +00:00
|
|
|
}
|
|
|
|
sort.Sort(ByName{values})
|
|
|
|
|
2024-02-27 22:16:45 +00:00
|
|
|
return values
|
2020-10-21 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mkCheckError(w http.ResponseWriter) func(error) bool {
|
|
|
|
return func(err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("meme failed")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
e, _ := json.Marshal(err)
|
|
|
|
w.Write(e)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) rmMeme(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 22:16:45 +00:00
|
|
|
name := chi.URLParam(r, "name")
|
2020-10-21 14:02:59 +00:00
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
2024-02-27 22:16:45 +00:00
|
|
|
delete(formats, name)
|
|
|
|
err := p.c.SetMap("meme.memes", formats)
|
|
|
|
mkCheckError(w)(err)
|
2020-10-21 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 16:34:27 +00:00
|
|
|
type SaveReq struct {
|
|
|
|
Name string `in:"path=name"`
|
|
|
|
Config string `in:"form=config"`
|
|
|
|
URL string `in:"form=url"`
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:16:45 +00:00
|
|
|
func (p *MemePlugin) saveMeme(w http.ResponseWriter, r *http.Request) {
|
2024-03-13 16:34:27 +00:00
|
|
|
input := r.Context().Value(httpin.Input).(*SaveReq)
|
2020-10-21 14:02:59 +00:00
|
|
|
checkError := mkCheckError(w)
|
2024-02-27 22:16:45 +00:00
|
|
|
|
2020-10-21 14:02:59 +00:00
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
2024-03-13 16:34:27 +00:00
|
|
|
formats[input.Name] = input.URL
|
2024-02-27 22:16:45 +00:00
|
|
|
err := p.c.SetMap("meme.memes", formats)
|
2020-10-21 14:02:59 +00:00
|
|
|
checkError(err)
|
2021-01-11 21:23:55 +00:00
|
|
|
|
2024-03-13 16:34:27 +00:00
|
|
|
if input.Config == "" {
|
|
|
|
input.Config = p.defaultFormatConfigJSON()
|
2021-01-11 21:23:55 +00:00
|
|
|
}
|
2020-10-21 19:55:28 +00:00
|
|
|
configs := p.c.GetMap("meme.memeconfigs", map[string]string{})
|
2024-03-13 16:34:27 +00:00
|
|
|
configs[input.Name] = input.Config
|
2020-10-21 19:55:28 +00:00
|
|
|
err = p.c.SetMap("meme.memeconfigs", configs)
|
|
|
|
checkError(err)
|
2024-02-27 22:16:45 +00:00
|
|
|
|
|
|
|
meme := webResp{
|
2024-03-13 16:34:27 +00:00
|
|
|
Name: input.Name,
|
|
|
|
URL: formats[input.Name],
|
|
|
|
Config: configs[input.Name],
|
2024-02-27 22:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Show(meme).Render(r.Context(), w)
|
2020-10-21 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) webRoot(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 22:16:45 +00:00
|
|
|
p.bot.GetWeb().Index("Meme", p.index(p.all())).Render(r.Context(), w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) editMeme(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := chi.URLParam(r, "name")
|
|
|
|
memes := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
configs := p.c.GetMap("meme.memeconfigs", map[string]string{})
|
|
|
|
meme, ok := memes[name]
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintf(w, "Didn't find that meme.")
|
|
|
|
}
|
|
|
|
resp := webResp{
|
|
|
|
Name: name,
|
|
|
|
URL: meme,
|
|
|
|
Config: configs[name],
|
|
|
|
}
|
|
|
|
p.Edit(resp).Render(r.Context(), w)
|
2020-10-21 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) img(w http.ResponseWriter, r *http.Request) {
|
2021-02-18 22:37:24 +00:00
|
|
|
q := r.URL.Query()
|
|
|
|
spec := q.Get("spec")
|
|
|
|
if spec == "" {
|
|
|
|
log.Debug().Msgf("No spec found for img")
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := SpecFromJSON([]byte(spec))
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := p.genMeme(s)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
w.Write(img)
|
2020-10-21 14:02:59 +00:00
|
|
|
} else {
|
2021-02-18 22:37:24 +00:00
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Interface("spec", s).
|
|
|
|
Msg("Unable to generate meme image")
|
2020-10-21 14:02:59 +00:00
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte("not found"))
|
|
|
|
}
|
|
|
|
p.images.cleanup()
|
|
|
|
}
|