From 1debd1bbf7514351eae56f37ecad287812cb12ae Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 3 Aug 2024 19:50:38 +0100 Subject: [PATCH] checkpoint: can now edit media attachments --- internal/client/media.go | 38 ++++++++++++++++++++++++++++++ internal/executor/create.go | 2 +- internal/executor/edit.go | 46 +++++++++++++++++++++++++++++++++---- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/internal/client/media.go b/internal/client/media.go index f055611..182c7d8 100644 --- a/internal/client/media.go +++ b/internal/client/media.go @@ -2,6 +2,7 @@ package client import ( "bytes" + "encoding/json" "fmt" "io" "mime/multipart" @@ -119,3 +120,40 @@ func (g *Client) CreateMediaAttachment(path, description, focus string) (model.A return attachment, nil } + +func (g *Client) UpdateMediaAttachment(mediaAttachmentID, description, focus string) (model.Attachment, error) { + form := struct { + Description string `json:"description"` + Focus string `json:"focus"` + }{ + Description: description, + Focus: focus, + } + + data, err := json.Marshal(form) + if err != nil { + return model.Attachment{}, fmt.Errorf("unable to marshal the form: %w", err) + } + + requestBody := bytes.NewBuffer(data) + url := g.Authentication.Instance + baseMediaPath + "/" + mediaAttachmentID + + var updatedMediaAttachment model.Attachment + + params := requestParameters{ + httpMethod: http.MethodPut, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: &updatedMediaAttachment, + } + + if err := g.sendRequest(params); err != nil { + return model.Attachment{}, fmt.Errorf( + "received an error after sending the request to update the media attachment: %w", + err, + ) + } + + return updatedMediaAttachment, nil +} diff --git a/internal/executor/create.go b/internal/executor/create.go index 45a0a1a..08acb81 100644 --- a/internal/executor/create.go +++ b/internal/executor/create.go @@ -86,7 +86,7 @@ func NewCreateExecutor(printer *printer.Printer, config *config.Config, name, su createExe.StringVar(&createExe.listTitle, flagListTitle, "", "Specify the title of the list") createExe.StringVar(&createExe.listRepliesPolicy, flagListRepliesPolicy, "list", "Specify the policy of the replies for this list (valid values are followed, list and none)") createExe.StringVar(&createExe.description, flagDescription, "", "The description of the media attachment that will be used as the alt-text") - createExe.StringVar(&createExe.focus, flagFocus, "", "The focus of the media file.") + createExe.StringVar(&createExe.focus, flagFocus, "", "The focus of the media file") createExe.Usage = commandUsageFunc(name, summary, createExe.FlagSet) diff --git a/internal/executor/edit.go b/internal/executor/edit.go index a5f271b..d9666d4 100644 --- a/internal/executor/edit.go +++ b/internal/executor/edit.go @@ -15,6 +15,9 @@ type EditExecutor struct { printer *printer.Printer config *config.Config + attachmentID string + description string + focus string resourceType string listID string listTitle string @@ -29,10 +32,13 @@ func NewEditExecutor(printer *printer.Printer, config *config.Config, name, summ config: config, } - editExe.StringVar(&editExe.resourceType, flagType, "", "Specify the type of resource to update") - editExe.StringVar(&editExe.listID, flagListID, "", "Specify the ID of the list to update") + editExe.StringVar(&editExe.resourceType, flagType, "", "Specify the type of resource to edit") + editExe.StringVar(&editExe.attachmentID, flagAttachmentID, "", "Specify the ID of the media attachment to edit") + editExe.StringVar(&editExe.listID, flagListID, "", "Specify the ID of the list to edit") editExe.StringVar(&editExe.listTitle, flagListTitle, "", "Specify the title of the list") editExe.StringVar(&editExe.listRepliesPolicy, flagListRepliesPolicy, "", "Specify the policy of the replies for this list (valid values are followed, list and none)") + editExe.StringVar(&editExe.description, flagDescription, "", "The description of the media attachment that will be used as the alt-text") + editExe.StringVar(&editExe.focus, flagFocus, "", "The focus of the media file") editExe.Usage = commandUsageFunc(name, summary, editExe.FlagSet) @@ -45,7 +51,8 @@ func (e *EditExecutor) Execute() error { } funcMap := map[string]func(*client.Client) error{ - resourceList: e.editList, + resourceList: e.editList, + resourceMediaAttachment: e.editMediaAttachment, } doFunc, ok := funcMap[e.resourceType] @@ -89,8 +96,39 @@ func (e *EditExecutor) editList(gtsClient *client.Client) error { return fmt.Errorf("unable to update the list: %w", err) } - e.printer.PrintSuccess("Successfully updated the list.") + e.printer.PrintSuccess("Successfully edited the list.") e.printer.PrintList(updatedList) return nil } + +func (e *EditExecutor) editMediaAttachment(gtsClient *client.Client) error { + if e.attachmentID == "" { + return FlagNotSetError{flagText: flagAttachmentID} + } + + attachment, err := gtsClient.GetMediaAttachment(e.attachmentID) + 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) + } + + updatedAttachment, err := gtsClient.UpdateMediaAttachment(e.attachmentID, description, focus) + if err != nil { + return fmt.Errorf("unable to update the media attachment: %w", err) + } + + e.printer.PrintSuccess("Successfully edited the media attachment.") + e.printer.PrintMediaAttachment(updatedAttachment) + + return nil +}