summaryrefslogtreecommitdiff
path: root/internal/db/pg.go
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-05-09 14:06:06 +0200
committerLibravatar GitHub <noreply@github.com>2021-05-09 14:06:06 +0200
commit3363e0ebdd2ad8bde458037b82432bc3dd93adde (patch)
tree3b105083f75aa47f3872b588403d9e5cf2f45d14 /internal/db/pg.go
parentLetsencrypt (#17) (diff)
downloadgotosocial-3363e0ebdd2ad8bde458037b82432bc3dd93adde.tar.xz
add api/v1/instance info handler + instance model (#18)
Diffstat (limited to 'internal/db/pg.go')
-rw-r--r--internal/db/pg.go47
1 files changed, 42 insertions, 5 deletions
diff --git a/internal/db/pg.go b/internal/db/pg.go
index 647285032..f59103af7 100644
--- a/internal/db/pg.go
+++ b/internal/db/pg.go
@@ -307,17 +307,54 @@ func (ps *postgresService) DeleteWhere(key string, value interface{}, i interfac
func (ps *postgresService) CreateInstanceAccount() error {
username := ps.config.Host
- instanceAccount := &gtsmodel.Account{
- Username: username,
+ key, err := rsa.GenerateKey(rand.Reader, 2048)
+ if err != nil {
+ ps.log.Errorf("error creating new rsa key: %s", err)
+ return err
+ }
+
+ newAccountURIs := util.GenerateURIsForAccount(username, ps.config.Protocol, ps.config.Host)
+ a := &gtsmodel.Account{
+ Username: ps.config.Host,
+ DisplayName: username,
+ URL: newAccountURIs.UserURL,
+ PrivateKey: key,
+ PublicKey: &key.PublicKey,
+ PublicKeyURI: newAccountURIs.PublicKeyURI,
+ ActorType: gtsmodel.ActivityStreamsPerson,
+ URI: newAccountURIs.UserURI,
+ InboxURI: newAccountURIs.InboxURI,
+ OutboxURI: newAccountURIs.OutboxURI,
+ FollowersURI: newAccountURIs.FollowersURI,
+ FollowingURI: newAccountURIs.FollowingURI,
+ FeaturedCollectionURI: newAccountURIs.CollectionURI,
+ }
+ inserted, err := ps.conn.Model(a).Where("username = ?", username).SelectOrInsert()
+ if err != nil {
+ return err
+ }
+ if inserted {
+ ps.log.Infof("created instance account %s with id %s", username, a.ID)
+ } else {
+ ps.log.Infof("instance account %s already exists with id %s", username, a.ID)
+ }
+ return nil
+}
+
+func (ps *postgresService) CreateInstanceInstance() error {
+ i := &gtsmodel.Instance{
+ Domain: ps.config.Host,
+ Title: ps.config.Host,
+ URI: fmt.Sprintf("%s://%s", ps.config.Protocol, ps.config.Host),
}
- inserted, err := ps.conn.Model(instanceAccount).Where("username = ?", username).SelectOrInsert()
+ inserted, err := ps.conn.Model(i).Where("domain = ?", ps.config.Host).SelectOrInsert()
if err != nil {
return err
}
if inserted {
- ps.log.Infof("created instance account %s with id %s", username, instanceAccount.ID)
+ ps.log.Infof("created instance instance %s with id %s", ps.config.Host, i.ID)
} else {
- ps.log.Infof("instance account %s already exists with id %s", username, instanceAccount.ID)
+ ps.log.Infof("instance instance %s already exists with id %s", ps.config.Host, i.ID)
}
return nil
}