feat: add ability to show a status
This commit is contained in:
parent
2c5123253a
commit
4f561f9305
2 changed files with 74 additions and 5 deletions
|
@ -50,23 +50,51 @@ ACCOUNT URL:
|
||||||
%s
|
%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 {
|
type showCommand struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
|
myAccount bool
|
||||||
targetType string
|
targetType string
|
||||||
account string
|
account string
|
||||||
myAccount bool
|
statusID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newShowCommand(name, summary string) *showCommand {
|
func newShowCommand(name, summary string) *showCommand {
|
||||||
command := showCommand{
|
command := showCommand{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
targetType: "",
|
|
||||||
myAccount: false,
|
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.targetType, "type", "", "specify the type of resource to display")
|
||||||
command.StringVar(&command.account, "account", "", "specify the account URI to lookup")
|
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)
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||||
|
|
||||||
return &command
|
return &command
|
||||||
|
@ -81,6 +109,7 @@ func (c *showCommand) Execute() error {
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
"instance": c.showInstance,
|
"instance": c.showInstance,
|
||||||
"account": c.showAccount,
|
"account": c.showAccount,
|
||||||
|
"status": c.showStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[c.targetType]
|
doFunc, ok := funcMap[c.targetType]
|
||||||
|
@ -157,6 +186,33 @@ func (c *showCommand) showAccount(gts *client.Client) error {
|
||||||
return nil
|
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 {
|
func stripHTMLTags(text string) string {
|
||||||
token := html.NewTokenizer(strings.NewReader(text))
|
token := html.NewTokenizer(strings.NewReader(text))
|
||||||
|
|
||||||
|
|
|
@ -71,8 +71,8 @@ func (g *Client) GetInstance() (model.InstanceV2, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Client) GetAccount(accountURI string) (model.Account, error) {
|
func (g *Client) GetAccount(accountURI string) (model.Account, error) {
|
||||||
path := "/api/v1/accounts/lookup"
|
path := "/api/v1/accounts/lookup" + "?acct=" + accountURI
|
||||||
url := g.Authentication.Instance + path + "?acct=" + accountURI
|
url := g.Authentication.Instance + path
|
||||||
|
|
||||||
var account model.Account
|
var account model.Account
|
||||||
|
|
||||||
|
@ -83,6 +83,19 @@ func (g *Client) GetAccount(accountURI string) (model.Account, error) {
|
||||||
return account, nil
|
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 {
|
func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
Loading…
Reference in a new issue