diff options
author | 2021-08-26 18:55:39 +0200 | |
---|---|---|
committer | 2021-08-26 18:55:39 +0200 | |
commit | 1582cf8bad94890a1957595a0fd51bb128941280 (patch) | |
tree | be4ec777fa7f49dc7f272c4492ea92ace62fea41 /internal/db/bundb/session.go | |
parent | oops (#157) (diff) | |
download | gotosocial-1582cf8bad94890a1957595a0fd51bb128941280.tar.xz |
fix null returned session (#159)
Diffstat (limited to 'internal/db/bundb/session.go')
-rw-r--r-- | internal/db/bundb/session.go | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/internal/db/bundb/session.go b/internal/db/bundb/session.go index 87e20673d..55efd21d4 100644 --- a/internal/db/bundb/session.go +++ b/internal/db/bundb/session.go @@ -21,6 +21,7 @@ package bundb import ( "context" "crypto/rand" + "errors" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" @@ -37,21 +38,34 @@ type sessionDB struct { } func (s *sessionDB) GetSession(ctx context.Context) (*gtsmodel.RouterSession, db.Error) { - rs := new(gtsmodel.RouterSession) + rss := []*gtsmodel.RouterSession{} - q := s.conn. + _, err := s.conn. NewSelect(). - Model(rs). - Limit(1) + Model(&rss). + Limit(1). + Order("id DESC"). + Exec(ctx) + if err != nil { + return nil, processErrorResponse(err) + } - _, err := q.Exec(ctx) + if len(rss) <= 0 { + // no session created yet, so make one + return s.createSession(ctx) + } - err = processErrorResponse(err) + if len(rss) != 1 { + // we asked for 1 so we should get 1 + return nil, errors.New("more than 1 router session was returned") + } - return rs, err + // return the one session found + rs := rss[0] + return rs, nil } -func (s *sessionDB) CreateSession(ctx context.Context) (*gtsmodel.RouterSession, db.Error) { +func (s *sessionDB) createSession(ctx context.Context) (*gtsmodel.RouterSession, db.Error) { auth := make([]byte, 32) crypt := make([]byte, 32) |