2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e *EditExecutor) Execute() error {
|
|
|
|
if e.resourceType == "" {
|
|
|
|
return FlagNotSetError{flagText: flagType}
|
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-08-14 13:24:39 +01:00
|
|
|
resourceList: e.editList,
|
|
|
|
resourceMediaAttachment: e.editMediaAttachment,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[e.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: e.resourceType}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
gtsClient, err := client.NewClientFromFile(e.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)
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:18:11 +01:00
|
|
|
func (e *EditExecutor) editList(gtsClient *client.Client) error {
|
2024-05-23 18:06:49 +01:00
|
|
|
if e.listID == "" {
|
|
|
|
return FlagNotSetError{flagText: flagListID}
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := gtsClient.GetList(e.listID)
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to get the list: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.listTitle != "" {
|
|
|
|
list.Title = e.listTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.listRepliesPolicy != "" {
|
2024-06-02 11:35:43 +01:00
|
|
|
parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(e.listRepliesPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-05-30 19:42:11 +01:00
|
|
|
list.RepliesPolicy = parsedListRepliesPolicy
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
updatedList, err := gtsClient.UpdateList(list)
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to update the list: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-08-14 13:24:39 +01:00
|
|
|
e.printer.PrintSuccess("Successfully edited the list.")
|
2024-06-17 18:59:20 +01:00
|
|
|
e.printer.PrintList(updatedList)
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-14 13:24:39 +01:00
|
|
|
|
|
|
|
func (e *EditExecutor) editMediaAttachment(gtsClient *client.Client) error {
|
|
|
|
expectedNumMediaAttachmentIDs := 1
|
|
|
|
if !e.attachmentIDs.ExpectedLength(expectedNumMediaAttachmentIDs) {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"received an unexpected number of media attachment IDs: want %d",
|
|
|
|
expectedNumMediaAttachmentIDs,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment, err := gtsClient.GetMediaAttachment(e.attachmentIDs[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the media attachment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
description := e.description
|
|
|
|
if description == "" {
|
|
|
|
description = attachment.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
focus := e.focus
|
|
|
|
if focus == "" {
|
|
|
|
focus = fmt.Sprintf("%f,%f", attachment.Meta.Focus.X, attachment.Meta.Focus.Y)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gtsClient.UpdateMediaAttachment(e.attachmentIDs[0], description, focus); err != nil {
|
|
|
|
return fmt.Errorf("unable to update the media attachment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.printer.PrintSuccess("Successfully edited the media attachment.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|