checkpoint: can now edit media attachments

This commit is contained in:
Dan Anglin 2024-08-03 19:50:38 +01:00
parent 41770e2b59
commit 1debd1bbf7
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 81 additions and 5 deletions

View file

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

View file

@ -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)

View file

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