town/cmd/review/main.go

264 lines
6.0 KiB
Go
Raw Normal View History

2023-02-14 04:48:12 +00:00
package main
import (
2023-02-23 00:04:26 +00:00
"database/sql"
2023-02-17 05:53:05 +00:00
"errors"
2023-02-14 04:48:12 +00:00
"fmt"
2023-02-14 07:33:53 +00:00
"math/rand"
2023-02-14 04:48:12 +00:00
"os"
2023-02-17 05:53:05 +00:00
"os/user"
2023-02-14 07:33:53 +00:00
"strings"
"time"
2023-02-23 00:04:26 +00:00
"git.tilde.town/tildetown/town/models"
//"git.tilde.town/tildetown/town/review"
"git.tilde.town/tildetown/town/signup"
2023-02-17 05:53:05 +00:00
tuser "git.tilde.town/tildetown/town/user"
2023-02-14 07:33:53 +00:00
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func getTitle() string {
titles := []string{
"yo bum rush the show",
"can i kick it?",
"super nintendo sega genesis",
"birthdays was the worst days",
"where were you when we were getting high?",
"it's real time, real time, time to get real",
}
return titles[rand.Intn(len(titles))]
}
2023-02-23 00:04:26 +00:00
// TODO affordance for cleaning up email response
2023-02-16 05:48:26 +00:00
2023-02-23 00:04:26 +00:00
type reviewer struct {
db *sql.DB
adminName string
2023-02-14 07:33:53 +00:00
}
2023-02-23 00:04:26 +00:00
func newReviewer(db *sql.DB, adminName string) *reviewer {
return &reviewer{db: db, adminName: adminName}
2023-02-16 05:48:26 +00:00
}
2023-02-16 05:08:31 +00:00
2023-02-23 00:04:26 +00:00
func (r *reviewer) Review(s *models.TownSignup, decision models.SignupDecision) error {
2023-02-16 05:48:26 +00:00
s.DecisionTime = time.Now()
2023-02-23 00:04:26 +00:00
s.Decision = decision
s.DecidedBy = r.adminName
s.Insert(r.db)
return nil
2023-02-16 05:48:26 +00:00
}
2023-02-23 00:04:26 +00:00
func renderSignup(s models.TownSignup) string {
out := fmt.Sprintf("[-:-:b]submitted:[-:-:-] %s\n", s.Created.Format("2006-01-02 15:04"))
pairs := map[string]string{
"e-mail": s.Email,
"how found / referral": s.How,
"why like town / what do": s.Why,
"links": s.Links,
}
2023-02-16 05:48:26 +00:00
2023-02-23 00:04:26 +00:00
for k, v := range pairs {
2023-02-16 05:08:31 +00:00
out += fmt.Sprintf("[-:-:b]%s[-:-:-]\n", k)
out += strings.TrimSpace(v)
out += "\n\n"
}
return out
2023-02-14 07:33:53 +00:00
}
2023-02-23 00:04:26 +00:00
func _main() error {
/*
TODO will use this for invites
userDB, err := review.ConnectDB()
2023-02-16 05:08:31 +00:00
if err != nil {
2023-02-23 00:04:26 +00:00
return fmt.Errorf("could not connect to user database: %w", err)
2023-02-16 05:08:31 +00:00
}
2023-02-23 00:04:26 +00:00
*/
2023-02-16 05:08:31 +00:00
2023-02-23 00:04:26 +00:00
signupDB, err := signup.ConnectDB()
if err != nil {
return fmt.Errorf("could not connect to signups database: %w", err)
2023-02-14 07:33:53 +00:00
}
2023-02-17 05:53:05 +00:00
u, err := user.Current()
if err != nil {
return fmt.Errorf("that's my purse. I don't know you! %w", err)
}
isAdmin, err := tuser.IsAdmin(u)
if err != nil {
return fmt.Errorf("that's my purse. I don't know you! %w", err)
}
if !isAdmin {
return errors.New("this command can only be run by a town admin")
}
2023-02-23 00:04:26 +00:00
r := newReviewer(signupDB, u.Username)
2023-02-14 07:33:53 +00:00
rand.Seed(time.Now().Unix())
2023-02-23 00:04:26 +00:00
su := models.TownSignup{}
signups, err := su.All(signupDB)
2023-02-14 07:33:53 +00:00
if err != nil {
2023-02-23 00:04:26 +00:00
return fmt.Errorf("could not fetch signups: %w", err)
2023-02-14 07:33:53 +00:00
}
2023-02-16 05:08:31 +00:00
signupIx := 0
2023-02-14 07:33:53 +00:00
title := tview.NewTextView()
title.SetText(getTitle())
2023-02-16 05:08:31 +00:00
title.SetTextAlign(tview.AlignCenter)
2023-02-16 05:48:26 +00:00
title.SetTextColor(tcell.ColorPurple)
2023-02-16 05:08:31 +00:00
title.SetBackgroundColor(tcell.ColorBlack)
2023-02-14 07:33:53 +00:00
appView := tview.NewTextView()
2023-02-16 05:08:31 +00:00
appView.SetDynamicColors(true)
if len(signups) == 0 {
appView.SetText("no signups found.")
2023-02-14 07:33:53 +00:00
} else {
2023-02-23 00:04:26 +00:00
appView.SetText(renderSignup(*signups[signupIx]))
2023-02-14 07:33:53 +00:00
}
legend := tview.NewTextView()
2023-02-16 05:48:26 +00:00
legend.SetText("s: skip r: random A: approve R: reject N: notate Q: quit")
legend.SetTextColor(tcell.ColorPurple)
legend.SetTextAlign(tview.AlignCenter)
legend.SetBackgroundColor(tcell.ColorBlack)
2023-02-14 07:33:53 +00:00
2023-02-17 05:53:05 +00:00
count := tview.NewTextView()
count.SetDynamicColors(true)
updateCount := func() {
count.SetText(fmt.Sprintf("[-:-:b]%d pending signups[-:-:-]", len(signups)))
}
updateCount()
bottomFlex := tview.NewFlex()
bottomFlex.SetDirection(tview.FlexColumn)
bottomFlex.AddItem(count, 0, 1, false)
bottomFlex.AddItem(legend, 0, 10, false)
mainFlex := tview.NewFlex()
2023-02-14 07:33:53 +00:00
mainFlex.SetDirection(tview.FlexRow)
mainFlex.AddItem(title, 1, -1, false)
mainFlex.AddItem(appView, 0, 1, true)
2023-02-17 05:53:05 +00:00
mainFlex.AddItem(bottomFlex, 1, -1, false)
pages := tview.NewPages()
errorModal := tview.NewModal()
errorModal.AddButtons([]string{"damn"})
errorModal.SetDoneFunc(func(ix int, _ string) {
pages.SwitchToPage("main")
})
2023-02-14 07:33:53 +00:00
2023-02-17 06:06:37 +00:00
notate := tview.NewForm()
notate.AddTextArea("note", "", 80, 10, 1000, func(string) {})
notate.AddButton("submit", func() {
2023-02-23 00:04:26 +00:00
// add note and update
2023-02-17 06:06:37 +00:00
pages.SwitchToPage("main")
})
notate.AddButton("cancel", func() {
pages.SwitchToPage("main")
})
2023-02-14 07:33:53 +00:00
pages.AddPage("main", mainFlex, true, true)
2023-02-17 05:53:05 +00:00
pages.AddPage("error", errorModal, false, false)
2023-02-17 06:06:37 +00:00
pages.AddPage("notate", notate, true, false)
2023-02-14 07:33:53 +00:00
app := tview.NewApplication()
app.SetRoot(pages, true)
2023-02-17 06:06:37 +00:00
// TODO replace imperative shit with a signupManager
2023-02-17 05:53:05 +00:00
advanceSignup := func() {
if len(signups) == 0 {
appView.SetText("no signups found.")
return
}
signupIx++
if signupIx == len(signups) {
signupIx = 0
}
2023-02-23 00:04:26 +00:00
appView.SetText(renderSignup(*signups[signupIx]))
2023-02-17 05:53:05 +00:00
}
2023-02-23 00:04:26 +00:00
removeSignup := func(signup *models.TownSignup) {
newSignups := []*models.TownSignup{}
2023-02-17 05:53:05 +00:00
for ix, s := range signups {
if ix != signupIx {
newSignups = append(newSignups, s)
}
}
signups = newSignups
if len(signups) > 0 {
if signupIx >= len(signups) {
signupIx = 0
}
}
2023-02-23 00:04:26 +00:00
appView.SetText(renderSignup(*signups[signupIx]))
2023-02-17 05:53:05 +00:00
}
2023-02-14 07:33:53 +00:00
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() {
case 's':
2023-02-17 05:53:05 +00:00
advanceSignup()
2023-02-14 07:33:53 +00:00
case 'r':
2023-02-17 05:53:05 +00:00
if len(signups) > 0 {
signupIx = rand.Intn(len(signups))
2023-02-23 00:04:26 +00:00
appView.SetText(renderSignup(*signups[signupIx]))
2023-02-17 05:53:05 +00:00
}
2023-02-14 07:33:53 +00:00
case 'A':
2023-02-17 05:53:05 +00:00
if len(signups) == 0 {
return nil
}
2023-02-23 00:04:26 +00:00
// TODO modal for collecting clean email
2023-02-17 05:53:05 +00:00
signup := signups[signupIx]
2023-02-23 00:04:26 +00:00
err := r.Review(signup, models.SignupAccepted)
2023-02-16 05:48:26 +00:00
if err != nil {
2023-02-23 00:04:26 +00:00
errorModal.SetText(fmt.Sprintf("error! failed to approve '%d': %s", signup.ID, err.Error()))
2023-02-17 05:53:05 +00:00
pages.SwitchToPage("error")
2023-02-17 06:06:37 +00:00
return nil
2023-02-16 05:48:26 +00:00
}
2023-02-17 06:06:37 +00:00
removeSignup(signup)
updateCount()
2023-02-23 00:04:26 +00:00
// TODO generate invite token
// TODO send invite email
2023-02-14 07:33:53 +00:00
case 'R':
2023-02-17 05:53:05 +00:00
if len(signups) == 0 {
return nil
}
signup := signups[signupIx]
2023-02-23 00:04:26 +00:00
err = r.Review(signup, models.SignupRejected)
2023-02-16 05:48:26 +00:00
if err != nil {
2023-02-23 00:04:26 +00:00
errorModal.SetText(fmt.Sprintf("error! failed to reject '%d': %s", signup.ID, err.Error()))
2023-02-17 05:53:05 +00:00
pages.SwitchToPage("error")
2023-02-17 06:06:37 +00:00
return nil
2023-02-17 05:53:05 +00:00
}
2023-02-17 06:06:37 +00:00
removeSignup(signup)
updateCount()
2023-02-17 05:53:05 +00:00
case 'N':
if len(signups) == 0 {
return nil
2023-02-16 05:48:26 +00:00
}
2023-02-17 06:06:37 +00:00
pages.SwitchToPage("notate")
return nil
2023-02-14 07:33:53 +00:00
case 'Q':
app.Stop()
}
return event
})
return app.Run()
2023-02-14 04:48:12 +00:00
}
func main() {
err := _main()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}