From b6fb1a06e23c66af2404418b8b6c9e142c4d97b8 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 9 Jan 2023 12:55:51 -0800 Subject: [PATCH] Update go-lemmy library --- go.mod | 5 +++-- go.sum | 4 ++-- main.go | 31 ++++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index ac5a229..1e8e128 100644 --- a/go.mod +++ b/go.mod @@ -2,18 +2,19 @@ module go.arsenm.dev/lemmy-reply-bot go 1.19 -//replace go.arsenm.dev/go-lemmy => /home/arsen/Code/go-lemmy +replace go.arsenm.dev/go-lemmy => /home/arsen/Code/go-lemmy require ( github.com/pelletier/go-toml/v2 v2.0.6 github.com/spf13/pflag v1.0.5 github.com/vmihailenco/msgpack/v5 v5.3.5 - go.arsenm.dev/go-lemmy v0.16.7 + go.arsenm.dev/go-lemmy v0.16.8-0.20230109205406-c0aced05f0cd go.arsenm.dev/logger v0.0.0-20230104225304-d706171ea6df go.arsenm.dev/pcre v0.0.0-20220530205550-74594f6c8b0e ) require ( + github.com/cenkalti/backoff/v4 v4.2.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gookit/color v1.5.1 // indirect diff --git a/go.sum b/go.sum index 90146e8..33a427e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4= +github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -41,8 +43,6 @@ github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -go.arsenm.dev/go-lemmy v0.16.7 h1:aY343U5AMFryffRO9PsKPBMLKAl91WdwmlCYXW3TdGM= -go.arsenm.dev/go-lemmy v0.16.7/go.mod h1:Ytr6kiml1hmSvTHvOz+qV79HSNS4FtIynJfj8KWmT5Q= go.arsenm.dev/logger v0.0.0-20230104225304-d706171ea6df h1:8mBHvEe7BJmpOeKSMA5YLqrGo9dCpePocTeR0C1+/2w= go.arsenm.dev/logger v0.0.0-20230104225304-d706171ea6df/go.mod h1:RV2qydKDdoyaRkhAq8JEGvojR8eJ6bjq5WnSIlH7gYw= go.arsenm.dev/pcre v0.0.0-20220530205550-74594f6c8b0e h1:4XwLmFDvAKt7ZvS3E3hD2R++0wr75fBUEvXkK9dLXzk= diff --git a/main.go b/main.go index 7fab0c6..b5b78e1 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "os" "os/signal" "strconv" @@ -44,17 +45,12 @@ func main() { log.Info("Successfully logged in to Lemmy instance").Send() - err = c.Request(types.UserOperationUserJoin, nil) - if err != nil { - log.Fatal("Error joining WebSocket user context").Err(err).Send() - } + joinAll(c) - err = c.Request(types.UserOperationCommunityJoin, types.CommunityJoin{ - CommunityID: 0, + c.OnReconnect(func(c *lemmy.WSClient) { + joinAll(c) + log.Info("Successfully reconnected to WebSocket").Send() }) - if err != nil { - log.Fatal("Error joining WebSocket community context").Err(err).Send() - } replyCh := make(chan replyJob, 200) @@ -130,6 +126,9 @@ func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJ repliedIDs[cr.CommentView.Comment.ID] = struct{}{} } } + case err := <-c.Errors(): + fmt.Printf("%T\n", err) + log.Warn("Lemmy client error").Err(err).Send() case <-ctx.Done(): repliedStore, err := os.Create("replied.bin") if err != nil { @@ -186,3 +185,17 @@ func expandStr(s string, mapping func(string) string) string { return mapping(s) }) } + +func joinAll(c *lemmy.WSClient) { + err := c.Request(types.UserOperationUserJoin, nil) + if err != nil { + log.Fatal("Error joining WebSocket user context").Err(err).Send() + } + + err = c.Request(types.UserOperationCommunityJoin, types.CommunityJoin{ + CommunityID: 0, + }) + if err != nil { + log.Fatal("Error joining WebSocket community context").Err(err).Send() + } +}