checkpoint: receive an display relationship to an account

This commit is contained in:
Dan Anglin 2024-05-20 09:16:44 +01:00
parent b1240c42f7
commit 4d0522eb49
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 106 additions and 8 deletions

View file

@ -12,14 +12,15 @@ import (
type showCommand struct { type showCommand struct {
*flag.FlagSet *flag.FlagSet
myAccount bool myAccount bool
resourceType string showAccountRelationship bool
account string resourceType string
statusID string account string
timelineCategory string statusID string
listID string timelineCategory string
tag string listID string
timelineLimit int tag string
timelineLimit int
} }
func newShowCommand(name, summary string) *showCommand { func newShowCommand(name, summary string) *showCommand {
@ -28,6 +29,7 @@ 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.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.statusID, statusIDFlag, "", "specify the ID of the status to display") command.StringVar(&command.statusID, statusIDFlag, "", "specify the ID of the status to display")
@ -103,6 +105,15 @@ func (c *showCommand) showAccount(gts *client.Client) error {
fmt.Println(account) fmt.Println(account)
if c.showAccountRelationship {
relationship, err := gts.GetAccountRelationship(account.ID)
if err != nil {
return fmt.Errorf("unable to retrieve the relationship to this account; %w", err)
}
fmt.Println(relationship)
}
return nil return nil
} }

View file

@ -32,3 +32,20 @@ func (g *Client) GetAccount(accountURI string) (model.Account, error) {
return account, nil return account, nil
} }
func (g *Client) GetAccountRelationship(accountID string) (model.AccountRelationship, error) {
path := "/api/v1/accounts/relationships?id=" + accountID
url := g.Authentication.Instance + path
var relationships []model.AccountRelationship
if err := g.sendRequest(http.MethodGet, url, nil, &relationships); err != nil {
return model.AccountRelationship{}, fmt.Errorf("received an error after sending the request to get the account relationship; %w", err)
}
if len(relationships) != 1 {
return model.AccountRelationship{}, fmt.Errorf("unexpected number of account relationships returned; want 1, got %d", len(relationships))
}
return relationships[0], nil
}

View file

@ -0,0 +1,70 @@
package model
import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
type AccountRelationship struct {
ID string `json:"id"`
PrivateNote string `json:"note"`
BlockedBy bool `json:"blocked_by"`
Blocking bool `json:"blocking"`
DomainBlocking bool `json:"domain_blocking"`
Endorsed bool `json:"endorsed"`
FollowedBy bool `json:"followed_by"`
Following bool `json:"following"`
Muting bool `json:"muting"`
MutingNotifications bool `json:"muting_notifications"`
Notifying bool `json:"notifying"`
FollowRequested bool `json:"requested"`
FollowRequestedBy bool `json:"requested_by"`
ShowingReblogs bool `json:"showing_reblogs"`
}
func (a AccountRelationship) String() string {
format := `
%s
%s: %t
%s: %t
%s: %t
%s: %t
%s: %t
%s: %t
%s: %t
%s: %t
%s: %t
%s: %t
%s: %t`
privateNoteFormat := `
%s
%s`
output := fmt.Sprintf(
format,
utilities.HeaderFormat("YOUR RELATIONSHIP TO THIS ACCOUNT:"),
utilities.FieldFormat("Following"), a.Following,
utilities.FieldFormat("Is following you"), a.FollowedBy,
utilities.FieldFormat("A follow request was sent and is pending"), a.FollowRequested,
utilities.FieldFormat("Received a pending follow request"), a.FollowRequestedBy,
utilities.FieldFormat("Endorsed"), a.Endorsed,
utilities.FieldFormat("Showing Reposts (boosts)"), a.ShowingReblogs,
utilities.FieldFormat("Muted"), a.Muting,
utilities.FieldFormat("Notifications muted"), a.MutingNotifications,
utilities.FieldFormat("Blocking"), a.Blocking,
utilities.FieldFormat("Is blocking you"), a.BlockedBy,
utilities.FieldFormat("Blocking account's domain"), a.DomainBlocking,
)
if a.PrivateNote != "" {
output += fmt.Sprintf(
privateNoteFormat,
utilities.HeaderFormat("YOUR PRIVATE NOTE ABOUT THIS ACCOUNT:"),
utilities.WrapLines(a.PrivateNote, "\n ", 80),
)
}
return output
}