diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index dcd811a..b797d21 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -50,23 +50,51 @@ ACCOUNT URL: %s ` +var statusDetailsFormat = ` +%s (@%s) + +CONTENT: + %s + +STATUS ID: + %s + +CREATED AT: + %s + +STATS: + Boosts: %d + Likes: %d + Replies: %d + +VISIBILITY: + %s + +URL: + %s +` + type showCommand struct { *flag.FlagSet + myAccount bool targetType string account string - myAccount bool + statusID string } func newShowCommand(name, summary string) *showCommand { command := showCommand{ FlagSet: flag.NewFlagSet(name, flag.ExitOnError), - targetType: "", myAccount: false, + targetType: "", + account: "", + statusID: "", } + command.BoolVar(&command.myAccount, "my-account", false, "set to true to lookup your account") command.StringVar(&command.targetType, "type", "", "specify the type of resource to display") command.StringVar(&command.account, "account", "", "specify the account URI to lookup") - command.BoolVar(&command.myAccount, "my-account", false, "set to true to lookup your account") + command.StringVar(&command.statusID, "status-id", "", "specify the ID of the status to display") command.Usage = commandUsageFunc(name, summary, command.FlagSet) return &command @@ -81,6 +109,7 @@ func (c *showCommand) Execute() error { funcMap := map[string]func(*client.Client) error{ "instance": c.showInstance, "account": c.showAccount, + "status": c.showStatus, } doFunc, ok := funcMap[c.targetType] @@ -157,6 +186,33 @@ func (c *showCommand) showAccount(gts *client.Client) error { return nil } +func (c *showCommand) showStatus(gts *client.Client) error { + if c.statusID == "" { + return errors.New("the status-id flag is not set") + } + + status, err := gts.GetStatus(c.statusID) + if err != nil { + return fmt.Errorf("unable to retrieve the status; %w", err) + } + + fmt.Printf( + statusDetailsFormat, + status.Account.DisplayName, + status.Account.Username, + stripHTMLTags(status.Content), + status.ID, + status.CreatedAt.Format("02 Jan 2006, 03:04"), + status.RebloggsCount, + status.FavouritesCount, + status.RepliesCount, + status.Visibility, + status.URL, + ) + + return nil +} + func stripHTMLTags(text string) string { token := html.NewTokenizer(strings.NewReader(text)) diff --git a/internal/client/client.go b/internal/client/client.go index 672141c..6113653 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -71,8 +71,8 @@ func (g *Client) GetInstance() (model.InstanceV2, error) { } func (g *Client) GetAccount(accountURI string) (model.Account, error) { - path := "/api/v1/accounts/lookup" - url := g.Authentication.Instance + path + "?acct=" + accountURI + path := "/api/v1/accounts/lookup" + "?acct=" + accountURI + url := g.Authentication.Instance + path var account model.Account @@ -83,6 +83,19 @@ func (g *Client) GetAccount(accountURI string) (model.Account, error) { return account, nil } +func (g *Client) GetStatus(statusID string) (model.Status, error) { + path := "/api/v1/statuses/" + statusID + url := g.Authentication.Instance + path + + var status model.Status + + if err := g.sendRequest(http.MethodGet, url, nil, &status); err != nil { + return model.Status{}, fmt.Errorf("received an error after sending the request to get the status information; %w", err) + } + + return status, nil +} + func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error { ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) defer cancel()