| 
									
										
										
										
											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-07-06 13:29:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | package bundb | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtscontext" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/id" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/state" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/util" | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"github.com/uptrace/bun" | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | type instanceDB struct { | 
					
						
							| 
									
										
										
										
											2024-02-07 14:43:27 +00:00
										 |  |  | 	db    *bun.DB | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	state *state.State | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) CountInstanceUsers(ctx context.Context, domain string) (int, error) { | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 	localhost := (domain == config.GetHost() || domain == config.GetAccountDomain()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if localhost { | 
					
						
							|  |  |  | 		// Check for a cached instance user count, if so return this. | 
					
						
							|  |  |  | 		if n := i.state.Caches.DB.LocalInstance.Users.Load(); n != nil { | 
					
						
							|  |  |  | 			return *n, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := i.db. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		NewSelect(). | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")). | 
					
						
							|  |  |  | 		Column("account.id"). | 
					
						
							|  |  |  | 		Where("? != ?", bun.Ident("account.username"), domain). | 
					
						
							|  |  |  | 		Where("? IS NULL", bun.Ident("account.suspended_at")) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 	if localhost { | 
					
						
							| 
									
										
										
										
											2023-07-05 12:34:37 +02:00
										 |  |  | 		// If the domain is *this* domain, just | 
					
						
							|  |  |  | 		// count where the domain field is null. | 
					
						
							|  |  |  | 		q = q.Where("? IS NULL", bun.Ident("account.domain")) | 
					
						
							| 
									
										
										
										
											2021-08-29 12:03:08 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		q = q.Where("? = ?", bun.Ident("account.domain"), domain) | 
					
						
							| 
									
										
										
										
											2021-08-29 12:03:08 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-26 22:06:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	count, err := q.Count(ctx) | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return 0, err | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if localhost { | 
					
						
							|  |  |  | 		// Update cached instance users account value. | 
					
						
							|  |  |  | 		i.state.Caches.DB.LocalInstance.Users.Store(&count) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	return count, nil | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) CountInstanceStatuses(ctx context.Context, domain string) (int, error) { | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 	localhost := (domain == config.GetHost() || domain == config.GetAccountDomain()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if localhost { | 
					
						
							|  |  |  | 		// Check for a cached instance statuses count, if so return this. | 
					
						
							|  |  |  | 		if n := i.state.Caches.DB.LocalInstance.Statuses.Load(); n != nil { | 
					
						
							|  |  |  | 			return *n, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := i.db. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		NewSelect(). | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 	if localhost { | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 		// if the domain is *this* domain, just count where local is true | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		q = q.Where("? = ?", bun.Ident("status.local"), true) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		// join on the domain of the account | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		q = q. | 
					
						
							|  |  |  | 			Join("JOIN ? AS ? ON ? = ?", bun.Ident("accounts"), bun.Ident("account"), bun.Ident("account.id"), bun.Ident("status.account_id")). | 
					
						
							|  |  |  | 			Where("? = ?", bun.Ident("account.domain"), domain) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-24 11:49:37 +02:00
										 |  |  | 	// Ignore statuses that are currently pending approval. | 
					
						
							|  |  |  | 	q = q.Where("NOT ? = ?", bun.Ident("status.pending_approval"), true) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-21 10:06:06 +00:00
										 |  |  | 	// Ignore statuses that are direct messages. | 
					
						
							| 
									
										
										
										
											2024-11-25 13:48:59 +00:00
										 |  |  | 	q = q.Where("NOT ? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityDirect) | 
					
						
							| 
									
										
										
										
											2024-11-21 10:06:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	count, err := q.Count(ctx) | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return 0, err | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if localhost { | 
					
						
							|  |  |  | 		// Update cached instance statuses account value. | 
					
						
							|  |  |  | 		i.state.Caches.DB.LocalInstance.Statuses.Store(&count) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	return count, nil | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) CountInstanceDomains(ctx context.Context, domain string) (int, error) { | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 	localhost := (domain == config.GetHost() || domain == config.GetAccountDomain()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if localhost { | 
					
						
							|  |  |  | 		// Check for a cached instance domains count, if so return this. | 
					
						
							|  |  |  | 		if n := i.state.Caches.DB.LocalInstance.Domains.Load(); n != nil { | 
					
						
							|  |  |  | 			return *n, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := i.db. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		NewSelect(). | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		TableExpr("? AS ?", bun.Ident("instances"), bun.Ident("instance")) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 	if localhost { | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 		// if the domain is *this* domain, just count other instances it knows about | 
					
						
							| 
									
										
										
										
											2021-07-06 13:29:11 +02:00
										 |  |  | 		// exclude domains that are blocked | 
					
						
							| 
									
										
										
										
											2021-08-26 11:28:16 +02:00
										 |  |  | 		q = q. | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 			Where("? != ?", bun.Ident("instance.domain"), domain). | 
					
						
							|  |  |  | 			Where("? IS NULL", bun.Ident("instance.suspended_at")) | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		// TODO: implement federated domain counting properly for remote domains | 
					
						
							|  |  |  | 		return 0, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	count, err := q.Count(ctx) | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return 0, err | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if localhost { | 
					
						
							|  |  |  | 		// Update cached instance domains account value. | 
					
						
							|  |  |  | 		i.state.Caches.DB.LocalInstance.Domains.Store(&count) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	return count, nil | 
					
						
							| 
									
										
										
										
											2021-06-23 16:35:57 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) GetInstance(ctx context.Context, domain string) (*gtsmodel.Instance, error) { | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2025-01-14 14:23:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Normalize the domain as punycode | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	domain, err = util.Punify(domain) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, gtserror.Newf("error punifying domain %s: %w", domain, err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-14 17:11:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	return i.getInstance( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		"Domain", | 
					
						
							|  |  |  | 		func(instance *gtsmodel.Instance) error { | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 			return i.db.NewSelect(). | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 				Model(instance). | 
					
						
							|  |  |  | 				Where("? = ?", bun.Ident("instance.domain"), domain). | 
					
						
							|  |  |  | 				Scan(ctx) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		domain, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (i *instanceDB) GetInstanceByID(ctx context.Context, id string) (*gtsmodel.Instance, error) { | 
					
						
							|  |  |  | 	return i.getInstance( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		"ID", | 
					
						
							|  |  |  | 		func(instance *gtsmodel.Instance) error { | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 			return i.db.NewSelect(). | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 				Model(instance). | 
					
						
							|  |  |  | 				Where("? = ?", bun.Ident("instance.id"), id). | 
					
						
							|  |  |  | 				Scan(ctx) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		id, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) getInstance(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Instance) error, keyParts ...any) (*gtsmodel.Instance, error) { | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	// Fetch instance from database cache with loader callback | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	instance, err := i.state.Caches.DB.Instance.LoadOne(lookup, func() (*gtsmodel.Instance, error) { | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		var instance gtsmodel.Instance | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Not cached! Perform database query. | 
					
						
							|  |  |  | 		if err := dbQuery(&instance); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-19 14:33:15 +02:00
										 |  |  | 		if instance.Domain == config.GetHost() { | 
					
						
							|  |  |  | 			// also populate Rules | 
					
						
							|  |  |  | 			rules, err := i.state.DB.GetActiveRules(ctx) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				log.Error(ctx, err) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				instance.Rules = rules | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		return &instance, nil | 
					
						
							|  |  |  | 	}, keyParts...) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if gtscontext.Barebones(ctx) { | 
					
						
							|  |  |  | 		// no need to fully populate. | 
					
						
							|  |  |  | 		return instance, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Further populate the instance fields where applicable. | 
					
						
							| 
									
										
										
										
											2023-11-27 16:39:44 +01:00
										 |  |  | 	if err := i.PopulateInstance(ctx, instance); err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2023-03-14 17:11:04 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return instance, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 16:39:44 +01:00
										 |  |  | func (i *instanceDB) PopulateInstance(ctx context.Context, instance *gtsmodel.Instance) error { | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	var ( | 
					
						
							|  |  |  | 		err  error | 
					
						
							| 
									
										
										
										
											2023-08-02 17:21:46 +02:00
										 |  |  | 		errs = gtserror.NewMultiError(2) | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if instance.DomainBlockID != "" && instance.DomainBlock == nil { | 
					
						
							|  |  |  | 		// Instance domain block is not set, fetch from database. | 
					
						
							|  |  |  | 		instance.DomainBlock, err = i.state.DB.GetDomainBlock( | 
					
						
							|  |  |  | 			gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 			instance.Domain, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-02 17:21:46 +02:00
										 |  |  | 			errs.Appendf("error populating instance domain block: %w", err) | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if instance.ContactAccountID != "" && instance.ContactAccount == nil { | 
					
						
							|  |  |  | 		// Instance domain block is not set, fetch from database. | 
					
						
							|  |  |  | 		instance.ContactAccount, err = i.state.DB.GetAccountByID( | 
					
						
							|  |  |  | 			gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 			instance.ContactAccountID, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-02 17:21:46 +02:00
										 |  |  | 			errs.Appendf("error populating instance contact account: %w", err) | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 19:14:33 +02:00
										 |  |  | 	return errs.Combine() | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (i *instanceDB) PutInstance(ctx context.Context, instance *gtsmodel.Instance) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-14 14:23:18 +00:00
										 |  |  | 	// Normalize the domain as punycode, note the extra | 
					
						
							|  |  |  | 	// validation step for domain name write operations. | 
					
						
							|  |  |  | 	instance.Domain, err = util.PunifySafely(instance.Domain) | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return gtserror.Newf("error punifying domain %s: %w", instance.Domain, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 11:53:42 +00:00
										 |  |  | 	// Store the new instance model in database, invalidating cache. | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	return i.state.Caches.DB.Instance.Store(instance, func() error { | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 		_, err := i.db.NewInsert().Model(instance).Exec(ctx) | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (i *instanceDB) UpdateInstance(ctx context.Context, instance *gtsmodel.Instance, columns ...string) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2025-01-14 14:23:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Normalize the domain as punycode, note the extra | 
					
						
							|  |  |  | 	// validation step for domain name write operations. | 
					
						
							|  |  |  | 	instance.Domain, err = util.PunifySafely(instance.Domain) | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return gtserror.Newf("error punifying domain %s: %w", instance.Domain, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update the instance's last-updated | 
					
						
							|  |  |  | 	instance.UpdatedAt = time.Now() | 
					
						
							|  |  |  | 	if len(columns) != 0 { | 
					
						
							|  |  |  | 		columns = append(columns, "updated_at") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	return i.state.Caches.DB.Instance.Store(instance, func() error { | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 		_, err := i.db. | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 			NewUpdate(). | 
					
						
							|  |  |  | 			Model(instance). | 
					
						
							|  |  |  | 			Where("? = ?", bun.Ident("instance.id"), instance.ID). | 
					
						
							|  |  |  | 			Column(columns...). | 
					
						
							|  |  |  | 			Exec(ctx) | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) GetInstancePeers(ctx context.Context, includeSuspended bool) ([]*gtsmodel.Instance, error) { | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	instanceIDs := []string{} | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := i.db. | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 		NewSelect(). | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		TableExpr("? AS ?", bun.Ident("instances"), bun.Ident("instance")). | 
					
						
							|  |  |  | 		// Select just the IDs of each instance. | 
					
						
							|  |  |  | 		Column("instance.id"). | 
					
						
							|  |  |  | 		// Exclude our own instance. | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		Where("? != ?", bun.Ident("instance.domain"), config.GetHost()) | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if !includeSuspended { | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		q = q.Where("? IS NULL", bun.Ident("instance.suspended_at")) | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	if err := q.Scan(ctx, &instanceIDs); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	if len(instanceIDs) == 0 { | 
					
						
							|  |  |  | 		return make([]*gtsmodel.Instance, 0), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	instances := make([]*gtsmodel.Instance, 0, len(instanceIDs)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, id := range instanceIDs { | 
					
						
							|  |  |  | 		// Select each instance by its ID. | 
					
						
							|  |  |  | 		instance, err := i.GetInstanceByID(ctx, id) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			log.Errorf(ctx, "error getting instance %q: %v", id, err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Append to return slice. | 
					
						
							|  |  |  | 		instances = append(instances, instance) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 16:54:54 +02:00
										 |  |  | 	return instances, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) GetInstanceAccounts(ctx context.Context, domain string, maxID string, limit int) ([]*gtsmodel.Account, error) { | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	// Ensure reasonable | 
					
						
							|  |  |  | 	if limit < 0 { | 
					
						
							|  |  |  | 		limit = 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2025-01-14 14:23:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Normalize the domain as punycode | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	domain, err = util.Punify(domain) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, gtserror.Newf("error punifying domain %s: %w", domain, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make educated guess for slice size | 
					
						
							|  |  |  | 	accountIDs := make([]string, 0, limit) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := i.db. | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 		NewSelect(). | 
					
						
							|  |  |  | 		TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")). | 
					
						
							|  |  |  | 		// Select just the account ID. | 
					
						
							|  |  |  | 		Column("account.id"). | 
					
						
							|  |  |  | 		// Select accounts belonging to given domain. | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 		Where("? = ?", bun.Ident("account.domain"), domain). | 
					
						
							|  |  |  | 		Order("account.id DESC") | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	if maxID == "" { | 
					
						
							|  |  |  | 		maxID = id.Highest | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	q = q.Where("? < ?", bun.Ident("account.id"), maxID) | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if limit > 0 { | 
					
						
							|  |  |  | 		q = q.Limit(limit) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	if err := q.Scan(ctx, &accountIDs); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-18 17:44:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	// Catch case of no accounts early. | 
					
						
							|  |  |  | 	count := len(accountIDs) | 
					
						
							|  |  |  | 	if count == 0 { | 
					
						
							| 
									
										
										
										
											2022-04-18 17:44:36 +02:00
										 |  |  | 		return nil, db.ErrNoEntries | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:34:12 +02:00
										 |  |  | 	// Select each account by its ID. | 
					
						
							|  |  |  | 	accounts := make([]*gtsmodel.Account, 0, count) | 
					
						
							|  |  |  | 	for _, id := range accountIDs { | 
					
						
							|  |  |  | 		account, err := i.state.DB.GetAccountByID(ctx, id) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			log.Errorf(ctx, "error getting account %q: %v", id, err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Append to return slice. | 
					
						
							|  |  |  | 		accounts = append(accounts, account) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	return accounts, nil | 
					
						
							| 
									
										
										
										
											2021-07-05 13:23:03 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-03-19 13:11:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (i *instanceDB) GetInstanceModeratorAddresses(ctx context.Context) ([]string, error) { | 
					
						
							| 
									
										
										
										
											2023-03-19 13:11:46 +01:00
										 |  |  | 	addresses := []string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Select email addresses of approved, confirmed, | 
					
						
							|  |  |  | 	// and enabled moderators or admins. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := i.db. | 
					
						
							| 
									
										
										
										
											2023-03-19 13:11:46 +01:00
										 |  |  | 		NewSelect(). | 
					
						
							|  |  |  | 		TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")). | 
					
						
							|  |  |  | 		Column("user.email"). | 
					
						
							|  |  |  | 		Where("? = ?", bun.Ident("user.approved"), true). | 
					
						
							|  |  |  | 		Where("? IS NOT NULL", bun.Ident("user.confirmed_at")). | 
					
						
							|  |  |  | 		Where("? = ?", bun.Ident("user.disabled"), false). | 
					
						
							|  |  |  | 		WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery { | 
					
						
							|  |  |  | 			return q. | 
					
						
							|  |  |  | 				Where("? = ?", bun.Ident("user.moderator"), true). | 
					
						
							|  |  |  | 				WhereOr("? = ?", bun.Ident("user.admin"), true) | 
					
						
							|  |  |  | 		}). | 
					
						
							|  |  |  | 		OrderExpr("? ASC", bun.Ident("user.email")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := q.Scan(ctx, &addresses); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2023-03-19 13:11:46 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(addresses) == 0 { | 
					
						
							|  |  |  | 		return nil, db.ErrNoEntries | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return addresses, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-04-11 11:45:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (i *instanceDB) GetInstanceModerators(ctx context.Context) ([]*gtsmodel.Account, error) { | 
					
						
							|  |  |  | 	accountIDs := []string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Select account IDs of approved, confirmed, | 
					
						
							|  |  |  | 	// and enabled moderators or admins. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	q := i.db. | 
					
						
							|  |  |  | 		NewSelect(). | 
					
						
							|  |  |  | 		TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")). | 
					
						
							|  |  |  | 		Column("user.account_id"). | 
					
						
							|  |  |  | 		Where("? = ?", bun.Ident("user.approved"), true). | 
					
						
							|  |  |  | 		Where("? IS NOT NULL", bun.Ident("user.confirmed_at")). | 
					
						
							|  |  |  | 		Where("? = ?", bun.Ident("user.disabled"), false). | 
					
						
							|  |  |  | 		WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery { | 
					
						
							|  |  |  | 			return q. | 
					
						
							|  |  |  | 				Where("? = ?", bun.Ident("user.moderator"), true). | 
					
						
							|  |  |  | 				WhereOr("? = ?", bun.Ident("user.admin"), true) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := q.Scan(ctx, &accountIDs); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(accountIDs) == 0 { | 
					
						
							|  |  |  | 		return nil, db.ErrNoEntries | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return i.state.DB.GetAccountsByIDs(ctx, accountIDs) | 
					
						
							|  |  |  | } |