[feature] add support for polls + receiving federated status edits (#2330)

This commit is contained in:
kim
2023-11-08 14:32:17 +00:00
committed by GitHub
parent 7204ccedc3
commit e9e5dc5a40
84 changed files with 3992 additions and 570 deletions

View File

@@ -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)

View File

@@ -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": []
},

View File

@@ -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",

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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": []
}

View File

@@ -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": []
}

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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())
}
}