diff options
Diffstat (limited to 'internal/api/client')
-rw-r--r-- | internal/api/client/exports/blocks.go | 76 | ||||
-rw-r--r-- | internal/api/client/exports/exports.go | 54 | ||||
-rw-r--r-- | internal/api/client/exports/exports_test.go | 275 | ||||
-rw-r--r-- | internal/api/client/exports/followers.go | 76 | ||||
-rw-r--r-- | internal/api/client/exports/following.go | 76 | ||||
-rw-r--r-- | internal/api/client/exports/lists.go | 76 | ||||
-rw-r--r-- | internal/api/client/exports/mutes.go | 76 | ||||
-rw-r--r-- | internal/api/client/exports/stats.go | 77 |
8 files changed, 786 insertions, 0 deletions
diff --git a/internal/api/client/exports/blocks.go b/internal/api/client/exports/blocks.go new file mode 100644 index 000000000..c31e2b0b4 --- /dev/null +++ b/internal/api/client/exports/blocks.go @@ -0,0 +1,76 @@ +// 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 exports + +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" +) + +// ExportBlocksGETHandler swagger:operation GET /api/v1/exports/blocks.csv exportBlocks +// +// Export a CSV file of accounts that you block. +// +// --- +// tags: +// - import-export +// +// produces: +// - text/csv +// +// security: +// - OAuth2 Bearer: +// - read:blocks +// +// responses: +// '200': +// name: accounts +// description: CSV file of accounts that you block. +// '401': +// description: unauthorized +// '406': +// description: not acceptable +// '500': +// description: internal server error +func (m *Module) ExportBlocksGETHandler(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.CSVHeaders...); err != nil { + apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) + return + } + + records, errWithCode := m.processor.Account().ExportBlocks( + c.Request.Context(), + authed.Account, + ) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + + apiutil.EncodeCSVResponse(c.Writer, c.Request, http.StatusOK, records) +} diff --git a/internal/api/client/exports/exports.go b/internal/api/client/exports/exports.go new file mode 100644 index 000000000..90a246a74 --- /dev/null +++ b/internal/api/client/exports/exports.go @@ -0,0 +1,54 @@ +// 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 exports + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/superseriousbusiness/gotosocial/internal/processing" +) + +const ( + BasePath = "/v1/exports" + StatsPath = BasePath + "/stats" + FollowingPath = BasePath + "/following.csv" + FollowersPath = BasePath + "/followers.csv" + ListsPath = BasePath + "/lists.csv" + BlocksPath = BasePath + "/blocks.csv" + MutesPath = BasePath + "/mutes.csv" +) + +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, StatsPath, m.ExportStatsGETHandler) + attachHandler(http.MethodGet, FollowingPath, m.ExportFollowingGETHandler) + attachHandler(http.MethodGet, FollowersPath, m.ExportFollowersGETHandler) + attachHandler(http.MethodGet, ListsPath, m.ExportListsGETHandler) + attachHandler(http.MethodGet, BlocksPath, m.ExportBlocksGETHandler) + attachHandler(http.MethodGet, MutesPath, m.ExportMutesGETHandler) +} diff --git a/internal/api/client/exports/exports_test.go b/internal/api/client/exports/exports_test.go new file mode 100644 index 000000000..1943f2582 --- /dev/null +++ b/internal/api/client/exports/exports_test.go @@ -0,0 +1,275 @@ +// 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 exports_test + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/api/client/exports" + apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" + "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/oauth" + "github.com/superseriousbusiness/gotosocial/internal/state" + "github.com/superseriousbusiness/gotosocial/internal/typeutils" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type ExportsTestSuite struct { + // Suite interfaces + suite.Suite + state state.State + + // standard suite models + testTokens map[string]*gtsmodel.Token + testClients map[string]*gtsmodel.Client + testApplications map[string]*gtsmodel.Application + testUsers map[string]*gtsmodel.User + testAccounts map[string]*gtsmodel.Account + + // module being tested + exportsModule *exports.Module +} + +func (suite *ExportsTestSuite) SetupSuite() { + suite.testTokens = testrig.NewTestTokens() + suite.testClients = testrig.NewTestClients() + suite.testApplications = testrig.NewTestApplications() + suite.testUsers = testrig.NewTestUsers() + suite.testAccounts = testrig.NewTestAccounts() +} + +func (suite *ExportsTestSuite) SetupTest() { + suite.state.Caches.Init() + testrig.StartNoopWorkers(&suite.state) + + testrig.InitTestConfig() + testrig.InitTestLog() + + suite.state.DB = testrig.NewTestDB(&suite.state) + suite.state.Storage = testrig.NewInMemoryStorage() + + testrig.StartTimelines( + &suite.state, + visibility.NewFilter(&suite.state), + typeutils.NewConverter(&suite.state), + ) + + testrig.StandardDBSetup(suite.state.DB, nil) + testrig.StandardStorageSetup(suite.state.Storage, "../../../../testrig/media") + + mediaManager := testrig.NewTestMediaManager(&suite.state) + + federator := testrig.NewTestFederator( + &suite.state, + testrig.NewTestTransportController( + &suite.state, + testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), + ), + mediaManager, + ) + + processor := testrig.NewTestProcessor( + &suite.state, + federator, + testrig.NewEmailSender("../../../../web/template/", nil), + mediaManager, + ) + + suite.exportsModule = exports.New(processor) +} + +func (suite *ExportsTestSuite) TriggerHandler( + handler gin.HandlerFunc, + path string, + contentType string, + application *gtsmodel.Application, + token *gtsmodel.Token, + user *gtsmodel.User, + account *gtsmodel.Account, +) *httptest.ResponseRecorder { + // Set up request. + recorder := httptest.NewRecorder() + ctx, _ := testrig.CreateGinTestContext(recorder, nil) + + // Authorize the request ctx as though it + // had passed through API auth handlers. + ctx.Set(oauth.SessionAuthorizedApplication, application) + ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(token)) + ctx.Set(oauth.SessionAuthorizedUser, user) + ctx.Set(oauth.SessionAuthorizedAccount, account) + + // Create test request. + target := "http://localhost:8080/api" + path + ctx.Request = httptest.NewRequest(http.MethodGet, target, nil) + ctx.Request.Header.Set("Accept", contentType) + + // Trigger handler. + handler(ctx) + + return recorder +} + +func (suite *ExportsTestSuite) TearDownTest() { + testrig.StandardDBTeardown(suite.state.DB) + testrig.StandardStorageTeardown(suite.state.Storage) + testrig.StopWorkers(&suite.state) +} + +func (suite *ExportsTestSuite) TestExports() { + type testCase struct { + handler gin.HandlerFunc + path string + contentType string + application *gtsmodel.Application + token *gtsmodel.Token + user *gtsmodel.User + account *gtsmodel.Account + expect string + } + + testCases := []testCase{ + // Export Following + { + handler: suite.exportsModule.ExportFollowingGETHandler, + path: exports.FollowingPath, + contentType: apiutil.TextCSV, + application: suite.testApplications["application_1"], + token: suite.testTokens["local_account_1"], + user: suite.testUsers["local_account_1"], + account: suite.testAccounts["local_account_1"], + expect: `Account address,Show boosts +admin@localhost:8080,true +1happyturtle@localhost:8080,true +`, + }, + // Export Followers. + { + handler: suite.exportsModule.ExportFollowersGETHandler, + path: exports.FollowingPath, + contentType: apiutil.TextCSV, + application: suite.testApplications["application_1"], + token: suite.testTokens["local_account_1"], + user: suite.testUsers["local_account_1"], + account: suite.testAccounts["local_account_1"], + expect: `Account address +1happyturtle@localhost:8080 +admin@localhost:8080 +`, + }, + // Export Lists. + { + handler: suite.exportsModule.ExportListsGETHandler, + path: exports.ListsPath, + contentType: apiutil.TextCSV, + application: suite.testApplications["application_1"], + token: suite.testTokens["local_account_1"], + user: suite.testUsers["local_account_1"], + account: suite.testAccounts["local_account_1"], + expect: `Cool Ass Posters From This Instance,admin@localhost:8080 +Cool Ass Posters From This Instance,1happyturtle@localhost:8080 +`, + }, + // Export Mutes. + { + handler: suite.exportsModule.ExportMutesGETHandler, + path: exports.MutesPath, + contentType: apiutil.TextCSV, + application: suite.testApplications["application_1"], + token: suite.testTokens["local_account_1"], + user: suite.testUsers["local_account_1"], + account: suite.testAccounts["local_account_1"], + expect: `Account address,Hide notifications +`, + }, + // Export Blocks. + { + handler: suite.exportsModule.ExportBlocksGETHandler, + path: exports.BlocksPath, + contentType: apiutil.TextCSV, + application: suite.testApplications["application_1"], + token: suite.testTokens["local_account_2"], + user: suite.testUsers["local_account_2"], + account: suite.testAccounts["local_account_2"], + expect: `foss_satan@fossbros-anonymous.io +`, + }, + // Export Stats. + { + handler: suite.exportsModule.ExportStatsGETHandler, + path: exports.StatsPath, + contentType: apiutil.AppJSON, + application: suite.testApplications["application_1"], + token: suite.testTokens["local_account_1"], + user: suite.testUsers["local_account_1"], + account: suite.testAccounts["local_account_1"], + expect: `{ + "media_storage": "", + "followers_count": 2, + "following_count": 2, + "statuses_count": 8, + "lists_count": 1, + "blocks_count": 0, + "mutes_count": 0 +}`, + }, + } + + for _, test := range testCases { + recorder := suite.TriggerHandler( + test.handler, + test.path, + test.contentType, + test.application, + test.token, + test.user, + test.account, + ) + + // Check response code. + suite.EqualValues(http.StatusOK, recorder.Code) + + // Check response body. + b, err := io.ReadAll(recorder.Body) + if err != nil { + suite.FailNow(err.Error()) + } + + // If json response, indent it nicely. + if recorder.Result().Header.Get("Content-Type") == "application/json" { + dst := &bytes.Buffer{} + if err := json.Indent(dst, b, "", " "); err != nil { + suite.FailNow(err.Error()) + } + b = dst.Bytes() + } + + suite.Equal(test.expect, string(b)) + } +} + +func TestExportsTestSuite(t *testing.T) { + suite.Run(t, new(ExportsTestSuite)) +} diff --git a/internal/api/client/exports/followers.go b/internal/api/client/exports/followers.go new file mode 100644 index 000000000..ceef94659 --- /dev/null +++ b/internal/api/client/exports/followers.go @@ -0,0 +1,76 @@ +// 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 exports + +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" +) + +// ExportFollowersGETHandler swagger:operation GET /api/v1/exports/followers.csv exportFollowers +// +// Export a CSV file of accounts that follow you. +// +// --- +// tags: +// - import-export +// +// produces: +// - text/csv +// +// security: +// - OAuth2 Bearer: +// - read:follows +// +// responses: +// '200': +// name: accounts +// description: CSV file of accounts that follow you. +// '401': +// description: unauthorized +// '406': +// description: not acceptable +// '500': +// description: internal server error +func (m *Module) ExportFollowersGETHandler(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.CSVHeaders...); err != nil { + apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) + return + } + + records, errWithCode := m.processor.Account().ExportFollowers( + c.Request.Context(), + authed.Account, + ) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + + apiutil.EncodeCSVResponse(c.Writer, c.Request, http.StatusOK, records) +} diff --git a/internal/api/client/exports/following.go b/internal/api/client/exports/following.go new file mode 100644 index 000000000..e61cafc2a --- /dev/null +++ b/internal/api/client/exports/following.go @@ -0,0 +1,76 @@ +// 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 exports + +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" +) + +// ExportFollowingGETHandler swagger:operation GET /api/v1/exports/following.csv exportFollowing +// +// Export a CSV file of accounts that you follow. +// +// --- +// tags: +// - import-export +// +// produces: +// - text/csv +// +// security: +// - OAuth2 Bearer: +// - read:follows +// +// responses: +// '200': +// name: accounts +// description: CSV file of accounts that you follow. +// '401': +// description: unauthorized +// '406': +// description: not acceptable +// '500': +// description: internal server error +func (m *Module) ExportFollowingGETHandler(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.CSVHeaders...); err != nil { + apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) + return + } + + records, errWithCode := m.processor.Account().ExportFollowing( + c.Request.Context(), + authed.Account, + ) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + + apiutil.EncodeCSVResponse(c.Writer, c.Request, http.StatusOK, records) +} diff --git a/internal/api/client/exports/lists.go b/internal/api/client/exports/lists.go new file mode 100644 index 000000000..2debcc701 --- /dev/null +++ b/internal/api/client/exports/lists.go @@ -0,0 +1,76 @@ +// 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 exports + +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" +) + +// ExportListsGETHandler swagger:operation GET /api/v1/exports/lists.csv exportLists +// +// Export a CSV file of lists created by you. +// +// --- +// tags: +// - import-export +// +// produces: +// - text/csv +// +// security: +// - OAuth2 Bearer: +// - read:lists +// +// responses: +// '200': +// name: accounts +// description: CSV file of lists. +// '401': +// description: unauthorized +// '406': +// description: not acceptable +// '500': +// description: internal server error +func (m *Module) ExportListsGETHandler(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.CSVHeaders...); err != nil { + apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) + return + } + + records, errWithCode := m.processor.Account().ExportLists( + c.Request.Context(), + authed.Account, + ) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + + apiutil.EncodeCSVResponse(c.Writer, c.Request, http.StatusOK, records) +} diff --git a/internal/api/client/exports/mutes.go b/internal/api/client/exports/mutes.go new file mode 100644 index 000000000..ab49b7719 --- /dev/null +++ b/internal/api/client/exports/mutes.go @@ -0,0 +1,76 @@ +// 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 exports + +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" +) + +// ExportMutesGETHandler swagger:operation GET /api/v1/exports/mutes.csv exportMutes +// +// Export a CSV file of accounts that you mute. +// +// --- +// tags: +// - import-export +// +// produces: +// - text/csv +// +// security: +// - OAuth2 Bearer: +// - read:mutes +// +// responses: +// '200': +// name: accounts +// description: CSV file of accounts that you mute. +// '401': +// description: unauthorized +// '406': +// description: not acceptable +// '500': +// description: internal server error +func (m *Module) ExportMutesGETHandler(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.CSVHeaders...); err != nil { + apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) + return + } + + records, errWithCode := m.processor.Account().ExportMutes( + c.Request.Context(), + authed.Account, + ) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + + apiutil.EncodeCSVResponse(c.Writer, c.Request, http.StatusOK, records) +} diff --git a/internal/api/client/exports/stats.go b/internal/api/client/exports/stats.go new file mode 100644 index 000000000..9e3f1b600 --- /dev/null +++ b/internal/api/client/exports/stats.go @@ -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 exports + +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" +) + +// ExportStatsGETHandler swagger:operation GET /api/v1/exports/stats exportStats +// +// Returns informational stats on the number of items that can be exported for requesting account. +// +// --- +// tags: +// - import-export +// +// produces: +// - application/json +// +// security: +// - OAuth2 Bearer: +// - read:account +// +// responses: +// '200': +// description: Export stats for the requesting account. +// schema: +// "$ref": "#/definitions/accountExportStats" +// '401': +// description: unauthorized +// '406': +// description: not acceptable +// '500': +// description: internal server error +func (m *Module) ExportStatsGETHandler(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 + } + + exportStats, errWithCode := m.processor.Account().ExportStats( + c.Request.Context(), + authed.Account, + ) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + + apiutil.JSON(c, http.StatusOK, exportStats) +} |