diff --git a/internal/executor/create.go b/internal/executor/create.go index 5e0e409..194400a 100644 --- a/internal/executor/create.go +++ b/internal/executor/create.go @@ -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 } diff --git a/internal/executor/show.go b/internal/executor/show.go index 6a3fd64..cb51157 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -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,10 +85,13 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error { } } - if s.myAccount && s.showUserPreferences { - preferences, err = gtsClient.GetUserPreferences() - if err != nil { - return fmt.Errorf("unable to retrieve the user preferences: %w", err) + 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) + } } } @@ -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") } diff --git a/internal/printer/account.go b/internal/printer/account.go index cc101ad..004c649 100644 --- a/internal/printer/account.go +++ b/internal/printer/account.go @@ -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") diff --git a/internal/printer/status.go b/internal/printer/status.go index 8095407..9b847aa 100644 --- a/internal/printer/status.go +++ b/internal/printer/status.go @@ -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,26 +196,34 @@ func (p Printer) pollDetails(poll model.Poll) string { percentage int ) - if poll.VotesCount == 0 { - percentage = 0 - } else { - votage = float64(option.VotesCount) / float64(poll.VotesCount) - percentage = int(math.Floor(100 * votage)) - } - - optionTitle := "\n\n" + "[" + strconv.Itoa(ind) + "] " + option.Title - - for _, vote := range poll.OwnVotes { - if ind == vote { - optionTitle += " " + symbolCheckMark - - break + // 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 { + votage = float64(option.VotesCount) / float64(poll.VotesCount) + percentage = int(math.Floor(100 * votage)) } - } - builder.WriteString(optionTitle) - builder.WriteString(p.pollMeter(votage)) - builder.WriteString("\n" + strconv.Itoa(option.VotesCount) + " votes " + "(" + strconv.Itoa(percentage) + "%)") + optionTitle := "\n\n" + "[" + strconv.Itoa(ind) + "] " + option.Title + + 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: "