From 13996535324770fde1ba2ed641fcdcab5513199f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Fri, 30 Dec 2022 00:39:51 -0500 Subject: [PATCH] Implemented Lemmy system --- cmd/root.go | 2 +- go.mod | 3 +++ go.sum | 6 +++++ system/lemmy/connect.go | 54 +++++++++++++++++++++++++++++++++++++++++ system/lemmy/lemmy.go | 51 ++++++++++++++++++++++++++++++++++---- 5 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 system/lemmy/connect.go diff --git a/cmd/root.go b/cmd/root.go index 70c1036..d212a06 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -79,7 +79,7 @@ var rootCmd = &cobra.Command{ c := ctx.New(&CFG, LOG) _ = loadSystems(&c) // TODO: Handle errs - posts, err := (*c.Systems[0]).ListPosts() + posts, err := (*c.Systems[1]).ListPosts() fmt.Println("-----------------------") fmt.Printf("%v\n", posts) fmt.Printf("%v\n", err) diff --git a/go.mod b/go.mod index 76565fd..f5e69a1 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,8 @@ require ( github.com/aymanbagabas/go-osc52 v1.2.1 // indirect github.com/containerd/console v1.0.3 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/google/go-querystring v1.1.0 // indirect + github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.7.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -42,6 +44,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.1 // indirect + go.arsenm.dev/go-lemmy v0.0.0-20221213184958-e097e6c8b5be // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/sys v0.3.0 // indirect diff --git a/go.sum b/go.sum index 254c513..a35a6fd 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -135,6 +137,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= @@ -235,6 +239,8 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.arsenm.dev/go-lemmy v0.0.0-20221213184958-e097e6c8b5be h1:BZieH3Bp5rSercEMdXz56t0UdaxUipGBCAC6U9tqQwk= +go.arsenm.dev/go-lemmy v0.0.0-20221213184958-e097e6c8b5be/go.mod h1:q7CIDksHIqltFNugJAixGTqk1Gp+a9emZMlILhU7Uh0= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= diff --git a/system/lemmy/connect.go b/system/lemmy/connect.go new file mode 100644 index 0000000..47a386e --- /dev/null +++ b/system/lemmy/connect.go @@ -0,0 +1,54 @@ +package lemmy + +import ( + "bufio" + "fmt" + "os" + "strings" + "syscall" + + "golang.org/x/term" +) + +func (sys *System) Connect(sysURL string) error { + // Request input from user + scanner := bufio.NewScanner(os.Stdin) + var username string = "" + for username == "" { + fmt.Printf( + "Please enter your username or email: ", + ) + scanner.Scan() + username = strings.ReplaceAll(scanner.Text(), " ", "") + if username == "" { + fmt.Println("Invalid input") + } + } + + // Request input from user + var password string = "" + for password == "" { + fmt.Printf( + "Please enter your password (will not echo): ", + ) + bytepw, err := term.ReadPassword(int(syscall.Stdin)) + fmt.Println("") + if err != nil || len(bytepw) == 0 { + fmt.Println("Invalid input") + } + password = string(bytepw) + } + + // Credentials + credentials := make(map[string]string) + credentials["username"] = username + credentials["password"] = password + + if sys.config == nil { + sys.config = make(map[string]interface{}) + } + sys.config["url"] = sysURL + sys.config["credentials"] = credentials + + return nil +} diff --git a/system/lemmy/lemmy.go b/system/lemmy/lemmy.go index 574c425..2cce7b4 100644 --- a/system/lemmy/lemmy.go +++ b/system/lemmy/lemmy.go @@ -1,14 +1,19 @@ package lemmy import ( + "context" + "github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/system/adapter" + "go.arsenm.dev/go-lemmy" + "go.arsenm.dev/go-lemmy/types" "go.uber.org/zap" ) type System struct { config map[string]interface{} logger *zap.SugaredLogger + client *lemmy.Client } func (sys *System) GetConfig() map[string]interface{} { @@ -23,16 +28,52 @@ func (sys *System) SetLogger(logger *zap.SugaredLogger) { sys.logger = logger } -func (sys *System) Connect(sysURL string) error { - return nil -} - func (sys *System) Load() error { + var err error + + url := sys.config["url"] + if url == nil { + return nil + } + + sys.client, err = lemmy.New(sys.config["url"].(string)) + if err != nil { + return err + } + + credentials := make(map[string]string) + for k, v := range (sys.config["credentials"]).(map[string]interface{}) { + credentials[k] = v.(string) + } + + err = sys.client.Login(context.Background(), types.Login{ + UsernameOrEmail: credentials["username"], + Password: credentials["password"], + }) + if err != nil { + return err + } return nil } func (sys *System) ListPosts() ([]post.Post, error) { - return []post.Post{}, nil + resp, err := sys.client.Posts(context.Background(), types.GetPosts{ + Type: types.NewOptional(types.ListingLocal), + Sort: types.NewOptional(types.New), + }) + if err != nil { + return []post.Post{}, err + } + + var models []post.Post + for _, i := range resp.Posts { + models = append(models, post.Post{ + ID: string(i.Post.ID), + Subject: i.Post.Name, + }) + } + + return models, nil } func (sys *System) GetCapabilities() []adapter.Capability {