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/>.
|
2022-04-15 12:33:01 +00:00
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-06-08 18:38:03 +00:00
|
|
|
"strings"
|
2022-04-15 12:33:01 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-06-08 18:22:49 +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"
|
2022-04-15 12:33:01 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2022-06-08 18:38:03 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2022-04-15 12:33:01 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
)
|
|
|
|
|
2022-06-08 18:38:03 +00:00
|
|
|
func (m *Module) profileGETHandler(c *gin.Context) {
|
2022-04-15 12:33:01 +00:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// We'll need the instance later, and we can also use it
|
|
|
|
// before then to make it easier to return a web error.
|
|
|
|
instance, errWithCode := m.processor.InstanceGetV1(ctx)
|
|
|
|
if errWithCode != nil {
|
|
|
|
apiutil.WebErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
2022-06-08 18:38:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// Return instance we already got from the db,
|
|
|
|
// don't try to fetch it again when erroring.
|
|
|
|
instanceGet := func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode) {
|
|
|
|
return instance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse account targetUsername from the URL.
|
|
|
|
targetUsername, errWithCode := apiutil.ParseWebUsername(c.Param(apiutil.WebUsernameKey))
|
|
|
|
if errWithCode != nil {
|
|
|
|
apiutil.WebErrorHandler(c, errWithCode, instanceGet)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// Normalize requested username:
|
|
|
|
//
|
|
|
|
// - Usernames on our instance are (currently) always lowercase.
|
|
|
|
//
|
|
|
|
// todo: Update this logic when different username patterns
|
|
|
|
// are allowed, and/or when status slugs are introduced.
|
|
|
|
targetUsername = strings.ToLower(targetUsername)
|
|
|
|
|
|
|
|
// Check what type of content is being requested. If we're getting an AP
|
|
|
|
// request on this endpoint we should render the AP representation instead.
|
|
|
|
accept, err := apiutil.NegotiateAccept(c, apiutil.HTMLOrActivityPubHeaders...)
|
2022-04-15 12:33:01 +00:00
|
|
|
if err != nil {
|
2023-07-07 12:58:53 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), instanceGet)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
if accept == string(apiutil.AppActivityJSON) || accept == string(apiutil.AppActivityLDJSON) {
|
|
|
|
// AP account representation has been requested.
|
|
|
|
m.returnAPAccount(c, targetUsername, accept, instanceGet)
|
|
|
|
return
|
2022-04-15 12:33:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// text/html has been requested. Proceed with getting the web view of the account.
|
|
|
|
|
|
|
|
// Don't require auth for web endpoints, but do take it if it was provided.
|
|
|
|
// authed.Account might end up nil here, but that's fine in case of public pages.
|
|
|
|
authed, err := oauth.Authed(c, false, false, false, false)
|
|
|
|
if err != nil {
|
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the target account so we can do some checks on it.
|
|
|
|
targetAccount, errWithCode := m.processor.Account().GetLocalByUsername(ctx, authed.Account, targetUsername)
|
2022-04-15 12:33:01 +00:00
|
|
|
if errWithCode != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, errWithCode, instanceGet)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// If target account is suspended, this page should not be visible.
|
|
|
|
// TODO: change this to 410?
|
|
|
|
if targetAccount.Suspended {
|
|
|
|
err := fmt.Errorf("target account %s is suspended", targetUsername)
|
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorNotFound(err), instanceGet)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// Only generate RSS link if account has RSS enabled.
|
2022-10-08 12:00:39 +00:00
|
|
|
var rssFeed string
|
2023-07-07 12:58:53 +00:00
|
|
|
if targetAccount.EnableRSS {
|
|
|
|
rssFeed = "/@" + targetAccount.Username + "/feed.rss"
|
2022-10-08 12:00:39 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// Only allow search engines / robots to
|
|
|
|
// index if account is discoverable.
|
2022-09-29 10:03:17 +00:00
|
|
|
var robotsMeta string
|
2023-07-07 12:58:53 +00:00
|
|
|
if targetAccount.Discoverable {
|
2023-01-02 12:10:50 +00:00
|
|
|
robotsMeta = robotsMetaAllowSome
|
2022-09-29 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 12:16:30 +00:00
|
|
|
// We need to change our response slightly if the
|
|
|
|
// profile visitor is paging through statuses.
|
|
|
|
var (
|
2023-07-07 12:58:53 +00:00
|
|
|
maxStatusID = apiutil.ParseMaxID(c.Query(apiutil.MaxIDKey), "")
|
|
|
|
paging = maxStatusID != ""
|
2023-11-17 10:35:28 +00:00
|
|
|
pinnedStatuses []*apimodel.Status
|
2023-02-25 12:16:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if !paging {
|
2023-07-07 12:58:53 +00:00
|
|
|
// Client opened bare profile (from the top)
|
|
|
|
// so load + display pinned statuses.
|
2023-11-17 10:35:28 +00:00
|
|
|
pinnedStatuses, errWithCode = m.processor.Account().WebStatusesGetPinned(ctx, targetAccount.ID)
|
2023-02-25 12:16:30 +00:00
|
|
|
if errWithCode != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, errWithCode, instanceGet)
|
2023-02-25 12:16:30 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-07 12:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get statuses from maxStatusID onwards (or from top if empty string).
|
|
|
|
statusResp, errWithCode := m.processor.Account().WebStatusesGet(ctx, targetAccount.ID, maxStatusID)
|
|
|
|
if errWithCode != nil {
|
|
|
|
apiutil.WebErrorHandler(c, errWithCode, instanceGet)
|
|
|
|
return
|
2023-02-25 12:16:30 +00:00
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
distPathPrefix + "/profile.css",
|
2022-09-12 11:14:29 +00:00
|
|
|
}
|
|
|
|
if config.GetAccountsAllowCustomCSS() {
|
2023-07-07 12:58:53 +00:00
|
|
|
stylesheets = append(stylesheets, "/@"+targetAccount.Username+"/custom.css")
|
2022-09-12 11:14:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 12:33:01 +00:00
|
|
|
c.HTML(http.StatusOK, "profile.tmpl", gin.H{
|
2022-07-13 07:57:47 +00:00
|
|
|
"instance": instance,
|
2023-07-07 12:58:53 +00:00
|
|
|
"account": targetAccount,
|
|
|
|
"ogMeta": ogBase(instance).withAccount(targetAccount),
|
2022-10-08 12:00:39 +00:00
|
|
|
"rssFeed": rssFeed,
|
2022-09-29 10:03:17 +00:00
|
|
|
"robotsMeta": robotsMeta,
|
2022-07-13 07:57:47 +00:00
|
|
|
"statuses": statusResp.Items,
|
|
|
|
"statuses_next": statusResp.NextLink,
|
2023-11-17 10:35:28 +00:00
|
|
|
"pinned_statuses": pinnedStatuses,
|
2023-02-25 12:16:30 +00:00
|
|
|
"show_back_to_top": paging,
|
2022-09-12 11:14:29 +00:00
|
|
|
"stylesheets": stylesheets,
|
2022-11-23 14:32:57 +00:00
|
|
|
"javascript": []string{distPathPrefix + "/frontend.js"},
|
2022-04-15 12:33:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
// returnAPAccount returns an ActivityPub representation of
|
|
|
|
// target account. It will do http signature authentication.
|
|
|
|
func (m *Module) returnAPAccount(
|
|
|
|
c *gin.Context,
|
|
|
|
targetUsername string,
|
|
|
|
accept string,
|
|
|
|
instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode),
|
|
|
|
) {
|
|
|
|
user, errWithCode := m.processor.Fedi().UserGet(c.Request.Context(), targetUsername, c.Request.URL)
|
2022-04-15 12:33:01 +00:00
|
|
|
if errWithCode != nil {
|
2023-06-13 14:47:56 +00:00
|
|
|
apiutil.WebErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:58:53 +00:00
|
|
|
b, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
err := gtserror.Newf("could not marshal json: %w", err)
|
2023-06-13 14:47:56 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, accept, b)
|
|
|
|
}
|