feat: show user preferences

Optionally show the user's preferences when viewing account.
This commit is contained in:
Dan Anglin 2024-05-20 19:46:05 +01:00
parent de5cf65aa8
commit a77f3741a0
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 84 additions and 19 deletions

View file

@ -11,23 +11,25 @@ import (
) )
const ( const (
accountFlag = "account" accountFlag = "account"
accountIDFlag = "account-id" accountIDFlag = "account-id"
addToFlag = "add-to" addToFlag = "add-to"
instanceFlag = "instance" instanceFlag = "instance"
listIDFlag = "list-id" listIDFlag = "list-id"
listTitleFlag = "list-title" listTitleFlag = "list-title"
listRepliesPolicyFlag = "list-replies-policy" listRepliesPolicyFlag = "list-replies-policy"
myAccountFlag = "my-account" myAccountFlag = "my-account"
removeFromFlag = "remove-from" removeFromFlag = "remove-from"
resourceTypeFlag = "type" resourceTypeFlag = "type"
statusIDFlag = "status-id" statusIDFlag = "status-id"
tagFlag = "tag" tagFlag = "tag"
timelineCategoryFlag = "timeline-category" timelineCategoryFlag = "timeline-category"
limitFlag = "limit" limitFlag = "limit"
toAccountFlag = "to-account" toAccountFlag = "to-account"
showRepostsFlag = "show-reposts" showRepostsFlag = "show-reposts"
notifyFlag = "notify" notifyFlag = "notify"
showAccountRelationshipFlag = "show-account-relationship"
showUserPreferencesFlag = "show-preferences"
) )
const ( const (

View file

@ -13,6 +13,7 @@ type showCommand struct {
*flag.FlagSet *flag.FlagSet
myAccount bool myAccount bool
showAccountRelationship bool showAccountRelationship bool
showUserPreferences bool
resourceType string resourceType string
account string account string
accountID 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.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.resourceType, resourceTypeFlag, "", "specify the type of resource to display")
command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup") command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup")
command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID") command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID")
@ -109,7 +111,7 @@ func (c *showCommand) showAccount(gts *client.Client) error {
fmt.Println(account) fmt.Println(account)
if c.showAccountRelationship { if !c.myAccount && c.showAccountRelationship {
relationship, err := gts.GetAccountRelationship(account.ID) relationship, err := gts.GetAccountRelationship(account.ID)
if err != nil { if err != nil {
return fmt.Errorf("unable to retrieve the relationship to this account; %w", err) 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) 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 return nil
} }

View file

@ -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
}

View file

@ -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,
)
}