feat: add ability to show a status

This commit is contained in:
Dan Anglin 2024-02-23 13:14:50 +00:00
parent 2c5123253a
commit 4f561f9305
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 74 additions and 5 deletions

View file

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

View file

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