forked from tildetown/town
ask db about users
parent
880511a79a
commit
2a966bf842
|
@ -1,11 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.tilde.town/tildetown/town/towndb"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/mattn/go-tty"
|
"github.com/mattn/go-tty"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -83,6 +87,11 @@ func numberPrompt(cs colorScheme, tty *tty.TTY, prompt string, opts []string) (i
|
||||||
}
|
}
|
||||||
|
|
||||||
func _main() error {
|
func _main() error {
|
||||||
|
db, err := towndb.ConnectDB()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not connect to database. please let root@tilde.town know about this.")
|
||||||
|
}
|
||||||
|
|
||||||
cs := newColorScheme()
|
cs := newColorScheme()
|
||||||
fmt.Println(cs.Header("Hi, you have reached the tilde town help desk."))
|
fmt.Println(cs.Header("Hi, you have reached the tilde town help desk."))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
@ -101,29 +110,60 @@ func _main() error {
|
||||||
}
|
}
|
||||||
c, err := numberPrompt(cs, tty, "What do you need help with?", options)
|
c, err := numberPrompt(cs, tty, "What do you need help with?", options)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println(cs.Header("bye~"))
|
||||||
|
}()
|
||||||
|
|
||||||
switch c {
|
switch c {
|
||||||
case 0:
|
case 0:
|
||||||
return collectEmail(cs, tty)
|
return collectEmail(db, cs, tty)
|
||||||
case 1:
|
case 1:
|
||||||
return redeemCode(tty)
|
return redeemCode(tty)
|
||||||
case 2:
|
case 2:
|
||||||
fmt.Println()
|
|
||||||
fmt.Println(cs.Header("bye~"))
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectEmail(cs colorScheme, tty *tty.TTY) error {
|
func collectEmail(db *sql.DB, cs colorScheme, tty *tty.TTY) error {
|
||||||
fmt.Println(cs.Header("We can send a reset code to an email associated with your town account."))
|
fmt.Println(cs.Header("We can send a authorization code to an email associated with your town account."))
|
||||||
email, err := stringPrompt(cs, tty, "email to send reset code to?")
|
email, err := stringPrompt(cs, tty, "email to send reset code to?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(email)
|
fmt.Println()
|
||||||
// TODO confirm email
|
fmt.Println(cs.Header("thanks!"))
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Printf("If %s is associated with a town account we'll email an authorization code.\n", cs.Email(email))
|
||||||
|
|
||||||
|
mustHave := []string{"@", "."}
|
||||||
|
found := 0
|
||||||
|
for _, s := range mustHave {
|
||||||
|
if strings.Contains(email, s) {
|
||||||
|
found++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found != len(mustHave) {
|
||||||
|
// TODO log
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := towndb.UserForEmail(db, email)
|
||||||
|
if err != nil {
|
||||||
|
// TODO log
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if user == nil {
|
||||||
|
// TODO log
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("found a user so gonna email")
|
||||||
|
|
||||||
// TODO generate reset code
|
// TODO generate reset code
|
||||||
// TODO send email
|
// TODO send email
|
||||||
// TODO report success
|
// TODO report success
|
||||||
|
|
|
@ -2,6 +2,7 @@ package towndb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
@ -142,6 +143,28 @@ func (u *TownUser) Insert(db *sql.DB) (err error) {
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserForEmail returns the user associated with an email or nil if no matching user is found
|
||||||
|
func UserForEmail(db *sql.DB, address string) (*TownUser, error) {
|
||||||
|
stmt, err := db.Prepare(`
|
||||||
|
SELECT u.id, u.username FROM users u
|
||||||
|
JOIN emails e ON e.userid = u.id
|
||||||
|
WHERE e.address = ?
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
row := stmt.QueryRow(address)
|
||||||
|
u := &TownUser{}
|
||||||
|
if err = row.Scan(u.ID, u.Username); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return u, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ConnectDB() (*sql.DB, error) {
|
func ConnectDB() (*sql.DB, error) {
|
||||||
db, err := sql.Open("sqlite3", dsn)
|
db, err := sql.Open("sqlite3", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue