Compare commits

..

No commits in common. "0002aa347e98f666b754b86625dc3fa459d56468" and "9d8cb2c68e23f4b22758d0aa85948b6d09a3bf7d" have entirely different histories.

9 changed files with 155 additions and 195 deletions

View file

@ -1,76 +0,0 @@
package main
import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
)
func getAccountID(gts *client.Client, myAccount bool, accountName string) (string, error) {
var (
accountID string
err error
)
switch {
case myAccount:
accountID, err = getMyAccountID(gts)
if err != nil {
return "", fmt.Errorf("unable to get your account ID; %w", err)
}
case accountName != "":
accountID, err = getTheirAccountID(gts, accountName)
if err != nil {
return "", fmt.Errorf("unable to get their account ID; %w", err)
}
default:
return "", noAccountSpecifiedError{}
}
return accountID, nil
}
func getTheirAccountID(gts *client.Client, accountURI string) (string, error) {
account, err := getAccount(gts, accountURI)
if err != nil {
return "", fmt.Errorf("unable to retrieve your account; %w", err)
}
return account.ID, nil
}
func getMyAccountID(gts *client.Client) (string, error) {
account, err := getMyAccount(gts)
if err != nil {
return "", fmt.Errorf("received an error while getting your account details; %w", err)
}
return account.ID, nil
}
func getMyAccount(gts *client.Client) (model.Account, error) {
authConfig, err := config.NewAuthenticationConfigFromFile()
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration; %w", err)
}
accountURI := authConfig.CurrentAccount
account, err := getAccount(gts, accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve your account; %w", err)
}
return account, nil
}
func getAccount(gts *client.Client, accountURI string) (model.Account, error) {
account, err := gts.GetAccount(accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the account details; %w", err)
}
return account, nil
}

View file

