summaryrefslogtreecommitdiff
path: root/internal/api
diff options
context:
space:
mode:
authorLibravatar Daenney <daenney@users.noreply.github.com>2023-05-06 17:42:58 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-06 17:42:58 +0200
commit6d138588d8fc450121a3230612e8cf2d9b4e9908 (patch)
tree5c0885badc38facd90ec5e532e67795f5b39e945 /internal/api
parent[chore] update readthedocs deps, use conda (#1742) (diff)
downloadgotosocial-6d138588d8fc450121a3230612e8cf2d9b4e9908.tar.xz
[feature] Implement the preferences client API (#1740)
This adds the preferences endpoint to our Mastodon Client API implementation. It's a read-only endpoint that returns a number of user preferences. Applications can query these settings when logging in a user (for the first time) to configure themselves.
Diffstat (limited to 'internal/api')
-rw-r--r--internal/api/client.go4
-rw-r--r--internal/api/client/preferences/preferences.go44
-rw-r--r--internal/api/client/preferences/preferencesget.go91
-rw-r--r--internal/api/model/preferences.go2
4 files changed, 141 insertions, 0 deletions
diff --git a/internal/api/client.go b/internal/api/client.go
index 3750b41d4..59fa0cf96 100644
--- a/internal/api/client.go
+++ b/internal/api/client.go
@@ -35,6 +35,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/api/client/lists"
"github.com/superseriousbusiness/gotosocial/internal/api/client/media"
"github.com/superseriousbusiness/gotosocial/internal/api/client/notifications"
+ "github.com/superseriousbusiness/gotosocial/internal/api/client/preferences"
"github.com/superseriousbusiness/gotosocial/internal/api/client/reports"
"github.com/superseriousbusiness/gotosocial/internal/api/client/search"
"github.com/superseriousbusiness/gotosocial/internal/api/client/statuses"
@@ -65,6 +66,7 @@ type Client struct {
lists *lists.Module // api/v1/lists
media *media.Module // api/v1/media, api/v2/media
notifications *notifications.Module // api/v1/notifications
+ 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
@@ -101,6 +103,7 @@ func (c *Client) Route(r router.Router, m ...gin.HandlerFunc) {
c.lists.Route(h)
c.media.Route(h)
c.notifications.Route(h)
+ c.preferences.Route(h)
c.reports.Route(h)
c.search.Route(h)
c.statuses.Route(h)
@@ -128,6 +131,7 @@ func NewClient(db db.DB, p *processing.Processor) *Client {
lists: lists.New(p),
media: media.New(p),
notifications: notifications.New(p),
+ preferences: preferences.New(p),
reports: reports.New(p),
search: search.New(p),
statuses: statuses.New(p),
diff --git a/internal/api/client/preferences/preferences.go b/internal/api/client/preferences/preferences.go
new file mode 100644
index 000000000..4a1ba8fe6
--- /dev/null
+++ b/internal/api/client/preferences/preferences.go
@@ -0,0 +1,44 @@
+// 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 preferences
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ "github.com/superseriousbusiness/gotosocial/internal/processing"
+)
+
+const (
+ // BasePath is the base URI path for serving preferences, minus the api prefix.
+ BasePath = "/v1/preferences"
+)
+
+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, BasePath, m.PreferencesGETHandler)
+}
diff --git a/internal/api/client/preferences/preferencesget.go b/internal/api/client/preferences/preferencesget.go
new file mode 100644
index 000000000..2834134de
--- /dev/null
+++ b/internal/api/client/preferences/preferencesget.go
@@ -0,0 +1,91 @@
+// GoToSocial
+// Copyright (C) GoToSocial Authors admin@gotosocial.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package preferences
+
+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"
+)
+
+// PreferencesGETHandler swagger:operation GET /api/v1/preferences preferencesGet
+//
+// Return an object of user preferences.
+//
+// Example:
+//
+// ```
+//
+// {
+// "posting:default:visibility": "public",
+// "posting:default:sensitive": false,
+// "posting:default:language": "en",
+// "reading:expand:media": "default",
+// "reading:expand:spoilers": false,
+// "reading:autoplay:gifs": false
+// }
+//
+// ````
+//
+// ---
+// tags:
+// - preferences
+//
+// produces:
+// - application/json
+//
+// security:
+// - OAuth2 Bearer:
+// - read:accounts
+//
+// responses:
+// '200':
+// schema:
+// type: object
+// '400':
+// description: bad request
+// '401':
+// description: unauthorized
+// '404':
+// description: not found
+// '406':
+// description: not acceptable
+// '500':
+// description: internal server error
+func (m *Module) PreferencesGETHandler(c *gin.Context) {
+ authed, err := oauth.Authed(c, false, false, false, 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.PreferencesGet(c.Request.Context(), authed.Account.ID)
+ if errWithCode != nil {
+ apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
+ return
+ }
+ c.JSON(http.StatusOK, resp)
+}
diff --git a/internal/api/model/preferences.go b/internal/api/model/preferences.go
index 7a4c96b18..35ad040a2 100644
--- a/internal/api/model/preferences.go
+++ b/internal/api/model/preferences.go
@@ -36,4 +36,6 @@ type Preferences struct {
ReadingExpandMedia string `json:"reading:expand:media"`
// Whether CWs should be expanded by default.
ReadingExpandSpoilers bool `json:"reading:expand:spoilers"`
+ // Whether gifs should automatically play.
+ ReadingAutoPlayGifs bool `json:"reading:autoplay:gifs"`
}