enbas/internal/executor/create.go
Dan Anglin eb016b96e9
fix(BREAKING): update poll interaction
Summary:

This commit updates and enhances poll interaction. From now on users
will interact with a poll via the status that contains it. Direct
interaction with the poll (via the poll's ID) is no longer supported.
This helps resolve an issue where it wasn't possible to find the owner
of the poll when interacting with it directly.

Changes:

- Users can no longer view a poll directly using the Poll ID.
  Instead polls can be viewed when viewing statuses or timelines.
- More details about a poll is shown in statuses and timelines.
- Votes are now added to polls via statuses.
- Poll results are hidden unless the following conditions are met.
    - The user is the owner of the poll.
    - The poll has expired.
    - The user has already voted in the poll.
- Enbas can now detect and stop a poll owner from voting in their own
  poll.
- When a status is created Enbas will now only print the ID of the
  created status instead of the whole thing.

PR: apollo/enbas#43

Resolves apollo/enbas#39
2024-08-14 11:29:30 +01:00

155 lines
3.5 KiB
Go

package executor
import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
func (c *CreateExecutor) Execute() error {
if c.resourceType == "" {
return FlagNotSetError{flagText: flagType}
}
gtsClient, err := client.NewClientFromFile(c.config.CredentialsFile)
if err != nil {
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
}
funcMap := map[string]func(*client.Client) error{
resourceList: c.createList,
resourceStatus: c.createStatus,
}
doFunc, ok := funcMap[c.resourceType]
if !ok {
return UnsupportedTypeError{resourceType: c.resourceType}
}
return doFunc(gtsClient)
}
func (c *CreateExecutor) createList(gtsClient *client.Client) error {
if c.listTitle == "" {
return FlagNotSetError{flagText: flagListTitle}
}
parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(c.listRepliesPolicy)
if err != nil {
return err
}
form := client.CreateListForm{
Title: c.listTitle,
RepliesPolicy: parsedListRepliesPolicy,
}
list, err := gtsClient.CreateList(form)
if err != nil {
return fmt.Errorf("unable to create the list: %w", err)
}
c.printer.PrintSuccess("Successfully created the following list:")
c.printer.PrintList(list)
return nil
}
func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
var (
err error
content string
language string
visibility string
sensitive bool
)
switch {
case c.content != "":
content = c.content
case c.fromFile != "":
content, err = utilities.ReadFile(c.fromFile)
if err != nil {
return fmt.Errorf("unable to get the status contents from %q: %w", c.fromFile, err)
}
default:
return EmptyContentError{
ResourceType: resourceStatus,
Hint: "please use --" + flagContent + " or --" + flagFromFile,
}
}
preferences, err := gtsClient.GetUserPreferences()
if err != nil {
fmt.Println("WARNING: Unable to get your posting preferences: %w", err)
}
if c.language != "" {
language = c.language
} else {
language = preferences.PostingDefaultLanguage
}
if c.visibility != "" {
visibility = c.visibility
} else {
visibility = preferences.PostingDefaultVisibility
}
if c.sensitive.Value != nil {
sensitive = *c.sensitive.Value
} else {
sensitive = preferences.PostingDefaultSensitive
}
parsedVisibility, err := model.ParseStatusVisibility(visibility)
if err != nil {
return err
}
parsedContentType, err := model.ParseStatusContentType(c.contentType)
if err != nil {
return err
}
form := client.CreateStatusForm{
Content: content,
ContentType: parsedContentType,
Language: language,
SpoilerText: c.spoilerText,
Boostable: c.boostable,
Federated: c.federated,
InReplyTo: c.inReplyTo,
Likeable: c.likeable,
Replyable: c.replyable,
Sensitive: sensitive,
Visibility: parsedVisibility,
Poll: nil,
}
if c.addPoll {
if len(c.pollOptions) == 0 {
return NoPollOptionError{}
}
poll := client.CreateStatusPollForm{
Options: c.pollOptions,
Multiple: c.pollAllowsMultipleChoices,
HideTotals: c.pollHidesVoteCounts,
ExpiresIn: int(c.pollExpiresIn.Duration.Seconds()),
}
form.Poll = &poll
}
status, err := gtsClient.CreateStatus(form)
if err != nil {
return fmt.Errorf("unable to create the status: %w", err)
}
c.printer.PrintSuccess("Successfully created the status with ID: " + status.ID)
return nil
}