@ -12,20 +12,20 @@ type addCommand struct {
toResourceType string
listID string
accountNames accountNames
accountIDs accountIDs
}
func newAddCommand(name, summary string) *addCommand {
emptyArr := make([]string, 0, 3)
command := addCommand{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
accountNames: accountNames(emptyArr),
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
accountIDs: accountIDs(emptyArr),
}
command.StringVar(&command.toResourceType, addToFlag, "", "specify the type of resource to add to")
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to add to")
command.Var(&command.accountNames, accountNameFlag, "the name of the account to add to the resource")
command.Var(&command.accountIDs, accountIDFlag, "the ID of the account to add to the list")
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
@ -34,7 +34,7 @@ func newAddCommand(name, summary string) *addCommand {
func (c *addCommand) Execute() error {
if c.toResourceType == "" {
return flagNotSetError{flagText: addToFlag}
return flagNotSetError{flagText: "add-to"}
}
funcMap := map[string]func(*client.Client) error{
@ -59,21 +59,11 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
return flagNotSetError{flagText: listIDFlag}
}
if len(c.accountNames) == 0 {
return noAccountSpecifiedError{}
if len(c.accountIDs) == 0 {
return noAccountIDsSpecifiedError{}
}
accountIDs := make([]string, len(c.accountNames))
for i := range c.accountNames {
accountID, err := getTheirAccountID(gtsClient, c.accountNames[i])
if err != nil {
return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err)
}
accountIDs[i] = accountID
}
if err := gtsClient.AddAccountsToList(c.listID, accountIDs); err != nil {
if err := gtsClient.AddAccountsToList(c.listID, []string(c.accountIDs)); err != nil {
return fmt.Errorf("unable to add the accounts to the list; %w", err)
}

View file

@ -32,8 +32,8 @@ func (e unknownSubcommandError) Error() string {
return "unknown subcommand '" + e.subcommand + "'"
}
type noAccountSpecifiedError struct{}
type noAccountIDsSpecifiedError struct{}
func (e noAccountSpecifiedError) Error() string {
return "no account specified in this request"
func (e noAccountIDsSpecifiedError) Error() string {
return "no account IDs specified"
}

View file

@ -2,13 +2,13 @@ package main
import "strings"
type accountNames []string
type accountIDs []string
func (a *accountNames) String() string {
func (a *accountIDs) String() string {
return strings.Join(*a, ", ")
}
func (a *accountNames) Set(value string) error {
func (a *accountIDs) Set(value string) error {
if len(value) > 0 {
*a = append(*a, value)
}

View file

@ -4,10 +4,15 @@ import (
"flag"
"fmt"
"os"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
)
const (
accountNameFlag = "account-name"
accountFlag = "account"
accountIDFlag = "account-id"
addToFlag = "add-to"
instanceFlag = "instance"
listIDFlag = "list-id"
@ -146,3 +151,19 @@ func run() error {
return nil
}
func getMyAccount(gts *client.Client) (model.Account, error) {
authConfig, err := config.NewAuthenticationConfigFromFile()
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration; %w", err)
}
accountURI := authConfig.CurrentAccount
account, err := gts.GetAccount(accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the account details; %w", err)
}
return account, nil
}

View file

@ -12,20 +12,20 @@ type removeCommand struct {
fromResourceType string
listID string
accountNames accountNames
accountIDs accountIDs
}
func newRemoveCommand(name, summary string) *removeCommand {
emptyArr := make([]string, 0, 3)
command := removeCommand{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
accountNames: accountNames(emptyArr),
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
accountIDs: accountIDs(emptyArr),
}
command.StringVar(&command.fromResourceType, removeFromFlag, "", "specify the type of resource to remove from")
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to remove from")
command.Var(&command.accountNames, accountNameFlag, "the name of the account to remove from the resource")
command.Var(&command.accountIDs, accountIDFlag, "the ID of the account to remove from the list")
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
@ -34,7 +34,7 @@ func newRemoveCommand(name, summary string) *removeCommand {
func (c *removeCommand) Execute() error {
if c.fromResourceType == "" {
return flagNotSetError{flagText: removeFromFlag}
return flagNotSetError{flagText: "remove-from"}
}
funcMap := map[string]func(*client.Client) error{
@ -59,21 +59,11 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error {
return flagNotSetError{flagText: listIDFlag}
}
if len(c.accountNames) == 0 {
return noAccountSpecifiedError{}
if len(c.accountIDs) == 0 {
return noAccountIDsSpecifiedError{}
}
accountIDs := make([]string, len(c.accountNames))
for i := range c.accountNames {
accountID, err := getTheirAccountID(gtsClient, c.accountNames[i])
if err != nil {
return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err)
}
accountIDs[i] = accountID
}
if err := gtsClient.RemoveAccountsFromList(c.listID, accountIDs); err != nil {
if err := gtsClient.RemoveAccountsFromList(c.listID, []string(c.accountIDs)); err != nil {
return fmt.Errorf("unable to remove the accounts from the list; %w", err)
}

View file

@ -15,7 +15,8 @@ type showCommand struct {
showAccountRelationship bool
showUserPreferences bool
resourceType string
accountName string
account string
accountID string
statusID string
timelineCategory string
listID string
@ -32,7 +33,8 @@ func newShowCommand(name, summary string) *showCommand {
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.accountName, accountNameFlag, "", "specify the account name in full (username@domain)")
command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup")
command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID")
command.StringVar(&command.statusID, statusIDFlag, "", "specify the ID of the status to display")
command.StringVar(&command.timelineCategory, timelineCategoryFlag, "home", "specify the type of timeline to display (valid values are home, public, list and tag)")
command.StringVar(&command.listID, listIDFlag, "", "specify the ID of the list to display")
@ -93,16 +95,18 @@ func (c *showCommand) showAccount(gts *client.Client) error {
if c.myAccount {
account, err = getMyAccount(gts)
if err != nil {
return fmt.Errorf("received an error while getting the account details; %w", err)
return fmt.Errorf("received an error while getting account details; %w", err)
}
} else {
if c.accountName == "" {
return flagNotSetError{flagText: accountNameFlag}
if c.account == "" {
return flagNotSetError{flagText: accountFlag}
}
account, err = getAccount(gts, c.accountName)
accountURI := c.account
account, err = gts.GetAccount(accountURI)
if err != nil {
return fmt.Errorf("received an error while getting the account details; %w", err)
return fmt.Errorf("unable to retrieve the account details; %w", err)
}
}
@ -234,9 +238,21 @@ func (c *showCommand) showLists(gts *client.Client) error {
}
func (c *showCommand) showFollowers(gts *client.Client) error {
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
if err != nil {
return fmt.Errorf("received an error while getting the account ID; %w", err)
var accountID string
if c.myAccount {
account, err := getMyAccount(gts)
if err != nil {
return fmt.Errorf("received an error while getting account details; %w", err)
}
accountID = account.ID
} else {
if c.accountID == "" {
return flagNotSetError{flagText: accountIDFlag}
}
accountID = c.accountID
}
followers, err := gts.GetFollowers(accountID, c.limit)
@ -254,9 +270,21 @@ func (c *showCommand) showFollowers(gts *client.Client) error {
}
func (c *showCommand) showFollowing(gts *client.Client) error {
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
if err != nil {
return fmt.Errorf("received an error while getting the account ID; %w", err)
var accountID string
if c.myAccount {
account, err := getMyAccount(gts)
if err != nil {
return fmt.Errorf("received an error while getting account details; %w", err)
}
accountID = account.ID
} else {
if c.accountID == "" {
return flagNotSetError{flagText: accountIDFlag}
}
accountID = c.accountID
}
following, err := gts.GetFollowing(accountID, c.limit)

View file

@ -113,69 +113,6 @@ func (a Account) String() string {
)
}
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 WITH 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
}
type AccountListType int
const (

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