Compare commits

...

2 Commits

Author SHA1 Message Date
Chris Sexton 61cdb66546 emojy: indicate if an emojy is on the server 2022-06-06 10:49:27 -04:00
Chris Sexton e8c7e1e07e emojy: make the web page nicer
- show custom emojy
- categorize by source type
- record emojy used in messages
2022-06-06 10:24:54 -04:00
5 changed files with 99 additions and 5 deletions

2
go.mod
View File

@ -11,6 +11,7 @@ require (
github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff
github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035 github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90 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/go-chi/chi/v5 v5.0.7
github.com/gocolly/colly v1.2.0 github.com/gocolly/colly v1.2.0
github.com/google/uuid v1.3.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/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // 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/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect

4
go.sum
View File

@ -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/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 h1:WXb3TSNmHp2vHoCroCIB1foO/yQ36swABL8aOVeDpgg=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= 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 h1:GOfMz6cRgTJ9jWV0qAezv642OhPnKEG7gtUjJSdStHE=
github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17/go.mod h1:HfkOCN6fkKKaPSAeNq/er3xObxTW4VLeY6UUK895gLQ= 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= 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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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 h1:1VUlQbCfkoSGv7qP7Y+ro3ap1P1pPZxgdGVqiTVy5C4=
github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY= github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=

View File

@ -1,11 +1,15 @@
package emojy package emojy
import ( import (
"github.com/forPelevin/gomoji"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
"os"
"path"
"regexp" "regexp"
"strings"
"time" "time"
) )
@ -38,6 +42,18 @@ func (p *EmojyPlugin) setupDB() {
func (p *EmojyPlugin) register() { func (p *EmojyPlugin) register() {
ht := bot.HandlerTable{ 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, Kind: bot.Reaction, IsCmd: false,
Regex: regexp.MustCompile(`.*`), Regex: regexp.MustCompile(`.*`),
@ -67,16 +83,59 @@ type EmojyEntry struct {
} }
type EmojyCount struct { type EmojyCount struct {
Emojy string `json:"emojy"` Emojy string `json:"emojy"`
Count int `json:"count"` URL string `json:"url"`
Count int `json:"count"`
OnServer bool `json:"onServer"`
} }
func (p *EmojyPlugin) allCounts() ([]EmojyCount, error) { func invertEmojyList(emojy map[string]string) map[string]string {
out := map[string]string{}
for k, v := range emojy {
out[v] = k
}
return out
}
func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) {
out := map[string][]EmojyCount{}
onServerList := invertEmojyList(p.b.DefaultConnector().GetEmojiList())
q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc` q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc`
result := []EmojyCount{} result := []EmojyCount{}
err := p.db.Select(&result, q) err := p.db.Select(&result, q)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return result, nil for _, e := range result {
_, e.OnServer = onServerList[e.Emojy]
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)
} }

View File

@ -34,7 +34,21 @@
</b-alert> </b-alert>
<ul> <ul>
<li v-for="emojy in results" key="Emojy">{{emojy.count}} - {{emojy.emojy}}</li> <li v-for="(category, name) in results" key="name">
{{name}}:
<ul>
<li v-for="emojy in category" key="emojy">
{{emojy.count}} -
<span v-if="name != 'emoji'">
<span v-if="emojy.onServer"></span>
<span v-else></span>
-
</span>
<img v-if="emojy.url" :src="emojy.url" :alt="emojy.name" class="img-thumbnail" style="max-width: 64px; max-height: 64px" />
<span v-else>{{emojy.emojy}}</span>
</li>
</ul>
</li>
</ul> </ul>
</div> </div>

View File

@ -5,7 +5,9 @@ import (
"encoding/json" "encoding/json"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"io/ioutil"
"net/http" "net/http"
"path"
) )
//go:embed *.html //go:embed *.html
@ -14,6 +16,7 @@ var embeddedFS embed.FS
func (p *EmojyPlugin) registerWeb() { func (p *EmojyPlugin) registerWeb() {
r := chi.NewRouter() r := chi.NewRouter()
r.HandleFunc("/all", p.handleAll) r.HandleFunc("/all", p.handleAll)
r.HandleFunc("/file/{name}", p.handleEmojy)
r.HandleFunc("/", p.handleIndex) r.HandleFunc("/", p.handleIndex)
p.b.RegisterWebName(r, "/emojy", "Emojys") p.b.RegisterWebName(r, "/emojy", "Emojys")
} }
@ -34,3 +37,15 @@ func (p *EmojyPlugin) handleAll(w http.ResponseWriter, r *http.Request) {
out, _ := json.Marshal(emojy) out, _ := json.Marshal(emojy)
w.Write(out) w.Write(out)
} }
func (p *EmojyPlugin) handleEmojy(w http.ResponseWriter, r *http.Request) {
fname := chi.URLParam(r, "name")
emojyPath := p.c.Get("emojy.path", "emojy")
contents, err := ioutil.ReadFile(path.Join(emojyPath, fname))
if err != nil {
w.WriteHeader(404)
out, _ := json.Marshal(struct{ err error }{err})
w.Write(out)
}
w.Write(contents)
}