diff --git a/cmd/root.go b/cmd/root.go index 6b81d59..a35aa89 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -80,8 +80,8 @@ var rootCmd = &cobra.Command{ _ = loadSystems(&c) // TODO: Handle errs posts, err := (*c.Systems[0]).ListPosts() + fmt.Println("-----------------------") fmt.Printf("%v\n", posts) - fmt.Printf("%s\n", err) os.Exit(0) tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen()) diff --git a/system/discourse/client.go b/system/discourse/client.go index f6ffc0b..608525e 100644 --- a/system/discourse/client.go +++ b/system/discourse/client.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "errors" - "fmt" "io/ioutil" "net/http" "net/url" @@ -110,7 +109,6 @@ func NewClient(cc *ClientConfig) *Client { c.httpClient.HTTPClient = cc.HTTPClient c.endpoint, _ = url.Parse(cc.Endpoint) c.credentials = cc.Credentials - fmt.Printf("%v\n", c.credentials) c.Posts = &PostServiceHandler{client: c} @@ -149,10 +147,9 @@ func (c *Client) NewRequest( req.Header.Add("User-Agent", "gobbs") req.Header.Add("Accept", "application/json") req.Header.Add("Content-Type", "application/json") - req.Header.Add("Api-Username", c.credentials["username"]) - req.Header.Add("Api-Key", c.credentials["key"]) + req.Header.Add("User-Api-Client-Id", c.credentials["client_id"]) + req.Header.Add("User-Api-Key", c.credentials["key"]) - fmt.Printf("%v\n", req) return req, nil } @@ -186,8 +183,6 @@ func (c *Client) Do( } } - fmt.Printf("%v\n", res) - if res.StatusCode < http.StatusOK || res.StatusCode > http.StatusNoContent { return &RequestError{ diff --git a/system/discourse/connect.go b/system/discourse/connect.go index f585ec9..33a1a8f 100644 --- a/system/discourse/connect.go +++ b/system/discourse/connect.go @@ -2,7 +2,6 @@ package discourse import ( "bufio" - "context" "crypto/rand" "crypto/rsa" "crypto/x509" @@ -10,13 +9,11 @@ import ( "encoding/json" "encoding/pem" "fmt" - "net/http" "net/url" "os" "strings" "github.com/google/uuid" - "github.com/mrusme/gobbs/models/post" "github.com/pkg/browser" ) @@ -79,7 +76,7 @@ func (sys *System) Connect(sysURL string) error { values := url.Values{} values.Set("application_name", "gobbs") values.Set("client_id", clientID) - values.Set("scopes", "read,write,notifications") + values.Set("scopes", "read,write") values.Set("public_key", publicKeyPEM) values.Set("nonce", nonce) @@ -108,6 +105,7 @@ func (sys *System) Connect(sysURL string) error { if err != nil { return err } + fmt.Println(string(decodedUserAPIKey)) decryptedUserAPIKey, err := privateKey.Decrypt( rand.Reader, @@ -117,18 +115,21 @@ func (sys *System) Connect(sysURL string) error { if err != nil { return err } + fmt.Println(string(decryptedUserAPIKey)) var userAPIKey UserAPIKey err = json.Unmarshal(decryptedUserAPIKey, &userAPIKey) if err != nil { return err } + fmt.Printf("%v\n", userAPIKey) // Credentials credentials := make(map[string]string) credentials["pk"] = privateKeyPEM credentials["username"] = username credentials["key"] = userAPIKey.Key + credentials["client_id"] = clientID if sys.config == nil { sys.config = make(map[string]interface{}) @@ -138,25 +139,3 @@ func (sys *System) Connect(sysURL string) error { return nil } - -func (sys *System) ListPosts() ([]post.Post, error) { - credentials := make(map[string]string) - for k, v := range (sys.config["credentials"]).(map[string]interface{}) { - credentials[k] = v.(string) - } - c := NewClient(&ClientConfig{ - Endpoint: sys.config["url"].(string), - Credentials: credentials, - HTTPClient: http.DefaultClient, - Logger: sys.logger, - }) - - posts, err := c.Posts.List(context.Background()) - if err != nil { - return []post.Post{}, err - } - - fmt.Printf("%v\n", posts) - - return []post.Post{}, nil -} diff --git a/system/discourse/discourse.go b/system/discourse/discourse.go index 7671718..0b2fe7c 100644 --- a/system/discourse/discourse.go +++ b/system/discourse/discourse.go @@ -1,6 +1,10 @@ package discourse import ( + "context" + "net/http" + + "github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/system/adapter" "go.uber.org/zap" ) @@ -45,3 +49,31 @@ func (sys *System) GetCapabilities() []adapter.Capability { return caps } + +func (sys *System) ListPosts() ([]post.Post, error) { + credentials := make(map[string]string) + for k, v := range (sys.config["credentials"]).(map[string]interface{}) { + credentials[k] = v.(string) + } + c := NewClient(&ClientConfig{ + Endpoint: sys.config["url"].(string), + Credentials: credentials, + HTTPClient: http.DefaultClient, + Logger: sys.logger, + }) + + posts, err := c.Posts.List(context.Background()) + if err != nil { + return []post.Post{}, err + } + + var mPosts []post.Post + for _, p := range posts { + mPosts = append(mPosts, post.Post{ + ID: string(p.ID), + Subject: p.TopicTitle, + }) + } + + return mPosts, nil +} diff --git a/system/discourse/posts.go b/system/discourse/posts.go index 2caa206..e46e02d 100644 --- a/system/discourse/posts.go +++ b/system/discourse/posts.go @@ -8,40 +8,40 @@ import ( const PostsBaseURL = "/posts" type PostModel struct { - ID string `json:"id"` - Name string `json:"name"` - Username string `json:"username"` - AvatarTemplate string `json:"avater_template"` - CreatedAt string `json:"created_at"` - Cooked string `json:"cooked"` - PostNumber int `json:"post_number"` - PostType int `json:"post_type"` - UpdatedAt string `json:"updated_at"` - ReplyCount int `json:"reply_count"` - IncomingLinkCount int `json:"incoming_link_count"` - Reads int `json:"reads"` - ReadersCount int `json:"readers_count"` - Score int `json:"score"` - Yours bool `json:"yours"` - TopicID int `json:"topic_id"` - TopicSlug string `json:"topic_slug"` - TopicTitle string `json:"topic_title"` - TopicHTMLTitle string `json:"topic_html_title"` - CategoryID int `json:"category_id"` - DisplayUsername string `json:"display_username"` - PrimaryGroupName string `json:"primary_group_name"` - FlairName string `json:"flair_name"` - FlairURL string `json:"flair_url"` - FlairBGColor string `json:"flair_bg_color"` - FlairColor string `json:"flair_color"` - Version int `json:"version"` - CanEdit bool `json:"can_edit"` - CanDelete bool `json:"can_delete"` - CanRecover bool `json:"can_recover"` - CanWiki bool `json:"can_wiki"` - UserTitle string `json:"user_title"` - Raw string `json:"raw"` - ActionsSummary struct { + ID int `json:"id"` + Name string `json:"name"` + Username string `json:"username"` + AvatarTemplate string `json:"avater_template"` + CreatedAt string `json:"created_at"` + Cooked string `json:"cooked"` + PostNumber int `json:"post_number"` + PostType int `json:"post_type"` + UpdatedAt string `json:"updated_at"` + ReplyCount int `json:"reply_count"` + IncomingLinkCount int `json:"incoming_link_count"` + Reads int `json:"reads"` + ReadersCount int `json:"readers_count"` + Score float32 `json:"score"` + Yours bool `json:"yours"` + TopicID int `json:"topic_id"` + TopicSlug string `json:"topic_slug"` + TopicTitle string `json:"topic_title"` + TopicHTMLTitle string `json:"topic_html_title"` + CategoryID int `json:"category_id"` + DisplayUsername string `json:"display_username"` + PrimaryGroupName string `json:"primary_group_name"` + FlairName string `json:"flair_name"` + FlairURL string `json:"flair_url"` + FlairBGColor string `json:"flair_bg_color"` + FlairColor string `json:"flair_color"` + Version int `json:"version"` + CanEdit bool `json:"can_edit"` + CanDelete bool `json:"can_delete"` + CanRecover bool `json:"can_recover"` + CanWiki bool `json:"can_wiki"` + UserTitle string `json:"user_title"` + Raw string `json:"raw"` + ActionsSummary []struct { ID int `json:"id"` CanAct bool `json:"can_act"` } `json:"actions_summary"`