finish up

This commit is contained in:
tobi 2024-04-17 14:20:27 +02:00
parent ca85d39a42
commit f5b758210c
4 changed files with 257 additions and 0 deletions

View file

@ -2399,6 +2399,27 @@ definitions:
type: object
x-go-name: StatusReblogged
x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model
statusSource:
description: |-
StatusSource represents the source text of a
status as submitted to the API when it was created.
properties:
id:
description: ID of the status.
example: 01FBVD42CQ3ZEEVMW180SBX03B
type: string
x-go-name: ID
source:
description: Plain-text source of a status.
type: string
x-go-name: Text
spoiler_text:
description: Plain-text version of spoiler text.
type: string
x-go-name: SpoilerText
type: object
x-go-name: StatusSource
x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model
swaggerCollection:
properties:
'@context':
@ -7693,6 +7714,42 @@ paths:
summary: View accounts that have reblogged/boosted the target status.
tags:
- statuses
/api/v1/statuses/{id}/source:
get:
operationId: statusSourceGet
parameters:
- description: Target status ID.
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: ""
schema:
items:
$ref: '#/definitions/statusSource'
type: array
"400":
description: bad request
"401":
description: unauthorized
"403":
description: forbidden
"404":
description: not found
"406":
description: not acceptable
"500":
description: internal server error
security:
- OAuth2 Bearer:
- read:statuses
summary: View source text of status with the given ID. Requester must own the status.
tags:
- statuses
/api/v1/statuses/{id}/unbookmark:
post:
operationId: statusUnbookmark

View file

@ -67,6 +67,9 @@ const (
// HistoryPath is used for fetching history of posts.
HistoryPath = BasePathWithID + "/history"
// SourcePath is used for fetching source of a post.
SourcePath = BasePathWithID + "/source"
)
type Module struct {
@ -110,4 +113,5 @@ func (m *Module) Route(attachHandler func(method string, path string, f ...gin.H
// history/edit stuff
attachHandler(http.MethodGet, HistoryPath, m.StatusHistoryGETHandler)
attachHandler(http.MethodGet, SourcePath, m.StatusSourceGETHandler)
}

View file

@ -0,0 +1,95 @@
// 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"
)
// StatusSourceGETHandler swagger:operation GET /api/v1/statuses/{id}/source statusSourceGet
//
// View source text of status with the given ID. Requester must own the status.
//
// ---
// tags:
// - statuses
//
// produces:
// - application/json
//
// parameters:
// -
// name: id
// type: string
// description: Target status ID.
// in: path
// required: true
//
// security:
// - OAuth2 Bearer:
// - read:statuses
//
// responses:
// '200':
// schema:
// type: array
// items:
// "$ref": "#/definitions/statusSource"
// '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) StatusSourceGETHandler(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(IDKey))
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
resp, errWithCode := m.processor.Status().SourceGet(c.Request.Context(), authed.Account, targetStatusID)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
c.JSON(http.StatusOK, resp)
}

View file

@ -0,0 +1,101 @@
// 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"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/testrig"
)
type StatusSourceTestSuite struct {
StatusStandardTestSuite
}
func (suite *StatusSourceTestSuite) TestGetSource() {
var (
testApplication = suite.testApplications["application_1"]
testAccount = suite.testAccounts["local_account_1"]
testUser = suite.testUsers["local_account_1"]
testToken = oauth.DBTokenToToken(suite.testTokens["local_account_1"])
targetStatusID = suite.testStatuses["local_account_1_status_1"].ID
target = fmt.Sprintf("http://localhost:8080%s", strings.ReplaceAll(statuses.SourcePath, ":id", targetStatusID))
)
// Setup request.
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, target, nil)
request.Header.Set("accept", "application/json")
ctx, _ := testrig.CreateGinTestContext(recorder, request)
// Set auth + path params.
ctx.Set(oauth.SessionAuthorizedApplication, testApplication)
ctx.Set(oauth.SessionAuthorizedToken, testToken)
ctx.Set(oauth.SessionAuthorizedUser, testUser)
ctx.Set(oauth.SessionAuthorizedAccount, testAccount)
ctx.Params = gin.Params{
gin.Param{
Key: statuses.IDKey,
Value: targetStatusID,
},
}
// Call the handler.
suite.statusModule.StatusSourceGETHandler(ctx)
// Check code.
if code := recorder.Code; code != http.StatusOK {
suite.FailNow("", "unexpected http code: %d", code)
}
// Read body.
result := recorder.Result()
defer result.Body.Close()
b, err := io.ReadAll(result.Body)
if err != nil {
suite.FailNow(err.Error())
}
// Indent nicely.
dst := new(bytes.Buffer)
if err := json.Indent(dst, b, "", " "); err != nil {
suite.FailNow(err.Error())
}
suite.Equal(`{
"id": "01F8MHAMCHF6Y650WCRSCP4WMY",
"source": "hello everyone!",
"spoiler_text": "introduction post"
}`, dst.String())
}
func TestStatusSourceTestSuite(t *testing.T) {
suite.Run(t, new(StatusSourceTestSuite))
}