summaryrefslogtreecommitdiff
path: root/internal/processing/polls
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processing/polls')
-rw-r--r--internal/processing/polls/expiry.go126
-rw-r--r--internal/processing/polls/get.go37
-rw-r--r--internal/processing/polls/poll.go91
-rw-r--r--internal/processing/polls/poll_test.go234
-rw-r--r--internal/processing/polls/vote.go108
5 files changed, 596 insertions, 0 deletions
diff --git a/internal/processing/polls/expiry.go b/internal/processing/polls/expiry.go
new file mode 100644
index 000000000..59d0f17fe
--- /dev/null
+++ b/internal/processing/polls/expiry.go
@@ -0,0 +1,126 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package polls
+
+import (
+ "context"
+ "time"
+
+ "github.com/superseriousbusiness/gotosocial/internal/ap"
+ "github.com/superseriousbusiness/gotosocial/internal/gtscontext"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/log"
+ "github.com/superseriousbusiness/gotosocial/internal/messages"
+)
+
+func (p *Processor) ScheduleAll(ctx context.Context) error {
+ // Fetch all open polls from the database (barebones models are enough).
+ polls, err := p.state.DB.GetOpenPolls(gtscontext.SetBarebones(ctx))
+ if err != nil {
+ return gtserror.Newf("error getting open polls from db: %w", err)
+ }
+
+ var errs gtserror.MultiError
+
+ for _, poll := range polls {
+ // Schedule each of the polls and catch any errors.
+ if err := p.ScheduleExpiry(ctx, poll); err != nil {
+ errs.Append(err)
+ }
+ }
+
+ return errs.Combine()
+}
+
+func (p *Processor) ScheduleExpiry(ctx context.Context, poll *gtsmodel.Poll) error {
+ // Ensure has a valid expiry.
+ if !poll.ClosedAt.IsZero() {
+ return gtserror.Newf("poll %s already expired", poll.ID)
+ }
+
+ // Add the given poll to the scheduler.
+ ok := p.state.Workers.Scheduler.AddOnce(
+ poll.ID,
+ poll.ExpiresAt,
+ p.onExpiry(poll.ID),
+ )
+
+ if !ok {
+ // Failed to add the poll to the scheduler, either it was
+ // starting / stopping or there already exists a task for poll.
+ return gtserror.Newf("failed adding poll %s to scheduler", poll.ID)
+ }
+
+ atStr := poll.ExpiresAt.Local().Format("Jan _2 2006 15:04:05")
+ log.Infof(ctx, "scheduled poll expiry for %s at '%s'", poll.ID, atStr)
+ return nil
+}
+
+// onExpiry returns a callback function to be used by the scheduler when the given poll expires.
+func (p *Processor) onExpiry(pollID string) func(context.Context, time.Time) {
+ return func(ctx context.Context, now time.Time) {
+ // Get the latest version of poll from database.
+ poll, err := p.state.DB.GetPollByID(ctx, pollID)
+ if err != nil {
+ log.Errorf(ctx, "error getting poll %s from db: %v", pollID, err)
+ return
+ }
+
+ if !poll.ClosedAt.IsZero() {
+ // Expiry handler has already been run for this poll.
+ log.Errorf(ctx, "poll %s already closed", pollID)
+ return
+ }
+
+ // Extract status and
+ // set its Poll field.
+ status := poll.Status
+ status.Poll = poll
+
+ // Ensure the status is fully populated (we need the account)
+ if err := p.state.DB.PopulateStatus(ctx, status); err != nil {
+ log.Errorf(ctx, "error populating poll %s status: %v", pollID, err)
+
+ if status.Account == nil {
+ // cannot continue without
+ // status account author.
+ return
+ }
+ }
+
+ // Set "closed" time.
+ poll.ClosedAt = now
+ poll.Closing = true
+
+ // Update the Poll to mark it as closed in the database.
+ if err := p.state.DB.UpdatePoll(ctx, poll, "closed_at"); err != nil {
+ log.Errorf(ctx, "error updating poll %s in db: %v", pollID, err)
+ return
+ }
+
+ // Enqueue a status update operation to the client API worker,
+ // this will asynchronously send an update with the Poll close time.
+ p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{
+ APActivityType: ap.ActivityUpdate,
+ APObjectType: ap.ObjectNote,
+ GTSModel: status,
+ OriginAccount: status.Account,
+ })
+ }
+}
diff --git a/internal/processing/polls/get.go b/internal/processing/polls/get.go
new file mode 100644
index 000000000..42fecbd43
--- /dev/null
+++ b/internal/processing/polls/get.go
@@ -0,0 +1,37 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package polls
+
+import (
+ "context"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+)
+
+func (p *Processor) PollGet(ctx context.Context, requester *gtsmodel.Account, pollID string) (*apimodel.Poll, gtserror.WithCode) {
+ // Get (+ check visibility of) requested poll with ID.
+ poll, errWithCode := p.getTargetPoll(ctx, requester, pollID)
+ if errWithCode != nil {
+ return nil, errWithCode
+ }
+
+ // Return converted API model poll.
+ return p.toAPIPoll(ctx, requester, poll)
+}
diff --git a/internal/processing/polls/poll.go b/internal/processing/polls/poll.go
new file mode 100644
index 000000000..3b258b76c
--- /dev/null
+++ b/internal/processing/polls/poll.go
@@ -0,0 +1,91 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package polls
+
+import (
+ "context"
+ "errors"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtscontext"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/processing/common"
+ "github.com/superseriousbusiness/gotosocial/internal/state"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
+)
+
+type Processor struct {
+ // common processor logic
+ c *common.Processor
+
+ state *state.State
+ converter *typeutils.Converter
+}
+
+func New(common *common.Processor, state *state.State, converter *typeutils.Converter) Processor {
+ return Processor{
+ c: common,
+ state: state,
+ converter: converter,
+ }
+}
+
+// getTargetPoll fetches a target poll ID for requesting account, taking visibility of the poll's originating status into account.
+func (p *Processor) getTargetPoll(ctx context.Context, requestingAccount *gtsmodel.Account, targetID string) (*gtsmodel.Poll, gtserror.WithCode) {
+ // Load the requested poll with ID.
+ // (barebones as we fetch status below)
+ poll, err := p.state.DB.GetPollByID(
+ gtscontext.SetBarebones(ctx),
+ targetID,
+ )
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ if poll == nil {
+ // No poll could be found for given ID.
+ const text = "target poll not found"
+ return nil, gtserror.NewErrorNotFound(
+ errors.New(text),
+ text,
+ )
+ }
+
+ // Check that we can see + fetch the originating status for requesting account.
+ status, errWithCode := p.c.GetVisibleTargetStatus(ctx, requestingAccount, poll.StatusID)
+ if errWithCode != nil {
+ return nil, errWithCode
+ }
+
+ // Update poll status.
+ poll.Status = status
+
+ return poll, nil
+}
+
+// toAPIPoll converrts a given Poll to frontend API model, returning an appropriate error with HTTP code on failure.
+func (p *Processor) toAPIPoll(ctx context.Context, requester *gtsmodel.Account, poll *gtsmodel.Poll) (*apimodel.Poll, gtserror.WithCode) {
+ apiPoll, err := p.converter.PollToAPIPoll(ctx, requester, poll)
+ if err != nil {
+ err := gtserror.Newf("error converting to api model: %w", err)
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+ return apiPoll, nil
+}
diff --git a/internal/processing/polls/poll_test.go b/internal/processing/polls/poll_test.go
new file mode 100644
index 000000000..15a1938a8
--- /dev/null
+++ b/internal/processing/polls/poll_test.go
@@ -0,0 +1,234 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package polls_test
+
+import (
+ "context"
+ "math/rand"
+ "net/http"
+ "testing"
+
+ "github.com/stretchr/testify/suite"
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/media"
+ "github.com/superseriousbusiness/gotosocial/internal/processing/common"
+ "github.com/superseriousbusiness/gotosocial/internal/processing/polls"
+ "github.com/superseriousbusiness/gotosocial/internal/state"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
+ "github.com/superseriousbusiness/gotosocial/internal/visibility"
+ "github.com/superseriousbusiness/gotosocial/testrig"
+)
+
+type PollTestSuite struct {
+ suite.Suite
+ state state.State
+ filter *visibility.Filter
+ polls polls.Processor
+
+ testAccounts map[string]*gtsmodel.Account
+ testPolls map[string]*gtsmodel.Poll
+}
+
+func (suite *PollTestSuite) SetupTest() {
+ testrig.InitTestConfig()
+ testrig.InitTestLog()
+ suite.state.Caches.Init()
+ testrig.StartWorkers(&suite.state)
+ testrig.NewTestDB(&suite.state)
+ converter := typeutils.NewConverter(&suite.state)
+ controller := testrig.NewTestTransportController(&suite.state, nil)
+ mediaMgr := media.NewManager(&suite.state)
+ federator := testrig.NewTestFederator(&suite.state, controller, mediaMgr)
+ suite.filter = visibility.NewFilter(&suite.state)
+ common := common.New(&suite.state, converter, federator, suite.filter)
+ suite.polls = polls.New(&common, &suite.state, converter)
+}
+
+func (suite *PollTestSuite) TearDownTest() {
+ testrig.StopWorkers(&suite.state)
+ testrig.StandardDBTeardown(suite.state.DB)
+}
+
+func (suite *PollTestSuite) TestPollGet() {
+ // Create a new context for this test.
+ ctx, cncl := context.WithCancel(context.Background())
+ defer cncl()
+
+ // Perform test for all requester + poll combos.
+ for _, account := range suite.testAccounts {
+ for _, poll := range suite.testPolls {
+ suite.testPollGet(ctx, account, poll)
+ }
+ }
+}
+
+func (suite *PollTestSuite) testPollGet(ctx context.Context, requester *gtsmodel.Account, poll *gtsmodel.Poll) {
+ // Ensure poll model is fully populated before anything.
+ if err := suite.state.DB.PopulatePoll(ctx, poll); err != nil {
+ suite.T().Fatalf("error populating poll: %v", err)
+ }
+
+ var check func(*apimodel.Poll, gtserror.WithCode) bool
+
+ switch {
+ case !pollIsVisible(suite.filter, ctx, requester, poll):
+ // Poll should not be visible to requester, this should
+ // return an error code 404 (to prevent info leak).
+ check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
+ return poll == nil && err.Code() == http.StatusNotFound
+ }
+
+ default:
+ // All other cases should succeed! i.e. no error and poll returned.
+ check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
+ return poll != nil && err == nil
+ }
+ }
+
+ // Perform the poll vote and check the expected response.
+ if !check(suite.polls.PollGet(ctx, requester, poll.ID)) {
+ suite.T().Errorf("unexpected response for poll get by %s", requester.DisplayName)
+ }
+
+}
+
+func (suite *PollTestSuite) TestPollVote() {
+ // Create a new context for this test.
+ ctx, cncl := context.WithCancel(context.Background())
+ defer cncl()
+
+ // randomChoices generates random vote choices in poll.
+ randomChoices := func(poll *gtsmodel.Poll) []int {
+ var max int
+ if *poll.Multiple {
+ max = len(poll.Options)
+ } else {
+ max = 1
+ }
+ count := 1 + rand.Intn(max)
+ choices := make([]int, count)
+ for i := range choices {
+ choices[i] = rand.Intn(len(poll.Options))
+ }
+ return choices
+ }
+
+ // Perform test for all requester + poll combos.
+ for _, account := range suite.testAccounts {
+ for _, poll := range suite.testPolls {
+ // Generate some valid choices and test.
+ choices := randomChoices(poll)
+ suite.testPollVote(ctx,
+ account,
+ poll,
+ choices,
+ )
+
+ // Test with empty choices.
+ suite.testPollVote(ctx,
+ account,
+ poll,
+ nil,
+ )
+
+ // Test with out of range choice.
+ suite.testPollVote(ctx,
+ account,
+ poll,
+ []int{len(poll.Options)},
+ )
+ }
+ }
+}
+
+func (suite *PollTestSuite) testPollVote(ctx context.Context, requester *gtsmodel.Account, poll *gtsmodel.Poll, choices []int) {
+ // Ensure poll model is fully populated before anything.
+ if err := suite.state.DB.PopulatePoll(ctx, poll); err != nil {
+ suite.T().Fatalf("error populating poll: %v", err)
+ }
+
+ var check func(*apimodel.Poll, gtserror.WithCode) bool
+
+ switch {
+ case !poll.ClosedAt.IsZero():
+ // Poll is already closed, i.e. no new votes allowed!
+ // This should return an error 422 (unprocessable entity).
+ check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
+ return poll == nil && err.Code() == http.StatusUnprocessableEntity
+ }
+
+ case !voteChoicesAreValid(poll, choices):
+ // These are invalid vote choices, this should return
+ // an error code 400 to indicate invalid request data.
+ check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
+ return poll == nil && err.Code() == http.StatusBadRequest
+ }
+
+ case poll.Status.AccountID == requester.ID:
+ // Immediately we know that poll owner cannot vote in
+ // their own poll. this should return an error 422.
+ check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
+ return poll == nil && err.Code() == http.StatusUnprocessableEntity
+ }
+
+ case !pollIsVisible(suite.filter, ctx, requester, poll):
+ // Poll should not be visible to requester, this should
+ // return an error code 404 (to prevent info leak).
+ check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
+ return poll == nil && err.Code() == http.StatusNotFound
+ }
+
+ default:
+ // All other cases should succeed! i.e. no error and poll returned.
+ check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
+ return poll != nil && err == nil
+ }
+ }
+
+ // Perform the poll vote and check the expected response.
+ if !check(suite.polls.PollVote(ctx, requester, poll.ID, choices)) {
+ suite.T().Errorf("unexpected response for poll vote by %s with %v", requester.DisplayName, choices)
+ }
+}
+
+// voteChoicesAreValid is a utility function to check whether choices are valid for poll.
+func voteChoicesAreValid(poll *gtsmodel.Poll, choices []int) bool {
+ if len(choices) == 0 || !*poll.Multiple && len(choices) > 1 {
+ // Invalid number of vote choices.
+ return false
+ }
+ for _, choice := range choices {
+ if choice < 0 || choice >= len(poll.Options) {
+ // Choice index out of range.
+ return false
+ }
+ }
+ return true
+}
+
+// pollIsVisible is a short-hand function to return only a single boolean value for a visibility check on poll source status to account.
+func pollIsVisible(filter *visibility.Filter, ctx context.Context, to *gtsmodel.Account, poll *gtsmodel.Poll) bool {
+ visible, _ := filter.StatusVisible(ctx, to, poll.Status)
+ return visible
+}
+
+func TestPollTestSuite(t *testing.T) {
+ suite.Run(t, new(PollTestSuite))
+}
diff --git a/internal/processing/polls/vote.go b/internal/processing/polls/vote.go
new file mode 100644
index 000000000..8c8f22225
--- /dev/null
+++ b/internal/processing/polls/vote.go
@@ -0,0 +1,108 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package polls
+
+import (
+ "context"
+ "errors"
+
+ "github.com/superseriousbusiness/gotosocial/internal/ap"
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/id"
+ "github.com/superseriousbusiness/gotosocial/internal/messages"
+)
+
+func (p *Processor) PollVote(ctx context.Context, requester *gtsmodel.Account, pollID string, choices []int) (*apimodel.Poll, gtserror.WithCode) {
+ // Get (+ check visibility of) requested poll with ID.
+ poll, errWithCode := p.getTargetPoll(ctx, requester, pollID)
+ if errWithCode != nil {
+ return nil, errWithCode
+ }
+
+ switch {
+ // Poll author isn't allowed to vote in their own poll.
+ case requester.ID == poll.Status.AccountID:
+ const text = "you can't vote in your own poll"
+ return nil, gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
+
+ // Poll has already closed, no more voting!
+ case !poll.ClosedAt.IsZero():
+ const text = "poll already closed"
+ return nil, gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
+
+ // No choices given, or multiple given for single-choice poll.
+ case len(choices) == 0 || (!*poll.Multiple && len(choices) > 1):
+ const text = "invalid number of choices for poll"
+ return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
+ }
+
+ for _, choice := range choices {
+ if choice < 0 || choice >= len(poll.Options) {
+ // This is an invalid choice (index out of range).
+ const text = "invalid option index for poll"
+ return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
+ }
+ }
+
+ // Wrap the choices in a PollVote model.
+ vote := &gtsmodel.PollVote{
+ ID: id.NewULID(),
+ Choices: choices,
+ AccountID: requester.ID,
+ Account: requester,
+ PollID: pollID,
+ Poll: poll,
+ }
+
+ // Insert the new poll votes into the database.
+ err := p.state.DB.PutPollVote(ctx, vote)
+ switch {
+
+ case err == nil:
+ // no issue.
+
+ case errors.Is(err, db.ErrAlreadyExists):
+ // Users cannot vote multiple *times* (not choices).
+ const text = "you have already voted in poll"
+ return nil, gtserror.NewErrorUnprocessableEntity(err, text)
+
+ default:
+ // Any other irrecoverable database error.
+ err := gtserror.Newf("error inserting poll vote: %w", err)
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+
+ // Enqueue worker task to handle side-effects of user poll vote(s).
+ p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{
+ APActivityType: ap.ActivityCreate,
+ APObjectType: ap.ActivityQuestion,
+ GTSModel: vote, // the vote choices
+ OriginAccount: requester,
+ })
+
+ // Before returning the converted poll model,
+ // increment the vote counts on our local copy
+ // to get latest, instead of another db query.
+ poll.IncrementVotes(choices)
+
+ // Return converted API model poll.
+ return p.toAPIPoll(ctx, requester, poll)
+}