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) return fmt.Errorf("unable to create the status: %w", err)
} }
c.printer.PrintSuccess("Successfully created the following status:") c.printer.PrintSuccess("Successfully created the status with ID: " + status.ID)
c.printer.PrintStatus(status)
return nil return nil
} }

View file

@ -75,6 +75,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
relationship *model.AccountRelationship relationship *model.AccountRelationship
preferences *model.Preferences preferences *model.Preferences
statuses *model.StatusList statuses *model.StatusList
myAccountID string
) )
if !s.myAccount && !s.skipAccountRelationship { if !s.myAccount && !s.skipAccountRelationship {
@ -84,10 +85,13 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
} }
} }
if s.myAccount && s.showUserPreferences { if s.myAccount {
preferences, err = gtsClient.GetUserPreferences() myAccountID = account.ID
if err != nil { if s.showUserPreferences {
return fmt.Errorf("unable to retrieve the user preferences: %w", err) preferences, err = gtsClient.GetUserPreferences()
if err != nil {
return fmt.Errorf("unable to retrieve the user preferences: %w", err)
}
} }
} }
@ -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 return nil
} }
@ -131,7 +135,12 @@ func (s *ShowExecutor) showStatus(gtsClient *client.Client) error {
return nil 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 return nil
} }
@ -180,7 +189,12 @@ func (s *ShowExecutor) showTimeline(gtsClient *client.Client) error {
return nil 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 return nil
} }
@ -333,7 +347,12 @@ func (s *ShowExecutor) showBookmarks(gtsClient *client.Client) error {
} }
if len(bookmarks.Statuses) > 0 { 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 { } else {
s.printer.PrintInfo("You have no bookmarks.\n") 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 { 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 { } else {
s.printer.PrintInfo("You have no " + s.resourceType + " statuses.\n") s.printer.PrintInfo("You have no " + s.resourceType + " statuses.\n")
} }

View file

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

View file

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