Compare commits

...

4 Commits

Author SHA1 Message Date
Chris Sexton 0ce8a21e0e fact: fix formatting 2024-02-27 22:21:44 -05:00
Chris Sexton 344bcb9f64 fact: templ and htmx 2024-02-27 22:05:06 -05:00
Chris Sexton 41e8241cb5 emojy: add updated versions 2024-02-27 21:49:03 -05:00
Chris Sexton b66038354a meme: finishing up templating 2024-02-27 21:49:03 -05:00
14 changed files with 595 additions and 274 deletions

View File

@ -431,6 +431,9 @@ func (d *Discord) SetRole(userID, roleID string) error {
type CmdHandler func(s *discordgo.Session, i *discordgo.InteractionCreate) type CmdHandler func(s *discordgo.Session, i *discordgo.InteractionCreate)
func (d *Discord) RegisterSlashCmd(c discordgo.ApplicationCommand, handler CmdHandler) error { func (d *Discord) RegisterSlashCmd(c discordgo.ApplicationCommand, handler CmdHandler) error {
if !d.config.GetBool("registerSlash", true) {
return nil
}
cmd, err := d.client.ApplicationCommandCreate(d.client.State.User.ID, d.guildID, &c) cmd, err := d.client.ApplicationCommandCreate(d.client.State.User.ID, d.guildID, &c)
d.cmdHandlers[c.Name] = handler d.cmdHandlers[c.Name] = handler
if err != nil { if err != nil {

View File

@ -1,107 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Load required Bootstrap and BootstrapVue CSS -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.css"/>
<!-- Load polyfills to support older browsers -->
<script src="//polyfill.io/v3/polyfill.min.js?features=es2015%2CMutationObserver"></script>
<!-- Load Vue followed by BootstrapVue -->
<script src="//unpkg.com/vue@^2/dist/vue.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<meta charset="UTF-8">
<title>Memes</title>
</head>
<body>
<div id="app">
<b-navbar>
<b-navbar-brand>Emojys</b-navbar-brand>
<b-navbar-nav>
<b-nav-item v-for="item in nav" :href="item.url" :active="item.name === 'Meme'" :key="item.key">{{ item.name
}}
</b-nav-item>
</b-navbar-nav>
</b-navbar>
<b-navbar>
<b-navbar-nav>
<b-nav-item href="/emojy/stats">Stats</b-nav-item>
<b-nav-item active href="/emojy/list">List</b-nav-item>
<b-nav-item href="/emojy/new">Upload</b-nav-item>
</b-navbar-nav>
</b-navbar>
<div
style="background-color:red;"
variant="error"
v-if="err != ''"
@click="err = ''">
{{ err }}
</div>
<div class="row row-cols-5">
<div class="card text-center" v-for="name in fileKeys" key="name">
<div class="card-body">
<span>
<b-img-lazy :src="fileList[name]" class="card-img-top mx-auto d-block" :alt="name" width=100 style="max-width: 100px">
</span>
<h5 class="card-title">{{name}}</h5>
</div>
</div>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: function () {
return {
err: '',
view: '',
nav: [],
results: [],
fileKeys: [],
fileList: {},
image: null,
password: ''
}
},
watch: {
view(newView, oldView) {
this.err = '';
}
},
mounted() {
axios.get('/nav')
.then(resp => {
this.nav = resp.data;
})
.catch(err => console.log(err))
this.refresh();
},
methods: {
refresh: function () {
axios.get('/emojy/all')
.then(resp => {
this.results = resp.data
this.err = ''
})
.catch(err => (this.err = err))
axios.get('/emojy/allFiles')
.then(resp => {
// stole this somewhere or other as a quick hack
this.fileKeys = Object.keys(resp.data).sort()
this.fileList = resp.data
this.err = ''
})
.catch(err => (this.err = err))
},
}
})
</script>
</body>
</html>

23
plugins/emojy/list.templ Normal file
View File

@ -0,0 +1,23 @@
package emojy
templ (p *EmojyPlugin) listTempl(emojy emojyMap) {
@p.emojyNav()
for _, v := range emojy {
<div class="row row-cols-5">
for _, c := range v {
<div class="card text-center">
<div class="card-body">
<span>
<img src={ c.URL }
class="card-img-top mx-auto d-block"
alt={ c.Emojy }
width="100"
style="max-width: 100px" />
</span>
<h5 class="card-title">{ c.Emojy }</h5>
</div>
</div>
}
</div>
}
}

View File

@ -0,0 +1,80 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.543
package emojy
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
func (p *EmojyPlugin) listTempl(emojy emojyMap) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = p.emojyNav().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, v := range emojy {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"row row-cols-5\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, c := range v {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"card text-center\"><div class=\"card-body\"><span><img src=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(c.URL))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" class=\"card-img-top mx-auto d-block\" alt=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(c.Emojy))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" width=\"100\" style=\"max-width: 100px\"></span><h5 class=\"card-title\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(c.Emojy)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/emojy/list.templ`, Line: 16, Col: 56}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</h5></div></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}

View File

@ -1,113 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Load required Bootstrap and BootstrapVue CSS -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.css"/>
<!-- Load polyfills to support older browsers -->
<script src="//polyfill.io/v3/polyfill.min.js?features=es2015%2CMutationObserver"></script>
<!-- Load Vue followed by BootstrapVue -->
<script src="//unpkg.com/vue@^2/dist/vue.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<meta charset="UTF-8">
<title>Memes</title>
</head>
<body>
<div id="app">
<b-navbar>
<b-navbar-brand>Emojys</b-navbar-brand>
<b-navbar-nav>
<b-nav-item v-for="item in nav" :href="item.url" :active="item.name === 'Meme'" :key="item.key">{{ item.name
}}
</b-nav-item>
</b-navbar-nav>
</b-navbar>
<b-navbar>
<b-navbar-nav>
<b-nav-item active href="/emojy/stats">Stats</b-nav-item>
<b-nav-item href="/emojy/list">List</b-nav-item>
<b-nav-item href="/emojy/new">Upload</b-nav-item>
</b-navbar-nav>
</b-navbar>
<div
style="background-color:red;"
variant="error"
v-if="err != ''"
@click="err = ''">
{{ err }}
</div>
<ul style="list-style-type: none;">
<li v-for="category in results">
<ul v-if="category" style="list-style-type: none;">
<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>
<span v-if="emojy.url">
<img :src="emojy.url" :alt="emojy.name" class="img-thumbnail"
style="max-width: 64px; max-height: 64px"/> {{emojy.emojy}}
</span>
<span v-else>{{emojy.emojy}}</span>
</li>
</ul>
</li>
</ul>
</div>
<script>
var app = new Vue({
el: '#app',
data: function () {
return {
err: '',
view: '',
nav: [],
results: [],
fileList: {},
image: null,
password: ''
}
},
watch: {
view(newView, oldView) {
this.err = '';
}
},
mounted() {
axios.get('/nav')
.then(resp => {
this.nav = resp.data;
})
.catch(err => console.log(err))
this.refresh();
},
methods: {
refresh: function () {
axios.get('/emojy/all')
.then(resp => {
this.results = [resp.data["emojy"], resp.data["unknown"], resp.data["emoji"]]
this.err = ''
})
.catch(err => (this.err = err))
axios.get('/emojy/allFiles')
.then(resp => {
this.fileList = resp.data
this.err = ''
})
.catch(err => (this.err = err))
},
}
})
</script>
</body>
</html>

46
plugins/emojy/stats.templ Normal file
View File

@ -0,0 +1,46 @@
package emojy
import "fmt"
templ (p *EmojyPlugin) emojyNav() {
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/emojy/stats">Stats</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/emojy/list">List</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/emojy/new">Upload</a>
</li>
</ul>
</div>
</nav>
}
templ (p *EmojyPlugin) statsIndex(emojy emojyMap) {
@p.emojyNav()
for categoryName, v := range emojy {
<div class="container">
<ul class="list-group list-group-flush">
for _, c := range v {
<li class="list-group-item">
{ fmt.Sprintf("%d", c.Count) } -
if categoryName != "emoji" && c.OnServer {
<span>✅</span>
} else if categoryName != "emoji" {
<span>✅</span>
}
if c.URL != "" {
<img src={ c.URL } alt={ c.Emojy } />
} else {
{ c.Emojy }
}
</li>
}
</ul>
</div>
}
}

View File

@ -0,0 +1,137 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.543
package emojy
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
import "fmt"
func (p *EmojyPlugin) emojyNav() templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<nav class=\"navbar navbar-expand-lg bg-body-tertiary\"><div class=\"container-fluid\"><ul class=\"navbar-nav\"><li class=\"nav-item\"><a class=\"nav-link\" href=\"/emojy/stats\">Stats</a></li><li class=\"nav-item\"><a class=\"nav-link\" href=\"/emojy/list\">List</a></li><li class=\"nav-item\"><a class=\"nav-link\" href=\"/emojy/new\">Upload</a></li></ul></div></nav>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}
func (p *EmojyPlugin) statsIndex(emojy emojyMap) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = p.emojyNav().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for categoryName, v := range emojy {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"container\"><ul class=\"list-group list-group-flush\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, c := range v {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<li class=\"list-group-item\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", c.Count))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/emojy/stats.templ`, Line: 29, Col: 44}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" - ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if categoryName != "emoji" && c.OnServer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<span>✅</span>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if categoryName != "emoji" {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<span>✅</span>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
if c.URL != "" {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<img src=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(c.URL))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" alt=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(c.Emojy))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(c.Emojy)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/emojy/stats.templ`, Line: 38, Col: 29}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</ul></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}

View File

@ -0,0 +1,13 @@
package emojy
templ (p *EmojyPlugin) uploadIndex() {
<div class="container">
<label>Passphrase</label>
<input type="text" name="password" placeholder="Password..."></input>
<label>File
<input type="file" />
</label>
<br />
<button hx-post="/emojy/upload">Submit</button>
</div>
}

View File

@ -0,0 +1,35 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.543
package emojy
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
func (p *EmojyPlugin) uploadIndex() templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"container\"><label>Passphrase</label> <input type=\"text\" name=\"password\" placeholder=\"Password...\"> <label>File <input type=\"file\"></label><br><button hx-post=\"/emojy/upload\">Submit</button></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}

View File

@ -25,15 +25,40 @@ func (p *EmojyPlugin) registerWeb() {
r.HandleFunc("/allFiles", p.handleAllFiles) r.HandleFunc("/allFiles", p.handleAllFiles)
r.HandleFunc("/upload", p.handleUpload) r.HandleFunc("/upload", p.handleUpload)
r.HandleFunc("/file/{name}", p.handleEmojy) r.HandleFunc("/file/{name}", p.handleEmojy)
r.HandleFunc("/stats", p.handlePage("stats.html")) r.HandleFunc("/stats", p.handleStats)
r.HandleFunc("/list", p.handlePage("list.html")) r.HandleFunc("/list", p.handleList)
r.HandleFunc("/new", p.handlePage("upload.html")) r.HandleFunc("/new", p.handleUploadForm)
r.HandleFunc("/", p.handleIndex) r.HandleFunc("/", p.handleStats)
p.b.GetWeb().RegisterWebName(r, "/emojy", "Emojys") p.b.GetWeb().RegisterWebName(r, "/emojy", "Emojys")
} }
func (p *EmojyPlugin) handleIndex(w http.ResponseWriter, r *http.Request) { type emojyMap map[string][]EmojyCount
http.Redirect(w, r, "/emojy/stats", http.StatusPermanentRedirect)
func (p *EmojyPlugin) handleUploadForm(w http.ResponseWriter, r *http.Request) {
p.b.GetWeb().Index("Emojy", p.uploadIndex()).Render(r.Context(), w)
}
func (p *EmojyPlugin) handleList(w http.ResponseWriter, r *http.Request) {
threshold := p.c.GetInt("emojy.statthreshold", 1)
emojy, err := p.allCounts(threshold)
if err != nil {
fmt.Fprintf(w, "Error: %s", err)
return
}
p.b.GetWeb().Index("Emojy", p.listTempl(emojy)).Render(r.Context(), w)
}
func (p *EmojyPlugin) handleStats(w http.ResponseWriter, r *http.Request) {
threshold := p.c.GetInt("emojy.statthreshold", 1)
emojy, err := p.allCounts(threshold)
if err != nil {
w.WriteHeader(500)
log.Error().Err(err).Msgf("handleAll")
out, _ := json.Marshal(struct{ err error }{err})
w.Write(out)
return
}
p.b.GetWeb().Index("Emojy", p.statsIndex(emojy)).Render(r.Context(), w)
} }
func (p *EmojyPlugin) handlePage(file string) func(w http.ResponseWriter, r *http.Request) { func (p *EmojyPlugin) handlePage(file string) func(w http.ResponseWriter, r *http.Request) {
@ -74,18 +99,16 @@ func (p *EmojyPlugin) handleAllFiles(w http.ResponseWriter, r *http.Request) {
} }
func (p *EmojyPlugin) handleUpload(w http.ResponseWriter, r *http.Request) { func (p *EmojyPlugin) handleUpload(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
newFilePath, err := p.FileSave(r) newFilePath, err := p.FileSave(r)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("could not upload file") log.Error().Err(err).Msgf("could not upload file")
w.WriteHeader(500) w.WriteHeader(500)
enc.Encode(struct{ err error }{fmt.Errorf("file not saved: %s", err)}) fmt.Fprintf(w, "Error with file upload")
return return
} }
log.Debug().Msgf("uploaded file to %s", newFilePath) log.Debug().Msgf("uploaded file to %s", newFilePath)
w.WriteHeader(200) w.WriteHeader(200)
enc.Encode(struct{ file string }{newFilePath}) fmt.Fprintf(w, "success")
} }
func (p *EmojyPlugin) FileSave(r *http.Request) (string, error) { func (p *EmojyPlugin) FileSave(r *http.Request) (string, error) {
@ -101,7 +124,7 @@ func (p *EmojyPlugin) FileSave(r *http.Request) (string, error) {
return "", fmt.Errorf("no files") return "", fmt.Errorf("no files")
} }
password := r.Form.Get("password") password := r.FormValue("password")
if password != p.b.GetPassword() { if password != p.b.GetPassword() {
return "", fmt.Errorf("incorrect password") return "", fmt.Errorf("incorrect password")
} }

55
plugins/fact/fact.templ Normal file
View File

@ -0,0 +1,55 @@
package fact
import "fmt"
templ (p *FactoidPlugin) factIndex() {
<div class="container">
<form
hx-post="/factoid/search"
hx-target="#results">
<div class="row">
<div class="col-10">
<input type="text"
name="query"
class="form-control"
placeholder="Query..."
/>
</div>
<div class="col-1">
<button
class="btn btn-secondary"
>Search</button>
</div>
</div>
</form>
<div class="row" id="results">
</div>
</div>
}
templ (p *FactoidPlugin) searchResults(facts []*Factoid) {
<table class="table">
<thead>
<tr class="d-flex">
<th class="col-2">Fact</th>
<th class="col-8">Tidbit</th>
<th class="col-1">Owner</th>
<th class="col-1">Count</th>
</tr>
</thead>
<tbody>
for _, f := range facts {
@p.searchResult(f)
}
</tbody>
</table>
}
templ (p *FactoidPlugin) searchResult(fact *Factoid) {
<tr class="d-flex">
<td class="col-2">{ fact.Fact }</td>
<td class="col-8">{ fact.Tidbit }</td>
<td class="col-1">{ fact.Owner }</td>
<td class="col-1">{ fmt.Sprint(fact.Count) }</td>
</tr>
}

147
plugins/fact/fact_templ.go Normal file
View File

@ -0,0 +1,147 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.543
package fact
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
import "fmt"
func (p *FactoidPlugin) factIndex() templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"container\"><form hx-post=\"/factoid/search\" hx-target=\"#results\"><div class=\"row\"><div class=\"col-10\"><input type=\"text\" name=\"query\" class=\"form-control\" placeholder=\"Query...\"></div><div class=\"col-1\"><button class=\"btn btn-secondary\">Search</button></div></div></form><div class=\"row\" id=\"results\"></div></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}
func (p *FactoidPlugin) searchResults(facts []*Factoid) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<table class=\"table\"><thead><tr class=\"d-flex\"><th class=\"col-2\">Fact</th><th class=\"col-8\">Tidbit</th><th class=\"col-1\">Owner</th><th class=\"col-1\">Count</th></tr></thead> <tbody>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, f := range facts {
templ_7745c5c3_Err = p.searchResult(f).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</tbody></table>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}
func (p *FactoidPlugin) searchResult(fact *Factoid) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<tr class=\"d-flex\"><td class=\"col-2\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(fact.Fact)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 49, Col: 37}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td class=\"col-8\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(fact.Tidbit)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 50, Col: 39}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td class=\"col-1\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(fact.Owner)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 51, Col: 38}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td class=\"col-1\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprint(fact.Count))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 52, Col: 50}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td></tr>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}

