| 
									
										
										
										
											2023-03-12 16:00:57 +01: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 14:33:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package web | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:22:49 +02:00
										 |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/oauth" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 09:57:47 +02:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	// MaxStatusIDKey is for specifying the maximum ID of the status to retrieve. | 
					
						
							|  |  |  | 	MaxStatusIDKey = "max_id" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | func (m *Module) profileGETHandler(c *gin.Context) { | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	ctx := c.Request.Context() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	authed, err := oauth.Authed(c, false, false, false, false) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	username := strings.ToLower(c.Param(usernameKey)) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	if username == "" { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		err := errors.New("no account username specified") | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 	instance, err := m.processor.InstanceGetV1(ctx) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 	instanceGet := func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return instance, nil | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	account, errWithCode := m.processor.Account().GetLocalByUsername(ctx, authed.Account, username) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, errWithCode, instanceGet) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	// if we're getting an AP request on this endpoint we | 
					
						
							|  |  |  | 	// should render the account's AP representation instead | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	accept := c.NegotiateFormat(string(apiutil.TextHTML), string(apiutil.AppActivityJSON), string(apiutil.AppActivityLDJSON)) | 
					
						
							|  |  |  | 	if accept == string(apiutil.AppActivityJSON) || accept == string(apiutil.AppActivityLDJSON) { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		m.returnAPProfile(ctx, c, username, accept) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 	var rssFeed string | 
					
						
							|  |  |  | 	if account.EnableRSS { | 
					
						
							|  |  |  | 		rssFeed = "/@" + account.Username + "/feed.rss" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 12:03:17 +02:00
										 |  |  | 	// only allow search engines / robots to view this page if account is discoverable | 
					
						
							|  |  |  | 	var robotsMeta string | 
					
						
							|  |  |  | 	if account.Discoverable { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		robotsMeta = robotsMetaAllowSome | 
					
						
							| 
									
										
										
										
											2022-09-29 12:03:17 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-25 13:16:30 +01: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 09:57:47 +02:00
										 |  |  | 		maxStatusID = maxStatusIDString | 
					
						
							| 
									
										
										
										
											2023-02-25 13:16:30 +01:00
										 |  |  | 		paging = true | 
					
						
							| 
									
										
										
										
											2022-07-13 09:57:47 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	statusResp, errWithCode := m.processor.Account().WebStatusesGet(ctx, account.ID, maxStatusID) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, errWithCode, instanceGet) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-25 13:16:30 +01: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 { | 
					
						
							|  |  |  | 			apiutil.ErrorHandler(c, errWithCode, instanceGet) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-12 13:14:29 +02:00
										 |  |  | 	stylesheets := []string{ | 
					
						
							| 
									
										
										
										
											2022-11-23 15:32:57 +01:00
										 |  |  | 		assetsPathPrefix + "/Fork-Awesome/css/fork-awesome.min.css", | 
					
						
							|  |  |  | 		distPathPrefix + "/status.css", | 
					
						
							|  |  |  | 		distPathPrefix + "/profile.css", | 
					
						
							| 
									
										
										
										
											2022-09-12 13:14:29 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if config.GetAccountsAllowCustomCSS() { | 
					
						
							|  |  |  | 		stylesheets = append(stylesheets, "/@"+account.Username+"/custom.css") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	c.HTML(http.StatusOK, "profile.tmpl", gin.H{ | 
					
						
							| 
									
										
										
										
											2022-07-13 09:57:47 +02:00
										 |  |  | 		"instance":         instance, | 
					
						
							|  |  |  | 		"account":          account, | 
					
						
							| 
									
										
										
										
											2022-09-07 16:53:12 +02:00
										 |  |  | 		"ogMeta":           ogBase(instance).withAccount(account), | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 		"rssFeed":          rssFeed, | 
					
						
							| 
									
										
										
										
											2022-09-29 12:03:17 +02:00
										 |  |  | 		"robotsMeta":       robotsMeta, | 
					
						
							| 
									
										
										
										
											2022-07-13 09:57:47 +02:00
										 |  |  | 		"statuses":         statusResp.Items, | 
					
						
							|  |  |  | 		"statuses_next":    statusResp.NextLink, | 
					
						
							| 
									
										
										
										
											2023-02-25 13:16:30 +01:00
										 |  |  | 		"pinned_statuses":  pinnedResp.Items, | 
					
						
							|  |  |  | 		"show_back_to_top": paging, | 
					
						
							| 
									
										
										
										
											2022-09-12 13:14:29 +02:00
										 |  |  | 		"stylesheets":      stylesheets, | 
					
						
							| 
									
										
										
										
											2022-11-23 15:32:57 +01:00
										 |  |  | 		"javascript":       []string{distPathPrefix + "/frontend.js"}, | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | func (m *Module) returnAPProfile(ctx context.Context, c *gin.Context, username string, accept string) { | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	verifier, signed := c.Get(string(ap.ContextRequestingPublicKeyVerifier)) | 
					
						
							|  |  |  | 	if signed { | 
					
						
							|  |  |  | 		ctx = context.WithValue(ctx, ap.ContextRequestingPublicKeyVerifier, verifier) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	signature, signed := c.Get(string(ap.ContextRequestingPublicKeySignature)) | 
					
						
							|  |  |  | 	if signed { | 
					
						
							|  |  |  | 		ctx = context.WithValue(ctx, ap.ContextRequestingPublicKeySignature, signature) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	user, errWithCode := m.processor.Fedi().UserGet(ctx, username, c.Request.URL) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) //nolint:contextcheck | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b, mErr := json.Marshal(user) | 
					
						
							|  |  |  | 	if mErr != nil { | 
					
						
							|  |  |  | 		err := fmt.Errorf("could not marshal json: %s", mErr) | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1) //nolint:contextcheck | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.Data(http.StatusOK, accept, b) | 
					
						
							|  |  |  | } |