Dan Anglin
eb016b96e9
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
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
pollPath string = "/api/v1/polls"
|
|
)
|
|
|
|
// func (g *Client) GetPoll(pollID string) (model.Poll, error) {
|
|
// url := g.Authentication.Instance + pollPath + "/" + pollID
|
|
//
|
|
// var poll model.Poll
|
|
//
|
|
// params := requestParameters{
|
|
// httpMethod: http.MethodGet,
|
|
// url: url,
|
|
// requestBody: nil,
|
|
// contentType: "",
|
|
// output: &poll,
|
|
// }
|
|
//
|
|
// if err := g.sendRequest(params); err != nil {
|
|
// return model.Poll{}, fmt.Errorf(
|
|
// "received an error after sending the request to get the poll: %w",
|
|
// err,
|
|
// )
|
|
// }
|
|
//
|
|
// return poll, nil
|
|
// }
|
|
|
|
func (g *Client) VoteInPoll(pollID string, choices []int) error {
|
|
form := struct {
|
|
Choices []int `json:"choices"`
|
|
}{
|
|
Choices: choices,
|
|
}
|
|
|
|
data, err := json.Marshal(form)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to encode the JSON form: %w", err)
|
|
}
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
|
url := g.Authentication.Instance + pollPath + "/" + pollID + "/votes"
|
|
|
|
params := requestParameters{
|
|
httpMethod: http.MethodPost,
|
|
url: url,
|
|
requestBody: requestBody,
|
|
contentType: applicationJSON,
|
|
output: nil,
|
|
}
|
|
|
|
if err := g.sendRequest(params); err != nil {
|
|
return fmt.Errorf("received an error after sending the request to vote in the poll: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|