diff options
Diffstat (limited to 'internal/api')
19 files changed, 392 insertions, 94 deletions
diff --git a/internal/api/activitypub/users/outboxget_test.go b/internal/api/activitypub/users/outboxget_test.go index 1abe31ef6..4829a8946 100644 --- a/internal/api/activitypub/users/outboxget_test.go +++ b/internal/api/activitypub/users/outboxget_test.go @@ -25,6 +25,7 @@ import (  	"net/http"  	"net/http/httptest"  	"testing" +	"time"  	"github.com/gin-gonic/gin"  	"github.com/stretchr/testify/suite" @@ -101,12 +102,6 @@ func (suite *OutboxGetTestSuite) TestGetOutboxFirstPage() {  	signedRequest := derefRequests["foss_satan_dereference_zork_outbox_first"]  	targetAccount := suite.testAccounts["local_account_1"] -	tc := testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")) -	federator := testrig.NewTestFederator(&suite.state, tc, suite.mediaManager) -	emailSender := testrig.NewEmailSender("../../../../web/template/", nil) -	processor := testrig.NewTestProcessor(&suite.state, federator, emailSender, suite.mediaManager) -	userModule := users.New(processor) -  	// setup request  	recorder := httptest.NewRecorder()  	ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -128,7 +123,7 @@ func (suite *OutboxGetTestSuite) TestGetOutboxFirstPage() {  	}  	// trigger the function being tested -	userModule.OutboxGETHandler(ctx) +	suite.userModule.OutboxGETHandler(ctx)  	// check response  	suite.EqualValues(http.StatusOK, recorder.Code) @@ -137,6 +132,7 @@ func (suite *OutboxGetTestSuite) TestGetOutboxFirstPage() {  	defer result.Body.Close()  	b, err := ioutil.ReadAll(result.Body)  	suite.NoError(err) +	b = checkDropPublished(suite.T(), b, "orderedItems")  	dst := new(bytes.Buffer)  	err = json.Indent(dst, b, "", "  ")  	suite.NoError(err) @@ -147,9 +143,8 @@ func (suite *OutboxGetTestSuite) TestGetOutboxFirstPage() {    "orderedItems": {      "actor": "http://localhost:8080/users/the_mighty_zork",      "cc": "http://localhost:8080/users/the_mighty_zork/followers", -    "id": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/activity", +    "id": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/activity#Create",      "object": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY", -    "published": "2021-10-20T10:40:37Z",      "to": "https://www.w3.org/ns/activitystreams#Public",      "type": "Create"    }, @@ -175,12 +170,6 @@ func (suite *OutboxGetTestSuite) TestGetOutboxNextPage() {  	signedRequest := derefRequests["foss_satan_dereference_zork_outbox_next"]  	targetAccount := suite.testAccounts["local_account_1"] -	tc := testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")) -	federator := testrig.NewTestFederator(&suite.state, tc, suite.mediaManager) -	emailSender := testrig.NewEmailSender("../../../../web/template/", nil) -	processor := testrig.NewTestProcessor(&suite.state, federator, emailSender, suite.mediaManager) -	userModule := users.New(processor) -  	// setup request  	recorder := httptest.NewRecorder()  	ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -206,7 +195,7 @@ func (suite *OutboxGetTestSuite) TestGetOutboxNextPage() {  	}  	// trigger the function being tested -	userModule.OutboxGETHandler(ctx) +	suite.userModule.OutboxGETHandler(ctx)  	// check response  	suite.EqualValues(http.StatusOK, recorder.Code) @@ -240,3 +229,30 @@ func (suite *OutboxGetTestSuite) TestGetOutboxNextPage() {  func TestOutboxGetTestSuite(t *testing.T) {  	suite.Run(t, new(OutboxGetTestSuite))  } + +// checkDropPublished checks the published field at given key position for formatting, and drops from the JSON. +// This is useful because the published property is usually set to the current time string (which is difficult to test). +func checkDropPublished(t *testing.T, b []byte, at ...string) []byte { +	m := make(map[string]any) +	if err := json.Unmarshal(b, &m); err != nil { +		t.Fatalf("error unmarshaling json into map: %v", err) +	} +	mm := m +	for _, key := range at { +		switch vt := mm[key].(type) { +		case map[string]any: +			mm = vt +		} +	} +	if s, ok := mm["published"].(string); !ok { +		t.Fatal("missing published data on json") +	} else if _, err := time.Parse(time.RFC3339, s); err != nil { +		t.Fatalf("error parsing published time: %v", err) +	} +	delete(mm, "published") +	b, err := json.Marshal(m) +	if err != nil { +		t.Fatalf("error remarshaling json: %v", err) +	} +	return b +} diff --git a/internal/api/activitypub/users/repliesget_test.go b/internal/api/activitypub/users/repliesget_test.go index f81dddadd..26492d8ce 100644 --- a/internal/api/activitypub/users/repliesget_test.go +++ b/internal/api/activitypub/users/repliesget_test.go @@ -101,12 +101,6 @@ func (suite *RepliesGetTestSuite) TestGetRepliesNext() {  	targetAccount := suite.testAccounts["local_account_1"]  	targetStatus := suite.testStatuses["local_account_1_status_1"] -	tc := testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")) -	federator := testrig.NewTestFederator(&suite.state, tc, suite.mediaManager) -	emailSender := testrig.NewEmailSender("../../../../web/template/", nil) -	processor := testrig.NewTestProcessor(&suite.state, federator, emailSender, suite.mediaManager) -	userModule := users.New(processor) -  	// setup request  	recorder := httptest.NewRecorder()  	ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -132,7 +126,7 @@ func (suite *RepliesGetTestSuite) TestGetRepliesNext() {  	}  	// trigger the function being tested -	userModule.StatusRepliesGETHandler(ctx) +	suite.userModule.StatusRepliesGETHandler(ctx)  	// check response  	suite.EqualValues(http.StatusOK, recorder.Code) @@ -165,12 +159,6 @@ func (suite *RepliesGetTestSuite) TestGetRepliesLast() {  	targetAccount := suite.testAccounts["local_account_1"]  	targetStatus := suite.testStatuses["local_account_1_status_1"] -	tc := testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")) -	federator := testrig.NewTestFederator(&suite.state, tc, suite.mediaManager) -	emailSender := testrig.NewEmailSender("../../../../web/template/", nil) -	processor := testrig.NewTestProcessor(&suite.state, federator, emailSender, suite.mediaManager) -	userModule := users.New(processor) -  	// setup request  	recorder := httptest.NewRecorder()  	ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -196,7 +184,7 @@ func (suite *RepliesGetTestSuite) TestGetRepliesLast() {  	}  	// trigger the function being tested -	userModule.StatusRepliesGETHandler(ctx) +	suite.userModule.StatusRepliesGETHandler(ctx)  	// check response  	suite.EqualValues(http.StatusOK, recorder.Code) diff --git a/internal/api/auth/auth_test.go b/internal/api/auth/auth_test.go index a8d029795..ddf75970e 100644 --- a/internal/api/auth/auth_test.go +++ b/internal/api/auth/auth_test.go @@ -91,11 +91,14 @@ func (suite *AuthStandardTestSuite) SetupTest() {  	suite.emailSender = testrig.NewEmailSender("../../../web/template/", nil)  	suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager)  	suite.authModule = auth.New(suite.db, suite.processor, suite.idp) +  	testrig.StandardDBSetup(suite.db, suite.testAccounts) +	testrig.StartWorkers(&suite.state)  }  func (suite *AuthStandardTestSuite) TearDownTest() {  	testrig.StandardDBTeardown(suite.db) +	testrig.StopWorkers(&suite.state)  }  func (suite *AuthStandardTestSuite) newContext(requestMethod string, requestPath string, requestBody []byte, bodyContentType string) (*gin.Context, *httptest.ResponseRecorder) { diff --git a/internal/api/client.go b/internal/api/client.go index e91ee2d2c..ec8fa6034 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -36,6 +36,7 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/api/client/markers"  	"github.com/superseriousbusiness/gotosocial/internal/api/client/media"  	"github.com/superseriousbusiness/gotosocial/internal/api/client/notifications" +	"github.com/superseriousbusiness/gotosocial/internal/api/client/polls"  	"github.com/superseriousbusiness/gotosocial/internal/api/client/preferences"  	"github.com/superseriousbusiness/gotosocial/internal/api/client/reports"  	"github.com/superseriousbusiness/gotosocial/internal/api/client/search" @@ -68,6 +69,7 @@ type Client struct {  	markers        *markers.Module        // api/v1/markers  	media          *media.Module          // api/v1/media, api/v2/media  	notifications  *notifications.Module  // api/v1/notifications +	polls          *polls.Module          // api/v1/polls  	preferences    *preferences.Module    // api/v1/preferences  	reports        *reports.Module        // api/v1/reports  	search         *search.Module         // api/v1/search, api/v2/search @@ -109,6 +111,7 @@ func (c *Client) Route(r router.Router, m ...gin.HandlerFunc) {  	c.markers.Route(h)  	c.media.Route(h)  	c.notifications.Route(h) +	c.polls.Route(h)  	c.preferences.Route(h)  	c.reports.Route(h)  	c.search.Route(h) @@ -138,6 +141,7 @@ func NewClient(db db.DB, p *processing.Processor) *Client {  		markers:        markers.New(p),  		media:          media.New(p),  		notifications:  notifications.New(p), +		polls:          polls.New(p),  		preferences:    preferences.New(p),  		reports:        reports.New(p),  		search:         search.New(p), diff --git a/internal/api/client/accounts/accountverify_test.go b/internal/api/client/accounts/accountverify_test.go index 1690a0271..7827cc732 100644 --- a/internal/api/client/accounts/accountverify_test.go +++ b/internal/api/client/accounts/accountverify_test.go @@ -79,7 +79,7 @@ func (suite *AccountVerifyTestSuite) TestAccountVerifyGet() {  	suite.Equal("http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/small/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", apimodelAccount.HeaderStatic)  	suite.Equal(2, apimodelAccount.FollowersCount)  	suite.Equal(2, apimodelAccount.FollowingCount) -	suite.Equal(5, apimodelAccount.StatusesCount) +	suite.Equal(6, apimodelAccount.StatusesCount)  	suite.EqualValues(gtsmodel.VisibilityPublic, apimodelAccount.Source.Privacy)  	suite.Equal(testAccount.Language, apimodelAccount.Source.Language)  	suite.Equal(testAccount.NoteRaw, apimodelAccount.Source.Note) diff --git a/internal/api/client/admin/reportsget_test.go b/internal/api/client/admin/reportsget_test.go index 4c714a9e0..ecef1d465 100644 --- a/internal/api/client/admin/reportsget_test.go +++ b/internal/api/client/admin/reportsget_test.go @@ -180,8 +180,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetAll() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 0,          "following_count": 0, -        "statuses_count": 1, -        "last_status_at": "2021-09-20T10:40:37.000Z", +        "statuses_count": 2, +        "last_status_at": "2021-09-11T09:40:37.000Z",          "emojis": [],          "fields": []        } @@ -221,8 +221,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetAll() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 1,          "following_count": 1, -        "statuses_count": 7, -        "last_status_at": "2021-10-20T10:40:37.000Z", +        "statuses_count": 8, +        "last_status_at": "2021-07-28T08:40:37.000Z",          "emojis": [],          "fields": [            { @@ -382,8 +382,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetAll() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 1,          "following_count": 1, -        "statuses_count": 7, -        "last_status_at": "2021-10-20T10:40:37.000Z", +        "statuses_count": 8, +        "last_status_at": "2021-07-28T08:40:37.000Z",          "emojis": [],          "fields": [            { @@ -438,8 +438,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetAll() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 0,          "following_count": 0, -        "statuses_count": 1, -        "last_status_at": "2021-09-20T10:40:37.000Z", +        "statuses_count": 2, +        "last_status_at": "2021-09-11T09:40:37.000Z",          "emojis": [],          "fields": []        } @@ -485,8 +485,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetAll() {            "header_static": "http://localhost:8080/assets/default_header.png",            "followers_count": 0,            "following_count": 0, -          "statuses_count": 1, -          "last_status_at": "2021-09-20T10:40:37.000Z", +          "statuses_count": 2, +          "last_status_at": "2021-09-11T09:40:37.000Z",            "emojis": [],            "fields": []          }, @@ -603,8 +603,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetCreatedByAccount() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 1,          "following_count": 1, -        "statuses_count": 7, -        "last_status_at": "2021-10-20T10:40:37.000Z", +        "statuses_count": 8, +        "last_status_at": "2021-07-28T08:40:37.000Z",          "emojis": [],          "fields": [            { @@ -659,8 +659,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetCreatedByAccount() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 0,          "following_count": 0, -        "statuses_count": 1, -        "last_status_at": "2021-09-20T10:40:37.000Z", +        "statuses_count": 2, +        "last_status_at": "2021-09-11T09:40:37.000Z",          "emojis": [],          "fields": []        } @@ -706,8 +706,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetCreatedByAccount() {            "header_static": "http://localhost:8080/assets/default_header.png",            "followers_count": 0,            "following_count": 0, -          "statuses_count": 1, -          "last_status_at": "2021-09-20T10:40:37.000Z", +          "statuses_count": 2, +          "last_status_at": "2021-09-11T09:40:37.000Z",            "emojis": [],            "fields": []          }, @@ -824,8 +824,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetTargetAccount() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 1,          "following_count": 1, -        "statuses_count": 7, -        "last_status_at": "2021-10-20T10:40:37.000Z", +        "statuses_count": 8, +        "last_status_at": "2021-07-28T08:40:37.000Z",          "emojis": [],          "fields": [            { @@ -880,8 +880,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetTargetAccount() {          "header_static": "http://localhost:8080/assets/default_header.png",          "followers_count": 0,          "following_count": 0, -        "statuses_count": 1, -        "last_status_at": "2021-09-20T10:40:37.000Z", +        "statuses_count": 2, +        "last_status_at": "2021-09-11T09:40:37.000Z",          "emojis": [],          "fields": []        } @@ -927,8 +927,8 @@ func (suite *ReportsGetTestSuite) TestReportsGetTargetAccount() {            "header_static": "http://localhost:8080/assets/default_header.png",            "followers_count": 0,            "following_count": 0, -          "statuses_count": 1, -          "last_status_at": "2021-09-20T10:40:37.000Z", +          "statuses_count": 2, +          "last_status_at": "2021-09-11T09:40:37.000Z",            "emojis": [],            "fields": []          }, diff --git a/internal/api/client/instance/instancepatch_test.go b/internal/api/client/instance/instancepatch_test.go index a402f8347..2af226357 100644 --- a/internal/api/client/instance/instancepatch_test.go +++ b/internal/api/client/instance/instancepatch_test.go @@ -130,7 +130,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch1() {    },    "stats": {      "domain_count": 2, -    "status_count": 16, +    "status_count": 18,      "user_count": 4    },    "thumbnail": "http://localhost:8080/assets/logo.png", @@ -244,7 +244,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch2() {    },    "stats": {      "domain_count": 2, -    "status_count": 16, +    "status_count": 18,      "user_count": 4    },    "thumbnail": "http://localhost:8080/assets/logo.png", @@ -358,7 +358,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch3() {    },    "stats": {      "domain_count": 2, -    "status_count": 16, +    "status_count": 18,      "user_count": 4    },    "thumbnail": "http://localhost:8080/assets/logo.png", @@ -523,7 +523,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch6() {    },    "stats": {      "domain_count": 2, -    "status_count": 16, +    "status_count": 18,      "user_count": 4    },    "thumbnail": "http://localhost:8080/assets/logo.png", @@ -659,7 +659,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch8() {    },    "stats": {      "domain_count": 2, -    "status_count": 16, +    "status_count": 18,      "user_count": 4    },    "thumbnail": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/attachment/original/`+instanceAccount.AvatarMediaAttachment.ID+`.gif",`+` @@ -810,7 +810,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch9() {    },    "stats": {      "domain_count": 2, -    "status_count": 16, +    "status_count": 18,      "user_count": 4    },    "thumbnail": "http://localhost:8080/assets/logo.png", diff --git a/internal/api/client/polls/polls.go b/internal/api/client/polls/polls.go new file mode 100644 index 000000000..a2c176449 --- /dev/null +++ b/internal/api/client/polls/polls.go @@ -0,0 +1,48 @@ +// 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 ( +	"net/http" + +	"github.com/gin-gonic/gin" +	"github.com/superseriousbusiness/gotosocial/internal/api/util" +	"github.com/superseriousbusiness/gotosocial/internal/processing" +) + +const ( +	IDKey           = "id"                                 // IDKey is the key for poll IDs +	BasePath        = "/:" + util.APIVersionKey + "/polls" // BasePath is the base API path for making poll requests through v1 or v2 of the api (for mastodon API compatibility) +	PollWithID      = BasePath + "/:" + IDKey              // +	PollVotesWithID = BasePath + "/:" + IDKey + "/votes"   // +) + +type Module struct { +	processor *processing.Processor +} + +func New(processor *processing.Processor) *Module { +	return &Module{ +		processor: processor, +	} +} + +func (m *Module) Route(attachHandler func(method string, path string, f ...gin.HandlerFunc) gin.IRoutes) { +	attachHandler(http.MethodGet, PollWithID, m.PollGETHandler) +	attachHandler(http.MethodPost, PollVotesWithID, m.PollVotePOSTHandler) +} diff --git a/internal/api/client/polls/polls_get.go b/internal/api/client/polls/polls_get.go new file mode 100644 index 000000000..0b15c0ed1 --- /dev/null +++ b/internal/api/client/polls/polls_get.go @@ -0,0 +1,100 @@ +// 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 ( +	"net/http" + +	"github.com/gin-gonic/gin" +	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" +	"github.com/superseriousbusiness/gotosocial/internal/gtserror" +	"github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// PollGETHandler swagger:operation GET /api/v1/polls/{id} poll +// +// View poll with given ID. +// +//	--- +//	tags: +//	- polls +// +//	produces: +//	- application/json +// +//	parameters: +//	- +//		name: id +//		type: string +//		description: Target poll ID. +//		in: path +//		required: true +// +//	security: +//	- OAuth2 Bearer: +//		- read:statuses +// +//	responses: +//		'200': +//			description: "The requested poll." +//			schema: +//				"$ref": "#/definitions/poll" +//		'400': +//			description: bad request +//		'401': +//			description: unauthorized +//		'403': +//			description: forbidden +//		'404': +//			description: not found +//		'406': +//			description: not acceptable +//		'500': +//			description: internal server error +func (m *Module) PollGETHandler(c *gin.Context) { +	authed, err := oauth.Authed(c, true, true, true, true) +	if err != nil { +		errWithCode := gtserror.NewErrorUnauthorized(err, err.Error()) +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { +		errWithCode := gtserror.NewErrorNotAcceptable(err, err.Error()) +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	pollID, errWithCode := apiutil.ParseID(c.Param(IDKey)) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	poll, errWithCode := m.processor.Polls().PollGet( +		c.Request.Context(), +		authed.Account, +		pollID, +	) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	c.JSON(http.StatusOK, poll) +} diff --git a/internal/api/client/polls/polls_vote.go b/internal/api/client/polls/polls_vote.go new file mode 100644 index 000000000..8773d0606 --- /dev/null +++ b/internal/api/client/polls/polls_vote.go @@ -0,0 +1,112 @@ +// 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 ( +	"net/http" + +	"github.com/gin-gonic/gin" +	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" +	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" +	"github.com/superseriousbusiness/gotosocial/internal/gtserror" +	"github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// PollVotePOSTHandler swagger:operation POST /api/v1/polls/{id}/vote poll +// +// Vote with choices in the given poll. +// +//	--- +//	tags: +//	- polls +// +//	produces: +//	- application/json +// +//	parameters: +//	- +//		name: id +//		type: string +//		description: Target poll ID. +//		in: path +//		required: true +// +//	security: +//	- OAuth2 Bearer: +//		- write:statuses +// +//	responses: +//		'200': +//			description: "The updated poll with user vote choices." +//			schema: +//				"$ref": "#/definitions/poll" +//		'400': +//			description: bad request +//		'401': +//			description: unauthorized +//		'403': +//			description: forbidden +//		'404': +//			description: not found +//		'406': +//			description: not acceptable +//		'422': +//			description: unprocessable entity +//		'500': +//			description: internal server error +func (m *Module) PollVotePOSTHandler(c *gin.Context) { +	authed, err := oauth.Authed(c, true, true, true, true) +	if err != nil { +		errWithCode := gtserror.NewErrorUnauthorized(err, err.Error()) +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { +		errWithCode := gtserror.NewErrorNotAcceptable(err, err.Error()) +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	pollID, errWithCode := apiutil.ParseID(c.Param(IDKey)) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	var form apimodel.PollVoteRequest + +	if err := c.ShouldBind(&form); err != nil { +		errWithCode := gtserror.NewErrorBadRequest(err, err.Error()) +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	poll, errWithCode := m.processor.Polls().PollVote( +		c.Request.Context(), +		authed.Account, +		pollID, +		form.Choices, +	) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	c.JSON(http.StatusOK, poll) +} diff --git a/internal/api/client/reports/reportget_test.go b/internal/api/client/reports/reportget_test.go index 1bdb7557c..bc9fcaa7b 100644 --- a/internal/api/client/reports/reportget_test.go +++ b/internal/api/client/reports/reportget_test.go @@ -129,8 +129,8 @@ func (suite *ReportGetTestSuite) TestGetReport1() {      "header_static": "http://localhost:8080/assets/default_header.png",      "followers_count": 0,      "following_count": 0, -    "statuses_count": 1, -    "last_status_at": "2021-09-20T10:40:37.000Z", +    "statuses_count": 2, +    "last_status_at": "2021-09-11T09:40:37.000Z",      "emojis": [],      "fields": []    } diff --git a/internal/api/client/reports/reportsget_test.go b/internal/api/client/reports/reportsget_test.go index e58a622db..64e79ea00 100644 --- a/internal/api/client/reports/reportsget_test.go +++ b/internal/api/client/reports/reportsget_test.go @@ -154,8 +154,8 @@ func (suite *ReportsGetTestSuite) TestGetReports() {        "header_static": "http://localhost:8080/assets/default_header.png",        "followers_count": 0,        "following_count": 0, -      "statuses_count": 1, -      "last_status_at": "2021-09-20T10:40:37.000Z", +      "statuses_count": 2, +      "last_status_at": "2021-09-11T09:40:37.000Z",        "emojis": [],        "fields": []      } @@ -244,8 +244,8 @@ func (suite *ReportsGetTestSuite) TestGetReports4() {        "header_static": "http://localhost:8080/assets/default_header.png",        "followers_count": 0,        "following_count": 0, -      "statuses_count": 1, -      "last_status_at": "2021-09-20T10:40:37.000Z", +      "statuses_count": 2, +      "last_status_at": "2021-09-11T09:40:37.000Z",        "emojis": [],        "fields": []      } @@ -318,8 +318,8 @@ func (suite *ReportsGetTestSuite) TestGetReports6() {        "header_static": "http://localhost:8080/assets/default_header.png",        "followers_count": 0,        "following_count": 0, -      "statuses_count": 1, -      "last_status_at": "2021-09-20T10:40:37.000Z", +      "statuses_count": 2, +      "last_status_at": "2021-09-11T09:40:37.000Z",        "emojis": [],        "fields": []      } @@ -376,8 +376,8 @@ func (suite *ReportsGetTestSuite) TestGetReports7() {        "header_static": "http://localhost:8080/assets/default_header.png",        "followers_count": 0,        "following_count": 0, -      "statuses_count": 1, -      "last_status_at": "2021-09-20T10:40:37.000Z", +      "statuses_count": 2, +      "last_status_at": "2021-09-11T09:40:37.000Z",        "emojis": [],        "fields": []      } diff --git a/internal/api/client/search/searchget_test.go b/internal/api/client/search/searchget_test.go index 2670fff92..307b928bd 100644 --- a/internal/api/client/search/searchget_test.go +++ b/internal/api/client/search/searchget_test.go @@ -877,7 +877,7 @@ func (suite *SearchGetTestSuite) TestSearchAAny() {  	}  	suite.Len(searchResult.Accounts, 5) -	suite.Len(searchResult.Statuses, 4) +	suite.Len(searchResult.Statuses, 5)  	suite.Len(searchResult.Hashtags, 0)  } @@ -918,7 +918,7 @@ func (suite *SearchGetTestSuite) TestSearchAAnyFollowingOnly() {  	}  	suite.Len(searchResult.Accounts, 2) -	suite.Len(searchResult.Statuses, 4) +	suite.Len(searchResult.Statuses, 5)  	suite.Len(searchResult.Hashtags, 0)  } @@ -959,7 +959,7 @@ func (suite *SearchGetTestSuite) TestSearchAStatuses() {  	}  	suite.Len(searchResult.Accounts, 0) -	suite.Len(searchResult.Statuses, 4) +	suite.Len(searchResult.Statuses, 5)  	suite.Len(searchResult.Hashtags, 0)  } diff --git a/internal/api/client/statuses/statuscreate.go b/internal/api/client/statuses/statuscreate.go index e8378f461..5034e53b1 100644 --- a/internal/api/client/statuses/statuscreate.go +++ b/internal/api/client/statuses/statuscreate.go @@ -103,7 +103,12 @@ func (m *Module) StatusCreatePOSTHandler(c *gin.Context) {  		return  	} -	apiStatus, errWithCode := m.processor.Status().Create(c.Request.Context(), authed.Account, authed.Application, form) +	apiStatus, errWithCode := m.processor.Status().Create( +		c.Request.Context(), +		authed.Account, +		authed.Application, +		form, +	)  	if errWithCode != nil {  		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)  		return @@ -144,7 +149,7 @@ func validateNormalizeCreateStatus(form *apimodel.AdvancedStatusCreateForm) erro  	}  	if form.Poll != nil { -		if form.Poll.Options == nil { +		if len(form.Poll.Options) == 0 {  			return errors.New("poll with no options")  		}  		if len(form.Poll.Options) > maxPollOptions { diff --git a/internal/api/client/statuses/statusmute_test.go b/internal/api/client/statuses/statusmute_test.go index e642fb308..91a361bd6 100644 --- a/internal/api/client/statuses/statusmute_test.go +++ b/internal/api/client/statuses/statusmute_test.go @@ -130,8 +130,8 @@ func (suite *StatusMuteTestSuite) TestMuteUnmuteStatus() {      "header_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/small/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg",      "followers_count": 2,      "following_count": 2, -    "statuses_count": 5, -    "last_status_at": "2022-05-20T11:37:55.000Z", +    "statuses_count": 6, +    "last_status_at": "2022-05-20T11:41:10.000Z",      "emojis": [],      "fields": [],      "enable_rss": true, @@ -193,8 +193,8 @@ func (suite *StatusMuteTestSuite) TestMuteUnmuteStatus() {      "header_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/small/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg",      "followers_count": 2,      "following_count": 2, -    "statuses_count": 5, -    "last_status_at": "2022-05-20T11:37:55.000Z", +    "statuses_count": 6, +    "last_status_at": "2022-05-20T11:41:10.000Z",      "emojis": [],      "fields": [],      "enable_rss": true, diff --git a/internal/api/client/statuses/statusunpin_test.go b/internal/api/client/statuses/statusunpin_test.go index 9f6602b98..14253f804 100644 --- a/internal/api/client/statuses/statusunpin_test.go +++ b/internal/api/client/statuses/statusunpin_test.go @@ -107,7 +107,7 @@ func (suite *StatusUnpinTestSuite) TestUnpinStatusNotFound() {  	// Unpin a pinned followers-only status owned by another account.  	targetStatus := suite.testStatuses["local_account_2_status_7"] -	if _, err := suite.createUnpin(http.StatusNotFound, `{"error":"Not Found"}`, targetStatus.ID); err != nil { +	if _, err := suite.createUnpin(http.StatusNotFound, `{"error":"Not Found: target status not found"}`, targetStatus.ID); err != nil {  		suite.FailNow(err.Error())  	}  } diff --git a/internal/api/model/poll.go b/internal/api/model/poll.go index 2f9ce7476..ca479d117 100644 --- a/internal/api/model/poll.go +++ b/internal/api/model/poll.go @@ -24,35 +24,44 @@ type Poll struct {  	// The ID of the poll in the database.  	// example: 01FBYKMD1KBMJ0W6JF1YZ3VY5D  	ID string `json:"id"` -	// When the poll ends. (ISO 8601 Datetime), or null if the poll does not end -	ExpiresAt string `json:"expires_at,omitempty"` + +	// When the poll ends. (ISO 8601 Datetime). +	ExpiresAt string `json:"expires_at"` +  	// Is the poll currently expired?  	Expired bool `json:"expired"` +  	// Does the poll allow multiple-choice answers?  	Multiple bool `json:"multiple"` +  	// How many votes have been received.  	VotesCount int `json:"votes_count"` -	// How many unique accounts have voted on a multiple-choice poll. Null if multiple is false. -	VotersCount int `json:"voters_count,omitempty"` + +	// How many unique accounts have voted on a multiple-choice poll. +	VotersCount int `json:"voters_count"` +  	// When called with a user token, has the authorized user voted?  	Voted bool `json:"voted,omitempty"` +  	// When called with a user token, which options has the authorized user chosen? Contains an array of index values for options.  	OwnVotes []int `json:"own_votes,omitempty"` +  	// Possible answers for the poll. -	Options []PollOptions `json:"options"` +	Options []PollOption `json:"options"` +  	// Custom emoji to be used for rendering poll options.  	Emojis []Emoji `json:"emojis"`  } -// PollOptions represents the current vote counts for different poll options. +// PollOption represents the current vote counts for different poll options.  // -// swagger:model pollOptions -type PollOptions struct { +// swagger:model pollOption +type PollOption struct {  	// The text value of the poll option. String.  	Title string `json:"title"` +  	// The number of received votes for this option. -	// Number, or null if results are not published yet. -	VotesCount int `json:"votes_count,omitempty"` +	VotesCount int `json:"votes_count"`  }  // PollRequest models a request to create a poll. @@ -63,11 +72,23 @@ type PollRequest struct {  	// If provided, media_ids cannot be used, and poll[expires_in] must be provided.  	// name: poll[options]  	Options []string `form:"options" json:"options" xml:"options"` +  	// Duration the poll should be open, in seconds.  	// If provided, media_ids cannot be used, and poll[options] must be provided.  	ExpiresIn int `form:"expires_in" json:"expires_in" xml:"expires_in"` +  	// Allow multiple choices on this poll.  	Multiple bool `form:"multiple" json:"multiple" xml:"multiple"` +  	// Hide vote counts until the poll ends.  	HideTotals bool `form:"hide_totals" json:"hide_totals" xml:"hide_totals"`  } + +// PollVoteRequest models a request to vote in a poll. +// +// swagger:parameters pollVote +type PollVoteRequest struct { +	// Choices contains poll vote choice indices. Note that form +	// uses a different key than the JSON, i.e. the '[]' suffix. +	Choices []int `form:"choices[]" json:"choices" xml:"choices"` +} diff --git a/internal/api/util/errorhandling.go b/internal/api/util/errorhandling.go index 9128ce499..33f501474 100644 --- a/internal/api/util/errorhandling.go +++ b/internal/api/util/errorhandling.go @@ -40,7 +40,7 @@ import (  // 404 header and footer.  //  // If an error is returned by InstanceGet, the function will panic. -func NotFoundHandler(c *gin.Context, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), accept string) { +func NotFoundHandler(c *gin.Context, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), accept string, errWithCode gtserror.WithCode) {  	switch accept {  	case string(TextHTML):  		ctx := c.Request.Context() @@ -54,9 +54,7 @@ func NotFoundHandler(c *gin.Context, instanceGet func(ctx context.Context) (*api  			"requestID": gtscontext.RequestID(ctx),  		})  	default: -		c.JSON(http.StatusNotFound, gin.H{ -			"error": http.StatusText(http.StatusNotFound), -		}) +		c.JSON(http.StatusNotFound, gin.H{"error": errWithCode.Safe()})  	}  } @@ -122,7 +120,7 @@ func ErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet fun  	if errWithCode.Code() == http.StatusNotFound {  		// Use our special not found handler with useful status text. -		NotFoundHandler(c, instanceGet, accept) +		NotFoundHandler(c, instanceGet, accept, errWithCode)  	} else {  		genericErrorHandler(c, instanceGet, accept, errWithCode)  	} diff --git a/internal/api/wellknown/webfinger/webfingerget_test.go b/internal/api/wellknown/webfinger/webfingerget_test.go index 6b3e2cc5d..0fefe028c 100644 --- a/internal/api/wellknown/webfinger/webfingerget_test.go +++ b/internal/api/wellknown/webfinger/webfingerget_test.go @@ -83,8 +83,11 @@ func (suite *WebfingerGetTestSuite) funkifyAccountDomain(host string, accountDom  	// to new host + account domain.  	config.SetHost(host)  	config.SetAccountDomain(accountDomain) +	testrig.StopWorkers(&suite.state) +	testrig.StartWorkers(&suite.state)  	suite.processor = processing.NewProcessor(cleaner.New(&suite.state), suite.tc, suite.federator, testrig.NewTestOauthServer(suite.db), testrig.NewTestMediaManager(&suite.state), &suite.state, suite.emailSender)  	suite.webfingerModule = webfinger.New(suite.processor) +	testrig.StartWorkers(&suite.state)  	// Generate a new account for the  	// tester, which uses the new host.  | 
