2023-03-12 15:00:57 +00:00
|
|
|
// 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/>.
|
2021-09-13 12:45:33 +00:00
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2022-06-08 18:38:03 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-09-13 12:45:33 +00:00
|
|
|
"net/http"
|
2022-04-15 12:33:01 +00:00
|
|
|
"strings"
|
2021-09-13 12:45:33 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-06-08 18:38:03 +00:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2023-01-02 12:10:50 +00:00
|
|
|
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
|
2021-12-07 12:31:39 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2022-06-08 18:38:03 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-09-13 12:45:33 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
)
|
|
|
|
|
2022-06-08 18:38:03 +00:00
|
|
|
func (m *Module) threadGETHandler(c *gin.Context) {
|
2021-09-13 12:45:33 +00:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
2022-06-08 18:38:03 +00:00
|
|
|
authed, err := oauth.Authed(c, false, false, false, false)
|
|
|
|
if err != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
|
2022-06-08 18:38:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-02 10:53:46 +00:00
|
|
|
// usernames on our instance will always be lowercase
|
|
|
|
username := strings.ToLower(c.Param(usernameKey))
|
2022-04-15 12:33:01 +00:00
|
|
|
if username == "" {
|
2022-06-08 18:38:03 +00:00
|
|
|
err := errors.New("no account username specified")
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-13 12:45:33 +00:00
|
|
|
|
2022-05-02 10:53:46 +00:00
|
|
|
// status ids will always be uppercase
|
|
|
|
statusID := strings.ToUpper(c.Param(statusIDKey))
|
|
|
|
if statusID == "" {
|
2022-06-08 18:38:03 +00:00
|
|
|
err := errors.New("no status id specified")
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
|
2021-09-13 12:45:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-02 13:08:13 +00:00
|
|
|
instance, err := m.processor.InstanceGetV1(ctx)
|
2021-09-13 12:45:33 +00:00
|
|
|
if err != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
|
2021-09-13 12:45:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-02 13:08:13 +00:00
|
|
|
instanceGet := func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode) {
|
2022-06-08 18:38:03 +00:00
|
|
|
return instance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// do this check to make sure the status is actually from a local account,
|
|
|
|
// we shouldn't render threads from statuses that don't belong to us!
|
2023-02-22 15:05:26 +00:00
|
|
|
if _, errWithCode := m.processor.Account().GetLocalByUsername(ctx, authed.Account, username); errWithCode != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, errWithCode, instanceGet)
|
2021-09-13 12:45:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
status, errWithCode := m.processor.Status().Get(ctx, authed.Account, statusID)
|
2022-06-08 18:38:03 +00:00
|
|
|
if errWithCode != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, errWithCode, instanceGet)
|
2021-09-13 12:45:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 12:33:01 +00:00
|
|
|
if !strings.EqualFold(username, status.Account.Username) {
|
2022-06-08 18:38:03 +00:00
|
|
|
err := gtserror.NewErrorNotFound(errors.New("path username not equal to status author username"))
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorNotFound(err), instanceGet)
|
2021-09-13 12:45:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:38:03 +00:00
|
|
|
// if we're getting an AP request on this endpoint we
|
|
|
|
// should render the status's AP representation instead
|
2023-05-16 13:08:45 +00:00
|
|
|
accept := apiutil.NegotiateFormat(c, string(apiutil.TextHTML), string(apiutil.AppActivityJSON), string(apiutil.AppActivityLDJSON))
|
2023-01-02 12:10:50 +00:00
|
|
|
if accept == string(apiutil.AppActivityJSON) || accept == string(apiutil.AppActivityLDJSON) {
|
2023-06-13 14:47:56 +00:00
|
|
|
m.returnAPStatus(c, username, statusID, accept)
|
2022-06-08 18:38:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
context, errWithCode := m.processor.Status().ContextGet(ctx, authed.Account, statusID)
|
2022-06-08 18:38:03 +00:00
|
|
|
if errWithCode != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, errWithCode, instanceGet)
|
2021-09-13 12:45:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:14:29 +00:00
|
|
|
stylesheets := []string{
|
2022-11-23 14:32:57 +00:00
|
|
|
assetsPathPrefix + "/Fork-Awesome/css/fork-awesome.min.css",
|
|
|
|
distPathPrefix + "/status.css",
|
2022-09-12 11:14:29 +00:00
|
|
|
}
|
|
|
|
if config.GetAccountsAllowCustomCSS() {
|
|
|
|
stylesheets = append(stylesheets, "/@"+username+"/custom.css")
|
|
|
|
}
|
|
|
|
|
2021-09-13 12:45:33 +00:00
|
|
|
c.HTML(http.StatusOK, "thread.tmpl", gin.H{
|
2022-09-12 11:14:29 +00:00
|
|
|
"instance": instance,
|
|
|
|
"status": status,
|
|
|
|
"context": context,
|
|
|
|
"ogMeta": ogBase(instance).withStatus(status),
|
|
|
|
"stylesheets": stylesheets,
|
2022-11-23 14:32:57 +00:00
|
|
|
"javascript": []string{distPathPrefix + "/frontend.js"},
|
2021-09-13 12:45:33 +00:00
|
|
|
})
|
|
|
|
}
|
2022-06-08 18:38:03 +00:00
|
|
|
|
2023-06-13 14:47:56 +00:00
|
|
|
func (m *Module) returnAPStatus(c *gin.Context, username string, statusID string, accept string) {
|
|
|
|
status, errWithCode := m.processor.Fedi().StatusGet(c.Request.Context(), username, statusID)
|
2022-06-08 18:38:03 +00:00
|
|
|
if errWithCode != nil {
|
2023-06-13 14:47:56 +00:00
|
|
|
apiutil.WebErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
2022-06-08 18:38:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, mErr := json.Marshal(status)
|
|
|
|
if mErr != nil {
|
|
|
|
err := fmt.Errorf("could not marshal json: %s", mErr)
|
2023-06-13 14:47:56 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
|
2022-06-08 18:38:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, accept, b)
|
|
|
|
}
|