2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
2024-06-25 12:39:39 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
2024-06-17 18:59:20 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
|
2024-05-23 18:06:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type RemoveExecutor struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
printer *printer.Printer
|
2024-06-25 12:39:39 +01:00
|
|
|
config *config.Config
|
2024-05-23 18:06:49 +01:00
|
|
|
resourceType string
|
|
|
|
fromResourceType string
|
|
|
|
listID string
|
2024-06-03 06:53:31 +01:00
|
|
|
statusID string
|
2024-06-15 18:40:13 +01:00
|
|
|
accountNames MultiStringFlagValue
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
func NewRemoveExecutor(printer *printer.Printer, config *config.Config, name, summary string) *RemoveExecutor {
|
2024-05-23 18:06:49 +01:00
|
|
|
emptyArr := make([]string, 0, 3)
|
|
|
|
|
|
|
|
removeExe := RemoveExecutor{
|
2024-06-17 18:59:20 +01:00
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
|
|
|
|
printer: printer,
|
2024-06-25 12:39:39 +01:00
|
|
|
config: config,
|
2024-06-17 18:59:20 +01:00
|
|
|
accountNames: MultiStringFlagValue(emptyArr),
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 15:58:10 +01:00
|
|
|
removeExe.StringVar(&removeExe.resourceType, flagType, "", "Specify the resource type to remove (e.g. account, note)")
|
|
|
|
removeExe.StringVar(&removeExe.fromResourceType, flagFrom, "", "Specify the resource type to remove from (e.g. list, account, etc)")
|
|
|
|
removeExe.StringVar(&removeExe.listID, flagListID, "", "The ID of the list to remove from")
|
|
|
|
removeExe.StringVar(&removeExe.statusID, flagStatusID, "", "The ID of the status")
|
|
|
|
removeExe.Var(&removeExe.accountNames, flagAccountName, "The name of the account to remove from the resource")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
removeExe.Usage = commandUsageFunc(name, summary, removeExe.FlagSet)
|
|
|
|
|
|
|
|
return &removeExe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoveExecutor) Execute() error {
|
|
|
|
if r.fromResourceType == "" {
|
|
|
|
return FlagNotSetError{flagText: flagFrom}
|
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-06-03 06:53:31 +01:00
|
|
|
resourceList: r.removeFromList,
|
|
|
|
resourceAccount: r.removeFromAccount,
|
|
|
|
resourceBookmarks: r.removeFromBookmarks,
|
2024-06-04 17:01:33 +01:00
|
|
|
resourceStatus: r.removeFromStatus,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[r.fromResourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: r.fromResourceType}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
gtsClient, err := client.NewClientFromFile(r.config.CredentialsFile)
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeFromList(gtsClient *client.Client) error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceAccount: r.removeAccountsFromList,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[r.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedRemoveOperationError{
|
|
|
|
ResourceType: r.resourceType,
|
|
|
|
RemoveFromResourceType: r.fromResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeAccountsFromList(gtsClient *client.Client) error {
|
|
|
|
if r.listID == "" {
|
|
|
|
return FlagNotSetError{flagText: flagListID}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.accountNames) == 0 {
|
|
|
|
return NoAccountSpecifiedError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
accountIDs := make([]string, len(r.accountNames))
|
|
|
|
|
2024-06-03 06:53:31 +01:00
|
|
|
for ind := range r.accountNames {
|
|
|
|
accountID, err := getTheirAccountID(gtsClient, r.accountNames[ind])
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-03 07:00:39 +01:00
|
|
|
return fmt.Errorf("unable to get the account ID for %s: %w", r.accountNames[ind], err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-03 06:53:31 +01:00
|
|
|
accountIDs[ind] = accountID
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.RemoveAccountsFromList(r.listID, accountIDs); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to remove the accounts from the list: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
r.printer.PrintSuccess("Successfully removed the account(s) from the list.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeFromAccount(gtsClient *client.Client) error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceNote: r.removeNoteFromAccount,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[r.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedRemoveOperationError{
|
|
|
|
ResourceType: r.resourceType,
|
|
|
|
RemoveFromResourceType: r.fromResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeNoteFromAccount(gtsClient *client.Client) error {
|
|
|
|
if len(r.accountNames) != 1 {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unexpected number of accounts specified: want 1, got %d", len(r.accountNames))
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, r.accountNames[0], r.config.CredentialsFile)
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.SetPrivateNote(accountID, ""); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to remove the private note from the account: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
r.printer.PrintSuccess("Successfully removed the private note from the account.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-03 06:53:31 +01:00
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeFromBookmarks(gtsClient *client.Client) error {
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
|
|
resourceStatus: r.removeStatusFromBookmarks,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[r.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedRemoveOperationError{
|
|
|
|
ResourceType: r.resourceType,
|
|
|
|
RemoveFromResourceType: r.fromResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) error {
|
|
|
|
if r.statusID == "" {
|
|
|
|
return FlagNotSetError{flagText: flagStatusID}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.RemoveStatusFromBookmarks(r.statusID); err != nil {
|
|
|
|
return fmt.Errorf("unable to remove the status from your bookmarks: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
r.printer.PrintSuccess("Successfully removed the status from your bookmarks.")
|
2024-06-03 06:53:31 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-04 17:01:33 +01:00
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error {
|
2024-06-04 18:38:47 +01:00
|
|
|
if r.statusID == "" {
|
|
|
|
return FlagNotSetError{flagText: flagStatusID}
|
|
|
|
}
|
|
|
|
|
2024-06-04 17:01:33 +01:00
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-06-04 18:38:47 +01:00
|
|
|
resourceStar: r.removeStarFromStatus,
|
|
|
|
resourceLike: r.removeStarFromStatus,
|
|
|
|
resourceBoost: r.removeBoostFromStatus,
|
2024-06-04 17:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[r.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedRemoveOperationError{
|
|
|
|
ResourceType: r.resourceType,
|
|
|
|
RemoveFromResourceType: r.fromResourceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error {
|
|
|
|
if err := gtsClient.UnlikeStatus(r.statusID); err != nil {
|
|
|
|
return fmt.Errorf("unable to remove the %s from the status: %w", r.resourceType, err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
r.printer.PrintSuccess("Successfully removed the " + r.resourceType + " from the status.")
|
2024-06-04 17:01:33 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-04 18:38:47 +01:00
|
|
|
|
|
|
|
func (r *RemoveExecutor) removeBoostFromStatus(gtsClient *client.Client) error {
|
|
|
|
if err := gtsClient.UnreblogStatus(r.statusID); err != nil {
|
|
|
|
return fmt.Errorf("unable to remove the boost from the status: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
r.printer.PrintSuccess("Successfully removed the boost from the status.")
|
2024-06-04 18:38:47 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|