diff options
| author | 2023-10-25 16:04:53 +0200 | |
|---|---|---|
| committer | 2023-10-25 15:04:53 +0100 | |
| commit | c7b6cd7770cad9bfdc23decffa7c4068752dbbbd (patch) | |
| tree | 0f039fd34fb0287860fce06ff1c30dedd1882136 /internal/api/client | |
| parent | [bugfix] allow store smaller PNG image than 261 bytes (#2263) (#2298) (diff) | |
| download | gotosocial-c7b6cd7770cad9bfdc23decffa7c4068752dbbbd.tar.xz | |
[feature] Status thread mute/unmute functionality (#2278)
* add db models + functions for keeping track of threads
* give em the old linty testy
* create, remove, check mutes
* swagger
* testerino
* test mute/unmute via api
* add info log about new index creation
* thread + allow muting of any remote statuses that mention a local account
* IsStatusThreadMutedBy -> IsThreadMutedByAccount
* use common processing functions in status processor
* set = NULL
* favee!
* get rekt darlings, darlings get rekt
* testrig please, have mercy muy liege
Diffstat (limited to 'internal/api/client')
| -rw-r--r-- | internal/api/client/statuses/status.go | 4 | ||||
| -rw-r--r-- | internal/api/client/statuses/statusmute.go | 99 | ||||
| -rw-r--r-- | internal/api/client/statuses/statusmute_test.go | 217 | ||||
| -rw-r--r-- | internal/api/client/statuses/statusunmute.go | 99 | 
4 files changed, 419 insertions, 0 deletions
| diff --git a/internal/api/client/statuses/status.go b/internal/api/client/statuses/status.go index d12665a9d..c93d69994 100644 --- a/internal/api/client/statuses/status.go +++ b/internal/api/client/statuses/status.go @@ -91,6 +91,10 @@ func (m *Module) Route(attachHandler func(method string, path string, f ...gin.H  	attachHandler(http.MethodPost, PinPath, m.StatusPinPOSTHandler)  	attachHandler(http.MethodPost, UnpinPath, m.StatusUnpinPOSTHandler) +	// mute stuff +	attachHandler(http.MethodPost, MutePath, m.StatusMutePOSTHandler) +	attachHandler(http.MethodPost, UnmutePath, m.StatusUnmutePOSTHandler) +  	// reblog stuff  	attachHandler(http.MethodPost, ReblogPath, m.StatusBoostPOSTHandler)  	attachHandler(http.MethodPost, UnreblogPath, m.StatusUnboostPOSTHandler) diff --git a/internal/api/client/statuses/statusmute.go b/internal/api/client/statuses/statusmute.go new file mode 100644 index 000000000..95ada8939 --- /dev/null +++ b/internal/api/client/statuses/statusmute.go @@ -0,0 +1,99 @@ +// 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 statuses + +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" +) + +// StatusMutePOSTHandler swagger:operation POST /api/v1/statuses/{id}/mute statusMute +// +// Mute a status's thread. This prevents notifications from being created for future replies, likes, boosts etc in the thread of which the target status is a part. +// +// Target status must belong to you or mention you. +// +// Status thread mutes and unmutes are idempotent. If you already mute a thread, muting it again just means it stays muted and you'll get 200 OK back. +// +//	--- +//	tags: +//	- statuses +// +//	produces: +//	- application/json +// +//	parameters: +//	- +//		name: id +//		type: string +//		description: Target status ID. +//		in: path +//		required: true +// +//	security: +//	- OAuth2 Bearer: +//		- write:mutes +// +//	responses: +//		'200': +//			name: status +//			description: The now-muted status. +//			schema: +//				"$ref": "#/definitions/status" +//		'400': +//			description: bad request; you're not part of the target status thread +//		'401': +//			description: unauthorized +//		'403': +//			description: forbidden +//		'404': +//			description: not found +//		'406': +//			description: not acceptable +//		'500': +//			description: internal server error +func (m *Module) StatusMutePOSTHandler(c *gin.Context) { +	authed, err := oauth.Authed(c, true, true, true, true) +	if err != nil { +		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) +		return +	} + +	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { +		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) +		return +	} + +	targetStatusID, errWithCode := apiutil.ParseID(c.Param(apiutil.IDKey)) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	apiStatus, errWithCode := m.processor.Status().MuteCreate(c.Request.Context(), authed.Account, targetStatusID) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	c.JSON(http.StatusOK, apiStatus) +} diff --git a/internal/api/client/statuses/statusmute_test.go b/internal/api/client/statuses/statusmute_test.go new file mode 100644 index 000000000..e642fb308 --- /dev/null +++ b/internal/api/client/statuses/statusmute_test.go @@ -0,0 +1,217 @@ +// 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 statuses_test + +import ( +	"bytes" +	"encoding/json" +	"fmt" +	"io" +	"net/http" +	"net/http/httptest" +	"strings" +	"testing" + +	"github.com/gin-gonic/gin" +	"github.com/stretchr/testify/suite" +	"github.com/superseriousbusiness/gotosocial/internal/api/client/statuses" +	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" +	"github.com/superseriousbusiness/gotosocial/internal/oauth" +	"github.com/superseriousbusiness/gotosocial/testrig" +) + +type StatusMuteTestSuite struct { +	StatusStandardTestSuite +} + +func (suite *StatusMuteTestSuite) post(path string, handler func(*gin.Context), targetStatusID string) (int, string) { +	t := suite.testTokens["local_account_1"] +	oauthToken := oauth.DBTokenToToken(t) + +	recorder := httptest.NewRecorder() +	ctx, _ := testrig.CreateGinTestContext(recorder, nil) +	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) +	ctx.Set(oauth.SessionAuthorizedToken, oauthToken) +	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) +	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) +	ctx.Request = httptest.NewRequest(http.MethodPost, path, nil) // the endpoint we're hitting +	ctx.Request.Header.Set("accept", "application/json") + +	// normally the router would populate these params from the path values, +	// but because we're calling the function directly, we need to set them manually. +	ctx.Params = gin.Params{ +		gin.Param{ +			Key:   apiutil.IDKey, +			Value: targetStatusID, +		}, +	} + +	handler(ctx) + +	result := recorder.Result() +	defer result.Body.Close() + +	b, err := io.ReadAll(result.Body) +	if err != nil { +		suite.FailNow(err.Error()) +	} + +	indented := bytes.Buffer{} +	if err := json.Indent(&indented, b, "", "  "); err != nil { +		suite.FailNow(err.Error()) +	} + +	return recorder.Code, indented.String() +} + +func (suite *StatusMuteTestSuite) TestMuteUnmuteStatus() { +	var ( +		targetStatus = suite.testStatuses["local_account_1_status_1"] +		path         = fmt.Sprintf("http://localhost:8080/api%s", strings.ReplaceAll(statuses.MutePath, ":id", targetStatus.ID)) +	) + +	// Mute the status, ensure `muted` is `true`. +	code, muted := suite.post(path, suite.statusModule.StatusMutePOSTHandler, targetStatus.ID) +	suite.Equal(http.StatusOK, code) +	suite.Equal(`{ +  "id": "01F8MHAMCHF6Y650WCRSCP4WMY", +  "created_at": "2021-10-20T10:40:37.000Z", +  "in_reply_to_id": null, +  "in_reply_to_account_id": null, +  "sensitive": true, +  "spoiler_text": "introduction post", +  "visibility": "public", +  "language": "en", +  "uri": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY", +  "url": "http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY", +  "replies_count": 2, +  "reblogs_count": 1, +  "favourites_count": 1, +  "favourited": false, +  "reblogged": false, +  "muted": true, +  "bookmarked": false, +  "pinned": false, +  "content": "hello everyone!", +  "reblog": null, +  "application": { +    "name": "really cool gts application", +    "website": "https://reallycool.app" +  }, +  "account": { +    "id": "01F8MH1H7YV1Z7D2C8K2730QBF", +    "username": "the_mighty_zork", +    "acct": "the_mighty_zork", +    "display_name": "original zork (he/they)", +    "locked": false, +    "discoverable": true, +    "bot": false, +    "created_at": "2022-05-20T11:09:18.000Z", +    "note": "\u003cp\u003ehey yo this is my profile!\u003c/p\u003e", +    "url": "http://localhost:8080/@the_mighty_zork", +    "avatar": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/original/01F8MH58A357CV5K7R7TJMSH6S.jpg", +    "avatar_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/small/01F8MH58A357CV5K7R7TJMSH6S.jpg", +    "header": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", +    "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", +    "emojis": [], +    "fields": [], +    "enable_rss": true, +    "role": { +      "name": "user" +    } +  }, +  "media_attachments": [], +  "mentions": [], +  "tags": [], +  "emojis": [], +  "card": null, +  "poll": null, +  "text": "hello everyone!" +}`, muted) + +	// Unmute the status, ensure `muted` is `false`. +	code, unmuted := suite.post(path, suite.statusModule.StatusUnmutePOSTHandler, targetStatus.ID) +	suite.Equal(http.StatusOK, code) +	suite.Equal(`{ +  "id": "01F8MHAMCHF6Y650WCRSCP4WMY", +  "created_at": "2021-10-20T10:40:37.000Z", +  "in_reply_to_id": null, +  "in_reply_to_account_id": null, +  "sensitive": true, +  "spoiler_text": "introduction post", +  "visibility": "public", +  "language": "en", +  "uri": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY", +  "url": "http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY", +  "replies_count": 2, +  "reblogs_count": 1, +  "favourites_count": 1, +  "favourited": false, +  "reblogged": false, +  "muted": false, +  "bookmarked": false, +  "pinned": false, +  "content": "hello everyone!", +  "reblog": null, +  "application": { +    "name": "really cool gts application", +    "website": "https://reallycool.app" +  }, +  "account": { +    "id": "01F8MH1H7YV1Z7D2C8K2730QBF", +    "username": "the_mighty_zork", +    "acct": "the_mighty_zork", +    "display_name": "original zork (he/they)", +    "locked": false, +    "discoverable": true, +    "bot": false, +    "created_at": "2022-05-20T11:09:18.000Z", +    "note": "\u003cp\u003ehey yo this is my profile!\u003c/p\u003e", +    "url": "http://localhost:8080/@the_mighty_zork", +    "avatar": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/original/01F8MH58A357CV5K7R7TJMSH6S.jpg", +    "avatar_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/small/01F8MH58A357CV5K7R7TJMSH6S.jpg", +    "header": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", +    "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", +    "emojis": [], +    "fields": [], +    "enable_rss": true, +    "role": { +      "name": "user" +    } +  }, +  "media_attachments": [], +  "mentions": [], +  "tags": [], +  "emojis": [], +  "card": null, +  "poll": null, +  "text": "hello everyone!" +}`, unmuted) +} + +func TestStatusMuteTestSuite(t *testing.T) { +	suite.Run(t, new(StatusMuteTestSuite)) +} diff --git a/internal/api/client/statuses/statusunmute.go b/internal/api/client/statuses/statusunmute.go new file mode 100644 index 000000000..e657992ca --- /dev/null +++ b/internal/api/client/statuses/statusunmute.go @@ -0,0 +1,99 @@ +// 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 statuses + +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" +) + +// StatusUnmutePOSTHandler swagger:operation POST /api/v1/statuses/{id}/unmute statusUnmute +// +// Unmute a status's thread. This reenables notifications for future replies, likes, boosts etc in the thread of which the target status is a part. +// +// Target status must belong to you or mention you. +// +// Status thread mutes and unmutes are idempotent. If you already unmuted a thread, unmuting it again just means it stays unmuted and you'll get 200 OK back. +// +//	--- +//	tags: +//	- statuses +// +//	produces: +//	- application/json +// +//	parameters: +//	- +//		name: id +//		type: string +//		description: Target status ID. +//		in: path +//		required: true +// +//	security: +//	- OAuth2 Bearer: +//		- write:mutes +// +//	responses: +//		'200': +//			name: status +//			description: The now-unmuted status. +//			schema: +//				"$ref": "#/definitions/status" +//		'400': +//			description: bad request; you're not part of the target status thread +//		'401': +//			description: unauthorized +//		'403': +//			description: forbidden +//		'404': +//			description: not found +//		'406': +//			description: not acceptable +//		'500': +//			description: internal server error +func (m *Module) StatusUnmutePOSTHandler(c *gin.Context) { +	authed, err := oauth.Authed(c, true, true, true, true) +	if err != nil { +		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) +		return +	} + +	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { +		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) +		return +	} + +	targetStatusID, errWithCode := apiutil.ParseID(c.Param(apiutil.IDKey)) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	apiStatus, errWithCode := m.processor.Status().MuteRemove(c.Request.Context(), authed.Account, targetStatusID) +	if errWithCode != nil { +		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) +		return +	} + +	c.JSON(http.StatusOK, apiStatus) +} | 
