From 789409e159ac68a7eaccabb49692f08fe7345e75 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 21 Oct 2020 15:55:28 -0400 Subject: [PATCH] meme: add web config --- config/config.go | 1 + plugins/meme/meme.go | 63 +++++++++++++++---------------------- plugins/meme/web.go | 17 +++++++--- plugins/meme/webHandlers.go | 43 +++++++++++++++++++++++-- 4 files changed, 79 insertions(+), 45 deletions(-) diff --git a/config/config.go b/config/config.go index a818026..beec0a0 100644 --- a/config/config.go +++ b/config/config.go @@ -103,6 +103,7 @@ func (c *Config) GetMap(key string, fallback map[string]string) map[string]strin vals := map[string]string{} err := json.Unmarshal([]byte(content), &vals) if err != nil { + log.Error().Err(err).Msgf("Could not decode config for %s", key) return fallback } return vals diff --git a/plugins/meme/meme.go b/plugins/meme/meme.go index 3232e64..c0572fa 100644 --- a/plugins/meme/meme.go +++ b/plugins/meme/meme.go @@ -102,30 +102,6 @@ func (p *MemePlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, a return true } -func (p *MemePlugin) registerWeb(c bot.Connector) { - http.HandleFunc("/slash/meme", p.slashMeme(c)) - http.HandleFunc("/meme/img/", p.img) - http.HandleFunc("/meme/all", p.all) - http.HandleFunc("/meme/add", p.addMeme) - http.HandleFunc("/meme/rm", p.rmMeme) - http.HandleFunc("/meme", p.webRoot) - p.bot.RegisterWeb("/meme", "Memes") -} - -type webResp struct { - Name string `json:"name"` - URL string `json:"url"` -} - -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 } - func (p *MemePlugin) bully(c bot.Connector, format, id string) image.Image { bullyIcon := "" @@ -185,27 +161,33 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin message += c.Text + "\n" } } else { - top, bottom := "", message if strings.Contains(message, "||") { parts = strings.Split(message, "||") } else { parts = strings.Split(message, "\n") } - if len(parts) > 1 { - top, bottom = parts[0], parts[1] + + allConfigs := p.c.GetMap("meme.memeconfigs", map[string]string{}) + configtxt, ok := allConfigs[format] + if !ok { + config = defaultFormatConfig + log.Debug().Msgf("Did not find %s in %+v", format, allConfigs) + } else { + err = json.Unmarshal([]byte(configtxt), &config) + if err != nil { + log.Error().Err(err).Msgf("Could not parse config for %s:\n%s", format, configtxt) + config = defaultFormatConfig + } } - if top == "_" { - message = bottom - } else if bottom == "_" { - message = top - } - - topPos := p.bot.Config().GetFloat64("meme.top", 0.05) - bottomPos := p.bot.Config().GetFloat64("meme.bottom", 0.95) - config = []memeText{ - {Text: top, XPerc: 0.5, YPerc: topPos}, - {Text: bottom, XPerc: 0.5, YPerc: bottomPos}, + j := 0 + for i := range config { + if len(parts) > i { + if parts[j] != "_" { + config[i].Text = parts[j] + } + j++ + } } } @@ -343,6 +325,11 @@ func (p *MemePlugin) findFontSize(config []memeText, w, h int, sizes []float64) return fontSize } +var defaultFormatConfig = []memeText{ + {XPerc: 0.5, YPerc: 0.05}, + {XPerc: 0.5, YPerc: 0.95}, +} + func (p *MemePlugin) genMeme(meme string, bully image.Image, config []memeText) (string, int, int, error) { fontSizes := []float64{48, 36, 24, 16, 12} formats := p.c.GetMap("meme.memes", defaultFormats) diff --git a/plugins/meme/web.go b/plugins/meme/web.go index 58f8dc9..3a799e0 100644 --- a/plugins/meme/web.go +++ b/plugins/meme/web.go @@ -38,13 +38,16 @@ var memeIndex = ` - + - + - + + + + Add Meme @@ -54,6 +57,9 @@ var memeIndex = ` fixed :items="results" :fields="fields"> + @@ -77,10 +83,12 @@ var memeIndex = ` nav: {{ .Nav }}, name: "", url: "", + config: "", results: [], fields: [ { key: 'name', sortable: true }, { key: 'url', sortable: true }, + { key: 'config' }, { key: 'image' } ] }, @@ -103,11 +111,12 @@ var memeIndex = ` evt.stopPropagation() } if (this.name && this.url) - axios.post('/meme/add', {Name: this.name, URL: this.url}) + axios.post('/meme/add', {name: this.name, url: this.url, config: this.config}) .then(resp => { this.results = resp.data; this.name = ""; this.url = ""; + this.config = ""; this.refresh(); }) .catch(err => (this.err = err)); diff --git a/plugins/meme/webHandlers.go b/plugins/meme/webHandlers.go index 5cc666e..7850185 100644 --- a/plugins/meme/webHandlers.go +++ b/plugins/meme/webHandlers.go @@ -14,21 +14,52 @@ import ( "github.com/velour/catbase/bot" ) +func (p *MemePlugin) registerWeb(c bot.Connector) { + http.HandleFunc("/slash/meme", p.slashMeme(c)) + http.HandleFunc("/meme/img/", p.img) + http.HandleFunc("/meme/all", p.all) + http.HandleFunc("/meme/add", p.addMeme) + http.HandleFunc("/meme/rm", p.rmMeme) + http.HandleFunc("/meme", p.webRoot) + p.bot.RegisterWeb("/meme", "Memes") +} + +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 } + func (p *MemePlugin) all(w http.ResponseWriter, r *http.Request) { memes := p.c.GetMap("meme.memes", defaultFormats) + configs := p.c.GetMap("meme.memeconfigs", map[string]string{}) + values := webResps{} for n, u := range memes { - + config, ok := configs[n] + if !ok { + b, _ := json.Marshal(defaultFormatConfig) + config = string(b) + } realURL, err := url.Parse(u) if err != nil || realURL.Scheme == "" { realURL, err = url.Parse("https://imgflip.com/s/meme/" + u) if err != nil { - values = append(values, webResp{n, "404.png"}) + values = append(values, webResp{n, "404.png", config}) log.Error().Err(err).Msgf("invalid URL") continue } } - values = append(values, webResp{n, realURL.String()}) + values = append(values, webResp{n, realURL.String(), config}) } sort.Sort(ByName{values}) @@ -84,12 +115,18 @@ func (p *MemePlugin) addMeme(w http.ResponseWriter, r *http.Request) { values := webResp{} err := decoder.Decode(&values) if checkError(err) { + log.Error().Err(err).Msgf("could not decode body") return } + log.Debug().Msgf("POSTed values: %+v", values) formats := p.c.GetMap("meme.memes", defaultFormats) formats[values.Name] = values.URL err = p.c.SetMap("meme.memes", formats) checkError(err) + configs := p.c.GetMap("meme.memeconfigs", map[string]string{}) + configs[values.Name] = values.Config + err = p.c.SetMap("meme.memeconfigs", configs) + checkError(err) } func (p *MemePlugin) webRoot(w http.ResponseWriter, r *http.Request) {