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"
|
2022-06-08 18:38:03 +00:00
|
|
|
"errors"
|
2022-04-15 12:33:01 +00:00
|
|
|
"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-07-13 07:57:47 +00:00
|
|
|
const (
|
|
|
|
// MaxStatusIDKey is for specifying the maximum ID of the status to retrieve.
|
|
|
|
MaxStatusIDKey = "max_id"
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-02 13:08:13 +00:00
|
|
|
instance, err := m.processor.InstanceGetV1(ctx)
|
2022-04-15 12:33:01 +00:00
|
|
|
if err != nil {
|
2023-05-12 08:16:41 +00:00
|
|
|
apiutil.WebErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
|
2022-04-15 12:33:01 +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
|
2022-04-15 12:33:01 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
account, errWithCode := m.processor.Account().GetLocalByUsername(ctx, authed.Account, username)
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:38:03 +00:00
|
|
|
// if we're getting an AP request on this endpoint we
|
|
|
|
// should render the account'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.returnAPProfile(c, username, accept)
|
2022-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-08 12:00:39 +00:00
|
|
|
var rssFeed string
|
|
|
|
if account.EnableRSS {
|
|
|
|
rssFeed = "/@" + account.Username + "/feed.rss"
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:03:17 +00:00
|
|
|
// only allow search engines / robots to view this page if account is discoverable
|
|
|
|
var robotsMeta string
|
|
|
|
if account.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 (
|
|
|
|
paging bool
|
|
|
|
pinnedResp = &apimodel.PageableResponse{}
|
|
|
|
maxStatusID string
|
|
|
|
)
|
|
|
|
|
|
|
|
if maxStatusIDString := c.Query(MaxStatusIDKey); maxStatusIDString != "" {
|
2022-07-13 07:57:47 +00:00
|
|
|
maxStatusID = maxStatusIDString
|
2023-02-25 12:16:30 +00:00
|
|
|
paging = true
|
2022-07-13 07:57:47 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
statusResp, errWithCode := m.processor.Account().WebStatusesGet(ctx, account.ID, maxStatusID)
|
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-02-25 12:16:30 +00:00
|
|
|
// If we're not paging, then the profile visitor
|
|
|
|
// is currently just opening the bare profile, so
|
|
|
|
// load pinned statuses so we can show them at the
|
|
|
|
// top of the profile.
|
|
|
|
if !paging {
|
|
|
|
pinnedResp, errWithCode = m.processor.Account().StatusesGet(ctx, authed.Account, account.ID, 0, false, false, "", "", true, false, false)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
stylesheets = append(stylesheets, "/@"+account.Username+"/custom.css")
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
"account": account,
|
2022-09-07 14:53:12 +00:00
|
|
|
"ogMeta": ogBase(instance).withAccount(account),
|
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-02-25 12:16:30 +00:00
|
|
|
"pinned_statuses": pinnedResp.Items,
|
|
|
|
"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-06-13 14:47:56 +00:00
|
|
|
func (m *Module) returnAPProfile(c *gin.Context, username string, accept string) {
|
|
|
|
user, errWithCode := m.processor.Fedi().UserGet(c.Request.Context(), username, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
b, mErr := json.Marshal(user)
|
|
|
|
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-04-15 12:33:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, accept, b)
|
|
|
|
}
|