Compare commits
3 commits
9d8cb2c68e
...
0002aa347e
Author | SHA1 | Date | |
---|---|---|---|
0002aa347e | |||
f9f817eaa1 | |||
d1ceecf697 |
9 changed files with 195 additions and 155 deletions
76
cmd/enbas/account.go
Normal file
76
cmd/enbas/account.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
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
|
||||
}
|
|
@ -12,20 +12,20 @@ type addCommand struct {
|
|||
|
||||
toResourceType string
|
||||
listID string
|
||||
accountIDs accountIDs
|
||||
accountNames accountNames
|
||||
}
|
||||
|
||||
func newAddCommand(name, summary string) *addCommand {
|
||||
emptyArr := make([]string, 0, 3)
|
||||
|
||||
command := addCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
accountIDs: accountIDs(emptyArr),
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
accountNames: accountNames(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.accountIDs, accountIDFlag, "the ID of the account to add to the list")
|
||||
command.Var(&command.accountNames, accountNameFlag, "the name of the account to add to the resource")
|
||||
|
||||
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: "add-to"}
|
||||
return flagNotSetError{flagText: addToFlag}
|
||||
}
|
||||
|
||||
funcMap := map[string]func(*client.Client) error{
|
||||
|
@ -59,11 +59,21 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
|
|||
return flagNotSetError{flagText: listIDFlag}
|
||||
}
|
||||
|
||||
if len(c.accountIDs) == 0 {
|
||||
return noAccountIDsSpecifiedError{}
|
||||
if len(c.accountNames) == 0 {
|
||||
return noAccountSpecifiedError{}
|
||||
}
|
||||
|
||||
if err := gtsClient.AddAccountsToList(c.listID, []string(c.accountIDs)); err != nil {
|
||||
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 {
|
||||
return fmt.Errorf("unable to add the accounts to the list; %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -32,8 +32,8 @@ func (e unknownSubcommandError) Error() string {
|
|||
return "unknown subcommand '" + e.subcommand + "'"
|
||||
}
|
||||
|
||||
type noAccountIDsSpecifiedError struct{}
|
||||
type noAccountSpecifiedError struct{}
|
||||
|
||||
func (e noAccountIDsSpecifiedError) Error() string {
|
||||
return "no account IDs specified"
|
||||
func (e noAccountSpecifiedError) Error() string {
|
||||
return "no account specified in this request"
|
||||
}
|
||||
|
|
|
@ -2,13 +2,13 @@ package main
|
|||
|
||||
import "strings"
|
||||
|
||||
type accountIDs []string
|
||||
type accountNames []string
|
||||
|
||||
func (a *accountIDs) String() string {
|
||||
func (a *accountNames) String() string {
|
||||
return strings.Join(*a, ", ")
|
||||
}
|
||||
|
||||
func (a *accountIDs) Set(value string) error {
|
||||
func (a *accountNames) Set(value string) error {
|
||||
if len(value) > 0 {
|
||||
*a = append(*a, value)
|
||||
}
|
||||
|
|
|
@ -4,15 +4,10 @@ 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 (
|
||||
accountFlag = "account"
|
||||
accountIDFlag = "account-id"
|
||||
accountNameFlag = "account-name"
|
||||
addToFlag = "add-to"
|
||||
instanceFlag = "instance"
|
||||
listIDFlag = "list-id"
|
||||
|
@ -151,19 +146,3 @@ 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
|
||||
}
|
||||
|
|
|
@ -12,20 +12,20 @@ type removeCommand struct {
|
|||
|
||||
fromResourceType string
|
||||
listID string
|
||||
accountIDs accountIDs
|
||||
accountNames accountNames
|
||||
}
|
||||
|
||||
func newRemoveCommand(name, summary string) *removeCommand {
|
||||
emptyArr := make([]string, 0, 3)
|
||||
|
||||
command := removeCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
accountIDs: accountIDs(emptyArr),
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
accountNames: accountNames(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.accountIDs, accountIDFlag, "the ID of the account to remove from the list")
|
||||
command.Var(&command.accountNames, accountNameFlag, "the name of the account to remove from the resource")
|
||||
|
||||
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: "remove-from"}
|
||||
return flagNotSetError{flagText: removeFromFlag}
|
||||
}
|
||||
|
||||
funcMap := map[string]func(*client.Client) error{
|
||||
|
@ -59,11 +59,21 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error {
|
|||
return flagNotSetError{flagText: listIDFlag}
|
||||
}
|
||||
|
||||
if len(c.accountIDs) == 0 {
|
||||
return noAccountIDsSpecifiedError{}
|
||||
if len(c.accountNames) == 0 {
|
||||
return noAccountSpecifiedError{}
|
||||
}
|
||||
|
||||
if err := gtsClient.RemoveAccountsFromList(c.listID, []string(c.accountIDs)); err != nil {
|
||||
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 {
|
||||
return fmt.Errorf("unable to remove the accounts from the list; %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -15,8 +15,7 @@ type showCommand struct {
|
|||
showAccountRelationship bool
|
||||
showUserPreferences bool
|
||||
resourceType string
|
||||
account string
|
||||
accountID string
|
||||
accountName string
|
||||
statusID string
|
||||
timelineCategory string
|
||||
listID string
|
||||
|
@ -33,8 +32,7 @@ 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.account, accountFlag, "", "specify the account URI to lookup")
|
||||
command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID")
|
||||
command.StringVar(&command.accountName, accountNameFlag, "", "specify the account name in full (username@domain)")
|
||||
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")
|
||||
|
@ -95,18 +93,16 @@ 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 account details; %w", err)
|
||||
return fmt.Errorf("received an error while getting the account details; %w", err)
|
||||
}
|
||||
} else {
|
||||
if c.account == "" {
|
||||
return flagNotSetError{flagText: accountFlag}
|
||||
if c.accountName == "" {
|
||||
return flagNotSetError{flagText: accountNameFlag}
|
||||
}
|
||||
|
||||
accountURI := c.account
|
||||
|
||||
account, err = gts.GetAccount(accountURI)
|
||||
account, err = getAccount(gts, c.accountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to retrieve the account details; %w", err)
|
||||
return fmt.Errorf("received an error while getting the account details; %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -238,21 +234,9 @@ func (c *showCommand) showLists(gts *client.Client) error {
|
|||
}
|
||||
|
||||
func (c *showCommand) showFollowers(gts *client.Client) error {
|
||||
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
|
||||
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
||||
followers, err := gts.GetFollowers(accountID, c.limit)
|
||||
|
@ -270,21 +254,9 @@ func (c *showCommand) showFollowers(gts *client.Client) error {
|
|||
}
|
||||
|
||||
func (c *showCommand) showFollowing(gts *client.Client) error {
|
||||
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
|
||||
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
||||
following, err := gts.GetFollowing(accountID, c.limit)
|
||||
|
|
|
@ -113,6 +113,69 @@ 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 (
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
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
|
||||
}
|
Loading…
Reference in a new issue