mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[feature] Allow users to set default interaction policies per status visibility (#3108)
* [feature] Allow users to set default interaction policies * use vars for default policies * avoid some code repetition * unfuck form binding * avoid bonkers loop * beep boop * put policyValsToAPIPolicyVals in separate function * don't bother with slices.Grow * oops
This commit is contained in:
@ -34,6 +34,7 @@ import (
|
||||
filtersV2 "github.com/superseriousbusiness/gotosocial/internal/api/client/filters/v2"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/client/followrequests"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/client/instance"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/client/interactionpolicies"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/client/lists"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/client/markers"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/client/media"
|
||||
@ -58,32 +59,33 @@ type Client struct {
|
||||
processor *processing.Processor
|
||||
db db.DB
|
||||
|
||||
accounts *accounts.Module // api/v1/accounts
|
||||
admin *admin.Module // api/v1/admin
|
||||
apps *apps.Module // api/v1/apps
|
||||
blocks *blocks.Module // api/v1/blocks
|
||||
bookmarks *bookmarks.Module // api/v1/bookmarks
|
||||
conversations *conversations.Module // api/v1/conversations
|
||||
customEmojis *customemojis.Module // api/v1/custom_emojis
|
||||
favourites *favourites.Module // api/v1/favourites
|
||||
featuredTags *featuredtags.Module // api/v1/featured_tags
|
||||
filtersV1 *filtersV1.Module // api/v1/filters
|
||||
filtersV2 *filtersV2.Module // api/v2/filters
|
||||
followRequests *followrequests.Module // api/v1/follow_requests
|
||||
instance *instance.Module // api/v1/instance
|
||||
lists *lists.Module // api/v1/lists
|
||||
markers *markers.Module // api/v1/markers
|
||||
media *media.Module // api/v1/media, api/v2/media
|
||||
mutes *mutes.Module // api/v1/mutes
|
||||
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
|
||||
statuses *statuses.Module // api/v1/statuses
|
||||
streaming *streaming.Module // api/v1/streaming
|
||||
timelines *timelines.Module // api/v1/timelines
|
||||
user *user.Module // api/v1/user
|
||||
accounts *accounts.Module // api/v1/accounts
|
||||
admin *admin.Module // api/v1/admin
|
||||
apps *apps.Module // api/v1/apps
|
||||
blocks *blocks.Module // api/v1/blocks
|
||||
bookmarks *bookmarks.Module // api/v1/bookmarks
|
||||
conversations *conversations.Module // api/v1/conversations
|
||||
customEmojis *customemojis.Module // api/v1/custom_emojis
|
||||
favourites *favourites.Module // api/v1/favourites
|
||||
featuredTags *featuredtags.Module // api/v1/featured_tags
|
||||
filtersV1 *filtersV1.Module // api/v1/filters
|
||||
filtersV2 *filtersV2.Module // api/v2/filters
|
||||
followRequests *followrequests.Module // api/v1/follow_requests
|
||||
instance *instance.Module // api/v1/instance
|
||||
interactionPolicies *interactionpolicies.Module // api/v1/interaction_policies
|
||||
lists *lists.Module // api/v1/lists
|
||||
markers *markers.Module // api/v1/markers
|
||||
media *media.Module // api/v1/media, api/v2/media
|
||||
mutes *mutes.Module // api/v1/mutes
|
||||
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
|
||||
statuses *statuses.Module // api/v1/statuses
|
||||
streaming *streaming.Module // api/v1/streaming
|
||||
timelines *timelines.Module // api/v1/timelines
|
||||
user *user.Module // api/v1/user
|
||||
}
|
||||
|
||||
func (c *Client) Route(r *router.Router, m ...gin.HandlerFunc) {
|
||||
@ -116,6 +118,7 @@ func (c *Client) Route(r *router.Router, m ...gin.HandlerFunc) {
|
||||
c.filtersV2.Route(h)
|
||||
c.followRequests.Route(h)
|
||||
c.instance.Route(h)
|
||||
c.interactionPolicies.Route(h)
|
||||
c.lists.Route(h)
|
||||
c.markers.Route(h)
|
||||
c.media.Route(h)
|
||||
@ -136,31 +139,32 @@ func NewClient(state *state.State, p *processing.Processor) *Client {
|
||||
processor: p,
|
||||
db: state.DB,
|
||||
|
||||
accounts: accounts.New(p),
|
||||
admin: admin.New(state, p),
|
||||
apps: apps.New(p),
|
||||
blocks: blocks.New(p),
|
||||
bookmarks: bookmarks.New(p),
|
||||
conversations: conversations.New(p),
|
||||
customEmojis: customemojis.New(p),
|
||||
favourites: favourites.New(p),
|
||||
featuredTags: featuredtags.New(p),
|
||||
filtersV1: filtersV1.New(p),
|
||||
filtersV2: filtersV2.New(p),
|
||||
followRequests: followrequests.New(p),
|
||||
instance: instance.New(p),
|
||||
lists: lists.New(p),
|
||||
markers: markers.New(p),
|
||||
media: media.New(p),
|
||||
mutes: mutes.New(p),
|
||||
notifications: notifications.New(p),
|
||||
polls: polls.New(p),
|
||||
preferences: preferences.New(p),
|
||||
reports: reports.New(p),
|
||||
search: search.New(p),
|
||||
statuses: statuses.New(p),
|
||||
streaming: streaming.New(p, time.Second*30, 4096),
|
||||
timelines: timelines.New(p),
|
||||
user: user.New(p),
|
||||
accounts: accounts.New(p),
|
||||
admin: admin.New(state, p),
|
||||
apps: apps.New(p),
|
||||
blocks: blocks.New(p),
|
||||
bookmarks: bookmarks.New(p),
|
||||
conversations: conversations.New(p),
|
||||
customEmojis: customemojis.New(p),
|
||||
favourites: favourites.New(p),
|
||||
featuredTags: featuredtags.New(p),
|
||||
filtersV1: filtersV1.New(p),
|
||||
filtersV2: filtersV2.New(p),
|
||||
followRequests: followrequests.New(p),
|
||||
instance: instance.New(p),
|
||||
interactionPolicies: interactionpolicies.New(p),
|
||||
lists: lists.New(p),
|
||||
markers: markers.New(p),
|
||||
media: media.New(p),
|
||||
mutes: mutes.New(p),
|
||||
notifications: notifications.New(p),
|
||||
polls: polls.New(p),
|
||||
preferences: preferences.New(p),
|
||||
reports: reports.New(p),
|
||||
search: search.New(p),
|
||||
statuses: statuses.New(p),
|
||||
streaming: streaming.New(p, time.Second*30, 4096),
|
||||
timelines: timelines.New(p),
|
||||
user: user.New(p),
|
||||
}
|
||||
}
|
||||
|
@ -528,7 +528,27 @@ func (suite *ReportsGetTestSuite) TestReportsGetAll() {
|
||||
"tags": [],
|
||||
"emojis": [],
|
||||
"card": null,
|
||||
"poll": null
|
||||
"poll": null,
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"rules": [
|
||||
@ -750,7 +770,27 @@ func (suite *ReportsGetTestSuite) TestReportsGetCreatedByAccount() {
|
||||
"tags": [],
|
||||
"emojis": [],
|
||||
"card": null,
|
||||
"poll": null
|
||||
"poll": null,
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"rules": [
|
||||
@ -972,7 +1012,27 @@ func (suite *ReportsGetTestSuite) TestReportsGetTargetAccount() {
|
||||
"tags": [],
|
||||
"emojis": [],
|
||||
"card": null,
|
||||
"poll": null
|
||||
"poll": null,
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"rules": [
|
||||
|
77
internal/api/client/interactionpolicies/getdefaults.go
Normal file
77
internal/api/client/interactionpolicies/getdefaults.go
Normal file
@ -0,0 +1,77 @@
|
||||
// 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 interactionpolicies
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// PoliciesDefaultsGETHandler swagger:operation GET /api/v1/interaction_policies/defaults policiesDefaultsGet
|
||||
//
|
||||
// Get default interaction policies for new statuses created by you.
|
||||
//
|
||||
// ---
|
||||
// tags:
|
||||
// - interaction_policies
|
||||
//
|
||||
// produces:
|
||||
// - application/json
|
||||
//
|
||||
// security:
|
||||
// - OAuth2 Bearer:
|
||||
// - read:accounts
|
||||
//
|
||||
// responses:
|
||||
// '200':
|
||||
// description: A default policies object containing a policy for each status visibility.
|
||||
// schema:
|
||||
// "$ref": "#/definitions/defaultPolicies"
|
||||
// '401':
|
||||
// description: unauthorized
|
||||
// '406':
|
||||
// description: not acceptable
|
||||
// '500':
|
||||
// description: internal server error
|
||||
func (m *Module) PoliciesDefaultsGETHandler(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
|
||||
}
|
||||
|
||||
resp, errWithCode := m.processor.Account().DefaultInteractionPoliciesGet(
|
||||
c.Request.Context(),
|
||||
authed.Account,
|
||||
)
|
||||
if errWithCode != nil {
|
||||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
||||
return
|
||||
}
|
||||
|
||||
apiutil.JSON(c, http.StatusOK, resp)
|
||||
}
|
45
internal/api/client/interactionpolicies/policies.go
Normal file
45
internal/api/client/interactionpolicies/policies.go
Normal file
@ -0,0 +1,45 @@
|
||||
// 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 interactionpolicies
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
||||
)
|
||||
|
||||
const (
|
||||
BasePath = "/v1/interaction_policies"
|
||||
DefaultsPath = BasePath + "/defaults"
|
||||
)
|
||||
|
||||
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, DefaultsPath, m.PoliciesDefaultsGETHandler)
|
||||
attachHandler(http.MethodPatch, DefaultsPath, m.PoliciesDefaultsPATCHHandler)
|
||||
}
|
334
internal/api/client/interactionpolicies/updatedefaults.go
Normal file
334
internal/api/client/interactionpolicies/updatedefaults.go
Normal file
@ -0,0 +1,334 @@
|
||||
// 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 interactionpolicies
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-playground/form/v4"
|
||||
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"
|
||||
)
|
||||
|
||||
// PoliciesDefaultsPATCHHandler swagger:operation PATCH /api/v1/interaction_policies/defaults policiesDefaultsUpdate
|
||||
//
|
||||
// Update default interaction policies per visibility level for new statuses created by you.
|
||||
//
|
||||
// If submitting using form data, use the following pattern:
|
||||
//
|
||||
// `VISIBILITY[INTERACTION_TYPE][CONDITION][INDEX]=Value`
|
||||
//
|
||||
// For example: `public[can_reply][always][0]=author`
|
||||
//
|
||||
// Using `curl` this might look something like:
|
||||
//
|
||||
// `curl -F 'public[can_reply][always][0]=author' -F 'public[can_reply][always][1]=followers'`
|
||||
//
|
||||
// The JSON equivalent would be:
|
||||
//
|
||||
// `curl -H 'Content-Type: application/json' -d '{"public":{"can_reply":{"always":["author","followers"]}}}'`
|
||||
//
|
||||
// Any visibility level left unspecified in the request body will be returned to the default.
|
||||
//
|
||||
// Ie., in the example above, "public" would be updated, but "unlisted", "private", and "direct" would be reset to defaults.
|
||||
//
|
||||
// The server will perform some normalization on submitted policies so that you can't submit totally invalid policies.
|
||||
//
|
||||
// ---
|
||||
// tags:
|
||||
// - interaction_policies
|
||||
//
|
||||
// consumes:
|
||||
// - multipart/form-data
|
||||
// - application/x-www-form-urlencoded
|
||||
// - application/json
|
||||
//
|
||||
// produces:
|
||||
// - application/json
|
||||
//
|
||||
// parameters:
|
||||
// -
|
||||
// name: public[can_favourite][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for public.can_favourite.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: public[can_favourite][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for public.can_favourite.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: public[can_reply][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for public.can_reply.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: public[can_reply][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for public.can_reply.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: public[can_reblog][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for public.can_reblog.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: public[can_reblog][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for public.can_reblog.with_approval.
|
||||
// type: string
|
||||
//
|
||||
// -
|
||||
// name: unlisted[can_favourite][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for unlisted.can_favourite.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: unlisted[can_favourite][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for unlisted.can_favourite.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: unlisted[can_reply][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for unlisted.can_reply.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: unlisted[can_reply][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for unlisted.can_reply.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: unlisted[can_reblog][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for unlisted.can_reblog.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: unlisted[can_reblog][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for unlisted.can_reblog.with_approval.
|
||||
// type: string
|
||||
//
|
||||
// -
|
||||
// name: private[can_favourite][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for private.can_favourite.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: private[can_favourite][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for private.can_favourite.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: private[can_reply][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for private.can_reply.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: private[can_reply][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for private.can_reply.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: private[can_reblog][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for private.can_reblog.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: private[can_reblog][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for private.can_reblog.with_approval.
|
||||
// type: string
|
||||
//
|
||||
// -
|
||||
// name: direct[can_favourite][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for direct.can_favourite.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: direct[can_favourite][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for direct.can_favourite.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: direct[can_reply][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for direct.can_reply.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: direct[can_reply][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for direct.can_reply.with_approval.
|
||||
// type: string
|
||||
// -
|
||||
// name: direct[can_reblog][always][0]
|
||||
// in: formData
|
||||
// description: Nth entry for direct.can_reblog.always.
|
||||
// type: string
|
||||
// -
|
||||
// name: direct[can_reblog][with_approval][0]
|
||||
// in: formData
|
||||
// description: Nth entry for direct.can_reblog.with_approval.
|
||||
// type: string
|
||||
//
|
||||
// security:
|
||||
// - OAuth2 Bearer:
|
||||
// - write:accounts
|
||||
//
|
||||
// responses:
|
||||
// '200':
|
||||
// description: Updated default policies object containing a policy for each status visibility.
|
||||
// schema:
|
||||
// "$ref": "#/definitions/defaultPolicies"
|
||||
// '400':
|
||||
// description: bad request
|
||||
// '401':
|
||||
// description: unauthorized
|
||||
// '406':
|
||||
// description: not acceptable
|
||||
// '422':
|
||||
// description: unprocessable
|
||||
// '500':
|
||||
// description: internal server error
|
||||
func (m *Module) PoliciesDefaultsPATCHHandler(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
|
||||
}
|
||||
|
||||
form, err := parseUpdateAccountForm(c)
|
||||
if err != nil {
|
||||
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
|
||||
return
|
||||
}
|
||||
|
||||
resp, errWithCode := m.processor.Account().DefaultInteractionPoliciesUpdate(
|
||||
c.Request.Context(),
|
||||
authed.Account,
|
||||
form,
|
||||
)
|
||||
if errWithCode != nil {
|
||||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
||||
return
|
||||
}
|
||||
|
||||
apiutil.JSON(c, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// intPolicyFormBinding satisfies gin's binding.Binding interface.
|
||||
// Should only be used specifically for multipart/form-data MIME type.
|
||||
type intPolicyFormBinding struct {
|
||||
visibility string
|
||||
}
|
||||
|
||||
func (i intPolicyFormBinding) Name() string {
|
||||
return i.visibility
|
||||
}
|
||||
|
||||
func (intPolicyFormBinding) Bind(req *http.Request, obj any) error {
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Change default namespace prefix and suffix to
|
||||
// allow correct parsing of the field attributes.
|
||||
decoder := form.NewDecoder()
|
||||
decoder.SetNamespacePrefix("[")
|
||||
decoder.SetNamespaceSuffix("]")
|
||||
|
||||
return decoder.Decode(obj, req.Form)
|
||||
}
|
||||
|
||||
// customBind does custom form binding for
|
||||
// each visibility in the form data.
|
||||
func customBind(
|
||||
c *gin.Context,
|
||||
form *apimodel.UpdateInteractionPoliciesRequest,
|
||||
) error {
|
||||
for _, vis := range []string{
|
||||
"Direct",
|
||||
"Private",
|
||||
"Unlisted",
|
||||
"Public",
|
||||
} {
|
||||
if err := c.ShouldBindWith(
|
||||
form,
|
||||
intPolicyFormBinding{
|
||||
visibility: vis,
|
||||
},
|
||||
); err != nil {
|
||||
return fmt.Errorf("custom form binding failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseUpdateAccountForm(c *gin.Context) (*apimodel.UpdateInteractionPoliciesRequest, error) {
|
||||
form := new(apimodel.UpdateInteractionPoliciesRequest)
|
||||
|
||||
switch ct := c.ContentType(); ct {
|
||||
case binding.MIMEJSON:
|
||||
// Just bind with default json binding.
|
||||
if err := c.ShouldBindWith(form, binding.JSON); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
case binding.MIMEPOSTForm:
|
||||
// Bind with default form binding first.
|
||||
if err := c.ShouldBindWith(form, binding.FormPost); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Now do custom binding.
|
||||
if err := customBind(c, form); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
case binding.MIMEMultipartPOSTForm:
|
||||
// Bind with default form binding first.
|
||||
if err := c.ShouldBindWith(form, binding.FormMultipart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Now do custom binding.
|
||||
if err := customBind(c, form); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
default:
|
||||
err := fmt.Errorf(
|
||||
"content-type %s not supported for this endpoint; supported content-types are %s, %s, %s",
|
||||
ct, binding.MIMEJSON, binding.MIMEPOSTForm, binding.MIMEMultipartPOSTForm,
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return form, nil
|
||||
}
|
@ -147,7 +147,27 @@ func (suite *StatusMuteTestSuite) TestMuteUnmuteStatus() {
|
||||
"emojis": [],
|
||||
"card": null,
|
||||
"poll": null,
|
||||
"text": "hello everyone!"
|
||||
"text": "hello everyone!",
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
}
|
||||
}`, muted)
|
||||
|
||||
// Unmute the status, ensure `muted` is `false`.
|
||||
@ -212,7 +232,27 @@ func (suite *StatusMuteTestSuite) TestMuteUnmuteStatus() {
|
||||
"emojis": [],
|
||||
"card": null,
|
||||
"poll": null,
|
||||
"text": "hello everyone!"
|
||||
"text": "hello everyone!",
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
}
|
||||
}`, unmuted)
|
||||
}
|
||||
|
||||
|
111
internal/api/model/interactionpolicy.go
Normal file
111
internal/api/model/interactionpolicy.go
Normal file
@ -0,0 +1,111 @@
|
||||
// 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 model
|
||||
|
||||
// One interaction policy entry for a status.
|
||||
//
|
||||
// It can be EITHER one of the internal keywords listed below, OR a full-fledged ActivityPub URI of an Actor, like "https://example.org/users/some_user".
|
||||
//
|
||||
// Internal keywords:
|
||||
//
|
||||
// - public - Public, aka anyone who can see the status according to its visibility level.
|
||||
// - followers - Followers of the status author.
|
||||
// - following - People followed by the status author.
|
||||
// - mutuals - Mutual follows of the status author (reserved, unused).
|
||||
// - mentioned - Accounts mentioned in, or replied-to by, the status.
|
||||
// - author - The status author themself.
|
||||
// - me - If request was made with an authorized user, "me" represents the user who made the request and is now looking at this interaction policy.
|
||||
//
|
||||
// swagger:model interactionPolicyValue
|
||||
type PolicyValue string
|
||||
|
||||
const (
|
||||
PolicyValuePublic PolicyValue = "public" // Public, aka anyone who can see the status according to its visibility level.
|
||||
PolicyValueFollowers PolicyValue = "followers" // Followers of the status author.
|
||||
PolicyValueFollowing PolicyValue = "following" // People followed by the status author.
|
||||
PolicyValueMutuals PolicyValue = "mutuals" // Mutual follows of the status author (reserved, unused).
|
||||
PolicyValueMentioned PolicyValue = "mentioned" // Accounts mentioned in, or replied-to by, the status.
|
||||
PolicyValueAuthor PolicyValue = "author" // The status author themself.
|
||||
PolicyValueMe PolicyValue = "me" // If request was made with an authorized user, "me" represents the user who made the request and is now looking at this interaction policy.
|
||||
)
|
||||
|
||||
// Rules for one interaction type.
|
||||
//
|
||||
// swagger:model interactionPolicyRules
|
||||
type PolicyRules struct {
|
||||
// Policy entries for accounts that can always do this type of interaction.
|
||||
Always []PolicyValue `form:"always" json:"always"`
|
||||
// Policy entries for accounts that require approval to do this type of interaction.
|
||||
WithApproval []PolicyValue `form:"with_approval" json:"with_approval"`
|
||||
}
|
||||
|
||||
// Interaction policy of a status.
|
||||
//
|
||||
// swagger:model interactionPolicy
|
||||
type InteractionPolicy struct {
|
||||
// Rules for who can favourite this status.
|
||||
CanFavourite PolicyRules `form:"can_favourite" json:"can_favourite"`
|
||||
// Rules for who can reply to this status.
|
||||
CanReply PolicyRules `form:"can_reply" json:"can_reply"`
|
||||
// Rules for who can reblog this status.
|
||||
CanReblog PolicyRules `form:"can_reblog" json:"can_reblog"`
|
||||
}
|
||||
|
||||
// Default interaction policies to use for new statuses by requesting account.
|
||||
//
|
||||
// swagger:model defaultPolicies
|
||||
type DefaultPolicies struct {
|
||||
// TODO: Add mutuals only default.
|
||||
|
||||
// Default policy for new direct visibility statuses.
|
||||
Direct InteractionPolicy `json:"direct"`
|
||||
// Default policy for new private/followers-only visibility statuses.
|
||||
Private InteractionPolicy `json:"private"`
|
||||
// Default policy for new unlisted/unlocked visibility statuses.
|
||||
Unlisted InteractionPolicy `json:"unlisted"`
|
||||
// Default policy for new public visibility statuses.
|
||||
Public InteractionPolicy `json:"public"`
|
||||
}
|
||||
|
||||
// swagger:ignore
|
||||
type UpdateInteractionPoliciesRequest struct {
|
||||
// Default policy for new direct visibility statuses.
|
||||
// Value `null` or omitted property resets policy to original default.
|
||||
//
|
||||
// in: formData
|
||||
// nullable: true
|
||||
Direct *InteractionPolicy `form:"direct" json:"direct"`
|
||||
// Default policy for new private/followers-only visibility statuses.
|
||||
// Value `null` or omitted property resets policy to original default.
|
||||
//
|
||||
// in: formData
|
||||
// nullable: true
|
||||
Private *InteractionPolicy `form:"private" json:"private"`
|
||||
// Default policy for new unlisted/unlocked visibility statuses.
|
||||
// Value `null` or omitted property resets policy to original default.
|
||||
//
|
||||
// in: formData
|
||||
// nullable: true
|
||||
Unlisted *InteractionPolicy `form:"unlisted" json:"unlisted"`
|
||||
// Default policy for new public visibility statuses.
|
||||
// Value `null` or omitted property resets policy to original default.
|
||||
//
|
||||
// in: formData
|
||||
// nullable: true
|
||||
Public *InteractionPolicy `form:"public" json:"public"`
|
||||
}
|
@ -102,6 +102,8 @@ type Status struct {
|
||||
Text string `json:"text,omitempty"`
|
||||
// A list of filters that matched this status and why they matched, if there are any such filters.
|
||||
Filtered []FilterResult `json:"filtered,omitempty"`
|
||||
// The interaction policy for this status, as set by the status author.
|
||||
InteractionPolicy InteractionPolicy `json:"interaction_policy"`
|
||||
}
|
||||
|
||||
// WebStatus is like *model.Status, but contains
|
||||
|
Reference in New Issue
Block a user