From e8c7e1e07e3e124e5494f4ed0b50e5f5ec5f04c1 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Mon, 6 Jun 2022 10:20:50 -0400 Subject: [PATCH] emojy: make the web page nicer - show custom emojy - categorize by source type - record emojy used in messages --- go.mod | 2 ++ go.sum | 4 +++ plugins/emojy/emojy.go | 53 ++++++++++++++++++++++++++++++++++++++-- plugins/emojy/index.html | 11 ++++++++- plugins/emojy/web.go | 15 ++++++++++++ 5 files changed, 82 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 033a1e8..c4ebfe6 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035 github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90 + github.com/forPelevin/gomoji v1.1.4 github.com/go-chi/chi/v5 v5.0.7 github.com/gocolly/colly v1.2.0 github.com/google/uuid v1.3.0 @@ -67,6 +68,7 @@ require ( github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d // indirect github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect diff --git a/go.sum b/go.sum index 7a9060d..e02a0d8 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad h1:Qk76DOWdOp+GlyDKB github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad/go.mod h1:mPKfmRa823oBIgl2r20LeMSpTAteW5j7FLkc0vjmzyQ= github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90 h1:WXb3TSNmHp2vHoCroCIB1foO/yQ36swABL8aOVeDpgg= github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= +github.com/forPelevin/gomoji v1.1.4 h1:mlxsZQgTO7v1qnpUUoS8kk0Lf/rEvxZYgYxuVUX7edg= +github.com/forPelevin/gomoji v1.1.4/go.mod h1:ypB7Kz3Fsp+LVR7KoT7mEFOioYBuTuAtaAT4RGl+ASY= github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 h1:GOfMz6cRgTJ9jWV0qAezv642OhPnKEG7gtUjJSdStHE= github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17/go.mod h1:HfkOCN6fkKKaPSAeNq/er3xObxTW4VLeY6UUK895gLQ= github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= @@ -134,6 +136,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d h1:1VUlQbCfkoSGv7qP7Y+ro3ap1P1pPZxgdGVqiTVy5C4= github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY= github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= diff --git a/plugins/emojy/emojy.go b/plugins/emojy/emojy.go index 02bfd3e..6843a85 100644 --- a/plugins/emojy/emojy.go +++ b/plugins/emojy/emojy.go @@ -1,11 +1,15 @@ package emojy import ( + "github.com/forPelevin/gomoji" "github.com/jmoiron/sqlx" "github.com/rs/zerolog/log" "github.com/velour/catbase/bot" "github.com/velour/catbase/config" + "os" + "path" "regexp" + "strings" "time" ) @@ -38,6 +42,18 @@ func (p *EmojyPlugin) setupDB() { func (p *EmojyPlugin) register() { ht := bot.HandlerTable{ + { + Kind: bot.Message, IsCmd: false, + Regex: regexp.MustCompile(`.*`), + Handler: func(request bot.Request) bool { + r := regexp.MustCompile(`:[a-zA-Z0-9_-]+:`) + for _, match := range r.FindAllString(request.Msg.Body, -1) { + log.Debug().Msgf("Emojy detected: %s", match) + p.recordReaction(match) + } + return false + }, + }, { Kind: bot.Reaction, IsCmd: false, Regex: regexp.MustCompile(`.*`), @@ -68,15 +84,48 @@ type EmojyEntry struct { type EmojyCount struct { Emojy string `json:"emojy"` + URL string `json:"url"` Count int `json:"count"` } -func (p *EmojyPlugin) allCounts() ([]EmojyCount, error) { +func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) { + out := map[string][]EmojyCount{} q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc` result := []EmojyCount{} err := p.db.Select(&result, q) if err != nil { return nil, err } - return result, nil + for _, e := range result { + if isEmoji(e.Emojy) { + + out["emoji"] = append(out["emoji"], e) + } else if ok, fname, _ := p.isKnownEmojy(e.Emojy); ok { + e.URL = fname + out["emojy"] = append(out["emojy"], e) + } else { + out["unknown"] = append(out["unknown"], e) + } + } + return out, nil +} + +func (p *EmojyPlugin) isKnownEmojy(name string) (bool, string, error) { + emojyPath := p.c.Get("emojy.path", "emojy") + baseURL := p.c.Get("emojy.baseURL", "/emojy/file") + entries, err := os.ReadDir(emojyPath) + if err != nil { + return false, "", err + } + for _, e := range entries { + if !e.IsDir() && strings.HasPrefix(e.Name(), name) { + url := path.Join(baseURL, e.Name()) + return true, url, nil + } + } + return false, "", nil +} + +func isEmoji(in string) bool { + return gomoji.ContainsEmoji(in) } diff --git a/plugins/emojy/index.html b/plugins/emojy/index.html index 3bb1afd..cd46257 100644 --- a/plugins/emojy/index.html +++ b/plugins/emojy/index.html @@ -34,7 +34,16 @@