]> Vexing Labs - dead-tooter.git/commitdiff
Add following cmd, move config dir to user config
authorAdam Shamblin <adam@vexingworkshop.com>
Tue, 20 Dec 2022 03:13:22 +0000 (20:13 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Tue, 20 Dec 2022 03:13:22 +0000 (20:13 -0700)
cmd/account.go
pkg/mastodon/account.go
pkg/mastodon/application.go
pkg/mastodon/oauth.go

index 93eed4d3428fb1518214ac0f534a38b0f13c1bc1..4f4397291adfc4fd3260cf7e78d5941b4d01271a 100644 (file)
@@ -11,6 +11,7 @@ var account mastodon.Account
 
 func init() {
        accountCmd.AddCommand(getFollowersCmd)
+       accountCmd.AddCommand(getFollowingCmd)
        rootCmd.AddCommand(accountCmd)
 }
 
@@ -18,8 +19,6 @@ var accountCmd = &cobra.Command{
        Use:   "account",
        Short: "Account commands",
        Long:  "Commands related to the logged in Mastodon account.",
-       Run: func(cmd *cobra.Command, args []string) {
-       },
 }
 
 var getFollowersCmd = &cobra.Command{
@@ -44,3 +43,26 @@ var getFollowersCmd = &cobra.Command{
                }
        },
 }
+
+var getFollowingCmd = &cobra.Command{
+       Use:   "following",
+       Short: "Get accounts followed",
+       Long:  "Get a list of followed accounts for the current account.",
+
+       PreRun: func(cmd *cobra.Command, args []string) {
+               err := account.VerifyCredentials(host, token)
+               if err != nil {
+                       panic(err.Error())
+               }
+       },
+
+       Run: func(cmd *cobra.Command, args []string) {
+               following, err := account.GetFollowing(host, token)
+               if err != nil {
+                       panic(err.Error())
+               }
+               for _, value := range following {
+                       fmt.Printf("%s\t%s\n", value.ID, value.Acct)
+               }
+       },
+}
index 130fc9ed96948dc7ee38f16ffcf7f219e95662f4..f554f9136f8bed1719950d43c233a44645e47e4f 100644 (file)
@@ -217,3 +217,50 @@ func (a *Account) GetFollowers(
 
        return
 }
+
+// GetFollowing returns a list of all accounts followed by the logged in user
+func (a *Account) GetFollowing(
+       host string, token Token) (following []Account, err error) {
+
+       client := &http.Client{}
+
+       id := url.PathEscape(a.ID)
+       path, err := url.JoinPath("api/v1/accounts", id, "following")
+       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, &following)
+       if err != nil {
+               return
+       }
+
+       return
+}
index 6d4e2bb098b5aa27161353d67cf30d3942674f87..df8af3f3cf09af7f3bc698ae943ac14514100395 100644 (file)
@@ -7,9 +7,12 @@ import (
        "net/http"
        "net/url"
        "os"
+       "path/filepath"
 )
 
-const ConfigDir = "foodir/"
+// ConfigDir points to the default location of serialized files,
+// such as application info and tokens
+const ConfigDir = ".dead-tooter"
 
 // RedirectUris when passed to the redirect_uris parameter, will
 // show return the authorization code instead of redirecting the client.
@@ -79,7 +82,13 @@ func Create(host string, client Client) (app Application, err error) {
 // Load will hydrate an Application instance based upon data stored in
 // a file.
 func Load(name string) (app Application, err error) {
-       data, err := os.ReadFile(ConfigDir + name)
+       configdir, err := os.UserConfigDir()
+       if err != nil {
+               return
+       }
+       configfile := filepath.Join(configdir, "dead-tooter", name)
+
+       data, err := os.ReadFile(configfile)
        if err != nil && os.IsNotExist(err) {
                return
        }
@@ -94,6 +103,12 @@ func Load(name string) (app Application, err error) {
 
 // Save will store a serialized version of the Application struct to a file.
 func (a *Application) Save() (err error) {
+       configdir, err := os.UserConfigDir()
+       if err != nil {
+               return
+       }
+       configdir = filepath.Join(configdir, "dead-tooter")
+
        err = os.Mkdir(ConfigDir, 0750)
        if err != nil && !os.IsExist(err) {
                return
@@ -104,7 +119,8 @@ func (a *Application) Save() (err error) {
                return
        }
 
-       err = os.WriteFile(ConfigDir+a.Name, data, 0666)
+       err = os.WriteFile(
+               filepath.Join(configdir, "dead-tooter", a.Name), data, 0666)
        if err != nil {
                return
        }
index 6f12a81144af98098722ba58563167422bf58f5f..105557560d40d5b9c326c9bb07bd08b0a288da9b 100644 (file)
@@ -3,6 +3,7 @@ package mastodon
 import (
        "encoding/json"
        "os"
+       "path/filepath"
 )
 
 // Token struct contains the data returned by the Application login request.
@@ -16,7 +17,13 @@ type Token struct {
 // LoadToken deserializes the application authentication token from disc, if it
 // exists.
 func LoadToken() (token Token, err error) {
-       data, err := os.ReadFile(ConfigDir + "/token")
+       configdir, err := os.UserConfigDir()
+       if err != nil {
+               return
+       }
+       tokenfile := filepath.Join(configdir, "dead-tooter", "token")
+
+       data, err := os.ReadFile(tokenfile)
        if err != nil && os.IsNotExist(err) {
                return
        }
@@ -36,7 +43,13 @@ func (t *Token) Save() (err error) {
                return
        }
 
-       err = os.WriteFile(ConfigDir+"/token", data, 0666)
+       configdir, err := os.UserConfigDir()
+       if err != nil {
+               return
+       }
+
+       tokenfile := filepath.Join(configdir, "dead-tooter", "token")
+       err = os.WriteFile(tokenfile, data, 0666)
        if err != nil {
                return
        }