summaryrefslogtreecommitdiff
path: root/internal/db/bundb/admin.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2024-04-11 11:45:53 +0200
committerLibravatar GitHub <noreply@github.com>2024-04-11 11:45:53 +0200
commit9fb8a78f91adffd5f4d28df1270e407c25a7a16e (patch)
treed68200744e28d07e75a52bb0c9f6593c86a38a91 /internal/db/bundb/admin.go
parent[performance] massively improved ActivityPub delivery worker efficiency (#2812) (diff)
downloadgotosocial-9fb8a78f91adffd5f4d28df1270e407c25a7a16e.tar.xz
[feature] New user sign-up via web page (#2796)
* [feature] User sign-up form and admin notifs * add chosen + filtered languages to migration * remove stray comment * chosen languages schmosen schmanguages * proper error on local account missing
Diffstat (limited to 'internal/db/bundb/admin.go')
-rw-r--r--internal/db/bundb/admin.go110
1 files changed, 109 insertions, 1 deletions
diff --git a/internal/db/bundb/admin.go b/internal/db/bundb/admin.go
index 832db1d8f..e52467b9b 100644
--- a/internal/db/bundb/admin.go
+++ b/internal/db/bundb/admin.go
@@ -27,6 +27,7 @@ import (
"strings"
"time"
+ "github.com/google/uuid"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -121,7 +122,6 @@ func (a *adminDB) NewSignup(ctx context.Context, newSignup gtsmodel.NewSignup) (
settings := &gtsmodel.AccountSettings{
AccountID: accountID,
- Reason: newSignup.Reason,
Privacy: gtsmodel.VisibilityDefault,
}
@@ -197,6 +197,7 @@ func (a *adminDB) NewSignup(ctx context.Context, newSignup gtsmodel.NewSignup) (
Account: account,
EncryptedPassword: string(encryptedPassword),
SignUpIP: newSignup.SignUpIP.To4(),
+ Reason: newSignup.Reason,
Locale: newSignup.Locale,
UnconfirmedEmail: newSignup.Email,
CreatedByApplicationID: newSignup.AppID,
@@ -331,6 +332,113 @@ func (a *adminDB) CreateInstanceInstance(ctx context.Context) error {
return nil
}
+func (a *adminDB) CreateInstanceApplication(ctx context.Context) error {
+ // Check if instance application already exists.
+ // Instance application client_id always = the
+ // instance account's ID so this is an easy check.
+ instanceAcct, err := a.state.DB.GetInstanceAccount(ctx, "")
+ if err != nil {
+ return err
+ }
+
+ exists, err := exists(
+ ctx,
+ a.db.
+ NewSelect().
+ Column("application.id").
+ TableExpr("? AS ?", bun.Ident("applications"), bun.Ident("application")).
+ Where("? = ?", bun.Ident("application.client_id"), instanceAcct.ID),
+ )
+ if err != nil {
+ return err
+ }
+
+ if exists {
+ log.Infof(ctx, "instance application already exists")
+ return nil
+ }
+
+ // Generate new IDs for this
+ // application and its client.
+ protocol := config.GetProtocol()
+ host := config.GetHost()
+ url := protocol + "://" + host
+
+ clientID := instanceAcct.ID
+ clientSecret := uuid.NewString()
+ appID, err := id.NewRandomULID()
+ if err != nil {
+ return err
+ }
+
+ // Generate the application
+ // to put in the database.
+ app := &gtsmodel.Application{
+ ID: appID,
+ Name: host + " instance application",
+ Website: url,
+ RedirectURI: url,
+ ClientID: clientID,
+ ClientSecret: clientSecret,
+ Scopes: "write:accounts",
+ }
+
+ // Store it.
+ if err := a.state.DB.PutApplication(ctx, app); err != nil {
+ return err
+ }
+
+ // Model an oauth client
+ // from the application.
+ oc := &gtsmodel.Client{
+ ID: clientID,
+ Secret: clientSecret,
+ Domain: url,
+ }
+
+ // Store it.
+ return a.state.DB.Put(ctx, oc)
+}
+
+func (a *adminDB) GetInstanceApplication(ctx context.Context) (*gtsmodel.Application, error) {
+ // Instance app clientID == instanceAcct.ID,
+ // so get the instance account first.
+ instanceAcct, err := a.state.DB.GetInstanceAccount(ctx, "")
+ if err != nil {
+ return nil, err
+ }
+
+ app := new(gtsmodel.Application)
+ if err := a.db.
+ NewSelect().
+ Model(app).
+ Where("? = ?", bun.Ident("application.client_id"), instanceAcct.ID).
+ Scan(ctx); err != nil {
+ return nil, err
+ }
+
+ return app, nil
+}
+
+func (a *adminDB) CountApprovedSignupsSince(ctx context.Context, since time.Time) (int, error) {
+ return a.db.
+ NewSelect().
+ TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")).
+ Where("? > ?", bun.Ident("user.created_at"), since).
+ Where("? = ?", bun.Ident("user.approved"), true).
+ Count(ctx)
+}
+
+func (a *adminDB) CountUnhandledSignups(ctx context.Context) (int, error) {
+ return a.db.
+ NewSelect().
+ TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")).
+ // Approved is false by default.
+ // Explicitly rejected sign-ups end up elsewhere.
+ Where("? = ?", bun.Ident("user.approved"), false).
+ Count(ctx)
+}
+
/*
ACTION FUNCS
*/