From 0a82dee153e7179dce9471e7a54e827feb484081 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Tue, 20 Dec 2022 17:40:03 -0700 Subject: [PATCH] Get account info command --- README.md | 27 +++++++++++++++++++++++ cmd/account.go | 27 +++++++++++++++++++++++ pkg/mastodon/account.go | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/README.md b/README.md index 35b30ec..1819d50 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,33 @@ able to do with my Fedi data. ## Usage +### Authenticate to a server + + ``` dead-tooter login --host hackers.town ``` + +The first time you call `login`, this will register with your chosen server and +store application information in your `~/.config/` directory. Your default +browser will open to a login page, or if you are logged in, an app authorization +page where you will be presented with an authorization token. Copy the token and +paste it at the prompt. A login token will be stored in the same directory. + +### Get account info + +``` +dead-tooter --host hackers.town account [id] +``` + +### Get a list of followers + +``` +dead-tooter --host hackers.town account followers +``` + +### Get a list of following + +``` +dead-tooter --host hackers.town account following +``` diff --git a/cmd/account.go b/cmd/account.go index 4f43972..963c8ee 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -10,8 +10,10 @@ import ( var account mastodon.Account func init() { + accountCmd.AddCommand(getAccountCmd) accountCmd.AddCommand(getFollowersCmd) accountCmd.AddCommand(getFollowingCmd) + rootCmd.AddCommand(accountCmd) } @@ -21,6 +23,31 @@ var accountCmd = &cobra.Command{ Long: "Commands related to the logged in Mastodon account.", } +var getAccountCmd = &cobra.Command{ + Use: "get [id]", + Short: "Get account info", + Long: "Return information the account specified by [id]. If no id is supplied, return information on the current logged in user.", + + Run: func(cmd *cobra.Command, args []string) { + err := account.VerifyCredentials(host, token) + if err != nil { + panic(err.Error()) + } + + if len(args) < 1 { + fmt.Printf("%+v", account) + return + } + + acct, err := account.Get(args[0], host, token) + if err != nil { + panic(err.Error()) + } + + fmt.Printf("%+v", acct) + }, +} + var getFollowersCmd = &cobra.Command{ Use: "followers", Short: "Get account followers", diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index dcc0896..75cbf9b 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -171,6 +171,53 @@ func (a *Account) VerifyCredentials(host string, token Token) (err error) { return } +// Get returns the details of account specified by ID. +func (a *Account) Get( + ID string, host string, token Token) (account Account, err error) { + + client := &http.Client{} + + id := url.PathEscape(ID) + path, err := url.JoinPath("api/v1/accounts", id) + if err != nil { + return + } + + u := url.URL{ + Host: host, + Path: path, + } + u.Scheme = "https" + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return + } + req.Header.Add("Authorization", "Bearer "+token.AccessToken) + + resp, err := client.Do(req) + if err != nil { + return + } + if resp.StatusCode != 200 { + err = errors.New(resp.Status) + return + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return + } + + err = json.Unmarshal(body, &account) + if err != nil { + return + } + + return +} + // GetFollowers returns a list of all accounts following the logged in user func (a *Account) GetFollowers( host string, token Token) (followers []Account, err error) { -- 2.39.5