View File

@ -2,7 +2,6 @@ package fact
import ( import (
"embed" "embed"
"encoding/json"
"fmt" "fmt"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"html/template" "html/template"
@ -16,7 +15,7 @@ var embeddedFS embed.FS
// Register any web URLs desired // Register any web URLs desired
func (p *FactoidPlugin) registerWeb() { func (p *FactoidPlugin) registerWeb() {
r := chi.NewRouter() r := chi.NewRouter()
r.HandleFunc("/api", p.serveAPI) r.Post("/search", p.handleSearch)
r.HandleFunc("/req", p.serveQuery) r.HandleFunc("/req", p.serveQuery)
r.HandleFunc("/", p.serveQuery) r.HandleFunc("/", p.serveQuery)
p.b.GetWeb().RegisterWebName(r, "/factoid", "Factoid") p.b.GetWeb().RegisterWebName(r, "/factoid", "Factoid")
@ -32,39 +31,19 @@ func linkify(text string) template.HTML {
return template.HTML(strings.Join(parts, " ")) return template.HTML(strings.Join(parts, " "))
} }
func (p *FactoidPlugin) serveAPI(w http.ResponseWriter, r *http.Request) { func (p *FactoidPlugin) handleSearch(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { query := r.FormValue("query")
fmt.Fprintf(w, "Incorrect HTTP method")
return entries, err := getFacts(p.db, query, "")
}
info := struct {
Query string `json:"query"`
}{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&info)
if err != nil { if err != nil {
w.WriteHeader(500) w.WriteHeader(500)
fmt.Fprint(w, err) fmt.Fprint(w, err)
return return
} }
entries, err := getFacts(p.db, info.Query, "") p.searchResults(entries).Render(r.Context(), w)
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) { func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) {
index, _ := embeddedFS.ReadFile("index.html") p.b.GetWeb().Index("Fact", p.factIndex()).Render(r.Context(), w)
w.Write(index)
} }

View File

@ -65,7 +65,20 @@ func (p *MemePlugin) Show(meme webResp) templ.Component {
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><div class=\"col-3\"><img class=\"img-thumbnail rounded\" alt=\"") _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><div class=\"col-3\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(meme.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/meme/meme.templ`, Line: 35, Col: 23}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" <img class=\"img-thumbnail rounded\" alt=\"")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -81,20 +94,7 @@ func (p *MemePlugin) Show(meme webResp) templ.Component {
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"> ") _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"></div><div class=\"col-3\"><pre>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(meme.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/meme/meme.templ`, Line: 39, Col: 27}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div><div class=\"col-3\"><pre>")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }