add a lil helper for generating user uris

This commit is contained in:
tsmethurst 2021-04-01 14:43:56 +02:00
commit dba33c2c5c
2 changed files with 57 additions and 16 deletions

View file

@ -37,6 +37,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db/model" "github.com/superseriousbusiness/gotosocial/internal/db/model"
"github.com/superseriousbusiness/gotosocial/internal/util"
"github.com/superseriousbusiness/gotosocial/pkg/mastotypes" "github.com/superseriousbusiness/gotosocial/pkg/mastotypes"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -104,14 +105,18 @@ func newPostgresService(ctx context.Context, c *config.Config, log *logrus.Entry
return nil, errors.New("db connection timeout") return nil, errors.New("db connection timeout")
} }
// we can confidently return this useable postgres service now ps := &postgresService{
return &postgresService{
config: c, config: c,
conn: conn, conn: conn,
log: log, log: log,
cancel: cancel, cancel: cancel,
federationDB: newPostgresFederation(conn), }
}, nil
federatingDB := newFederatingDB(ps, c)
ps.federationDB = federatingDB
// we can confidently return this useable postgres service now
return ps, nil
} }
/* /*
@ -437,17 +442,21 @@ func (ps *postgresService) NewSignup(username string, reason string, requireAppr
return nil, err return nil, err
} }
// should be something like https://example.org/@some_username uris := util.GenerateURIs(username, ps.config.Protocol, ps.config.Host)
url := fmt.Sprintf("%s://%s/@%s", ps.config.Protocol, ps.config.Host, username)
a := &model.Account{ a := &model.Account{
Username: username, Username: username,
DisplayName: username, DisplayName: username,
Reason: reason, Reason: reason,
URL: url, URL: uris.UserURL,
PrivateKey: key, PrivateKey: key,
PublicKey: &key.PublicKey, PublicKey: &key.PublicKey,
ActorType: "Person", ActorType: "Person",
URI: uris.UserURI,
InboxURL: uris.InboxURL,
OutboxURL: uris.OutboxURL,
FollowersURL: uris.FollowersURL,
FeaturedCollectionURL: uris.CollectionURL,
} }
if _, err = ps.conn.Model(a).Insert(); err != nil { if _, err = ps.conn.Model(a).Insert(); err != nil {
return nil, err return nil, err

32
internal/util/parse.go Normal file
View file

@ -0,0 +1,32 @@
package util
import "fmt"
type URIs struct {
HostURL string
UserURL string
UserURI string
InboxURL string
OutboxURL string
FollowersURL string
CollectionURL string
}
func GenerateURIs(username string, protocol string, host string) *URIs {
hostURL := fmt.Sprintf("%s://%s", protocol, host)
userURL := fmt.Sprintf("%s/@%s", hostURL, username)
userURI := fmt.Sprintf("%s/users/%s", hostURL, username)
inboxURL := fmt.Sprintf("%s/inbox", userURI)
outboxURL := fmt.Sprintf("%s/outbox", userURI)
followersURL := fmt.Sprintf("%s/followers", userURI)
collectionURL := fmt.Sprintf("%s/collections/featured", userURI)
return &URIs{
HostURL: hostURL,
UserURL: userURL,
UserURI: userURI,
InboxURL: inboxURL,
OutboxURL: outboxURL,
FollowersURL: followersURL,
CollectionURL: collectionURL,
}
}