From 85eb337e3b2e74389a5322a0f82311d76c25451e Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Tue, 24 Jan 2023 01:24:03 -0800 Subject: [PATCH] Add TmplContext struct with reply information --- config.go | 1 - lemmy-reply-bot.example.toml | 2 +- main.go | 69 +++++++++++++++++++++++++++--------- tmpl.go | 24 ------------- 4 files changed, 54 insertions(+), 42 deletions(-) delete mode 100644 tmpl.go diff --git a/config.go b/config.go index c1def07..c0beef5 100644 --- a/config.go +++ b/config.go @@ -77,7 +77,6 @@ func compileReplies(replies []Reply) error { tmpl, err := template. New(strconv.Itoa(i)). - Funcs(tmplFuncs). Funcs(sprig.TxtFuncMap()). Parse(reply.Msg) if err != nil { diff --git a/lemmy-reply-bot.example.toml b/lemmy-reply-bot.example.toml index 7f9d21c..5a25474 100644 --- a/lemmy-reply-bot.example.toml +++ b/lemmy-reply-bot.example.toml @@ -9,4 +9,4 @@ password = "ExamplePassword123" # after "!!BOT_TEST" [[reply]] regex = "!!BOT_TEST (.*)" -msg = "{{match . 0 1}}" \ No newline at end of file +msg = "{{.Match 0 1}}" \ No newline at end of file diff --git a/main.go b/main.go index 6feab12..a1d265d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "strings" "syscall" "text/template" + "unsafe" "github.com/spf13/pflag" "github.com/vmihailenco/msgpack/v5" @@ -15,6 +16,44 @@ import ( "go.arsenm.dev/logger/log" ) +type itemType uint8 + +const ( + comment itemType = iota + post +) + +type item struct { + Type itemType + ID int +} + +func (it itemType) String() string { + switch it { + case comment: + return "comment" + case post: + return "post" + default: + return "" + } +} + +type Submatches []string + +func (sm Submatches) Item(i int) string { + return sm[i] +} + +type TmplContext struct { + Matches []Submatches + Type itemType +} + +func (tc TmplContext) Match(i, j int) string { + return tc.Matches[i][j] +} + func main() { configPath := pflag.StringP("config", "c", "./lemmy-reply-bot.toml", "Path to the config file") dryRun := pflag.BoolP("dry-run", "D", false, "Don't actually send comments, just check for matches") @@ -60,18 +99,6 @@ func main() { commentWorker(ctx, c, replyCh) } -type itemType uint8 - -const ( - comment itemType = iota - post -) - -type item struct { - Type itemType - ID int -} - func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJob) { repliedIDs := map[item]struct{}{} @@ -116,7 +143,10 @@ func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJ } matches := re.FindAllStringSubmatch(cr.CommentView.Comment.Content, -1) - job.Content, err = executeTmpl(compiledTmpls[reply.Regex], matches) + job.Content, err = executeTmpl(compiledTmpls[reply.Regex], TmplContext{ + Matches: toSubmatches(matches), + Type: comment, + }) if err != nil { log.Warn("Error while executing template").Err(err).Send() continue @@ -153,7 +183,10 @@ func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJ job := replyJob{PostID: pr.PostView.Post.ID} matches := re.FindAllStringSubmatch(body, -1) - job.Content, err = executeTmpl(compiledTmpls[reply.Regex], matches) + job.Content, err = executeTmpl(compiledTmpls[reply.Regex], TmplContext{ + Matches: toSubmatches(matches), + Type: post, + }) if err != nil { log.Warn("Error while executing template").Err(err).Send() continue @@ -213,9 +246,9 @@ func commentReplyWorker(ctx context.Context, c *lemmy.WSClient, ch <-chan replyJ } } -func executeTmpl(tmpl *template.Template, matches [][]string) (string, error) { +func executeTmpl(tmpl *template.Template, tc TmplContext) (string, error) { sb := &strings.Builder{} - err := tmpl.Execute(sb, matches) + err := tmpl.Execute(sb, tc) return sb.String(), err } @@ -232,3 +265,7 @@ func joinAll(c *lemmy.WSClient) { log.Fatal("Error joining WebSocket community context").Err(err).Send() } } + +func toSubmatches(s [][]string) []Submatches { + return *(*[]Submatches)(unsafe.Pointer(&s)) +} diff --git a/tmpl.go b/tmpl.go deleted file mode 100644 index 3151f24..0000000 --- a/tmpl.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import "text/template" - -var tmplFuncs = template.FuncMap{ - "match": func(matches [][]string, i int, j int) string { - if len(matches) <= i { - return "" - } - - if len(matches[i]) <= j { - return "" - } - - return matches[i][j] - }, - "item": func(items []string, i int) string { - if len(items) <= i { - return "" - } - - return items[i] - }, -}