Load users from the database during startup

This commit is contained in:
Gary Kramlich
2022-01-05 14:59:35 -06:00
parent aa7059b1e3
commit de1f524e25
5 changed files with 60 additions and 8 deletions

View File

@@ -21,10 +21,10 @@ type User struct {
Session *discordgo.Session
}
// Login is just used to create the session and update the database and should
// only be called by bridge.User.Login which will continue setting up event
// handlers.
func (u *User) Login(token string) error {
// NewSession is just used to create the session and update the database. It
// should only be called by bridge.User.Connect which will continue setting up
// event handlers and everything else.
func (u *User) NewSession(token string) error {
session, err := discordgo.New(token)
if err != nil {
return err
@@ -50,7 +50,7 @@ func (u *User) Scan(row Scannable) *User {
}
if token.Valid {
if err := u.Login(token.String); err != nil {
if err := u.NewSession(token.String); err != nil {
u.log.Errorln("Failed to login: ", err)
}
}

View File

@@ -25,3 +25,19 @@ func (uq *UserQuery) GetByMXID(userID id.UserID) *User {
return uq.New().Scan(row)
}
func (uq *UserQuery) GetAll() []*User {
rows, err := uq.db.Query("SELECT mxid, id, management_room, token FROM user")
if err != nil || rows == nil {
return nil
}
defer rows.Close()
users := []*User{}
for rows.Next() {
users = append(users, uq.New().Scan(rows))
}
return users
}