From 20a56a4fcc370cc01bb2bab7708d45cb42d4b860 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 27 May 2019 22:16:35 -0400 Subject: [PATCH 1/3] web: small improvements --- bot/bot.go | 4 ++-- plugins/cli/index.go | 2 ++ plugins/fact/webTemplates.go | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index e66eda7..8674a34 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -138,8 +138,8 @@ var rootIndex = ` Factoids - - + + {{if .EndPoints}}
diff --git a/plugins/cli/index.go b/plugins/cli/index.go index 09b4364..04b0be2 100644 --- a/plugins/cli/index.go +++ b/plugins/cli/index.go @@ -117,6 +117,8 @@ var indexHTML = ` }, send(evt) { evt.preventDefault(); + evt.stopPropagation() + this.input = ""; if (!this.authenticated) { console.log("User is a bot."); this.err = "User appears to be a bot."; diff --git a/plugins/fact/webTemplates.go b/plugins/fact/webTemplates.go index 0c0c7b7..a2b2d25 100644 --- a/plugins/fact/webTemplates.go +++ b/plugins/fact/webTemplates.go @@ -12,7 +12,7 @@ var factoidIndex = ` Factoids - + @@ -24,6 +24,7 @@ var factoidIndex = ` +
@@ -107,6 +108,7 @@ var factoidIndex = ` }); }); + ` From ab3dac35ba36cca3b11d1c5a60fac5a71758ea06 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 27 May 2019 23:14:05 -0400 Subject: [PATCH 2/3] web: redo factoid page; add title to counter page --- plugins/counter/html.go | 1 + plugins/fact/factoid.go | 37 +++++++ plugins/fact/webTemplates.go | 181 +++++++++++++++++------------------ 3 files changed, 126 insertions(+), 93 deletions(-) diff --git a/plugins/counter/html.go b/plugins/counter/html.go index dca3964..3dd9b3a 100644 --- a/plugins/counter/html.go +++ b/plugins/counter/html.go @@ -14,6 +14,7 @@ var html = ` + Counters diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index eec9156..6546c7e 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -4,6 +4,7 @@ package fact import ( "database/sql" + "encoding/json" "fmt" "html/template" "math/rand" @@ -759,6 +760,7 @@ func (p *FactoidPlugin) factTimer(c bot.Connector, channel string) { // Register any web URLs desired func (p *FactoidPlugin) registerWeb() { + http.HandleFunc("/factoid/api", p.serveAPI) http.HandleFunc("/factoid/req", p.serveQuery) http.HandleFunc("/factoid", p.serveQuery) p.Bot.RegisterWeb("/factoid", "Factoid") @@ -773,8 +775,43 @@ func linkify(text string) template.HTML { } return template.HTML(strings.Join(parts, " ")) } +func (p *FactoidPlugin) serveAPI(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + fmt.Fprintf(w, "Incorrect HTTP method") + return + } + info := struct { + Query string `json:"query"` + }{} + decoder := json.NewDecoder(r.Body) + err := decoder.Decode(&info) + if err != nil { + w.WriteHeader(500) + fmt.Fprint(w, err) + return + } + + entries, err := getFacts(p.db, info.Query, "") + if err != nil { + w.WriteHeader(500) + fmt.Fprint(w, err) + return + } + + data, err := json.Marshal(entries) + if err != nil { + w.WriteHeader(500) + fmt.Fprint(w, err) + return + } + w.Write(data) +} func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, factoidIndex) +} + +func (p *FactoidPlugin) serveQueryOld(w http.ResponseWriter, r *http.Request) { context := make(map[string]interface{}) funcMap := template.FuncMap{ // The name "title" is what the function will be called in the template text. diff --git a/plugins/fact/webTemplates.go b/plugins/fact/webTemplates.go index a2b2d25..c890664 100644 --- a/plugins/fact/webTemplates.go +++ b/plugins/fact/webTemplates.go @@ -9,106 +9,101 @@ package fact var factoidIndex = ` - + - Factoids - + + + - - - - - - - - + + + + + + + + + Factoids -
- -
- Search for a factoid - - -
- -
-
- - - {{if .Error}} - {{.Error}} - {{end}} - - {{if .Count}} - Found {{.Count}} entries. - {{end}} -
- - {{if .Entries}} -
- - - - - - - - - - - - {{range .Entries}} - - - - - - - {{end}} - -
TriggerFull TextAuthor# Hits
{{linkify .Fact}}{{linkify .Tidbit}}{{linkify .Owner}}{{.Count}}
-
- {{end}} - - + - ` From a7712530f93fb4cb4a6f11f86de5fa8353364673 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 27 May 2019 23:15:42 -0400 Subject: [PATCH 3/3] fact: remove old handler --- plugins/fact/factoid.go | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index 6546c7e..452fcc0 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -810,28 +810,3 @@ func (p *FactoidPlugin) serveAPI(w http.ResponseWriter, r *http.Request) { func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, factoidIndex) } - -func (p *FactoidPlugin) serveQueryOld(w http.ResponseWriter, r *http.Request) { - context := make(map[string]interface{}) - funcMap := template.FuncMap{ - // The name "title" is what the function will be called in the template text. - "linkify": linkify, - } - if e := r.FormValue("entry"); e != "" { - entries, err := getFacts(p.db, e, "") - if err != nil { - log.Error().Err(err).Msg("Web error searching") - } - context["Count"] = fmt.Sprintf("%d", len(entries)) - context["Entries"] = entries - context["Search"] = e - } - t, err := template.New("factoidIndex").Funcs(funcMap).Parse(factoidIndex) - if err != nil { - log.Error().Err(err) - } - err = t.Execute(w, context) - if err != nil { - log.Error().Err(err) - } -}