From a77f3741a0b15d19f39c017670b515e79509a3c5 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 20 May 2024 19:46:05 +0100 Subject: [PATCH] feat: show user preferences Optionally show the user's preferences when viewing account. --- cmd/enbas/main.go | 36 ++++++++++++++++++---------------- cmd/enbas/show.go | 15 ++++++++++++-- internal/client/preferences.go | 20 +++++++++++++++++++ internal/model/preferences.go | 32 ++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 internal/client/preferences.go create mode 100644 internal/model/preferences.go diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 9be1fe1..5bfd4ea 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -11,23 +11,25 @@ import ( ) const ( - accountFlag = "account" - accountIDFlag = "account-id" - addToFlag = "add-to" - instanceFlag = "instance" - listIDFlag = "list-id" - listTitleFlag = "list-title" - listRepliesPolicyFlag = "list-replies-policy" - myAccountFlag = "my-account" - removeFromFlag = "remove-from" - resourceTypeFlag = "type" - statusIDFlag = "status-id" - tagFlag = "tag" - timelineCategoryFlag = "timeline-category" - limitFlag = "limit" - toAccountFlag = "to-account" - showRepostsFlag = "show-reposts" - notifyFlag = "notify" + accountFlag = "account" + accountIDFlag = "account-id" + addToFlag = "add-to" + instanceFlag = "instance" + listIDFlag = "list-id" + listTitleFlag = "list-title" + listRepliesPolicyFlag = "list-replies-policy" + myAccountFlag = "my-account" + removeFromFlag = "remove-from" + resourceTypeFlag = "type" + statusIDFlag = "status-id" + tagFlag = "tag" + timelineCategoryFlag = "timeline-category" + limitFlag = "limit" + toAccountFlag = "to-account" + showRepostsFlag = "show-reposts" + notifyFlag = "notify" + showAccountRelationshipFlag = "show-account-relationship" + showUserPreferencesFlag = "show-preferences" ) const ( diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index f58e841..cd4d148 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -13,6 +13,7 @@ type showCommand struct { *flag.FlagSet myAccount bool showAccountRelationship bool + showUserPreferences bool resourceType string account string accountID string @@ -29,7 +30,8 @@ func newShowCommand(name, summary string) *showCommand { } command.BoolVar(&command.myAccount, myAccountFlag, false, "set to true to lookup your account") - command.BoolVar(&command.showAccountRelationship, "show-account-relationship", false, "show your relationship to the specified account") + command.BoolVar(&command.showAccountRelationship, showAccountRelationshipFlag, false, "show your relationship to the specified account") + command.BoolVar(&command.showUserPreferences, showUserPreferencesFlag, false, "show your preferences") command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to display") command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup") command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID") @@ -109,7 +111,7 @@ func (c *showCommand) showAccount(gts *client.Client) error { fmt.Println(account) - if c.showAccountRelationship { + if !c.myAccount && c.showAccountRelationship { relationship, err := gts.GetAccountRelationship(account.ID) if err != nil { return fmt.Errorf("unable to retrieve the relationship to this account; %w", err) @@ -118,6 +120,15 @@ func (c *showCommand) showAccount(gts *client.Client) error { fmt.Println(relationship) } + if c.myAccount && c.showUserPreferences { + preferences, err := gts.GetUserPreferences() + if err != nil { + return fmt.Errorf("unable to retrieve the user preferences; %w", err) + } + + fmt.Println(preferences) + } + return nil } diff --git a/internal/client/preferences.go b/internal/client/preferences.go new file mode 100644 index 0000000..3bbc70b --- /dev/null +++ b/internal/client/preferences.go @@ -0,0 +1,20 @@ +package client + +import ( + "fmt" + "net/http" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +func (g *Client) GetUserPreferences() (model.Preferences, error) { + url := g.Authentication.Instance + "/api/v1/preferences" + + var preferences model.Preferences + + if err := g.sendRequest(http.MethodGet, url, nil, &preferences); err != nil { + return model.Preferences{}, fmt.Errorf("received an error after sending the request to get the user preferences; %w", err) + } + + return preferences, nil +} diff --git a/internal/model/preferences.go b/internal/model/preferences.go new file mode 100644 index 0000000..692c064 --- /dev/null +++ b/internal/model/preferences.go @@ -0,0 +1,32 @@ +package model + +import ( + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" +) + +type Preferences struct { + PostingDefaultVisibility string `json:"posting:default:visibility"` + PostingDefaultSensitive bool `json:"posting:default:sensitive"` + PostingDefaultLanguage string `json:"posting:default:language"` + ReadingExpandMedia string `json:"reading:expand:media"` + ReadingExpandSpoilers bool `json:"reading:expand:spoilers"` + ReadingAutoplayGifs bool `json:"reading:autoplay:gifs"` +} + +func (p Preferences) String() string { + format := ` +%s + %s: %s + %s: %s + %s: %t` + + return fmt.Sprintf( + format, + utilities.HeaderFormat("YOUR PREFERENCES:"), + utilities.FieldFormat("Default post language"), p.PostingDefaultLanguage, + utilities.FieldFormat("Default post visibility"), p.PostingDefaultVisibility, + utilities.FieldFormat("Mark posts as sensitive by default"), p.PostingDefaultSensitive, + ) +}