| 
									
										
										
										
											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/>. | 
					
						
							| 
									
										
										
										
											2021-05-17 19:06:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 13:12:00 +02:00
										 |  |  | package processing | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2021-05-21 15:48:26 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2023-05-07 19:53:21 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2021-07-26 20:25:54 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/text" | 
					
						
							| 
									
										
										
										
											2024-12-23 17:54:44 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/typeutils" | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/util" | 
					
						
							| 
									
										
										
										
											2021-09-01 18:29:25 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/validate" | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func (p *Processor) InstanceGetV1(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 	i, err := p.getThisInstance(ctx) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance: %s", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	ai, err := p.converter.InstanceToAPIV1Instance(ctx, i) | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ai, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func (p *Processor) InstanceGetV2(ctx context.Context) (*apimodel.InstanceV2, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 	i, err := p.getThisInstance(ctx) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance: %s", err)) | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	ai, err := p.converter.InstanceToAPIV2Instance(ctx, i) | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err)) | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 20:34:27 +02:00
										 |  |  | 	return ai, nil | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func (p *Processor) InstancePeersGet(ctx context.Context, includeSuspended bool, includeOpen bool, flat bool) (interface{}, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 	domains := []*apimodel.Domain{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if includeOpen { | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 		instances, err := p.state.DB.GetInstancePeers(ctx, false) | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 		if err != nil && err != db.ErrNoEntries { | 
					
						
							|  |  |  | 			err = fmt.Errorf("error selecting instance peers: %s", err) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, i := range instances { | 
					
						
							| 
									
										
										
										
											2023-05-07 19:53:21 +02:00
										 |  |  | 			// Domain may be in Punycode, | 
					
						
							|  |  |  | 			// de-punify it just in case. | 
					
						
							|  |  |  | 			d, err := util.DePunify(i.Domain) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				log.Errorf(ctx, "couldn't depunify domain %s: %s", i.Domain, err) | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			domains = append(domains, &apimodel.Domain{Domain: d}) | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if includeSuspended { | 
					
						
							|  |  |  | 		domainBlocks := []*gtsmodel.DomainBlock{} | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 		if err := p.state.DB.GetAll(ctx, &domainBlocks); err != nil && err != db.ErrNoEntries { | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 19:53:21 +02:00
										 |  |  | 		for _, domainBlock := range domainBlocks { | 
					
						
							|  |  |  | 			// Domain may be in Punycode, | 
					
						
							|  |  |  | 			// de-punify it just in case. | 
					
						
							|  |  |  | 			d, err := util.DePunify(domainBlock.Domain) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				log.Errorf(ctx, "couldn't depunify domain %s: %s", domainBlock.Domain, err) | 
					
						
							|  |  |  | 				continue | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 19:53:21 +02:00
										 |  |  | 			if *domainBlock.Obfuscate { | 
					
						
							|  |  |  | 				// Obfuscate the de-punified version. | 
					
						
							|  |  |  | 				d = obfuscate(d) | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-05-07 19:53:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			domains = append(domains, &apimodel.Domain{ | 
					
						
							|  |  |  | 				Domain:        d, | 
					
						
							|  |  |  | 				SuspendedAt:   util.FormatISO8601(domainBlock.CreatedAt), | 
					
						
							|  |  |  | 				PublicComment: domainBlock.PublicComment, | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sort.Slice(domains, func(i, j int) bool { | 
					
						
							|  |  |  | 		return domains[i].Domain < domains[j].Domain | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if flat { | 
					
						
							|  |  |  | 		flattened := []string{} | 
					
						
							|  |  |  | 		for _, d := range domains { | 
					
						
							|  |  |  | 			flattened = append(flattened, d.Domain) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return flattened, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return domains, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-19 14:33:15 +02:00
										 |  |  | func (p *Processor) InstanceGetRules(ctx context.Context) ([]apimodel.InstanceRule, gtserror.WithCode) { | 
					
						
							|  |  |  | 	i, err := p.getThisInstance(ctx) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance: %s", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 17:54:44 +00:00
										 |  |  | 	return typeutils.InstanceRulesToAPIRules(i.Rules), nil | 
					
						
							| 
									
										
										
										
											2023-08-19 14:33:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func (p *Processor) InstancePatch(ctx context.Context, form *apimodel.InstanceSettingsUpdateRequest) (*apimodel.InstanceV1, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Fetch this instance from the db for processing. | 
					
						
							|  |  |  | 	instance, err := p.getThisInstance(ctx) | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		err = fmt.Errorf("db error fetching instance: %w", err) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Fetch this instance account from the db for processing. | 
					
						
							|  |  |  | 	instanceAcc, err := p.state.DB.GetInstanceAccount(ctx, "") | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		err = fmt.Errorf("db error fetching instance account: %w", err) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Columns to update | 
					
						
							|  |  |  | 	// in the database. | 
					
						
							|  |  |  | 	var columns []string | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Validate & update site | 
					
						
							|  |  |  | 	// title if set on the form. | 
					
						
							| 
									
										
										
										
											2021-07-08 15:05:19 +02:00
										 |  |  | 	if form.Title != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		title := *form.Title | 
					
						
							|  |  |  | 		if err := validate.SiteTitle(title); err != nil { | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Don't allow html in site title. | 
					
						
							|  |  |  | 		instance.Title = text.SanitizeToPlaintext(title) | 
					
						
							|  |  |  | 		columns = append(columns, "title") | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Validate & update site contact | 
					
						
							|  |  |  | 	// account if set on the form. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// Empty username unsets contact. | 
					
						
							| 
									
										
										
										
											2021-07-08 15:05:19 +02:00
										 |  |  | 	if form.ContactUsername != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		contactAccountID, err := p.contactAccountIDForUsername(ctx, *form.ContactUsername) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:46:11 +02:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		columns = append(columns, "contact_account_id") | 
					
						
							|  |  |  | 		instance.ContactAccountID = contactAccountID | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Validate & update contact | 
					
						
							|  |  |  | 	// email if set on the form. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// Empty email unsets contact. | 
					
						
							| 
									
										
										
										
											2021-07-08 15:05:19 +02:00
										 |  |  | 	if form.ContactEmail != nil { | 
					
						
							| 
									
										
										
										
											2022-06-24 10:43:21 +02:00
										 |  |  | 		contactEmail := *form.ContactEmail | 
					
						
							|  |  |  | 		if contactEmail != "" { | 
					
						
							|  |  |  | 			if err := validate.Email(contactEmail); err != nil { | 
					
						
							|  |  |  | 				return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		columns = append(columns, "contact_email") | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		instance.ContactEmail = contactEmail | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Validate & update site short | 
					
						
							|  |  |  | 	// description if set on the form. | 
					
						
							| 
									
										
										
										
											2021-07-08 15:05:19 +02:00
										 |  |  | 	if form.ShortDescription != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		shortDescription := *form.ShortDescription | 
					
						
							|  |  |  | 		if err := validate.SiteShortDescription(shortDescription); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Parse description as Markdown, keep | 
					
						
							|  |  |  | 		// the raw version for later editing. | 
					
						
							|  |  |  | 		instance.ShortDescriptionText = shortDescription | 
					
						
							|  |  |  | 		instance.ShortDescription = p.formatter.FromMarkdown(ctx, p.parseMentionFunc, instanceAcc.ID, "", shortDescription).HTML | 
					
						
							|  |  |  | 		columns = append(columns, []string{"short_description", "short_description_text"}...) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// validate & update site description if it's set on the form | 
					
						
							| 
									
										
										
										
											2021-07-08 15:05:19 +02:00
										 |  |  | 	if form.Description != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		description := *form.Description | 
					
						
							|  |  |  | 		if err := validate.SiteDescription(description); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Parse description as Markdown, keep | 
					
						
							|  |  |  | 		// the raw version for later editing. | 
					
						
							|  |  |  | 		instance.DescriptionText = description | 
					
						
							|  |  |  | 		instance.Description = p.formatter.FromMarkdown(ctx, p.parseMentionFunc, instanceAcc.ID, "", description).HTML | 
					
						
							|  |  |  | 		columns = append(columns, []string{"description", "description_text"}...) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-02 06:24:48 -05:00
										 |  |  | 	// validate & update site custom css if it's set on the form | 
					
						
							|  |  |  | 	if form.CustomCSS != nil { | 
					
						
							|  |  |  | 		customCSS := *form.CustomCSS | 
					
						
							|  |  |  | 		if err := validate.InstanceCustomCSS(customCSS); err != nil { | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		instance.CustomCSS = text.SanitizeToPlaintext(customCSS) | 
					
						
							|  |  |  | 		columns = append(columns, []string{"custom_css"}...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	// Validate & update site | 
					
						
							|  |  |  | 	// terms if set on the form. | 
					
						
							| 
									
										
										
										
											2021-07-08 15:05:19 +02:00
										 |  |  | 	if form.Terms != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		terms := *form.Terms | 
					
						
							|  |  |  | 		if err := validate.SiteTerms(terms); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Parse terms as Markdown, keep | 
					
						
							|  |  |  | 		// the raw version for later editing. | 
					
						
							|  |  |  | 		instance.TermsText = terms | 
					
						
							|  |  |  | 		instance.Terms = p.formatter.FromMarkdown(ctx, p.parseMentionFunc, "", "", terms).HTML | 
					
						
							|  |  |  | 		columns = append(columns, []string{"terms", "terms_text"}...) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 	var updateInstanceAccount bool | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	if form.Avatar != nil && form.Avatar.Size != 0 { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		// Process instance avatar image + description. | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		avatarInfo, errWithCode := p.account.UpdateAvatar(ctx, | 
					
						
							|  |  |  | 			instanceAcc, | 
					
						
							|  |  |  | 			form.Avatar, | 
					
						
							|  |  |  | 			form.AvatarDescription, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if errWithCode != nil { | 
					
						
							|  |  |  | 			return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		instanceAcc.AvatarMediaAttachmentID = avatarInfo.ID | 
					
						
							|  |  |  | 		instanceAcc.AvatarMediaAttachment = avatarInfo | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 		updateInstanceAccount = true | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	} else if form.AvatarDescription != nil && instanceAcc.AvatarMediaAttachment != nil { | 
					
						
							|  |  |  | 		// Process just the description for the existing avatar. | 
					
						
							|  |  |  | 		instanceAcc.AvatarMediaAttachment.Description = *form.AvatarDescription | 
					
						
							|  |  |  | 		if err := p.state.DB.UpdateAttachment(ctx, instanceAcc.AvatarMediaAttachment, "description"); err != nil { | 
					
						
							|  |  |  | 			err = fmt.Errorf("db error updating instance avatar description: %w", err) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2023-02-04 15:53:11 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if form.Header != nil && form.Header.Size != 0 { | 
					
						
							| 
									
										
										
										
											2023-02-04 15:53:11 +01:00
										 |  |  | 		// process instance header image | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		headerInfo, errWithCode := p.account.UpdateHeader(ctx, | 
					
						
							|  |  |  | 			instanceAcc, | 
					
						
							|  |  |  | 			form.Header, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if errWithCode != nil { | 
					
						
							|  |  |  | 			return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		instanceAcc.HeaderMediaAttachmentID = headerInfo.ID | 
					
						
							|  |  |  | 		instanceAcc.HeaderMediaAttachment = headerInfo | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 		updateInstanceAccount = true | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 	if updateInstanceAccount { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		// If either avatar or header is updated, we need | 
					
						
							|  |  |  | 		// to update the instance account that stores them. | 
					
						
							|  |  |  | 		if err := p.state.DB.UpdateAccount(ctx, instanceAcc); err != nil { | 
					
						
							|  |  |  | 			err = fmt.Errorf("db error updating instance account: %w", err) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorInternalError(err, err.Error()) | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	if len(columns) != 0 { | 
					
						
							|  |  |  | 		if err := p.state.DB.UpdateInstance(ctx, instance, columns...); err != nil { | 
					
						
							|  |  |  | 			err = fmt.Errorf("db error updating instance: %w", err) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorInternalError(err, err.Error()) | 
					
						
							| 
									
										
										
										
											2022-11-08 18:11:06 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	return p.InstanceGetV1(ctx) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *Processor) getThisInstance(ctx context.Context) (*gtsmodel.Instance, error) { | 
					
						
							|  |  |  | 	instance, err := p.state.DB.GetInstance(ctx, config.GetHost()) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 13:39:31 +01:00
										 |  |  | 	return instance, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *Processor) contactAccountIDForUsername(ctx context.Context, username string) (string, error) { | 
					
						
							|  |  |  | 	if username == "" { | 
					
						
							|  |  |  | 		// Easy: unset | 
					
						
							|  |  |  | 		// contact account. | 
					
						
							|  |  |  | 		return "", nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make sure local account with the given username exists in the db. | 
					
						
							|  |  |  | 	contactAccount, err := p.state.DB.GetAccountByUsernameDomain(ctx, username, "") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		err = fmt.Errorf("db error getting selected contact account with username %s: %w", username, err) | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make sure account corresponds to a user. | 
					
						
							|  |  |  | 	contactUser, err := p.state.DB.GetUserByAccountID(ctx, contactAccount.ID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		err = fmt.Errorf("db error getting user for selected contact account %s: %w", username, err) | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Ensure account/user is: | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// - confirmed and approved | 
					
						
							|  |  |  | 	// - not suspended | 
					
						
							|  |  |  | 	// - an admin or a moderator | 
					
						
							|  |  |  | 	if contactUser.ConfirmedAt.IsZero() { | 
					
						
							|  |  |  | 		err := fmt.Errorf("user of selected contact account %s is not confirmed", contactAccount.Username) | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !*contactUser.Approved { | 
					
						
							|  |  |  | 		err := fmt.Errorf("user of selected contact account %s is not approved", contactAccount.Username) | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !contactAccount.SuspendedAt.IsZero() { | 
					
						
							|  |  |  | 		err := fmt.Errorf("selected contact account %s is suspended", contactAccount.Username) | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !*contactUser.Admin && !*contactUser.Moderator { | 
					
						
							|  |  |  | 		err := fmt.Errorf("user of selected contact account %s is neither admin nor moderator", contactAccount.Username) | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// All good! | 
					
						
							|  |  |  | 	return contactAccount.ID, nil | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func obfuscate(domain string) string { | 
					
						
							|  |  |  | 	obfuscated := make([]rune, len(domain)) | 
					
						
							|  |  |  | 	for i, r := range domain { | 
					
						
							|  |  |  | 		if i%3 == 1 || i%5 == 1 { | 
					
						
							|  |  |  | 			obfuscated[i] = '*' | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			obfuscated[i] = r | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return string(obfuscated) | 
					
						
							|  |  |  | } |