Merge branch 'main' into frogend-restructure

This commit is contained in:
f0x52 2022-06-08 21:12:48 +02:00 committed by GitHub
commit eec7e5573c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
144 changed files with 2556 additions and 1784 deletions

View file

@ -21,68 +21,69 @@ package web
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/api"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
)
func (m *Module) profileTemplateHandler(c *gin.Context) {
l := logrus.WithField("func", "profileTemplateHandler")
l.Trace("rendering profile template")
func (m *Module) profileGETHandler(c *gin.Context) {
ctx := c.Request.Context()
username := c.Param(usernameKey)
if username == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "no account username specified"})
return
}
authed, err := oauth.Authed(c, false, false, false, false)
if err != nil {
l.Errorf("error authing profile GET request: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
api.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGet)
return
}
instance, errWithCode := m.processor.InstanceGet(ctx, config.GetHost())
if errWithCode != nil {
l.Debugf("error getting instance from processor: %s", errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
// usernames on our instance will always be lowercase
username := strings.ToLower(c.Param(usernameKey))
if username == "" {
err := errors.New("no account username specified")
api.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGet)
return
}
host := config.GetHost()
instance, err := m.processor.InstanceGet(ctx, host)
if err != nil {
api.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGet)
return
}
instanceGet := func(ctx context.Context, domain string) (*apimodel.Instance, gtserror.WithCode) {
return instance, nil
}
account, errWithCode := m.processor.AccountGetLocalByUsername(ctx, authed, username)
if errWithCode != nil {
l.Debugf("error getting account from processor: %s", errWithCode.Error())
if errWithCode.Code() == http.StatusNotFound {
m.NotFoundHandler(c)
return
}
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
api.ErrorHandler(c, errWithCode, instanceGet)
return
}
// if we're getting an AP request on this endpoint we should render the account's AP representation instead
// if we're getting an AP request on this endpoint we
// should render the account's AP representation instead
accept := c.NegotiateFormat(string(api.TextHTML), string(api.AppActivityJSON), string(api.AppActivityLDJSON))
if accept == string(api.AppActivityJSON) || accept == string(api.AppActivityLDJSON) {
m.returnAPRepresentation(ctx, c, username, accept)
m.returnAPProfile(ctx, c, username, accept)
return
}
// get latest 10 top-level public statuses;
// ie., exclude replies and boosts, public only,
// with or without media
statuses, errWithCode := m.processor.AccountStatusesGet(ctx, authed, account.ID, 10, true, true, "", "", false, false, true)
statusResp, errWithCode := m.processor.AccountStatusesGet(ctx, authed, account.ID, 10, true, true, "", "", false, false, true)
if errWithCode != nil {
l.Debugf("error getting statuses from processor: %s", errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
api.ErrorHandler(c, errWithCode, instanceGet)
return
}
@ -92,7 +93,11 @@ func (m *Module) profileTemplateHandler(c *gin.Context) {
randomIndex := rand.Intn(len(m.defaultAvatars))
dummyAvatar := m.defaultAvatars[randomIndex]
account.Avatar = dummyAvatar
for _, s := range statuses {
for _, i := range statusResp.Items {
s, ok := i.(*apimodel.Status)
if !ok {
panic("timelineable was not *apimodel.Status")
}
s.Account.Avatar = dummyAvatar
}
}
@ -100,7 +105,7 @@ func (m *Module) profileTemplateHandler(c *gin.Context) {
c.HTML(http.StatusOK, "profile.tmpl", gin.H{
"instance": instance,
"account": account,
"statuses": statuses,
"statuses": statusResp.Items,
"stylesheets": []string{
"/assets/Fork-Awesome/css/fork-awesome.min.css",
"/assets/dist/status.css",
@ -109,7 +114,7 @@ func (m *Module) profileTemplateHandler(c *gin.Context) {
})
}
func (m *Module) returnAPRepresentation(ctx context.Context, c *gin.Context, username string, accept string) {
func (m *Module) returnAPProfile(ctx context.Context, c *gin.Context, username string, accept string) {
verifier, signed := c.Get(string(ap.ContextRequestingPublicKeyVerifier))
if signed {
ctx = context.WithValue(ctx, ap.ContextRequestingPublicKeyVerifier, verifier)
@ -120,17 +125,16 @@ func (m *Module) returnAPRepresentation(ctx context.Context, c *gin.Context, use
ctx = context.WithValue(ctx, ap.ContextRequestingPublicKeySignature, signature)
}
user, errWithCode := m.processor.GetFediUser(ctx, username, c.Request.URL) // GetFediUser handles auth as well
user, errWithCode := m.processor.GetFediUser(ctx, username, c.Request.URL)
if errWithCode != nil {
logrus.Infof(errWithCode.Error())
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
api.ErrorHandler(c, errWithCode, m.processor.InstanceGet)
return
}
b, mErr := json.Marshal(user)
if mErr != nil {
err := fmt.Errorf("could not marshal json: %s", mErr)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
api.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGet)
return
}