| 
									
										
										
										
											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" | 
					
						
							|  |  |  | 	"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" | 
					
						
							| 
									
										
										
										
											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-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-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() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 20:38:03 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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. | 
					
						
							| 
									
										
										
										
											2024-04-13 13:25:10 +02:00
										 |  |  | 	targetUsername, errWithCode := apiutil.ParseUsername(c.Param(apiutil.UsernameKey)) | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							|  |  |  | 		apiutil.WebErrorHandler(c, errWithCode, instanceGet) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 14:33:01 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		apiutil.WebErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), instanceGet) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 14:33:01 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 14:33:01 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-05-12 08:16:41 +00:00
										 |  |  | 		apiutil.WebErrorHandler(c, errWithCode, instanceGet) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	// Only generate RSS link if account has RSS enabled. | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 	var rssFeed string | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	if targetAccount.EnableRSS { | 
					
						
							|  |  |  | 		rssFeed = "/@" + targetAccount.Username + "/feed.rss" | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	// Only allow search engines / robots to | 
					
						
							|  |  |  | 	// index if account is discoverable. | 
					
						
							| 
									
										
										
										
											2022-09-29 12:03:17 +02:00
										 |  |  | 	var robotsMeta string | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	if targetAccount.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 ( | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		maxStatusID    = apiutil.ParseMaxID(c.Query(apiutil.MaxIDKey), "") | 
					
						
							|  |  |  | 		paging         = maxStatusID != "" | 
					
						
							| 
									
										
										
										
											2023-11-17 11:35:28 +01:00
										 |  |  | 		pinnedStatuses []*apimodel.Status | 
					
						
							| 
									
										
										
										
											2023-02-25 13:16:30 +01:00
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !paging { | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		// Client opened bare profile (from the top) | 
					
						
							|  |  |  | 		// so load + display pinned statuses. | 
					
						
							| 
									
										
										
										
											2023-11-17 11:35:28 +01:00
										 |  |  | 		pinnedStatuses, errWithCode = m.processor.Account().WebStatusesGetPinned(ctx, targetAccount.ID) | 
					
						
							| 
									
										
										
										
											2023-02-25 13:16:30 +01:00
										 |  |  | 		if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-05-12 08:16:41 +00:00
										 |  |  | 			apiutil.WebErrorHandler(c, errWithCode, instanceGet) | 
					
						
							| 
									
										
										
										
											2023-02-25 13:16:30 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 13:16:30 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 18:32:24 +01:00
										 |  |  | 	// Prepare stylesheets for profile. | 
					
						
							|  |  |  | 	stylesheets := make([]string, 0, 6) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Basic profile stylesheets. | 
					
						
							|  |  |  | 	stylesheets = append( | 
					
						
							|  |  |  | 		stylesheets, | 
					
						
							|  |  |  | 		[]string{ | 
					
						
							|  |  |  | 			cssFA, | 
					
						
							|  |  |  | 			cssStatus, | 
					
						
							|  |  |  | 			cssThread, | 
					
						
							|  |  |  | 			cssProfile, | 
					
						
							|  |  |  | 		}..., | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// User-selected theme if set. | 
					
						
							|  |  |  | 	if theme := targetAccount.Theme; theme != "" { | 
					
						
							|  |  |  | 		stylesheets = append( | 
					
						
							|  |  |  | 			stylesheets, | 
					
						
							|  |  |  | 			themesPathPrefix+"/"+theme, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Custom CSS for this user last in cascade. | 
					
						
							|  |  |  | 	stylesheets = append( | 
					
						
							|  |  |  | 		stylesheets, | 
					
						
							|  |  |  | 		"/@"+targetAccount.Username+"/custom.css", | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 	page := apiutil.WebPage{ | 
					
						
							| 
									
										
										
										
											2024-03-25 18:32:24 +01:00
										 |  |  | 		Template:    "profile.tmpl", | 
					
						
							|  |  |  | 		Instance:    instance, | 
					
						
							|  |  |  | 		OGMeta:      apiutil.OGBase(instance).WithAccount(targetAccount), | 
					
						
							|  |  |  | 		Stylesheets: stylesheets, | 
					
						
							|  |  |  | 		Javascript:  []string{jsFrontend}, | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 		Extra: map[string]any{ | 
					
						
							|  |  |  | 			"account":          targetAccount, | 
					
						
							|  |  |  | 			"rssFeed":          rssFeed, | 
					
						
							|  |  |  | 			"robotsMeta":       robotsMeta, | 
					
						
							|  |  |  | 			"statuses":         statusResp.Items, | 
					
						
							|  |  |  | 			"statuses_next":    statusResp.NextLink, | 
					
						
							|  |  |  | 			"pinned_statuses":  pinnedStatuses, | 
					
						
							|  |  |  | 			"show_back_to_top": paging, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apiutil.TemplateWebPage(c, page) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 14:33:01 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-06-13 16:47:56 +02:00
										 |  |  | 		apiutil.WebErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	b, err := json.Marshal(user) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		err := gtserror.Newf("could not marshal json: %w", err) | 
					
						
							| 
									
										
										
										
											2023-06-13 16:47:56 +02:00
										 |  |  | 		apiutil.WebErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.Data(http.StatusOK, accept, b) | 
					
						
							|  |  |  | } |