checkpoint:

- BREAKING: when creating the status, only print the ID on success.
- fix: Only display the poll results under certain conditions.
This commit is contained in:
Dan Anglin 2024-08-13 19:27:27 +01:00
parent ef2b02b799
commit 64e8d6f0ba
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 81 additions and 37 deletions

View file

@ -149,8 +149,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
return fmt.Errorf("unable to create the status: %w", err)
}
c.printer.PrintSuccess("Successfully created the following status:")
c.printer.PrintStatus(status)
c.printer.PrintSuccess("Successfully created the status with ID: " + status.ID)
return nil
}

View file

@ -75,6 +75,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
relationship *model.AccountRelationship
preferences *model.Preferences
statuses *model.StatusList
myAccountID string
)
if !s.myAccount && !s.skipAccountRelationship {
@ -84,12 +85,15 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
}
}
if s.myAccount && s.showUserPreferences {
if s.myAccount {
myAccountID = account.ID
if s.showUserPreferences {
preferences, err = gtsClient.GetUserPreferences()
if err != nil {
return fmt.Errorf("unable to retrieve the user preferences: %w", err)
}
}
}
if s.showStatuses {
form := client.GetAccountStatusesForm{
@ -108,7 +112,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
}
}
s.printer.PrintAccount(account, relationship, preferences, statuses)
s.printer.PrintAccount(account, relationship, preferences, statuses, myAccountID)
return nil
}
@ -131,7 +135,12 @@ func (s *ShowExecutor) showStatus(gtsClient *client.Client) error {
return nil
}
s.printer.PrintStatus(status)
myAccountID, err := getAccountID(gtsClient, true, nil, s.config.CredentialsFile)
if err != nil {
return fmt.Errorf("unable to get your account ID: %w", err)
}
s.printer.PrintStatus(status, myAccountID)
return nil
}
@ -180,7 +189,12 @@ func (s *ShowExecutor) showTimeline(gtsClient *client.Client) error {
return nil
}
s.printer.PrintStatusList(timeline)
myAccountID, err := getAccountID(gtsClient, true, nil, s.config.CredentialsFile)
if err != nil {
return fmt.Errorf("unable to get your account ID: %w", err)
}
s.printer.PrintStatusList(timeline, myAccountID)
return nil
}
@ -333,7 +347,12 @@ func (s *ShowExecutor) showBookmarks(gtsClient *client.Client) error {
}
if len(bookmarks.Statuses) > 0 {
s.printer.PrintStatusList(bookmarks)
myAccountID, err := getAccountID(gtsClient, true, nil, s.config.CredentialsFile)
if err != nil {
return fmt.Errorf("unable to get your account ID: %w", err)
}
s.printer.PrintStatusList(bookmarks, myAccountID)
} else {
s.printer.PrintInfo("You have no bookmarks.\n")
}
@ -348,7 +367,12 @@ func (s *ShowExecutor) showLiked(gtsClient *client.Client) error {
}
if len(liked.Statuses) > 0 {
s.printer.PrintStatusList(liked)
myAccountID, err := getAccountID(gtsClient, true, nil, s.config.CredentialsFile)
if err != nil {
return fmt.Errorf("unable to get your account ID: %w", err)
}
s.printer.PrintStatusList(liked, myAccountID)
} else {
s.printer.PrintInfo("You have no " + s.resourceType + " statuses.\n")
}

View file

@ -12,6 +12,7 @@ func (p Printer) PrintAccount(
relationship *model.AccountRelationship,
preferences *model.Preferences,
statuses *model.StatusList,
userAccountID string,
) {
var builder strings.Builder
@ -47,7 +48,7 @@ func (p Printer) PrintAccount(
}
if statuses != nil {
builder.WriteString("\n\n" + p.statusList(*statuses))
builder.WriteString("\n\n" + p.statusList(*statuses, userAccountID))
}
builder.WriteString("\n\n")

View file

@ -8,7 +8,7 @@ import (
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
)
func (p Printer) PrintStatus(status model.Status) {
func (p Printer) PrintStatus(status model.Status, userAccountID string) {
var builder strings.Builder
// The account information
@ -42,8 +42,13 @@ func (p Printer) PrintStatus(status model.Status) {
// If a poll exists in a status, write the contents to the builder.
if status.Poll != nil {
pollOwner := false
if status.Account.ID == userAccountID {
pollOwner = true
}
builder.WriteString("\n\n" + p.headerFormat("POLL DETAILS:"))
builder.WriteString(p.pollDetails(*status.Poll))
builder.WriteString(p.pollDetails(*status.Poll, pollOwner))
}
// Status creation time
@ -74,17 +79,18 @@ func (p Printer) PrintStatus(status model.Status) {
p.print(builder.String())
}
func (p Printer) PrintStatusList(list model.StatusList) {
p.print(p.statusList(list))
func (p Printer) PrintStatusList(list model.StatusList, userAccountID string) {
p.print(p.statusList(list, userAccountID))
}
func (p Printer) statusList(list model.StatusList) string {
func (p Printer) statusList(list model.StatusList, userAccountID string) string {
var builder strings.Builder
builder.WriteString(p.headerFormat(list.Name) + "\n")
for _, status := range list.Statuses {
statusID := status.ID
statusOwnerID := status.Account.ID
createdAt := p.formatDateTime(status.CreatedAt)
boostedAt := ""
content := status.Content
@ -102,6 +108,7 @@ func (p Printer) statusList(list model.StatusList) string {
))
statusID = status.Reblog.ID
statusOwnerID = status.Reblog.Account.ID
createdAt = p.formatDateTime(status.Reblog.CreatedAt)
boostedAt = p.formatDateTime(status.CreatedAt)
content = status.Reblog.Content
@ -122,7 +129,12 @@ func (p Printer) statusList(list model.StatusList) string {
builder.WriteString("\n" + p.convertHTMLToText(content, true))
if poll != nil {
builder.WriteString(p.pollDetails(*poll))
pollOwner := false
if statusOwnerID == userAccountID {
pollOwner = true
}
builder.WriteString(p.pollDetails(*poll, pollOwner))
}
for _, media := range mediaAttachments {
@ -175,7 +187,7 @@ func (p Printer) statusList(list model.StatusList) string {
return builder.String()
}
func (p Printer) pollDetails(poll model.Poll) string {
func (p Printer) pollDetails(poll model.Poll, owner bool) string {
var builder strings.Builder
for ind, option := range poll.Options {
@ -184,6 +196,11 @@ func (p Printer) pollDetails(poll model.Poll) string {
percentage int
)
// Show the poll results under any of the following conditions:
// - the user is the owner of the poll
// - the poll has expired
// - the user has voted in the poll
if owner || poll.Expired || poll.Voted {
if poll.VotesCount == 0 {
percentage = 0
} else {
@ -204,6 +221,9 @@ func (p Printer) pollDetails(poll model.Poll) string {
builder.WriteString(optionTitle)
builder.WriteString(p.pollMeter(votage))
builder.WriteString("\n" + strconv.Itoa(option.VotesCount) + " votes " + "(" + strconv.Itoa(percentage) + "%)")
} else {
builder.WriteString("\n" + "[" + strconv.Itoa(ind) + "] " + option.Title)
}
}
pollStatusField := "Poll is open until: "