Compare commits
No commits in common. "main" and "2024-12-29-fixes" have entirely different histories.
main
...
2024-12-29
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,5 +2,5 @@
|
||||
gin-bin
|
||||
*lishwist.db
|
||||
.env*.local
|
||||
init_sql.go
|
||||
server/api/db/init_sql.go
|
||||
.ignored/
|
||||
|
||||
@ -1,49 +0,0 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Eq[C comparable](t *testing.T, context string, expected, actual C) {
|
||||
if expected != actual {
|
||||
t.Errorf("%s: %#v != %#v", context, expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func True(t *testing.T, context string, condition bool) {
|
||||
if !condition {
|
||||
t.Errorf("false: %s", context)
|
||||
}
|
||||
}
|
||||
|
||||
func FatalTrue(t *testing.T, context string, condition bool) {
|
||||
if !condition {
|
||||
t.Fatalf("%s", context)
|
||||
}
|
||||
}
|
||||
|
||||
func FatalErr(t *testing.T, context string, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %s", context, err)
|
||||
}
|
||||
}
|
||||
|
||||
func FatalErrIs(t *testing.T, context string, err, target error) {
|
||||
if !errors.Is(err, target) {
|
||||
t.Fatalf("%s: encountered unexpected error: %s", context, err)
|
||||
}
|
||||
}
|
||||
|
||||
func FatalErrAs(t *testing.T, context string, err error, target any) {
|
||||
if !errors.As(err, target) {
|
||||
t.Fatalf("%s: encountered unexpected error: %s", context, err)
|
||||
}
|
||||
}
|
||||
|
||||
func SlicesEq[S ~[]E, E comparable](t *testing.T, context string, expected, actual S) {
|
||||
if !slices.Equal(expected, actual) {
|
||||
t.Errorf("%s: %#v != %#v", context, expected, actual)
|
||||
}
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
module lishwist/assert
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.23.3
|
||||
@ -1,88 +0,0 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func pathExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !errors.Is(err, os.ErrNotExist)
|
||||
}
|
||||
|
||||
var newline = []byte("\n")
|
||||
|
||||
func ensureSuffix(s, suffix []byte) []byte {
|
||||
if !bytes.HasSuffix(s, suffix) {
|
||||
s = append(s, suffix...)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func writeSnapshot(t *testing.T, path string, actual []byte) {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create snapshot file: %s", err)
|
||||
}
|
||||
_, err = f.Write(actual)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to write to snapshot file: %s", err)
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to close snapshot file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func snapshot(t *testing.T, path string, actual []byte) {
|
||||
if !pathExists(path) {
|
||||
writeSnapshot(t, path, actual)
|
||||
t.Errorf("Snapshot file created: %s", path)
|
||||
return
|
||||
}
|
||||
|
||||
actual = ensureSuffix(actual, newline)
|
||||
|
||||
expected, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read snapshot file: %s", err)
|
||||
}
|
||||
|
||||
expected = ensureSuffix(expected, newline)
|
||||
|
||||
if !bytes.Equal(expected, actual) {
|
||||
t.Errorf("Value doesn't match snapshot %s:\n\nExpected:\n%s\n\nActual:\n%s\n", path, string(expected), string(actual))
|
||||
}
|
||||
|
||||
if os.Getenv("UPDATE_SNAPSHOTS") == "" {
|
||||
return
|
||||
}
|
||||
|
||||
writeSnapshot(t, path, actual)
|
||||
t.Logf("Snapshot file %s updated\n", path)
|
||||
}
|
||||
|
||||
func SnapshotText(t *testing.T, actual string) {
|
||||
snapshot(t, fmt.Sprintf("%s.snap.txt", t.Name()), []byte(actual))
|
||||
}
|
||||
|
||||
func SnapshotJson(t *testing.T, actual any) {
|
||||
data, err := json.MarshalIndent(actual, "", " ")
|
||||
if err != nil {
|
||||
t.Fatalf("Snapshot failed to serialize actual to JSON: %s", err)
|
||||
}
|
||||
snapshot(t, fmt.Sprintf("%s.snap.json", t.Name()), data)
|
||||
}
|
||||
|
||||
func SnapshotXml(t *testing.T, actual any) {
|
||||
data, err := xml.MarshalIndent(actual, "", " ")
|
||||
if err != nil {
|
||||
t.Fatalf("Snapshot failed to serialize actual to XML: %s", err)
|
||||
}
|
||||
snapshot(t, fmt.Sprintf("%s.snap.xml", t.Name()), data)
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
type Admin struct {
|
||||
session *Session
|
||||
}
|
||||
|
||||
func (s *Session) Admin() *Admin {
|
||||
if s.User().IsAdmin {
|
||||
return &Admin{s}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -1,127 +0,0 @@
|
||||
package lishwist // import "."
|
||||
|
||||
|
||||
VARIABLES
|
||||
|
||||
var ErrorUsernameTaken = errors.New("username is taken")
|
||||
|
||||
FUNCTIONS
|
||||
|
||||
func Init(dataSourceName string) error
|
||||
func PrintTables(d *sql.DB)
|
||||
func PrintViews(d *sql.DB)
|
||||
|
||||
TYPES
|
||||
|
||||
type Admin struct {
|
||||
// Has unexported fields.
|
||||
}
|
||||
|
||||
func (a *Admin) AddUserToGroup(userId, groupId string) error
|
||||
|
||||
func (a *Admin) CreateGroup(name string, reference string) (*Group, error)
|
||||
|
||||
func (*Admin) GetUser(id string) (*User, error)
|
||||
|
||||
func (a *Admin) ListEvents() ([]Event, error)
|
||||
|
||||
func (a *Admin) ListGroups() ([]Group, error)
|
||||
|
||||
func (*Admin) ListUsers() ([]User, error)
|
||||
|
||||
func (a *Admin) RemoveUserFromGroup(userId, groupId string) error
|
||||
|
||||
func (u *Admin) RenameUser(userReference string, displayName string) error
|
||||
|
||||
func (u *Admin) SetUserPassword(userReference string, newPassword string) error
|
||||
|
||||
func (u *Admin) UserSetLive(userReference string, setting bool) error
|
||||
|
||||
type ErrorInvalidCredentials error
|
||||
|
||||
type Event struct {
|
||||
Id string
|
||||
ActorId string
|
||||
ActionType string
|
||||
TargetType string
|
||||
TargetId string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
Id string
|
||||
Name string
|
||||
Reference string
|
||||
Members []User
|
||||
}
|
||||
|
||||
func GetGroupByReference(reference string) (*Group, error)
|
||||
|
||||
func (g *Group) MemberIndex(userId string) int
|
||||
|
||||
type Session struct {
|
||||
Key string
|
||||
Expiry time.Time
|
||||
// Has unexported fields.
|
||||
}
|
||||
|
||||
func Login(username, password string, sessionMaxAge time.Duration) (*Session, error)
|
||||
|
||||
func SessionFromKey(key string) (*Session, error)
|
||||
|
||||
func (s *Session) Admin() *Admin
|
||||
|
||||
func (s *Session) ClaimWishes(claims, unclaims []string) error
|
||||
|
||||
func (s *Session) CompleteWishes(claims []string) error
|
||||
|
||||
func (s *Session) GetGroupByReference(reference string) (*Group, error)
|
||||
|
||||
func (u *Session) GetGroups() ([]Group, error)
|
||||
|
||||
func (s *Session) GetOthersWishes(userReference string) ([]Wish, error)
|
||||
|
||||
func (s *Session) GetWishes() ([]Wish, error)
|
||||
|
||||
func (s *Session) MakeWish(name string) (string, error)
|
||||
|
||||
func (s *Session) RecindWishesForUser(ids ...string) error
|
||||
|
||||
func (s *Session) RevokeWishes(ids ...string) error
|
||||
|
||||
func (u *Session) SuggestWishForUser(otherUserReference string, wishName string) error
|
||||
|
||||
func (s *Session) User() User
|
||||
|
||||
type User struct {
|
||||
Id string
|
||||
NormalName string
|
||||
Name string
|
||||
Reference string
|
||||
IsAdmin bool
|
||||
IsLive bool
|
||||
PasswordFromAdmin bool
|
||||
}
|
||||
|
||||
func GetUserByReference(reference string) (*User, error)
|
||||
|
||||
func Register(username, newPassword string) (*User, error)
|
||||
|
||||
func (u *User) GetTodo() ([]Wish, error)
|
||||
|
||||
func (u *User) SetPassword(newPassword string) error
|
||||
|
||||
func (u *User) WishCount() (int, error)
|
||||
|
||||
type Wish struct {
|
||||
Id string
|
||||
Name string
|
||||
ClaimantId string `json:",omitempty"`
|
||||
ClaimantName string `json:",omitempty"`
|
||||
Sent bool
|
||||
RecipientId string `json:",omitempty"`
|
||||
RecipientName string `json:",omitempty"`
|
||||
RecipientRef string `json:",omitempty"`
|
||||
CreatorId string `json:",omitempty"`
|
||||
CreatorName string `json:",omitempty"`
|
||||
}
|
||||
@ -1,55 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func PrintViews(d *sql.DB) {
|
||||
rows, err := d.Query("SELECT name FROM sqlite_master WHERE type = 'view';")
|
||||
if err != nil {
|
||||
log.Println("Query failed: %w", err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
fmt.Println("Printing view names...")
|
||||
for rows.Next() {
|
||||
var name string
|
||||
err = rows.Scan(&name)
|
||||
if err != nil {
|
||||
log.Println("Scan failed: %w", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("name: %s\n", name)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
log.Println("Rows returned error: %w", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func PrintTables(d *sql.DB) {
|
||||
rows, err := d.Query("SELECT name FROM sqlite_master WHERE type = 'table';")
|
||||
if err != nil {
|
||||
log.Println("Query failed: %w", err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
fmt.Println("Printing table names...")
|
||||
for rows.Next() {
|
||||
var name string
|
||||
err = rows.Scan(&name)
|
||||
if err != nil {
|
||||
fmt.Println("Scan failed: %w", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("name: %s\n", name)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
log.Println("Rows returned error: %w", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
150
core/event.go
150
core/event.go
@ -1,150 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lishwist/core/internal/db"
|
||||
)
|
||||
|
||||
const (
|
||||
eventActionCreate = "CREATE"
|
||||
eventActionHide = "HIDE"
|
||||
eventActionUnhide = "UNHIDE"
|
||||
eventActionClaim = "CLAIM"
|
||||
eventActionUnclaim = "UNCLAIM"
|
||||
eventActionComplete = "COMPLETE"
|
||||
// eventActionDelete = "DELETE" NOTE: We can't have this, because there'll be no target to reference
|
||||
)
|
||||
|
||||
const (
|
||||
eventTargetGroup = "GROUP"
|
||||
eventTargetUser = "USER"
|
||||
eventTargetWish = "WISH"
|
||||
eventTargetGroupMember = "GROUP_MEMBER"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Id string
|
||||
ActorId string
|
||||
ActionType string
|
||||
TargetType string
|
||||
TargetId string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// type EventCreateGroupMember struct {
|
||||
// Event
|
||||
// Actor User
|
||||
// User
|
||||
// Group
|
||||
// }
|
||||
|
||||
func queryManyEvents(query string, args ...any) ([]Event, error) {
|
||||
rows, err := db.Connection.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
events := []Event{}
|
||||
for rows.Next() {
|
||||
var g Event
|
||||
var createdAt string
|
||||
err = rows.Scan(&g.Id, &g.ActorId, &g.ActionType, &g.TargetType, &g.TargetId, &createdAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.CreatedAt, err = time.Parse(time.RFC3339Nano, createdAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse created_at: %w", err)
|
||||
}
|
||||
events = append(events, g)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func queryOneEvent(query string, args ...any) (*Event, error) {
|
||||
events, err := queryManyEvents(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(events) < 1 {
|
||||
return nil, nil
|
||||
}
|
||||
return &events[0], nil
|
||||
}
|
||||
|
||||
func (a *Admin) ListEvents() ([]Event, error) {
|
||||
query := "SELECT id, actor_id, action_type, target_type, target_id, created_at FROM event;"
|
||||
return queryManyEvents(query)
|
||||
}
|
||||
|
||||
func recordEvent(actorId, actionType, targetType string, targetIds ...string) {
|
||||
// TODO: If this were to accept sql.Tx it could be used in atomic transactions
|
||||
numTargets := len(targetIds)
|
||||
if numTargets < 1 {
|
||||
log.Println("Warning: recordEvent called with no target IDs. Skipping.")
|
||||
return
|
||||
}
|
||||
stmt := "INSERT INTO event (actor_id, action_type, target_type, target_id) VALUES (?, ?, ?, ?)"
|
||||
extraValuePlaceholders := strings.Repeat(", (?, ?, ?, ?)", numTargets-1)
|
||||
args := make([]any, numTargets*4)
|
||||
for i, id := range targetIds {
|
||||
args[i*4] = actorId
|
||||
args[i*4+1] = actionType
|
||||
args[i*4+2] = targetType
|
||||
args[i*4+3] = id
|
||||
}
|
||||
_, err := db.Connection.Exec(stmt+extraValuePlaceholders, args...)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if numTargets == 1 {
|
||||
log.Printf("Failed to record %s %s event: failed to execute query: %s\n", actionType, targetType, err)
|
||||
} else {
|
||||
log.Printf("Failed to record %d %s %s events: failed to execute query: %s\n", numTargets, actionType, targetType, err)
|
||||
}
|
||||
}
|
||||
|
||||
func recordEventCreateGroup(actorId, groupId string) {
|
||||
recordEvent(actorId, eventActionCreate, eventTargetGroup, groupId)
|
||||
}
|
||||
|
||||
func recordEventCreateUser(actorId, userId string) {
|
||||
recordEvent(actorId, eventActionCreate, eventTargetUser, userId)
|
||||
}
|
||||
|
||||
func recordEventCreateWish(actorId, wishId string) {
|
||||
recordEvent(actorId, eventActionCreate, eventTargetWish, wishId)
|
||||
}
|
||||
|
||||
func recordEventCreateGroupMember(actorId, groupMemberId string) {
|
||||
recordEvent(actorId, eventActionCreate, eventTargetGroupMember, groupMemberId)
|
||||
}
|
||||
|
||||
// FIXME: I can't use these yet because the associated actions use reference
|
||||
// func recordEventHideUser(actorId, userId string) {
|
||||
// recordEvent(actorId, eventActionHide, eventTargetUser, userId)
|
||||
// }
|
||||
|
||||
// func recordEventUnhideUser(actorId, userId string) {
|
||||
// recordEvent(actorId, eventActionUnhide, eventTargetUser, userId)
|
||||
// }
|
||||
|
||||
func recordEventClaimWishes(actorId string, wishIds ...string) {
|
||||
recordEvent(actorId, eventActionClaim, eventTargetWish, wishIds...)
|
||||
}
|
||||
|
||||
func recordEventUnclaimWishes(actorId string, wishIds ...string) {
|
||||
recordEvent(actorId, eventActionUnclaim, eventTargetWish, wishIds...)
|
||||
}
|
||||
|
||||
func recordEventCompleteWishes(actorId string, wishIds ...string) {
|
||||
recordEvent(actorId, eventActionComplete, eventTargetWish, wishIds...)
|
||||
}
|
||||
18
core/go.mod
18
core/go.mod
@ -1,18 +0,0 @@
|
||||
module lishwist/core
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.23.3
|
||||
|
||||
require golang.org/x/crypto v0.39.0
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/ncruces/go-sqlite3 v0.26.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ncruces/julianday v1.0.0 // indirect
|
||||
github.com/tetratelabs/wazero v1.9.0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
)
|
||||
14
core/go.sum
14
core/go.sum
@ -1,14 +0,0 @@
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/ncruces/go-sqlite3 v0.26.1 h1:lBXmbmucH1Bsj57NUQR6T84UoMN7jnNImhF+ibEITJU=
|
||||
github.com/ncruces/go-sqlite3 v0.26.1/go.mod h1:XFTPtFIo1DmGCh+XVP8KGn9b/o2f+z0WZuT09x2N6eo=
|
||||
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
|
||||
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
|
||||
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
|
||||
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
|
||||
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
|
||||
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
||||
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
||||
137
core/group.go
137
core/group.go
@ -1,137 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lishwist/core/internal/db"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
Id string
|
||||
Name string
|
||||
Reference string
|
||||
Members []User
|
||||
}
|
||||
|
||||
func (g *Group) MemberIndex(userId string) int {
|
||||
for i, u := range g.Members {
|
||||
if u.Id == userId {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func queryManyGroups(query string, args ...any) ([]Group, error) {
|
||||
groups := []Group{}
|
||||
rows, err := db.Connection.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Query failed: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var group Group
|
||||
err := rows.Scan(&group.Id, &group.Name, &group.Reference)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to scan row: %w", err)
|
||||
}
|
||||
members, err := queryManyGroupMembers(group.Id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to query for group members: %w", err)
|
||||
}
|
||||
group.Members = members
|
||||
groups = append(groups, group)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Rows error: %w", err)
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func queryOneGroup(query string, args ...any) (*Group, error) {
|
||||
groups, err := queryManyGroups(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(groups) < 1 {
|
||||
return nil, nil
|
||||
}
|
||||
return &groups[0], nil
|
||||
}
|
||||
|
||||
func queryManyGroupMembers(groupId string) ([]User, error) {
|
||||
query := "SELECT user.id, user.name, user.display_name, user.reference, user.is_admin, user.is_live, user.password_from_admin FROM v_user AS user JOIN group_member ON group_member.user_id = user.id JOIN [group] ON [group].id = group_member.group_id WHERE [group].id = ? ORDER BY group_member.user_id"
|
||||
members, err := queryManyUsers(query, groupId)
|
||||
if err != nil {
|
||||
return members, err
|
||||
}
|
||||
return members, nil
|
||||
}
|
||||
|
||||
func (s *Session) GetGroupByReference(reference string) (*Group, error) {
|
||||
// FIXME: This function doesn't make much sense when there's already a public function to fetch any group, below
|
||||
stmt := "SELECT [group].id, [group].name, [group].reference FROM [group] JOIN group_member ON [group].id == group_member.group_id WHERE [group].reference = ? AND group_member.user_id = ?;"
|
||||
return queryOneGroup(stmt, reference, s.User().Id)
|
||||
}
|
||||
|
||||
func GetGroupByReference(reference string) (*Group, error) {
|
||||
stmt := "SELECT [group].id, [group].name, [group].reference FROM [group] WHERE [group].reference = ?;"
|
||||
return queryOneGroup(stmt, reference)
|
||||
}
|
||||
|
||||
func (a *Admin) ListGroups() ([]Group, error) {
|
||||
query := "SELECT id, name, reference FROM [group];"
|
||||
return queryManyGroups(query)
|
||||
}
|
||||
|
||||
func (a *Admin) CreateGroup(name string, reference string) (*Group, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
reference = strings.TrimSpace(reference)
|
||||
stmt := "INSERT INTO [group] (name, reference) VALUES (?, ?)"
|
||||
result, err := db.Connection.Exec(stmt, name, reference)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
group := Group{
|
||||
Id: strconv.FormatInt(id, 10),
|
||||
Name: name,
|
||||
Reference: reference,
|
||||
}
|
||||
recordEventCreateGroup(a.session.user.Id, group.Id)
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
func (a *Admin) AddUserToGroup(userId, groupId string) error {
|
||||
stmt := "INSERT INTO group_member (user_id, group_id) VALUES (?, ?)"
|
||||
result, err := db.Connection.Exec(stmt, userId, groupId)
|
||||
if err != nil {
|
||||
return fmt.Errorf("query execution failed: %w", err)
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get last insert id: %w", err)
|
||||
}
|
||||
recordEventCreateGroupMember(a.session.user.Id, strconv.FormatInt(id, 10))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Admin) RemoveUserFromGroup(userId, groupId string) error {
|
||||
stmt := "DELETE FROM group_member WHERE group_id = ? AND user_id = ?"
|
||||
_, err := db.Connection.Exec(stmt, userId, groupId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the groups the session user belongs to
|
||||
func (u *Session) GetGroups() ([]Group, error) {
|
||||
stmt := "SELECT [group].id, [group].name, [group].reference FROM [group] JOIN group_member ON group_member.group_id = [group].id JOIN v_user AS user ON user.id = group_member.user_id WHERE user.id = ?"
|
||||
return queryManyGroups(stmt, u.User().Id)
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
package lishwist_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/core/internal/fixtures"
|
||||
)
|
||||
|
||||
func TestCreateGroup(t *testing.T) {
|
||||
s := fixtures.Login(t, "thomas", "123")
|
||||
|
||||
group, err := s.Admin().CreateGroup(" My Friends ", " my-friends ")
|
||||
fixtures.FailIfErr(t, err, "Failed to create group")
|
||||
|
||||
fixtures.AssertEq(t, "Number of users", "My Friends", group.Name)
|
||||
fixtures.AssertEq(t, "Number of users", "my-friends", group.Reference)
|
||||
|
||||
// FIXME: disabled for now because datetimes break this
|
||||
// events, err := s.Admin().ListEvents()
|
||||
// assert.FatalErr(t, "listing events", err)
|
||||
|
||||
// assert.JsonSnapshot(t, "TestCreateGroup.snap.txt", events)
|
||||
}
|
||||
|
||||
func TestCantSeeSelfInGroup(t *testing.T) {
|
||||
s := fixtures.Login(t, "thomas", "123")
|
||||
|
||||
caleb, err := lishwist.Register("caleb", "123")
|
||||
fixtures.FailIfErr(t, err, "Failed to register caleb")
|
||||
|
||||
group, err := s.Admin().CreateGroup(" My Friends ", " my-friends ")
|
||||
fixtures.FailIfErr(t, err, "Failed to create group")
|
||||
|
||||
err = s.Admin().AddUserToGroup(s.User().Id, group.Id)
|
||||
fixtures.FailIfErr(t, err, "Failed to add self to group")
|
||||
|
||||
err = s.Admin().AddUserToGroup(caleb.Id, group.Id)
|
||||
fixtures.FailIfErr(t, err, "Failed to add caleb to group")
|
||||
|
||||
group, err = s.GetGroupByReference("my-friends")
|
||||
fixtures.FailIfErr(t, err, "Failed to get group")
|
||||
fixtures.FatalAssert(t, "Group not nil", group != nil)
|
||||
|
||||
fixtures.AssertEq(t, "Group contains 2 users", 2, len(group.Members))
|
||||
fixtures.AssertEq(t, "Group user 1 is thomas", "thomas", group.Members[0].Name)
|
||||
fixtures.AssertEq(t, "Group user 2 is caleb", "caleb", group.Members[1].Name)
|
||||
|
||||
// FIXME: disabled for now because datetimes break this
|
||||
// events, err := s.Admin().ListEvents()
|
||||
// assert.FatalErr(t, "listing events", err)
|
||||
|
||||
// assert.JsonSnapshot(t, "TestCantSeeSelfInGroup.snap.txt", events)
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"lishwist/core/internal/db"
|
||||
)
|
||||
|
||||
func Init(dataSourceName string) error {
|
||||
return db.Init(dataSourceName)
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
//go:generate go run gen_init_sql.go
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
var Connection *sql.DB
|
||||
|
||||
func Init(dataSourceName string) error {
|
||||
db, err := sql.Open("sqlite3", dataSourceName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open db connection: %w", err)
|
||||
}
|
||||
|
||||
_, err = db.Exec(initQuery)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize db: %w", err)
|
||||
}
|
||||
|
||||
Connection = db
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -1,28 +0,0 @@
|
||||
BEGIN TRANSACTION;
|
||||
|
||||
ALTER TABLE gift RENAME TO wish;
|
||||
|
||||
ALTER TABLE user ADD COLUMN display_name TEXT NOT NULL DEFAULT "";
|
||||
|
||||
UPDATE user SET display_name = name;
|
||||
|
||||
ALTER TABLE user RENAME TO old_user;
|
||||
|
||||
CREATE TABLE "user" (
|
||||
"id" INTEGER NOT NULL UNIQUE,
|
||||
"name" TEXT NOT NULL UNIQUE,
|
||||
"display_name" TEXT NOT NULL UNIQUE,
|
||||
"reference" TEXT NOT NULL UNIQUE,
|
||||
"motto" TEXT NOT NULL DEFAULT "",
|
||||
"password_hash" TEXT NOT NULL,
|
||||
"is_admin" INTEGER NOT NULL DEFAULT 0,
|
||||
"is_live" INTEGER NOT NULL DEFAULT 1,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
|
||||
INSERT INTO user (id, name, display_name, reference, motto, password_hash, is_admin, is_live) SELECT id, name, name, reference, motto, password_hash, is_admin, is_live FROM old_user;
|
||||
|
||||
DROP TABLE "old_user";
|
||||
|
||||
COMMIT;
|
||||
|
||||
@ -1 +0,0 @@
|
||||
ALTER TABLE user ADD COLUMN "password_from_admin" INTEGER NOT NULL DEFAULT 0;
|
||||
@ -1,19 +0,0 @@
|
||||
BEGIN TRANSACTION;
|
||||
|
||||
ALTER TABLE group_member RENAME TO old_group_member;
|
||||
|
||||
CREATE TABLE "group_member" (
|
||||
"id" INTEGER NOT NULL UNIQUE,
|
||||
"group_id" INTEGER NOT NULL,
|
||||
"user_id" INTEGER NOT NULL,
|
||||
PRIMARY KEY("id" AUTOINCREMENT),
|
||||
UNIQUE("user_id","group_id"),
|
||||
FOREIGN KEY("group_id") REFERENCES "group"("id"),
|
||||
FOREIGN KEY("user_id") REFERENCES "user"("id")
|
||||
);
|
||||
|
||||
INSERT INTO group_member (group_id, user_id) SELECT group_id, user_id FROM old_group_member;
|
||||
|
||||
DROP TABLE "old_group_member";
|
||||
|
||||
COMMIT;
|
||||
@ -1,31 +0,0 @@
|
||||
package fixtures
|
||||
|
||||
import "testing"
|
||||
|
||||
// Deprecated: use internal/assert
|
||||
func AssertEq[C comparable](t *testing.T, context string, expected, actual C) {
|
||||
if expected != actual {
|
||||
t.Errorf("%s: %#v != %#v", context, expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated: use internal/assert
|
||||
func Assert(t *testing.T, context string, condition bool) {
|
||||
if !condition {
|
||||
t.Errorf("%s", context)
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated: use internal/assert
|
||||
func FatalAssert(t *testing.T, context string, condition bool) {
|
||||
if !condition {
|
||||
t.Fatalf("%s", context)
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated: use internal/assert
|
||||
func FailIfErr(t *testing.T, err error, context string) {
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %s\n", context, err)
|
||||
}
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
package fixtures
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
|
||||
"github.com/ncruces/go-sqlite3/vfs/memdb"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) error {
|
||||
uri := memdb.TestDB(t)
|
||||
return lishwist.Init(uri)
|
||||
}
|
||||
|
||||
// Deprecated: This function also inits the test, which prevents it from being used more than once per test
|
||||
func Login(t *testing.T, username, password string) *lishwist.Session {
|
||||
uri := memdb.TestDB(t)
|
||||
err := lishwist.Init(uri)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to init db: %s\n", err)
|
||||
}
|
||||
|
||||
_, err = lishwist.Register(username, password)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to register on login fixture: %s\n", err)
|
||||
}
|
||||
|
||||
session, err := lishwist.Login(username, password, time.Hour*24)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to login on fixture: %s\n", err)
|
||||
}
|
||||
|
||||
return session
|
||||
}
|
||||
|
||||
func Login2(t *testing.T, username, password string) *lishwist.Session {
|
||||
_, err := lishwist.Register(username, password)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to register on login fixture: %s\n", err)
|
||||
}
|
||||
|
||||
session, err := lishwist.Login(username, password, time.Hour*24)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to login on fixture: %s\n", err)
|
||||
}
|
||||
|
||||
return session
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func Generate() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
package normalize
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Name(name string) string {
|
||||
name = strings.TrimSpace(name)
|
||||
return strings.ToLower(name)
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type ErrorInvalidCredentials error
|
||||
|
||||
func Login(username, password string, sessionMaxAge time.Duration) (*Session, error) {
|
||||
user, err := getUserByName(username)
|
||||
if err != nil {
|
||||
return nil, ErrorInvalidCredentials(fmt.Errorf("Failed to fetch user: %w", err))
|
||||
}
|
||||
if user == nil {
|
||||
return nil, ErrorInvalidCredentials(fmt.Errorf("User not found by name: %s", username))
|
||||
}
|
||||
|
||||
passHash, err := user.getPassHash()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to get password hash: %w", err)
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword(passHash, []byte(password))
|
||||
if err != nil {
|
||||
return nil, ErrorInvalidCredentials(fmt.Errorf("Password compare failed: %w", err))
|
||||
}
|
||||
|
||||
session, err := insertSession(*user, sessionMaxAge)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to insert session: %w", err)
|
||||
}
|
||||
|
||||
return session, nil
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package lishwist_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/core/internal/fixtures"
|
||||
)
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
err := fixtures.TestInit(t)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to init db: %s\n", err)
|
||||
}
|
||||
|
||||
_, err = lishwist.Register("thomas", "123")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to register: %s\n", err)
|
||||
}
|
||||
|
||||
_, err = lishwist.Login("thomas", "123", time.Hour*24)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to login: %s\n", err)
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var ErrorUsernameTaken = errors.New("username is taken")
|
||||
|
||||
func Register(username, newPassword string) (*User, error) {
|
||||
if username == "" {
|
||||
return nil, errors.New("Username required")
|
||||
}
|
||||
if newPassword == "" {
|
||||
return nil, errors.New("newPassword required")
|
||||
}
|
||||
|
||||
existingUser, _ := getUserByName(username)
|
||||
if existingUser != nil {
|
||||
return nil, ErrorUsernameTaken
|
||||
}
|
||||
|
||||
hashedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to hash password: %w", err)
|
||||
}
|
||||
|
||||
usersExist, err := hasUsers()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to count users: %w", err)
|
||||
}
|
||||
|
||||
user, err := createUser(username, hashedPasswordBytes, !usersExist)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create user: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"lishwist/core/internal/db"
|
||||
"lishwist/core/internal/id"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
user User
|
||||
Key string
|
||||
Expiry time.Time
|
||||
}
|
||||
|
||||
// Returns a copy of the user associated with this session
|
||||
func (s *Session) User() User {
|
||||
return s.user
|
||||
}
|
||||
|
||||
func SessionFromKey(key string) (*Session, error) {
|
||||
s := Session{}
|
||||
query := "SELECT user.id, user.name, user.display_name, user.reference, user.is_admin, user.is_live, user.password_from_admin, session.key, session.expiry FROM v_user as user JOIN session ON user.id = session.user_id WHERE session.key = ?"
|
||||
var expiry string
|
||||
err := db.Connection.QueryRow(query, key).Scan(
|
||||
&s.user.Id,
|
||||
&s.user.NormalName,
|
||||
&s.user.Name,
|
||||
&s.user.Reference,
|
||||
&s.user.IsAdmin,
|
||||
&s.user.IsLive,
|
||||
&s.user.PasswordFromAdmin,
|
||||
&s.Key,
|
||||
&expiry,
|
||||
)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch session: %w", err)
|
||||
}
|
||||
s.Expiry, err = time.Parse(time.RFC3339Nano, expiry)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse session expiry: %w", err)
|
||||
}
|
||||
if time.Now().After(s.Expiry) {
|
||||
return nil, nil
|
||||
}
|
||||
return &s, err
|
||||
}
|
||||
|
||||
func insertSession(user User, maxAge time.Duration) (*Session, error) {
|
||||
s := Session{
|
||||
user: user,
|
||||
Key: id.Generate(),
|
||||
Expiry: time.Now().Add(maxAge),
|
||||
}
|
||||
stmt := "INSERT INTO session (key, user_id, expiry) VALUES (?, ?, ?)"
|
||||
_, err := db.Connection.Exec(stmt, &s.Key, &user.Id, &s.Expiry)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute query: %w", err)
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
202
core/user.go
202
core/user.go
@ -1,202 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"lishwist/core/internal/db"
|
||||
"lishwist/core/internal/normalize"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id string
|
||||
NormalName string
|
||||
Name string
|
||||
Reference string
|
||||
IsAdmin bool
|
||||
IsLive bool
|
||||
PasswordFromAdmin bool
|
||||
}
|
||||
|
||||
func queryManyUsers(query string, args ...any) ([]User, error) {
|
||||
rows, err := db.Connection.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
users := []User{}
|
||||
for rows.Next() {
|
||||
var u User
|
||||
err = rows.Scan(&u.Id, &u.NormalName, &u.Name, &u.Reference, &u.IsAdmin, &u.IsLive, &u.PasswordFromAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, u)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func queryOneUser(query string, args ...any) (*User, error) {
|
||||
users, err := queryManyUsers(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(users) < 1 {
|
||||
return nil, nil
|
||||
}
|
||||
return &users[0], nil
|
||||
}
|
||||
|
||||
func getUserByName(username string) (*User, error) {
|
||||
username = normalize.Name(username)
|
||||
stmt := "SELECT id, name, display_name, reference, is_admin, is_live, password_from_admin FROM v_user WHERE name = ?"
|
||||
return queryOneUser(stmt, username)
|
||||
}
|
||||
|
||||
func createUser(name string, passHash []byte, isAdmin bool) (*User, error) {
|
||||
username := normalize.Name(name)
|
||||
stmt := "INSERT INTO user (name, display_name, reference, password_hash, is_admin) VALUES (?, ?, ?, ?, ?)"
|
||||
reference, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to generate reference: %w", err)
|
||||
}
|
||||
result, err := db.Connection.Exec(stmt, username, name, reference, passHash, isAdmin)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to execute query: %w", err)
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to get last insert id: %w", err)
|
||||
}
|
||||
user := User{
|
||||
Id: fmt.Sprintf("%d", id),
|
||||
Name: name,
|
||||
}
|
||||
recordEventCreateUser(user.Id, user.Id)
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (u *User) getPassHash() ([]byte, error) {
|
||||
stmt := "SELECT password_hash FROM v_user WHERE id = ?"
|
||||
var passHash string
|
||||
err := db.Connection.QueryRow(stmt, u.Id).Scan(&passHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(passHash), nil
|
||||
}
|
||||
|
||||
func getUserByReference(reference string) (*User, error) {
|
||||
stmt := "SELECT id, name, display_name, reference, is_admin, is_live, password_from_admin FROM v_user WHERE reference = ?"
|
||||
return queryOneUser(stmt, reference)
|
||||
}
|
||||
|
||||
func getUserById(id string) (*User, error) {
|
||||
stmt := "SELECT id, name, display_name, reference, is_admin, is_live, password_from_admin FROM v_user WHERE id = ?"
|
||||
return queryOneUser(stmt, id)
|
||||
}
|
||||
|
||||
func hasUsers() (bool, error) {
|
||||
stmt := "SELECT COUNT(id) FROM v_user LIMIT 1"
|
||||
var userCount uint
|
||||
err := db.Connection.QueryRow(stmt).Scan(&userCount)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return userCount > 0, nil
|
||||
}
|
||||
|
||||
func (*Admin) ListUsers() ([]User, error) {
|
||||
stmt := "SELECT id, name, display_name, reference, is_admin, is_live, password_from_admin FROM user"
|
||||
return queryManyUsers(stmt)
|
||||
}
|
||||
|
||||
func (*Admin) GetUser(id string) (*User, error) {
|
||||
return getUserById(id)
|
||||
}
|
||||
|
||||
func GetUserByReference(reference string) (*User, error) {
|
||||
return getUserByReference(reference)
|
||||
}
|
||||
|
||||
func (u *User) GetTodo() ([]Wish, error) {
|
||||
stmt := "SELECT wish.id, wish.name, wish.sent, recipient.display_name, recipient.reference FROM wish JOIN v_user AS user ON wish.claimant_id = user.id JOIN v_user AS recipient ON wish.recipient_id = recipient.id WHERE user.id = ? ORDER BY wish.sent ASC, wish.name"
|
||||
rows, err := db.Connection.Query(stmt, u.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
wishes := []Wish{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var name string
|
||||
var sent bool
|
||||
var recipientName string
|
||||
var recipientRef string
|
||||
_ = rows.Scan(&id, &name, &sent, &recipientName, &recipientRef)
|
||||
wish := Wish{
|
||||
Id: id,
|
||||
Name: name,
|
||||
Sent: sent,
|
||||
RecipientName: recipientName,
|
||||
RecipientRef: recipientRef,
|
||||
}
|
||||
wishes = append(wishes, wish)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return wishes, nil
|
||||
}
|
||||
|
||||
func (u *Admin) UserSetLive(userReference string, setting bool) error {
|
||||
query := "UPDATE user SET is_live = ? WHERE reference = ?"
|
||||
_, err := db.Connection.Exec(query, setting, userReference)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (u *Admin) RenameUser(userReference string, displayName string) error {
|
||||
name := normalize.Name(displayName)
|
||||
query := "UPDATE user SET name = ?, display_name = ? WHERE reference = ?"
|
||||
_, err := db.Connection.Exec(query, name, displayName, userReference)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (u *Admin) SetUserPassword(userReference string, newPassword string) error {
|
||||
hashedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to hash password: %w", err)
|
||||
}
|
||||
query := "UPDATE user SET password_hash = ?, password_from_admin = 1 WHERE reference = ?"
|
||||
_, err = db.Connection.Exec(query, hashedPasswordBytes, userReference)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (u *User) SetPassword(newPassword string) error {
|
||||
hashedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to hash password: %w", err)
|
||||
}
|
||||
query := "UPDATE user SET password_hash = ?, password_from_admin = 0 WHERE id = ?"
|
||||
_, err = db.Connection.Exec(query, hashedPasswordBytes, u.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
package lishwist_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/core/internal/fixtures"
|
||||
)
|
||||
|
||||
func TestFirstUserIsAdmin(t *testing.T) {
|
||||
s := fixtures.Login(t, "thomas", "123")
|
||||
|
||||
_, err := lishwist.Register("caleb", "123")
|
||||
fixtures.FailIfErr(t, err, "Failed to register caleb")
|
||||
|
||||
users, err := s.Admin().ListUsers()
|
||||
fixtures.FailIfErr(t, err, "Failed to list users")
|
||||
|
||||
fixtures.AssertEq(t, "Number of users", 2, len(users))
|
||||
fixtures.Assert(t, "User 1 is admin", users[0].IsAdmin)
|
||||
fixtures.Assert(t, "User 2 is not admin", !users[1].IsAdmin)
|
||||
}
|
||||
325
core/wish.go
325
core/wish.go
@ -1,325 +0,0 @@
|
||||
package lishwist
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"lishwist/core/internal/db"
|
||||
)
|
||||
|
||||
type Wish struct {
|
||||
Id string
|
||||
Name string
|
||||
ClaimantId string `json:",omitempty"`
|
||||
ClaimantName string `json:",omitempty"`
|
||||
Sent bool
|
||||
RecipientId string `json:",omitempty"`
|
||||
RecipientName string `json:",omitempty"`
|
||||
RecipientRef string `json:",omitempty"`
|
||||
CreatorId string `json:",omitempty"`
|
||||
CreatorName string `json:",omitempty"`
|
||||
}
|
||||
|
||||
func (s *Session) GetWishes() ([]Wish, error) {
|
||||
stmt := "SELECT wish.id, wish.name, wish.sent FROM wish WHERE wish.creator_id = ?1 AND wish.recipient_id = ?1 ORDER BY wish.sent"
|
||||
rows, err := db.Connection.Query(stmt, s.User().Id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Query execution failed: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
wishs := []Wish{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var name string
|
||||
var sent bool
|
||||
err = rows.Scan(&id, &name, &sent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to scan a row: %w", err)
|
||||
}
|
||||
wish := Wish{
|
||||
Id: id,
|
||||
Name: name,
|
||||
Sent: sent,
|
||||
}
|
||||
wishs = append(wishs, wish)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Rows returned an error: %w", err)
|
||||
}
|
||||
return wishs, nil
|
||||
}
|
||||
|
||||
// May return the id of the wish
|
||||
func (s *Session) MakeWish(name string) (string, error) {
|
||||
stmt := "INSERT INTO wish (name, recipient_id, creator_id) VALUES (?, ?, ?)"
|
||||
result, err := db.Connection.Exec(stmt, strings.TrimSpace(name), s.User().Id, s.User().Id)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Query execution failed: %w", err)
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get last insert id: %w", err)
|
||||
}
|
||||
wishId := strconv.FormatInt(id, 10)
|
||||
recordEventCreateWish(s.user.Id, wishId)
|
||||
return wishId, nil
|
||||
}
|
||||
|
||||
func (s *Session) deleteWishes(tx *sql.Tx, ids []string) error {
|
||||
stmt := "DELETE FROM wish WHERE wish.creator_id = ? AND wish.id = ?"
|
||||
for _, id := range ids {
|
||||
r, err := tx.Exec(stmt, s.User().Id, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rE, err := r.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rE < 1 {
|
||||
return fmt.Errorf("Wish deletion failed for '%s'", id)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) RevokeWishes(ids ...string) error {
|
||||
if len(ids) < 1 {
|
||||
return fmt.Errorf("Attempt to remove zero wishes")
|
||||
}
|
||||
|
||||
tx, err := db.Connection.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.deleteWishes(tx, ids)
|
||||
if err != nil {
|
||||
rollBackErr := tx.Rollback()
|
||||
if rollBackErr != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Session) GetOthersWishes(userReference string) ([]Wish, error) {
|
||||
otherUser, err := getUserByReference(userReference)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to get other user: %w", err)
|
||||
}
|
||||
if otherUser.Id == s.User().Id {
|
||||
return nil, errors.New("Use (s *Session) GetWishes() to view your own wishes")
|
||||
}
|
||||
stmt := "SELECT wish.id, wish.name, claimant.id, claimant.display_name, wish.sent, wish.creator_id, creator.display_name, wish.recipient_id FROM wish JOIN v_user AS user ON wish.recipient_id = user.id LEFT JOIN v_user AS claimant ON wish.claimant_id = claimant.id LEFT JOIN v_user AS creator ON wish.creator_id = creator.id WHERE user.id = ? ORDER BY wish.sent"
|
||||
rows, err := db.Connection.Query(stmt, otherUser.Id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to execute query: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
wishes := []Wish{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var name string
|
||||
var claimantId sql.NullString
|
||||
var claimantName sql.NullString
|
||||
var sent bool
|
||||
var creatorId string
|
||||
var creatorName string
|
||||
var recipientId string
|
||||
err = rows.Scan(&id, &name, &claimantId, &claimantName, &sent, &creatorId, &creatorName, &recipientId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to scan a row: %w", err)
|
||||
}
|
||||
wish := Wish{
|
||||
Id: id,
|
||||
Name: name,
|
||||
ClaimantId: claimantId.String,
|
||||
ClaimantName: claimantName.String,
|
||||
Sent: sent,
|
||||
CreatorId: creatorId,
|
||||
CreatorName: creatorName,
|
||||
RecipientId: recipientId,
|
||||
}
|
||||
wishes = append(wishes, wish)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Rows returned an error: %w", err)
|
||||
}
|
||||
return wishes, nil
|
||||
}
|
||||
|
||||
// NOTE: This could just be a field on the user... but only if we get this often
|
||||
func (u *User) WishCount() (int, error) {
|
||||
stmt := "SELECT COUNT(wish.id) AS wish_count FROM wish WHERE wish.creator_id = ?1 AND wish.recipient_id = ?1"
|
||||
var wishCount int
|
||||
err := db.Connection.QueryRow(stmt, u.Id).Scan(&wishCount)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return wishCount, nil
|
||||
}
|
||||
|
||||
func (s *Session) executeClaims(tx *sql.Tx, claims, unclaims []string) error {
|
||||
claimStmt := "UPDATE wish SET claimant_id = ? WHERE id = ?"
|
||||
unclaimStmt := "UPDATE wish SET claimant_id = NULL WHERE id = ?"
|
||||
for _, id := range claims {
|
||||
r, err := tx.Exec(claimStmt, s.User().Id, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rE, err := r.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rE < 1 {
|
||||
return fmt.Errorf("Wish claim failed for '%s'", id)
|
||||
}
|
||||
}
|
||||
for _, id := range unclaims {
|
||||
r, err := tx.Exec(unclaimStmt, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rE, err := r.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rE < 1 {
|
||||
return fmt.Errorf("Wish unclaim failed for '%s'", id)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Undertake or abandon wishes made by other users
|
||||
func (s *Session) ClaimWishes(claims, unclaims []string) error {
|
||||
lenClaims := len(claims)
|
||||
lenUnclaims := len(unclaims)
|
||||
// TODO: Would be nice if this used a request builder
|
||||
if lenClaims < 1 && lenUnclaims < 1 {
|
||||
return fmt.Errorf("Attempt to claim/unclaim zero wishes")
|
||||
}
|
||||
|
||||
tx, err := db.Connection.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.executeClaims(tx, claims, unclaims)
|
||||
if err != nil {
|
||||
rollBackErr := tx.Rollback()
|
||||
if rollBackErr != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: This could be atomic. See recordEvent function
|
||||
if lenClaims > 0 {
|
||||
recordEventClaimWishes(s.user.Id, claims...)
|
||||
}
|
||||
if lenUnclaims > 0 {
|
||||
recordEventUnclaimWishes(s.user.Id, unclaims...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func executeCompletions(tx *sql.Tx, claims []string) error {
|
||||
claimStmt := "UPDATE wish SET sent = 1 WHERE id = ?"
|
||||
for _, id := range claims {
|
||||
r, err := tx.Exec(claimStmt, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rE, err := r.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rE < 1 {
|
||||
return fmt.Errorf("Wish completion failed for '%s'", id)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) CompleteWishes(claims []string) error {
|
||||
// TODO: User ought not be able to interact with wishes outside their group network
|
||||
if len(claims) < 1 {
|
||||
return fmt.Errorf("Attempt to complete zero wishes")
|
||||
}
|
||||
|
||||
tx, err := db.Connection.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = executeCompletions(tx, claims)
|
||||
if err != nil {
|
||||
rollBackErr := tx.Rollback()
|
||||
if rollBackErr != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
recordEventCompleteWishes(s.user.Id, claims...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Session) SuggestWishForUser(otherUserReference string, wishName string) error {
|
||||
otherUser, err := GetUserByReference(otherUserReference)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stmt := "INSERT INTO wish (name, recipient_id, creator_id) VALUES (?, ?, ?)"
|
||||
_, err = db.Connection.Exec(stmt, wishName, otherUser.Id, u.User().Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) RecindWishesForUser(ids ...string) error {
|
||||
if len(ids) < 1 {
|
||||
return fmt.Errorf("Attempt to remove zero wishes")
|
||||
}
|
||||
|
||||
tx, err := db.Connection.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.deleteWishes(tx, ids)
|
||||
if err != nil {
|
||||
rollBackErr := tx.Rollback()
|
||||
if rollBackErr != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -1,61 +0,0 @@
|
||||
package lishwist_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"lishwist/assert"
|
||||
"lishwist/core/internal/fixtures"
|
||||
)
|
||||
|
||||
func TestMakeWish(t *testing.T) {
|
||||
s := fixtures.Login(t, "thomas", "123")
|
||||
|
||||
if _, err := s.MakeWish("apple"); err != nil {
|
||||
t.Fatalf("Failed to make wish 1: %s\n", err)
|
||||
}
|
||||
|
||||
if _, err := s.MakeWish(" A car "); err != nil {
|
||||
t.Fatalf("Failed to make wish 2: %s\n", err)
|
||||
}
|
||||
|
||||
wishes, err := s.GetWishes()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get wishes: %s\n", err)
|
||||
}
|
||||
|
||||
fixtures.AssertEq(t, "Number of wishes", 2, len(wishes))
|
||||
fixtures.AssertEq(t, "Wish 1 name", wishes[0].Name, "apple")
|
||||
fixtures.AssertEq(t, "Wish 2 name", wishes[1].Name, "A car")
|
||||
}
|
||||
|
||||
func TestClaimUnclaimWishes(t *testing.T) {
|
||||
err := fixtures.TestInit(t)
|
||||
assert.FatalErr(t, "initializing db", err)
|
||||
|
||||
thomas := fixtures.Login2(t, "thomas", "123")
|
||||
assert.FatalErr(t, "registering thomas", err)
|
||||
|
||||
caleb := fixtures.Login2(t, "caleb", "123")
|
||||
assert.FatalErr(t, "registering caleb", err)
|
||||
|
||||
food, err := caleb.MakeWish("food")
|
||||
assert.FatalErr(t, "making wish 1", err)
|
||||
|
||||
box, err := caleb.MakeWish("box")
|
||||
assert.FatalErr(t, "making wish 2", err)
|
||||
|
||||
drink, err := caleb.MakeWish("drink")
|
||||
assert.FatalErr(t, "making wish 3", err)
|
||||
|
||||
err = thomas.ClaimWishes([]string{food, box, drink}, []string{})
|
||||
assert.FatalErr(t, "claiming wishes", err)
|
||||
|
||||
err = thomas.ClaimWishes([]string{}, []string{food, box, drink})
|
||||
assert.FatalErr(t, "unclaiming wishes", err)
|
||||
|
||||
// FIXME: disabled for now because datetimes break this
|
||||
// events, err := thomas.Admin().ListEvents()
|
||||
// assert.FatalErr(t, "listing events", err)
|
||||
|
||||
// assert.JsonSnapshot(t, "TestClaimUnclaimWishes.snap.txt", events)
|
||||
}
|
||||
10
go.work
10
go.work
@ -1,7 +1,5 @@
|
||||
go 1.25.5
|
||||
go 1.23
|
||||
|
||||
use (
|
||||
./assert
|
||||
./core
|
||||
./http
|
||||
)
|
||||
toolchain go1.23.3
|
||||
|
||||
use ./server
|
||||
|
||||
12
go.work.sum
12
go.work.sum
@ -1,16 +1,12 @@
|
||||
github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIXMPAkHc=
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
|
||||
github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/ncruces/sort v0.1.5/go.mod h1:obJToO4rYr6VWP0Uw5FYymgYGt3Br4RXcs/JdKaXAPk=
|
||||
github.com/psanford/httpreadat v0.1.0/go.mod h1:Zg7P+TlBm3bYbyHTKv/EdtSJZn3qwbPwpfZ/I9GKCRE=
|
||||
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
|
||||
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
|
||||
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
|
||||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
|
||||
lukechampine.com/adiantum v1.1.1/go.mod h1:LrAYVnTYLnUtE/yMp5bQr0HstAf060YUF8nM0B6+rUw=
|
||||
lukechampine.com/uint128 v1.3.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
|
||||
modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y=
|
||||
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
top_level=$(git rev-parse --show-toplevel)
|
||||
git_version=$($top_level/scripts/git-version)
|
||||
|
||||
go build -ldflags=-X=lishwist/http/env.GitVersion=$git_version .
|
||||
@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
passwordHash := []byte(os.Args[1])
|
||||
password := []byte(os.Args[2])
|
||||
err := bcrypt.CompareHashAndPassword(passwordHash, password)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to match: ", err)
|
||||
}
|
||||
fmt.Println("Match!")
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
top_level=$(git rev-parse --show-toplevel)
|
||||
git_version=$($top_level/scripts/git-version)
|
||||
|
||||
go generate ../core/internal/db
|
||||
../scripts/withenv .env .env.local .env.dev .env.dev.local -- go run -ldflags=-X=lishwist/http/env.GitVersion=$git_version main.go
|
||||
47
http/env/env.go
vendored
47
http/env/env.go
vendored
@ -1,47 +0,0 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
func GuaranteeEnv(key string) string {
|
||||
variable, ok := os.LookupEnv(key)
|
||||
if !ok || variable == "" {
|
||||
log.Fatalln("Missing environment variable:", key)
|
||||
}
|
||||
return variable
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
DatabaseFile string
|
||||
SessionSecret string
|
||||
HostRootUrl string
|
||||
HostPort string
|
||||
ServePort string
|
||||
InDev bool
|
||||
HostUrl string
|
||||
}
|
||||
|
||||
var Configuration Config
|
||||
|
||||
func init() {
|
||||
Configuration.DatabaseFile = GuaranteeEnv("LISHWIST_DATABASE_FILE")
|
||||
Configuration.SessionSecret = GuaranteeEnv("LISHWIST_SESSION_SECRET")
|
||||
Configuration.HostRootUrl = GuaranteeEnv("LISHWIST_HOST_ROOT_URL")
|
||||
Configuration.HostPort = os.Getenv("LISHWIST_HOST_PORT")
|
||||
Configuration.ServePort = GuaranteeEnv("LISHWIST_SERVE_PORT")
|
||||
Configuration.InDev = os.Getenv("LISHWIST_IN_DEV") != ""
|
||||
Configuration.HostUrl = func() string {
|
||||
rawUrl := Configuration.HostRootUrl
|
||||
if Configuration.HostPort != "" {
|
||||
rawUrl += ":" + Configuration.HostPort
|
||||
}
|
||||
u, err := url.Parse(rawUrl)
|
||||
if err != nil {
|
||||
log.Fatalln("Couldn't parse host url:", err)
|
||||
}
|
||||
return u.String()
|
||||
}()
|
||||
}
|
||||
3
http/env/version.go
vendored
3
http/env/version.go
vendored
@ -1,3 +0,0 @@
|
||||
package env
|
||||
|
||||
var GitVersion string
|
||||
@ -1,14 +0,0 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NameAtPathValue(r *http.Request, pathValueName string) (stem string, ext string) {
|
||||
filename := r.PathValue(pathValueName)
|
||||
ext = filepath.Ext(filename)
|
||||
stem = strings.TrimSuffix(filename, ext)
|
||||
return
|
||||
}
|
||||
19
http/go.mod
19
http/go.mod
@ -1,19 +0,0 @@
|
||||
module lishwist/http
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/Teajey/rsvp v0.21.1
|
||||
github.com/gorilla/sessions v1.4.0
|
||||
golang.org/x/crypto v0.39.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Teajey/hmc v0.9.0
|
||||
github.com/gorilla/securecookie v1.1.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
)
|
||||
28
http/go.sum
28
http/go.sum
@ -1,28 +0,0 @@
|
||||
github.com/Teajey/hmc v0.6.0-rc.12 h1:Vkx6mJa3pfMlYx4vyI9LfVLHa8fNyl8LikMvXBfDGZk=
|
||||
github.com/Teajey/hmc v0.6.0-rc.12/go.mod h1:9K7SL4RyybNLHooqsZcyFoL6QGqqa2zPgIBidw4qWQY=
|
||||
github.com/Teajey/hmc v0.9.0 h1:OYDn95/ZheJlVQTXFIztsuU2jUG57XahcMpJZ5dM1VQ=
|
||||
github.com/Teajey/hmc v0.9.0/go.mod h1:9K7SL4RyybNLHooqsZcyFoL6QGqqa2zPgIBidw4qWQY=
|
||||
github.com/Teajey/rsvp v0.19.0-rc.2 h1:h4V7c3uGlSKajkWKt3fldFV+XTMEI8A4o05PMY8xv9A=
|
||||
github.com/Teajey/rsvp v0.19.0-rc.2/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q=
|
||||
github.com/Teajey/rsvp v0.21.1 h1:qDo0VHvatOKFG6oqsq3daPOq0TU9pjicdz5mwxwGMO4=
|
||||
github.com/Teajey/rsvp v0.21.1/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
|
||||
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
|
||||
github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ=
|
||||
github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
|
||||
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@ -1,20 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/session"
|
||||
)
|
||||
|
||||
type Private interface {
|
||||
ServeHTTP(*lishwist.Session, *session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body
|
||||
}
|
||||
|
||||
type PrivateFunc func(*lishwist.Session, *session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body
|
||||
|
||||
func (f PrivateFunc) ServeHTTP(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
return f(app, s, w, r)
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
|
||||
"lishwist/http/session"
|
||||
)
|
||||
|
||||
type Public interface {
|
||||
ServeHTTP(*session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body
|
||||
}
|
||||
|
||||
type PublicFunc func(*session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body
|
||||
|
||||
func (f PublicFunc) ServeHTTP(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
return f(s, w, r)
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func Generate() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
26
http/main.go
26
http/main.go
@ -1,26 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/env"
|
||||
"lishwist/http/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := lishwist.Init(env.Configuration.DatabaseFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to init Lishwist: %s\n", err)
|
||||
}
|
||||
|
||||
useSecureCookies := !env.Configuration.InDev
|
||||
r := server.Create(useSecureCookies)
|
||||
|
||||
log.Printf("Running at http://127.0.0.1:%s\n", env.Configuration.ServePort)
|
||||
err = http.ListenAndServe(":"+env.Configuration.ServePort, r)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to listen and server:", err)
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
func PrefixMovedPermanently(before, after string) rsvp.HandlerFunc {
|
||||
return func(w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
suffix := strings.TrimPrefix(r.RequestURI, before)
|
||||
return rsvp.Blank().StatusMovedPermanently(after + suffix)
|
||||
}
|
||||
}
|
||||
|
||||
func PrefixPermanentRedirect(before, after string) rsvp.HandlerFunc {
|
||||
return func(w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
suffix := strings.TrimPrefix(r.RequestURI, before)
|
||||
return rsvp.Blank().StatusPermanentRedirect(after + suffix)
|
||||
}
|
||||
}
|
||||
@ -1,104 +0,0 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/handler"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
)
|
||||
|
||||
type Private struct {
|
||||
inner *http.ServeMux
|
||||
store *session.Store
|
||||
rsvp rsvp.Config
|
||||
}
|
||||
|
||||
func (m *Private) GetSession(r *http.Request) *session.Session {
|
||||
innerSession, _ := m.store.Get(r, "lishwist_user")
|
||||
return session.New(innerSession)
|
||||
}
|
||||
|
||||
func newPrivate(store *session.Store, cfg rsvp.Config) Private {
|
||||
mux := http.NewServeMux()
|
||||
return Private{
|
||||
inner: mux,
|
||||
store: store,
|
||||
rsvp: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Private) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
m.inner.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func expectAppSession(next handler.Private) handler.PublicFunc {
|
||||
return func(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
sessionKey, ok := session.GetString("sessionKey")
|
||||
if !ok {
|
||||
log.Printf("Failed to get key from session\n")
|
||||
return response.Error("Something went wrong.").StatusInternalServerError()
|
||||
}
|
||||
|
||||
appSession, err := lishwist.SessionFromKey(sessionKey)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get session by key %v: %s\n", sessionKey, err)
|
||||
return response.Error("Something went wrong.").StatusInternalServerError()
|
||||
}
|
||||
if appSession == nil {
|
||||
log.Printf("Session not found under key: %s\n", sessionKey)
|
||||
return response.Error("Something went wrong.").StatusInternalServerError()
|
||||
}
|
||||
|
||||
return next.ServeHTTP(appSession, session, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
type sessionResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
s *session.Session
|
||||
r *http.Request
|
||||
}
|
||||
|
||||
func (w sessionResponseWriter) WriteHeader(statusCode int) {
|
||||
if w.s.WasWritten() {
|
||||
err := w.s.Save(w.r, w)
|
||||
if err != nil {
|
||||
log.Printf("Failed to save session: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func (m *Private) handleWithSession(pattern string, next handler.PublicFunc) {
|
||||
getSessionMiddleware := func(w http.ResponseWriter, r *http.Request) {
|
||||
s := m.GetSession(r)
|
||||
|
||||
rw := sessionResponseWriter{
|
||||
ResponseWriter: w,
|
||||
s: s,
|
||||
r: r,
|
||||
}
|
||||
|
||||
err := rsvp.Write(rw, r, m.rsvp, rsvp.HandlerFunc(func(w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
return next(s, w, r)
|
||||
}))
|
||||
if err != nil {
|
||||
log.Printf("rsvp failed to write a response: %s", err)
|
||||
}
|
||||
}
|
||||
m.inner.HandleFunc(pattern, getSessionMiddleware)
|
||||
}
|
||||
|
||||
func (m *Private) Handle(pattern string, handler handler.Private) {
|
||||
m.handleWithSession(pattern, expectAppSession(handler))
|
||||
}
|
||||
|
||||
func (m *Private) HandleFunc(pattern string, handler handler.PrivateFunc) {
|
||||
m.Handle(pattern, handler)
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"lishwist/http/handler"
|
||||
"lishwist/http/session"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type Public struct {
|
||||
Private
|
||||
}
|
||||
|
||||
func newPublic(store *session.Store, cfg rsvp.Config) Public {
|
||||
return Public{
|
||||
newPrivate(store, cfg),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Public) Handle(pattern string, handler handler.Public) {
|
||||
m.handleWithSession(pattern, handler.ServeHTTP)
|
||||
}
|
||||
|
||||
func (m *Public) HandleFunc(pattern string, handler handler.PublicFunc) {
|
||||
m.Handle(pattern, handler)
|
||||
}
|
||||
@ -1,76 +0,0 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/routing"
|
||||
"lishwist/http/session"
|
||||
"lishwist/http/templates"
|
||||
"lishwist/http/templates/text"
|
||||
)
|
||||
|
||||
type Serve struct {
|
||||
store *session.Store
|
||||
rsvpCfg rsvp.Config
|
||||
Public Public
|
||||
Private Private
|
||||
}
|
||||
|
||||
func (m *Serve) GetSession(r *http.Request) *session.Session {
|
||||
innerSession, _ := m.store.Get(r, "lishwist_user")
|
||||
return session.New(innerSession)
|
||||
}
|
||||
|
||||
func (m *Serve) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
innerSession, _ := m.store.Get(r, "lishwist_user")
|
||||
s := session.New(innerSession)
|
||||
_, inSession := s.GetString("sessionKey")
|
||||
|
||||
if inSession {
|
||||
m.Private.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
username, password, ok := r.BasicAuth()
|
||||
if ok {
|
||||
loggedIn := nil == routing.LoginMiddle(username, password, s, r)
|
||||
if s.WasWritten() {
|
||||
err := s.Save(r, w)
|
||||
if err != nil {
|
||||
log.Printf("Failed to save session: %s", err)
|
||||
}
|
||||
}
|
||||
if loggedIn {
|
||||
m.Private.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
m.Public.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func New(store *session.Store) *Serve {
|
||||
cfg := rsvp.Config{}
|
||||
cfg.HtmlTemplate = templates.Template
|
||||
cfg.TextTemplate = text.Template
|
||||
sv := Serve{
|
||||
store: store,
|
||||
rsvpCfg: cfg,
|
||||
Public: newPublic(store, cfg),
|
||||
Private: newPrivate(store, cfg),
|
||||
}
|
||||
return &sv
|
||||
}
|
||||
|
||||
func (m *Serve) HandleFunc(pattern string, hnd rsvp.HandlerFunc) {
|
||||
m.Public.HandleFunc(pattern, func(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
return hnd(w, r)
|
||||
})
|
||||
m.Private.HandleFunc(pattern, func(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
return hnd(w, r)
|
||||
})
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
package props
|
||||
|
||||
import "github.com/Teajey/hmc"
|
||||
|
||||
type Login struct {
|
||||
GeneralError string `json:",omitempty" xml:",omitempty"`
|
||||
SuccessfulRegistration bool `json:",omitempty" xml:",omitempty"`
|
||||
SuccessfulSetPassword bool `json:",omitempty" xml:",omitempty"`
|
||||
Username hmc.Input
|
||||
Password hmc.Input
|
||||
}
|
||||
|
||||
func NewLogin(username, password string) Login {
|
||||
return Login{
|
||||
Username: hmc.Input{
|
||||
Label: "Username",
|
||||
Name: "username",
|
||||
Required: true,
|
||||
Value: username,
|
||||
},
|
||||
Password: hmc.Input{
|
||||
Label: "Password",
|
||||
Name: "password",
|
||||
Type: "password",
|
||||
Required: true,
|
||||
Value: password,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Login) Validate() (valid bool) {
|
||||
_ = p.Username.Validate()
|
||||
|
||||
_ = p.Password.Validate()
|
||||
|
||||
return p.Username.Error == "" && p.Password.Error == ""
|
||||
}
|
||||
@ -1,85 +0,0 @@
|
||||
package props
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
|
||||
"github.com/Teajey/hmc"
|
||||
|
||||
"lishwist/http/templates"
|
||||
)
|
||||
|
||||
type RegisterForm struct {
|
||||
Username hmc.Input
|
||||
Password hmc.Input
|
||||
ConfirmPassword hmc.Input
|
||||
}
|
||||
|
||||
type Register struct {
|
||||
hmc.Namespace
|
||||
Nav templates.NavCollapse
|
||||
Error string `json:",omitempty" xml:",omitempty"`
|
||||
Warning template.HTML `json:",omitempty" xml:",omitempty"`
|
||||
Form hmc.Form[RegisterForm]
|
||||
}
|
||||
|
||||
func (p *RegisterForm) Validate() (valid bool) {
|
||||
valid = true
|
||||
|
||||
if p.Password.Value != p.ConfirmPassword.Value {
|
||||
p.ConfirmPassword.Error = "Passwords didn't match"
|
||||
valid = false
|
||||
}
|
||||
|
||||
_ = p.Username.Validate()
|
||||
|
||||
_ = p.Password.Validate()
|
||||
|
||||
_ = p.ConfirmPassword.Validate()
|
||||
|
||||
if valid {
|
||||
valid = p.Username.Error == "" && p.Password.Error == "" && p.ConfirmPassword.Error == ""
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func NewRegister(usernameVal, passwordVal, confirmPassVal string) Register {
|
||||
return Register{
|
||||
Namespace: hmc.NS(),
|
||||
Nav: templates.NavCollapse{
|
||||
Links: []hmc.Link{{
|
||||
Label: "Login",
|
||||
Href: "/",
|
||||
}},
|
||||
},
|
||||
Error: "",
|
||||
Warning: template.HTML(`<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p><p class="mb-0">Maybe use a password here that you don't use for important things...</p>`),
|
||||
Form: hmc.Form[RegisterForm]{
|
||||
Method: "POST",
|
||||
Elements: RegisterForm{
|
||||
Username: hmc.Input{
|
||||
Label: "Username",
|
||||
Name: "username",
|
||||
Required: true,
|
||||
MinLength: 4,
|
||||
Value: usernameVal,
|
||||
},
|
||||
Password: hmc.Input{
|
||||
Label: "Password",
|
||||
Type: "password",
|
||||
Name: "newPassword",
|
||||
Required: true,
|
||||
MinLength: 5,
|
||||
Value: passwordVal,
|
||||
},
|
||||
ConfirmPassword: hmc.Input{
|
||||
Label: "Confirm password",
|
||||
Type: "password",
|
||||
Name: "confirmPassword",
|
||||
Required: true,
|
||||
Value: confirmPassVal,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -1,197 +0,0 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/templates"
|
||||
"net/url"
|
||||
|
||||
"github.com/Teajey/hmc"
|
||||
)
|
||||
|
||||
type LinksList struct {
|
||||
Label string `json:"label" xml:",chardata"`
|
||||
Links []hmc.Link `json:"links" xml:"Link"`
|
||||
}
|
||||
|
||||
type HomeProps struct {
|
||||
hmc.Namespace
|
||||
Error string
|
||||
Warning string
|
||||
Navbar templates.NavCollapse `json:"-"`
|
||||
Wishlist Wishlist `json:"wishlist"`
|
||||
Todo Wishlist `json:"todo"`
|
||||
GroupsList LinksList `json:"groups" xml:"LinksList"`
|
||||
}
|
||||
|
||||
type Wish struct {
|
||||
Input hmc.Input `json:"input"`
|
||||
Recipient *hmc.Link `json:"recipient,omitempty" xml:",omitempty"`
|
||||
Sent bool `json:"sent" xml:"sent,attr"`
|
||||
}
|
||||
|
||||
type Wishes struct {
|
||||
Items []Wish `json:"items"`
|
||||
Select hmc.Select
|
||||
Intent hmc.Select
|
||||
}
|
||||
|
||||
type Wishlist struct {
|
||||
Label string `json:"label" xml:",chardata"`
|
||||
Wishes *hmc.Form[Wishes] `json:"wishes"`
|
||||
NewWish *hmc.Form[hmc.Input] `json:"new_wish"`
|
||||
}
|
||||
|
||||
func InitialWishlist(wishes []lishwist.Wish) Wishlist {
|
||||
list := Wishlist{
|
||||
Label: "Your wishlist",
|
||||
NewWish: &hmc.Form[hmc.Input]{
|
||||
Method: "POST",
|
||||
Elements: hmc.Input{
|
||||
Label: "Add gift idea",
|
||||
Name: "gift_name",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if len(wishes) < 1 {
|
||||
return list
|
||||
}
|
||||
|
||||
wishInputs := make([]hmc.Option, len(wishes))
|
||||
items := make([]Wish, len(wishes))
|
||||
|
||||
for i, w := range wishes {
|
||||
wsh := hmc.Option{
|
||||
Label: w.Name,
|
||||
Value: w.Id,
|
||||
}
|
||||
|
||||
wishInputs[i] = wsh
|
||||
items[i] = Wish{
|
||||
Input: hmc.Input{
|
||||
Label: w.Name,
|
||||
Value: w.Id,
|
||||
Type: "checkbox",
|
||||
Name: "wish",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
list.Wishes = &hmc.Form[Wishes]{
|
||||
Method: "POST",
|
||||
Elements: Wishes{
|
||||
Items: items,
|
||||
Select: hmc.Select{
|
||||
Label: "Wishes",
|
||||
Multiple: true,
|
||||
Name: "wish",
|
||||
Required: true,
|
||||
Options: wishInputs,
|
||||
},
|
||||
Intent: hmc.Select{
|
||||
Label: "Intent",
|
||||
Required: true,
|
||||
Name: "intent",
|
||||
Options: []hmc.Option{{
|
||||
Label: "Delete",
|
||||
Value: "delete",
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func InitialTodo(todo []lishwist.Wish) Wishlist {
|
||||
list := Wishlist{
|
||||
Label: "Your todo list",
|
||||
}
|
||||
|
||||
if len(todo) < 1 {
|
||||
return list
|
||||
}
|
||||
|
||||
wishInputs := make([]Wish, len(todo))
|
||||
options := make([]hmc.Option, len(todo))
|
||||
|
||||
for i, w := range todo {
|
||||
wsh := Wish{
|
||||
Input: hmc.Input{
|
||||
Label: w.Name,
|
||||
Type: "checkbox",
|
||||
},
|
||||
Recipient: &hmc.Link{
|
||||
Label: w.RecipientName,
|
||||
Href: "/lists/" + w.RecipientRef,
|
||||
},
|
||||
Sent: w.Sent,
|
||||
}
|
||||
opt := hmc.Option{
|
||||
Label: w.Name,
|
||||
Value: w.Id,
|
||||
}
|
||||
|
||||
if w.Sent {
|
||||
wsh.Input.Disabled = true
|
||||
opt.Disabled = true
|
||||
} else {
|
||||
wsh.Input.Name = "todo"
|
||||
wsh.Input.Value = w.Id
|
||||
}
|
||||
|
||||
wishInputs[i] = wsh
|
||||
options[i] = opt
|
||||
}
|
||||
|
||||
list.Wishes = &hmc.Form[Wishes]{
|
||||
Method: "POST",
|
||||
Elements: Wishes{
|
||||
Items: wishInputs,
|
||||
Select: hmc.Select{
|
||||
Label: "Todos",
|
||||
Multiple: true,
|
||||
Name: "todo",
|
||||
Required: true,
|
||||
Options: options,
|
||||
},
|
||||
Intent: hmc.Select{
|
||||
Label: "Intent",
|
||||
Required: true,
|
||||
Name: "intent",
|
||||
Options: []hmc.Option{
|
||||
{
|
||||
Label: "Unclaim",
|
||||
Value: "unclaim_todo",
|
||||
},
|
||||
{
|
||||
Label: "Complete",
|
||||
Value: "complete_todo",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func (w Wishlist) ExtractFormValues(form url.Values) {
|
||||
if w.NewWish != nil {
|
||||
_ = w.NewWish.Elements.ExtractValue(form)
|
||||
}
|
||||
|
||||
if w.Wishes == nil {
|
||||
return
|
||||
}
|
||||
|
||||
err := w.Wishes.Elements.Select.ExtractValue(form)
|
||||
if err != nil {
|
||||
w.Wishes.Elements.Select.Error = err.Error()
|
||||
}
|
||||
|
||||
err = w.Wishes.Elements.Intent.ExtractValue(form)
|
||||
if err != nil {
|
||||
w.Wishes.Elements.Intent.Error = err.Error()
|
||||
}
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lishwist/http/templates"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
func NotFound() rsvp.Body {
|
||||
return Error("Page not found").StatusNotFound()
|
||||
}
|
||||
|
||||
type errorProps struct {
|
||||
Navbar templates.NavCollapse
|
||||
Message string
|
||||
}
|
||||
|
||||
func Error(format string, a ...any) rsvp.Body {
|
||||
return rsvp.Body{
|
||||
Data: errorProps{
|
||||
Message: fmt.Sprintf(format, a...),
|
||||
Navbar: templates.DefaultNavCollapse(),
|
||||
},
|
||||
TemplateName: "error_page.gotmpl",
|
||||
}
|
||||
}
|
||||
|
||||
func Data(templateName string, body any) rsvp.Body {
|
||||
return rsvp.Body{
|
||||
Data: body,
|
||||
TemplateName: templateName,
|
||||
}
|
||||
}
|
||||
@ -1,132 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/props"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
"lishwist/http/templates"
|
||||
|
||||
"github.com/Teajey/hmc"
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type AccountProps struct {
|
||||
Navbar templates.NavCollapse
|
||||
GeneralError string `json:",omitempty"`
|
||||
PasswordFromAdmin bool `json:",omitempty"`
|
||||
Password hmc.Input
|
||||
ConfirmPassword hmc.Input
|
||||
}
|
||||
|
||||
func (p *AccountProps) Validate() (valid bool) {
|
||||
valid = true
|
||||
|
||||
if p.Password.Value != p.ConfirmPassword.Value {
|
||||
p.ConfirmPassword.Error = "Passwords didn't match"
|
||||
valid = false
|
||||
}
|
||||
|
||||
_ = p.Password.Validate()
|
||||
|
||||
_ = p.ConfirmPassword.Validate()
|
||||
|
||||
if valid {
|
||||
valid = p.Password.Error == "" && p.ConfirmPassword.Error == ""
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confirmPassVal string) *AccountProps {
|
||||
return &AccountProps{
|
||||
Navbar: templates.NavCollapse{
|
||||
Links: []hmc.Link{{Href: "/", Label: "Home"}},
|
||||
User: &templates.NavUser{
|
||||
Label: fmt.Sprintf("Logged in as '%s'", username),
|
||||
Logout: hmc.Form[hmc.Input]{
|
||||
Method: "POST",
|
||||
Action: "/logout",
|
||||
Elements: hmc.Input{
|
||||
Label: "Logout",
|
||||
Type: "submit",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
PasswordFromAdmin: passwordFromAdmin,
|
||||
Password: hmc.Input{
|
||||
Type: "password",
|
||||
Name: "new_password",
|
||||
Required: true,
|
||||
MinLength: 5,
|
||||
Value: passwordVal,
|
||||
},
|
||||
ConfirmPassword: hmc.Input{
|
||||
Type: "password",
|
||||
Name: "confirm_password",
|
||||
Required: true,
|
||||
Value: confirmPassVal,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Account(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
user := app.User()
|
||||
props := NewAccountProps(user.Name, user.PasswordFromAdmin, "", "")
|
||||
|
||||
flash := session.FlashGet()
|
||||
|
||||
flashProps, _ := flash.(*AccountProps)
|
||||
if flashProps != nil {
|
||||
props.GeneralError = flashProps.GeneralError
|
||||
props.ConfirmPassword.Error = flashProps.ConfirmPassword.Error
|
||||
}
|
||||
|
||||
return response.Data("account.gotmpl", props)
|
||||
}
|
||||
|
||||
func AccountPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
user := app.User()
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
intent := r.Form.Get("intent")
|
||||
if intent != "set_password" {
|
||||
return response.Error("Invalid intent %q", intent).StatusBadRequest()
|
||||
}
|
||||
|
||||
newPassword := r.Form.Get("new_password")
|
||||
confirmPassword := r.Form.Get("confirm_password")
|
||||
|
||||
account := NewAccountProps(user.Name, user.PasswordFromAdmin, newPassword, confirmPassword)
|
||||
|
||||
valid := account.Validate()
|
||||
account.Password.Value = ""
|
||||
account.ConfirmPassword.Value = ""
|
||||
if !valid {
|
||||
log.Printf("Invalid account submission: %#v\n", account)
|
||||
session.FlashSet(&account)
|
||||
return rsvp.Data(account).StatusSeeOther("/account")
|
||||
}
|
||||
|
||||
err = user.SetPassword(newPassword)
|
||||
if err != nil {
|
||||
account.GeneralError = "Something went wrong."
|
||||
log.Printf("Set password failed: %s\n", err)
|
||||
session.FlashSet(&account)
|
||||
return rsvp.Data(account).StatusSeeOther("/account")
|
||||
}
|
||||
|
||||
session.RemoveValue("sessionKey")
|
||||
|
||||
session.FlashSet(&props.Login{SuccessfulSetPassword: true})
|
||||
return rsvp.Data("Set password successful!").StatusSeeOther("/")
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/env"
|
||||
"lishwist/http/session"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type HealthProps struct {
|
||||
GitVersion string
|
||||
Config env.Config
|
||||
}
|
||||
|
||||
func Health(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
if app.Admin() == nil {
|
||||
return rsvp.Blank()
|
||||
}
|
||||
|
||||
return rsvp.Body{Data: HealthProps{
|
||||
GitVersion: env.GitVersion,
|
||||
Config: env.Configuration,
|
||||
}}
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
package routing
|
||||
@ -1,31 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type Events struct {
|
||||
Events []lishwist.Event `xml:"Event"`
|
||||
}
|
||||
|
||||
func EventList(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
admin := app.Admin()
|
||||
if admin == nil {
|
||||
log.Println("Attempt to access EventList by non-admin. Responding 404 Not Found.")
|
||||
return response.NotFound()
|
||||
}
|
||||
|
||||
events, err := admin.ListEvents()
|
||||
if err != nil {
|
||||
log.Printf("Admin failed to ListEvents: %s\n", err)
|
||||
return response.Error("Failed to get events: %s", err).StatusInternalServerError()
|
||||
}
|
||||
|
||||
return response.Data("", Events{Events: events})
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/file"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
"lishwist/http/templates"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type foreignWishlistProps struct {
|
||||
Navbar templates.NavCollapse
|
||||
CurrentUserId string
|
||||
CurrentUserName string
|
||||
Username string
|
||||
Gifts []lishwist.Wish
|
||||
}
|
||||
|
||||
func ForeignWishlist(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
userReference, _ := file.NameAtPathValue(r, "userReference")
|
||||
user := app.User()
|
||||
if user.Reference == userReference {
|
||||
return rsvp.Data("You're not allowed to view your own wishlist!").StatusFound("/")
|
||||
}
|
||||
otherUser, err := lishwist.GetUserByReference(userReference)
|
||||
if err != nil {
|
||||
log.Printf("Couldn't get user by reference %q: %s\n", userReference, err)
|
||||
return response.Error("An error occurred while fetching this user :(").StatusInternalServerError()
|
||||
}
|
||||
if otherUser == nil {
|
||||
return response.Error("User not found").StatusInternalServerError()
|
||||
}
|
||||
wishes, err := app.GetOthersWishes(userReference)
|
||||
if err != nil {
|
||||
log.Printf("%q couldn't get wishes of other user %q: %s\n", user.Name, otherUser.Name, err)
|
||||
return response.Error("An error occurred while fetching this user :(").StatusInternalServerError()
|
||||
}
|
||||
p := foreignWishlistProps{Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin), CurrentUserId: user.Id, Username: otherUser.Name, Gifts: wishes}
|
||||
return response.Data("foreign_wishlist.gotmpl", p)
|
||||
}
|
||||
|
||||
type publicWishlistProps struct {
|
||||
Navbar templates.NavCollapse
|
||||
Username string
|
||||
GiftCount int
|
||||
Referrer string
|
||||
}
|
||||
|
||||
func PublicWishlist(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
userReference, _ := file.NameAtPathValue(r, "userReference")
|
||||
otherUser, err := lishwist.GetUserByReference(userReference)
|
||||
if err != nil {
|
||||
log.Printf("Couldn't get user by reference %q on public wishlist: %s\n", userReference, err)
|
||||
return response.Error("An error occurred while fetching this user :(").StatusInternalServerError()
|
||||
}
|
||||
if otherUser == nil {
|
||||
return response.Error("User not found").StatusInternalServerError()
|
||||
}
|
||||
giftCount, err := otherUser.WishCount()
|
||||
if err != nil {
|
||||
log.Printf("Couldn't get wishes of user %q on public wishlist: %s\n", otherUser.Name, err)
|
||||
return response.Error("An error occurred while fetching this user :(").StatusInternalServerError()
|
||||
}
|
||||
p := publicWishlistProps{Navbar: templates.DefaultNavCollapse(), Username: otherUser.Name, GiftCount: giftCount, Referrer: r.URL.Path}
|
||||
return response.Data("public_foreign_wishlist.gotmpl", p)
|
||||
}
|
||||
@ -1,168 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/file"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
"lishwist/http/templates"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type GroupProps struct {
|
||||
Navbar templates.NavCollapse
|
||||
Group *lishwist.Group
|
||||
Referrer string
|
||||
}
|
||||
|
||||
func AdminGroup(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
reference, _ := file.NameAtPathValue(r, "groupReference")
|
||||
group, err := lishwist.GetGroupByReference(reference)
|
||||
if err != nil {
|
||||
return response.Error("Couldn't get group: %s", err).StatusInternalServerError()
|
||||
}
|
||||
if group == nil {
|
||||
return response.Error("Group not found").StatusNotFound()
|
||||
}
|
||||
user := app.User()
|
||||
if !user.IsAdmin {
|
||||
index := group.MemberIndex(user.Id)
|
||||
group.Members = slices.Delete(group.Members, index, index+1)
|
||||
}
|
||||
p := GroupProps{
|
||||
Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin),
|
||||
Group: group,
|
||||
}
|
||||
return response.Data("group_page.gotmpl", p)
|
||||
}
|
||||
|
||||
func Group(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
user := app.User()
|
||||
if user.IsAdmin {
|
||||
return AdminGroup(app, w, r)
|
||||
}
|
||||
groupReference, _ := file.NameAtPathValue(r, "groupReference")
|
||||
group, err := app.GetGroupByReference(groupReference)
|
||||
if err != nil {
|
||||
log.Printf("Couldn't get group: %s\n", err)
|
||||
return response.Error("An error occurred while fetching this group :(").StatusInternalServerError()
|
||||
}
|
||||
if group == nil {
|
||||
return response.Error("Group not found. (It might be because you're not a member)").StatusNotFound()
|
||||
}
|
||||
index := group.MemberIndex(user.Id)
|
||||
group.Members = slices.Delete(group.Members, index, index+1)
|
||||
p := GroupProps{
|
||||
Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin),
|
||||
Group: group,
|
||||
}
|
||||
return response.Data("group_page.gotmpl", p)
|
||||
}
|
||||
|
||||
func PublicGroup(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
groupReference, _ := file.NameAtPathValue(r, "groupReference")
|
||||
group, err := lishwist.GetGroupByReference(groupReference)
|
||||
if err != nil {
|
||||
log.Printf("Couldn't get group: %s\n", err)
|
||||
return response.Error("An error occurred while fetching this group :(").StatusInternalServerError()
|
||||
}
|
||||
if group == nil {
|
||||
return response.Error("Group not found").StatusNotFound()
|
||||
}
|
||||
p := GroupProps{
|
||||
Navbar: templates.DefaultNavCollapse(),
|
||||
Group: group,
|
||||
Referrer: r.URL.Path,
|
||||
}
|
||||
return response.Data("public_group_page.gotmpl", p)
|
||||
}
|
||||
|
||||
func GroupPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
admin := app.Admin()
|
||||
if admin == nil {
|
||||
return response.NotFound()
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
var group *lishwist.Group
|
||||
|
||||
reference, _ := file.NameAtPathValue(r, "groupReference")
|
||||
name := r.Form.Get("name")
|
||||
addUsers := r.Form["addUser"]
|
||||
removeUsers := r.Form["removeUser"]
|
||||
|
||||
if name != "" {
|
||||
createdGroup, err := admin.CreateGroup(name, reference)
|
||||
if err != nil {
|
||||
return response.Error("Failed to create group: %s", err).StatusInternalServerError()
|
||||
}
|
||||
group = createdGroup
|
||||
} else {
|
||||
existingGroup, err := lishwist.GetGroupByReference(reference)
|
||||
if err != nil {
|
||||
return response.Error("Failed to get group: %s", err).StatusInternalServerError()
|
||||
}
|
||||
if existingGroup == nil {
|
||||
return response.Error("Group not found: %s", err).StatusNotFound()
|
||||
}
|
||||
group = existingGroup
|
||||
|
||||
for _, userId := range removeUsers {
|
||||
index := group.MemberIndex(userId)
|
||||
if index == -1 {
|
||||
return response.Error("Group %q does not contain a user with id %s", reference, userId).StatusBadRequest()
|
||||
}
|
||||
err = admin.RemoveUserFromGroup(userId, group.Id)
|
||||
if err != nil {
|
||||
return response.Error("On group %q failed to remove user with id %s: %s", reference, userId, err).StatusInternalServerError()
|
||||
}
|
||||
group.Members = slices.Delete(group.Members, index, index+1)
|
||||
}
|
||||
}
|
||||
|
||||
for _, userId := range addUsers {
|
||||
user, err := admin.GetUser(userId)
|
||||
if err != nil {
|
||||
return response.Error("Groups exists, but a user with id %s could not be fetched: %s", userId, err).StatusInternalServerError()
|
||||
}
|
||||
if user == nil {
|
||||
return response.Error("Groups exists, but a user with id %s does not exist", userId).StatusInternalServerError()
|
||||
}
|
||||
err = admin.AddUserToGroup(user.Id, group.Id)
|
||||
if err != nil {
|
||||
return response.Error("Groups exists, but failed to add user with id %s: %s", userId, err).StatusInternalServerError()
|
||||
}
|
||||
group.Members = append(group.Members, *user)
|
||||
}
|
||||
|
||||
return response.Data("", group)
|
||||
}
|
||||
|
||||
type GroupList struct {
|
||||
XMLName xml.Name `xml:"Groups" json:"-"`
|
||||
Groups []lishwist.Group `xml:"Group"`
|
||||
}
|
||||
|
||||
func Groups(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
admin := app.Admin()
|
||||
if admin == nil {
|
||||
return response.NotFound()
|
||||
}
|
||||
|
||||
groups, err := admin.ListGroups()
|
||||
if err != nil {
|
||||
return response.Error("Failed to get groups: %s", err).StatusInternalServerError()
|
||||
}
|
||||
|
||||
return response.Data("", GroupList{Groups: groups})
|
||||
}
|
||||
@ -1,131 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/resources"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
"lishwist/http/templates"
|
||||
|
||||
"github.com/Teajey/hmc"
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
func mapKeysIntersect(a, b map[string][]string) []string {
|
||||
var out []string
|
||||
for k := range a {
|
||||
if _, ok := b[k]; ok {
|
||||
out = append(out, k)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
w.DefaultTemplateName("home.gotmpl")
|
||||
|
||||
gifts, err := app.GetWishes()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get gifts: %s\n", err)
|
||||
return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError()
|
||||
}
|
||||
user := app.User()
|
||||
todo, err := user.GetTodo()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get todo: %s\n", err)
|
||||
return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError()
|
||||
}
|
||||
groups, err := app.GetGroups()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get groups: %s\n", err)
|
||||
return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError()
|
||||
}
|
||||
groupLinks := make([]hmc.Link, len(groups))
|
||||
for i, g := range groups {
|
||||
groupLinks[i] = hmc.Link{
|
||||
Label: g.Name,
|
||||
Href: fmt.Sprintf("groups/%s", g.Reference),
|
||||
}
|
||||
}
|
||||
navbar := templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin)
|
||||
navbar.Links = nil
|
||||
p := resources.HomeProps{
|
||||
Namespace: hmc.NS(),
|
||||
Navbar: navbar,
|
||||
Wishlist: resources.InitialWishlist(gifts),
|
||||
Todo: resources.InitialTodo(todo),
|
||||
GroupsList: resources.LinksList{
|
||||
Label: "Your groups",
|
||||
Links: groupLinks,
|
||||
},
|
||||
}
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
return rsvp.Data(p)
|
||||
}
|
||||
|
||||
err = r.ParseForm()
|
||||
if err != nil {
|
||||
p.Error = "Failed to parse form"
|
||||
return rsvp.Data(p).StatusUnprocessableEntity()
|
||||
}
|
||||
|
||||
if len(r.Form) < 1 {
|
||||
p.Error = "No parameters provided"
|
||||
return rsvp.Data(p).StatusBadRequest()
|
||||
}
|
||||
|
||||
wishForm := maps.Clone(r.Form)
|
||||
p.Wishlist.ExtractFormValues(wishForm)
|
||||
p.Todo.ExtractFormValues(r.Form)
|
||||
|
||||
extra := mapKeysIntersect(wishForm, r.Form)
|
||||
if len(extra) > 0 {
|
||||
p.Warning = "Extraneous form values"
|
||||
}
|
||||
|
||||
if p.Wishlist.Wishes != nil && p.Wishlist.Wishes.Elements.Intent.Value() == "delete" {
|
||||
wishesToDelete := slices.Collect(p.Wishlist.Wishes.Elements.Select.Values())
|
||||
err := app.RevokeWishes(wishesToDelete...)
|
||||
if err != nil {
|
||||
log.Printf("Failed to revoke wishes: %s\n", err)
|
||||
p.Wishlist.Wishes.Elements.Intent.Error = "Something went wrong"
|
||||
return rsvp.Data(p).StatusInternalServerError()
|
||||
}
|
||||
}
|
||||
|
||||
if p.Wishlist.NewWish.Elements.Value != "" {
|
||||
_, err := app.MakeWish(p.Wishlist.NewWish.Elements.Value)
|
||||
if err != nil {
|
||||
log.Printf("Failed to make wish: %s\n", err)
|
||||
p.Wishlist.NewWish.Elements.Error = "Something went wrong"
|
||||
return rsvp.Data(p).StatusInternalServerError()
|
||||
}
|
||||
}
|
||||
|
||||
if p.Todo.Wishes != nil && p.Todo.Wishes.Elements.Intent.Value() == "unclaim_todo" {
|
||||
todosToChange := slices.Collect(p.Todo.Wishes.Elements.Select.Values())
|
||||
err := app.ClaimWishes([]string{}, todosToChange)
|
||||
if err != nil {
|
||||
log.Printf("Failed to unclaim wish: %s\n", err)
|
||||
p.Todo.Wishes.Elements.Intent.Error = "Something went wrong"
|
||||
return rsvp.Data(p).StatusInternalServerError()
|
||||
}
|
||||
} else if p.Todo.Wishes != nil && p.Todo.Wishes.Elements.Intent.Value() == "complete_todo" {
|
||||
todosToChange := slices.Collect(p.Todo.Wishes.Elements.Select.Values())
|
||||
err := app.CompleteWishes(todosToChange)
|
||||
if err != nil {
|
||||
log.Printf("Failed to complete wish: %s\n", err)
|
||||
p.Todo.Wishes.Elements.Intent.Error = "Something went wrong"
|
||||
return rsvp.Data(p).StatusInternalServerError()
|
||||
}
|
||||
}
|
||||
|
||||
return rsvp.Data(p).StatusSeeOther(".")
|
||||
}
|
||||
@ -1,113 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/props"
|
||||
"lishwist/http/session"
|
||||
|
||||
"github.com/Teajey/hmc"
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type LoginPage struct {
|
||||
hmc.Namespace
|
||||
Form hmc.Form[props.Login]
|
||||
RegisterLink hmc.Link
|
||||
}
|
||||
|
||||
func NewLoginPage() LoginPage {
|
||||
return LoginPage{
|
||||
Namespace: hmc.NS(),
|
||||
Form: hmc.Form[props.Login]{
|
||||
Method: "POST",
|
||||
},
|
||||
RegisterLink: hmc.Link{
|
||||
Label: "Register",
|
||||
Href: "/register",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Login(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
login := props.NewLogin("", "")
|
||||
|
||||
flash := s.FlashGet()
|
||||
flashProps, ok := flash.(*props.Login)
|
||||
if ok {
|
||||
login.Username.Value = flashProps.Username.Value
|
||||
|
||||
login.GeneralError = flashProps.GeneralError
|
||||
login.Username.Error = flashProps.Username.Error
|
||||
login.Password.Error = flashProps.Password.Error
|
||||
|
||||
login.SuccessfulRegistration = flashProps.SuccessfulRegistration
|
||||
login.SuccessfulSetPassword = flashProps.SuccessfulSetPassword
|
||||
}
|
||||
|
||||
body := NewLoginPage()
|
||||
body.Form.Elements = login
|
||||
return rsvp.Body{TemplateName: "login.gotmpl", Data: body}
|
||||
}
|
||||
|
||||
func LoginMiddle(username, password string, session *session.Session, r *http.Request) *props.Login {
|
||||
login := props.NewLogin(username, password)
|
||||
|
||||
valid := login.Validate()
|
||||
login.Password.Value = ""
|
||||
if !valid {
|
||||
session.FlashSet(&login)
|
||||
log.Printf("Invalid login submission: %#v\n", login)
|
||||
return &login
|
||||
}
|
||||
|
||||
appSession, err := lishwist.Login(username, password, time.Hour*24)
|
||||
if err != nil {
|
||||
var targ lishwist.ErrorInvalidCredentials
|
||||
switch {
|
||||
case errors.As(err, &targ):
|
||||
login.GeneralError = "Username or password invalid. If you're having trouble accessing your account, you may want to consider asking the System Admin (Thomas) to reset your password"
|
||||
session.FlashSet(&login)
|
||||
log.Printf("Invalid credentials: %s: %#v\n", err, login)
|
||||
default:
|
||||
login.GeneralError = "Something went wrong."
|
||||
session.FlashSet(&login)
|
||||
log.Printf("Login error: %s\n", err)
|
||||
}
|
||||
|
||||
return &login
|
||||
}
|
||||
|
||||
session.SetID("")
|
||||
session.SetValue("sessionKey", appSession.Key)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoginPost(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
page := NewLoginPage()
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
page.Form.Elements.GeneralError = "Failed to parse form"
|
||||
return rsvp.Data(page).StatusUnprocessableEntity()
|
||||
}
|
||||
|
||||
username := r.Form.Get("username")
|
||||
password := r.Form.Get("password")
|
||||
|
||||
login := LoginMiddle(username, password, session, r)
|
||||
if login != nil {
|
||||
page.Form.Elements = *login
|
||||
return rsvp.Data(page).StatusSeeOther(".")
|
||||
}
|
||||
|
||||
referrer := r.URL.Query().Get("referrer")
|
||||
|
||||
return rsvp.Data("Login successful!").StatusSeeOther(cmp.Or(referrer, "."))
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/session"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
func LogoutPost(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
s.Options().MaxAge = 0
|
||||
s.ClearValues()
|
||||
|
||||
return rsvp.Data("Logout successful").StatusSeeOther("/")
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"lishwist/http/response"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
func NotFound(w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
return response.Error("Page not found").StatusNotFound()
|
||||
}
|
||||
@ -1,78 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/props"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
func Register(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
register := props.NewRegister("", "", "")
|
||||
|
||||
flash := session.FlashGet()
|
||||
|
||||
flashProps, _ := flash.(*props.Register)
|
||||
if flashProps != nil {
|
||||
register.Form.Elements.Username.Value = flashProps.Form.Elements.Username.Value
|
||||
|
||||
register.Error = flashProps.Error
|
||||
register.Form.Elements.Username.Error = flashProps.Form.Elements.Username.Error
|
||||
register.Form.Elements.ConfirmPassword.Error = flashProps.Form.Elements.ConfirmPassword.Error
|
||||
}
|
||||
|
||||
return response.Data("register.gotmpl", register)
|
||||
}
|
||||
|
||||
func RegisterPost(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
var confirmPassword string
|
||||
username, newPassword, ok := r.BasicAuth()
|
||||
if ok {
|
||||
confirmPassword = newPassword
|
||||
} else {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
username = r.Form.Get("username")
|
||||
newPassword = r.Form.Get("newPassword")
|
||||
confirmPassword = r.Form.Get("confirmPassword")
|
||||
}
|
||||
|
||||
register := props.NewRegister(username, newPassword, confirmPassword)
|
||||
|
||||
valid := register.Form.Elements.Validate()
|
||||
register.Form.Elements.Password.Value = ""
|
||||
register.Form.Elements.ConfirmPassword.Value = ""
|
||||
if !valid {
|
||||
s.FlashSet(®ister)
|
||||
log.Printf("Invalid register submission: %#v\n", register)
|
||||
return rsvp.Data(register).StatusSeeOther(r.URL.Path)
|
||||
}
|
||||
|
||||
_, err := lishwist.Register(username, newPassword)
|
||||
if err != nil {
|
||||
if errors.Is(err, lishwist.ErrorUsernameTaken) {
|
||||
register.Form.Elements.Username.Error = "Username is taken"
|
||||
} else {
|
||||
register.Error = "Something went wrong."
|
||||
}
|
||||
s.FlashSet(®ister)
|
||||
log.Printf("Registration failed: %s\n", err)
|
||||
return rsvp.Data(register).StatusSeeOther(r.URL.Path)
|
||||
}
|
||||
|
||||
s.FlashSet(&props.Login{SuccessfulRegistration: true})
|
||||
referrer := r.URL.Query().Get("referrer")
|
||||
if referrer != "" {
|
||||
return rsvp.Data("Registration successful!").StatusSeeOther("/login?=" + referrer)
|
||||
} else {
|
||||
return rsvp.Data("Registration successful!").StatusSeeOther("/")
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/response"
|
||||
)
|
||||
|
||||
func TodoUpdate(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
switch r.Form.Get("intent") {
|
||||
case "unclaim_todo":
|
||||
unclaims := r.Form["gift"]
|
||||
err := app.ClaimWishes([]string{}, unclaims)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to update claim...").StatusInternalServerError()
|
||||
}
|
||||
case "complete_todo":
|
||||
claims := r.Form["gift"]
|
||||
err := app.CompleteWishes(claims)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to complete gifts...").StatusInternalServerError()
|
||||
}
|
||||
default:
|
||||
return response.Error("Invalid intent").StatusBadRequest()
|
||||
}
|
||||
return rsvp.Data("Update successful").StatusSeeOther("/")
|
||||
}
|
||||
@ -1,100 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/file"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
type UserList struct {
|
||||
XMLName xml.Name `xml:"Users" json:"-"`
|
||||
Users []lishwist.User `xml:"User"`
|
||||
}
|
||||
|
||||
func Users(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
admin := app.Admin()
|
||||
if admin == nil {
|
||||
return response.NotFound()
|
||||
}
|
||||
|
||||
users, err := admin.ListUsers()
|
||||
if err != nil {
|
||||
return response.Error("Failed to get users: %s", err).StatusInternalServerError()
|
||||
}
|
||||
|
||||
return response.Data("", UserList{Users: users})
|
||||
}
|
||||
|
||||
func User(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
admin := app.Admin()
|
||||
if admin == nil {
|
||||
return response.NotFound()
|
||||
}
|
||||
|
||||
reference, _ := file.NameAtPathValue(r, "userReference")
|
||||
|
||||
user, err := lishwist.GetUserByReference(reference)
|
||||
if err != nil {
|
||||
return response.Error("Failed to get user: %s", err).StatusInternalServerError()
|
||||
}
|
||||
if user == nil {
|
||||
return response.Error("User not found").StatusNotFound()
|
||||
}
|
||||
|
||||
return response.Data("", user)
|
||||
}
|
||||
|
||||
func UserPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
admin := app.Admin()
|
||||
if admin == nil {
|
||||
return response.NotFound()
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
reference, _ := file.NameAtPathValue(r, "userReference")
|
||||
|
||||
intent := r.Form.Get("intent")
|
||||
|
||||
if intent != "" {
|
||||
switch intent {
|
||||
case "delete":
|
||||
if reference == app.User().Reference {
|
||||
return response.Error("You cannot delete yourself.").StatusForbidden()
|
||||
}
|
||||
err = admin.UserSetLive(reference, false)
|
||||
if err != nil {
|
||||
return response.Error("Failed to delete user: %s", err).StatusInternalServerError()
|
||||
}
|
||||
case "rename":
|
||||
name := r.Form.Get("display_name")
|
||||
err = admin.RenameUser(reference, name)
|
||||
if err != nil {
|
||||
return response.Error("Failed to rename user: %s", err).StatusInternalServerError()
|
||||
}
|
||||
case "set_password":
|
||||
newPassword := r.Form.Get("new_password")
|
||||
err = admin.SetUserPassword(reference, newPassword)
|
||||
if err != nil {
|
||||
return response.Error("Failed to set new password: %s", err).StatusInternalServerError()
|
||||
}
|
||||
default:
|
||||
return response.Error("Invalid intent %q", intent).StatusBadRequest()
|
||||
}
|
||||
}
|
||||
|
||||
user, err := lishwist.GetUserByReference(reference)
|
||||
if err != nil {
|
||||
return response.Error("Failed to get user: %s", err).StatusInternalServerError()
|
||||
}
|
||||
|
||||
return response.Data("", user)
|
||||
}
|
||||
@ -1,97 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/file"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
)
|
||||
|
||||
func WishlistAdd(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
newGiftName := r.Form.Get("gift_name")
|
||||
_, err = app.MakeWish(newGiftName)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to add gift.").StatusInternalServerError()
|
||||
}
|
||||
return rsvp.Data("Wish added!").StatusSeeOther("/")
|
||||
}
|
||||
|
||||
func WishlistDelete(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
targets := r.Form["gift"]
|
||||
err = app.RevokeWishes(targets...)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to remove gifts.").StatusInternalServerError()
|
||||
}
|
||||
return rsvp.Data("Wish deleted").StatusSeeOther("/")
|
||||
}
|
||||
|
||||
func ForeignWishlistPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
userReference, _ := file.NameAtPathValue(r, "userReference")
|
||||
resp := rsvp.Data("Update successful").StatusSeeOther("/lists/" + userReference)
|
||||
intent := r.Form.Get("intent")
|
||||
switch intent {
|
||||
case "claim":
|
||||
claims := r.Form["unclaimed"]
|
||||
unclaims := r.Form["claimed"]
|
||||
err := app.ClaimWishes(claims, unclaims)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to update claim...").StatusInternalServerError()
|
||||
}
|
||||
resp.Data = "Successfully claimed wishes"
|
||||
case "complete":
|
||||
claims := r.Form["claimed"]
|
||||
err := app.CompleteWishes(claims)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to complete gifts...").StatusInternalServerError()
|
||||
}
|
||||
resp.Data = "Successfully completed wishes"
|
||||
case "add":
|
||||
wishName := r.Form.Get("gift_name")
|
||||
if wishName == "" {
|
||||
return response.Error("Gift name not provided").StatusBadRequest()
|
||||
}
|
||||
err := app.SuggestWishForUser(userReference, wishName)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to add gift idea to other user...").StatusInternalServerError()
|
||||
}
|
||||
resp.Data = "Successfully added wishes"
|
||||
case "delete":
|
||||
claims := r.Form["unclaimed"]
|
||||
unclaims := r.Form["claimed"]
|
||||
gifts := append(claims, unclaims...)
|
||||
err := app.RecindWishesForUser(gifts...)
|
||||
if err != nil {
|
||||
log.Printf("%s\n", err)
|
||||
return response.Error("Failed to remove gift idea for other user...").StatusInternalServerError()
|
||||
}
|
||||
resp.Data = "Successfully removed wishes"
|
||||
default:
|
||||
return response.Error("Invalid intent %q", intent).StatusBadRequest()
|
||||
}
|
||||
return resp
|
||||
}
|
||||
@ -1,87 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"net/http"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/env"
|
||||
"lishwist/http/file"
|
||||
"lishwist/http/middleware"
|
||||
"lishwist/http/mux"
|
||||
"lishwist/http/props"
|
||||
"lishwist/http/routing"
|
||||
"lishwist/http/session"
|
||||
|
||||
"github.com/Teajey/rsvp"
|
||||
)
|
||||
|
||||
func Create(useSecureCookies bool) *mux.Serve {
|
||||
gob.Register(&props.Register{})
|
||||
gob.Register(&props.Login{})
|
||||
gob.Register(&routing.AccountProps{})
|
||||
|
||||
store := session.NewInMemoryStore([]byte(env.Configuration.SessionSecret))
|
||||
store.Options.MaxAge = 86_400 // 24 hours in seconds
|
||||
store.Options.Secure = useSecureCookies
|
||||
store.Options.HttpOnly = true
|
||||
store.Options.Path = "/"
|
||||
store.Options.SameSite = http.SameSiteLaxMode
|
||||
|
||||
r := mux.New(store)
|
||||
|
||||
r.Public.HandleFunc("GET /groups/{groupReference}", routing.PublicGroup)
|
||||
r.Public.HandleFunc("GET /lists/{userReference}", routing.PublicWishlist)
|
||||
r.Public.HandleFunc("GET /", routing.Login)
|
||||
r.Public.HandleFunc("GET /{file}", func(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
filestem, _ := file.NameAtPathValue(r, "file")
|
||||
switch filestem {
|
||||
case "login":
|
||||
return routing.Login(s, w, r)
|
||||
case "register":
|
||||
return routing.Register(s, w, r)
|
||||
}
|
||||
return rsvp.Blank().StatusNotFound()
|
||||
})
|
||||
|
||||
r.Public.HandleFunc("POST /register", routing.RegisterPost)
|
||||
r.Public.HandleFunc("POST /", routing.LoginPost)
|
||||
|
||||
r.Private.HandleFunc("GET /groups/{groupReference}", routing.Group)
|
||||
r.Private.HandleFunc("GET /lists/{userReference}", routing.ForeignWishlist)
|
||||
r.Private.HandleFunc("GET /users/{userReference}", routing.User)
|
||||
r.Private.HandleFunc("GET /{$}", routing.Home)
|
||||
r.Private.HandleFunc("GET /{file}", func(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
filestem, _ := file.NameAtPathValue(r, "file")
|
||||
switch filestem {
|
||||
case "account":
|
||||
return routing.Account(app, s, w, r)
|
||||
case "events":
|
||||
return routing.EventList(app, s, w, r)
|
||||
case "groups":
|
||||
return routing.Groups(app, s, w, r)
|
||||
case "health":
|
||||
return routing.Health(app, s, w, r)
|
||||
case "home":
|
||||
return routing.Home(app, s, w, r)
|
||||
case "users":
|
||||
return routing.Users(app, s, w, r)
|
||||
}
|
||||
return routing.NotFound(w, r)
|
||||
})
|
||||
|
||||
r.Private.HandleFunc("POST /account", routing.AccountPost)
|
||||
r.Private.HandleFunc("POST /groups/{groupReference}", routing.GroupPost)
|
||||
r.Private.HandleFunc("POST /lists/{userReference}", routing.ForeignWishlistPost)
|
||||
r.Private.HandleFunc("POST /logout", routing.LogoutPost)
|
||||
r.Private.HandleFunc("POST /users/{userReference}", routing.UserPost)
|
||||
r.Private.HandleFunc("POST /{$}", routing.Home)
|
||||
|
||||
// Deprecated
|
||||
r.HandleFunc("GET /group/{groupReference}", middleware.PrefixMovedPermanently("/group/", "/groups/"))
|
||||
r.HandleFunc("GET /list/{groupReference}", middleware.PrefixMovedPermanently("/list/", "/lists/"))
|
||||
r.HandleFunc("POST /group/{groupReference}", middleware.PrefixPermanentRedirect("/group/", "/groups/"))
|
||||
r.HandleFunc("POST /list/{groupReference}", middleware.PrefixPermanentRedirect("/list/", "/lists/"))
|
||||
|
||||
return r
|
||||
}
|
||||
@ -1,42 +0,0 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"lishwist/http/internal/id"
|
||||
|
||||
"github.com/gorilla/securecookie"
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
var inMemStore = make(map[string]string)
|
||||
|
||||
var errNotFound = errors.New("not found")
|
||||
|
||||
func NewInMemoryStore(keyPairs ...[]byte) *Store {
|
||||
return &Store{
|
||||
callbacks: Callbacks{
|
||||
Delete: func(key string) error {
|
||||
delete(inMemStore, key)
|
||||
return nil
|
||||
},
|
||||
Insert: func(encodedValues string) (string, error) {
|
||||
key := id.Generate()
|
||||
inMemStore[key] = encodedValues
|
||||
return key, nil
|
||||
},
|
||||
Select: func(key string) (string, error) {
|
||||
encodedValues, ok := inMemStore[key]
|
||||
if !ok {
|
||||
return "", errNotFound
|
||||
}
|
||||
return encodedValues, nil
|
||||
},
|
||||
Update: func(key string, encodedValues string) error {
|
||||
inMemStore[key] = encodedValues
|
||||
return nil
|
||||
},
|
||||
},
|
||||
Codecs: securecookie.CodecsFromPairs(keyPairs...),
|
||||
Options: &sessions.Options{},
|
||||
}
|
||||
}
|
||||
@ -1,87 +0,0 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
inner *sessions.Session
|
||||
written bool
|
||||
}
|
||||
|
||||
func New(inner *sessions.Session) *Session {
|
||||
return &Session{inner: inner}
|
||||
}
|
||||
|
||||
const flashKey = "_flash"
|
||||
|
||||
func (s *Session) FlashGet() any {
|
||||
val, ok := s.inner.Values[flashKey]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
delete(s.inner.Values, flashKey)
|
||||
s.written = true
|
||||
return val
|
||||
}
|
||||
|
||||
func (s *Session) FlashPeek() any {
|
||||
val, ok := s.inner.Values[flashKey]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (s *Session) FlashSet(value any) {
|
||||
s.inner.Values[flashKey] = value
|
||||
s.written = true
|
||||
}
|
||||
|
||||
func (s *Session) SetID(value string) {
|
||||
s.inner.ID = value
|
||||
s.written = true
|
||||
}
|
||||
|
||||
func (s *Session) SetValue(key any, value any) {
|
||||
s.inner.Values[key] = value
|
||||
s.written = true
|
||||
}
|
||||
|
||||
func (s *Session) RemoveValue(key any) {
|
||||
delete(s.inner.Values, key)
|
||||
s.written = true
|
||||
}
|
||||
|
||||
func (s *Session) GetValue(key any) (any, bool) {
|
||||
val, ok := s.inner.Values[key]
|
||||
return val, ok
|
||||
}
|
||||
|
||||
func (s *Session) GetString(key any) (string, bool) {
|
||||
val, ok := s.GetValue(key)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
str, ok := val.(string)
|
||||
return str, ok
|
||||
}
|
||||
|
||||
func (s *Session) ClearValues() {
|
||||
s.inner.Values = make(map[any]any)
|
||||
s.written = true
|
||||
}
|
||||
|
||||
func (s *Session) Options() *sessions.Options {
|
||||
return s.inner.Options
|
||||
}
|
||||
|
||||
func (s *Session) WasWritten() bool {
|
||||
return s.written
|
||||
}
|
||||
|
||||
func (s *Session) Save(r *http.Request, w http.ResponseWriter) error {
|
||||
return s.inner.Save(r, w)
|
||||
}
|
||||
@ -1,115 +0,0 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/securecookie"
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
type Callbacks struct {
|
||||
Delete func(id string) error
|
||||
Insert func(encodedValues string) (string, error)
|
||||
Select func(id string) (string, error)
|
||||
Update func(id, encodedValues string) error
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
callbacks Callbacks
|
||||
Codecs []securecookie.Codec
|
||||
Options *sessions.Options
|
||||
}
|
||||
|
||||
func NewGenericStore(cb Callbacks, keyPairs ...[]byte) *Store {
|
||||
return &Store{
|
||||
callbacks: cb,
|
||||
Codecs: securecookie.CodecsFromPairs(keyPairs...),
|
||||
Options: &sessions.Options{},
|
||||
}
|
||||
}
|
||||
|
||||
// Get should return a cached session.
|
||||
func (m *Store) Get(r *http.Request, name string) (*sessions.Session, error) {
|
||||
return sessions.GetRegistry(r).Get(m, name)
|
||||
}
|
||||
|
||||
// New should create and return a new session.
|
||||
//
|
||||
// Note that New should never return a nil session, even in the case of
|
||||
// an error if using the Registry infrastructure to cache the session.
|
||||
func (s *Store) New(r *http.Request, name string) (*sessions.Session, error) {
|
||||
session := sessions.NewSession(s, name)
|
||||
opts := *s.Options
|
||||
session.Options = &opts
|
||||
session.IsNew = true
|
||||
|
||||
var err error
|
||||
|
||||
c, errCookie := r.Cookie(name)
|
||||
if errCookie != nil {
|
||||
return session, nil
|
||||
}
|
||||
|
||||
err = securecookie.DecodeMulti(name, c.Value, &session.ID, s.Codecs...)
|
||||
if err != nil {
|
||||
return session, fmt.Errorf("failed to decode session id: %w", err)
|
||||
}
|
||||
|
||||
sessionValue, err := s.callbacks.Select(session.ID)
|
||||
if err != nil {
|
||||
return session, fmt.Errorf("failed to get session value: %w", err)
|
||||
}
|
||||
|
||||
err = securecookie.DecodeMulti(name, string(sessionValue), &session.Values, s.Codecs...)
|
||||
if err == nil {
|
||||
session.IsNew = false
|
||||
} else {
|
||||
err = fmt.Errorf("failed to decode session values: %w", err)
|
||||
}
|
||||
|
||||
return session, err
|
||||
}
|
||||
|
||||
// Save should persist session to the underlying store implementation.
|
||||
func (s *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
|
||||
// Delete if max-age is <= 0
|
||||
if session.Options.MaxAge <= 0 {
|
||||
err := s.callbacks.Delete(session.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete session: %w", err)
|
||||
}
|
||||
http.SetCookie(w, sessions.NewCookie(session.Name(), "", session.Options))
|
||||
return nil
|
||||
}
|
||||
|
||||
encodedValues, err := securecookie.EncodeMulti(session.Name(), session.Values,
|
||||
s.Codecs...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to encode cookie value: %w", err)
|
||||
}
|
||||
|
||||
if session.ID == "" {
|
||||
i, err := s.callbacks.Insert(encodedValues)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to insert session: %w", err)
|
||||
}
|
||||
|
||||
session.ID = i
|
||||
} else {
|
||||
err := s.callbacks.Update(session.ID, encodedValues)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update session: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
encodedId, err := securecookie.EncodeMulti(session.Name(), session.ID,
|
||||
s.Codecs...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to encode cookie value: %w", err)
|
||||
}
|
||||
|
||||
http.SetCookie(w, sessions.NewCookie(session.Name(), encodedId, session.Options))
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
{{template "head" .}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
<div class="overflow-y-scroll flex-grow-1">
|
||||
<div class="container py-5">
|
||||
{{with .GeneralError}}
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<p class="mb-0">{{.}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
<section class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h2>Submit new password</h2>
|
||||
<div class="form-text">You can set a new password by submitting this form.</div>
|
||||
{{with .PasswordFromAdmin}}
|
||||
<div class="alert alert-warning" role="alert">
|
||||
<p class="mb-0"><span class="badge text-bg-danger">!</span> This is recommended, because your password has
|
||||
been set by the admin. Change it to
|
||||
something the admin doesn't know!</p>
|
||||
</div>
|
||||
{{end}}
|
||||
<form method="post">
|
||||
<div class="d-flex flex-column">
|
||||
<label>
|
||||
New Password
|
||||
{{template "input" .Password}}
|
||||
</label>
|
||||
<label>
|
||||
Confirm password
|
||||
{{template "input" .ConfirmPassword}}
|
||||
</label>
|
||||
<button class="btn btn-primary" type="submit" name="intent" value="set_password">Submit</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@ -1,115 +0,0 @@
|
||||
{{define "input"}}
|
||||
<input style="padding: .375rem .75rem;" class="form-control{{if .Error}} is-invalid{{end}}" {{if .Type}}type="{{.Type}}" {{end}}name="{{.Name}}"
|
||||
value="{{.Value}}" {{if .Required}}required {{end}}{{if .MinLength}}minlength="{{.MinLength}}" {{end}}{{with .Error}} aria-describedby="{{$.Name}}Error"{{end}}>
|
||||
{{with .Error}}
|
||||
<div id="{{$.Name}}Error" class="invalid-feedback">
|
||||
{{.}}
|
||||
</div>
|
||||
{{else}}
|
||||
<div style="margin-top: .25rem; font-size: .875em;">
|
||||
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{define "hyperlink"}}
|
||||
<a href="{{.Href}}">{{.Label}}</a>
|
||||
{{end}}
|
||||
|
||||
{{define "head"}}
|
||||
<title>Lishwist</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
|
||||
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
||||
crossorigin="anonymous"></script>
|
||||
<script>
|
||||
function getSubmissionNames(form) {
|
||||
return Array.from(new FormData(form).keys());
|
||||
}
|
||||
function acceptNames(form, submitId, ...acceptedNames) {
|
||||
const submissionNames = getSubmissionNames(form);
|
||||
const submitter = document.getElementById(submitId);
|
||||
const accepted = submissionNames.length > 0 && submissionNames.every((name) => acceptedNames.includes(name));
|
||||
submitter.disabled = !accepted;
|
||||
}
|
||||
function acceptAttribute(form, submitId, acceptedAttribute) {
|
||||
const checkedInputs = Array.from(form.querySelectorAll("input")).filter((i) => i.checked);
|
||||
const submitter = document.getElementById(submitId);
|
||||
const accepted = checkedInputs.length > 0 && checkedInputs.every((i) => i.hasAttribute(acceptedAttribute));
|
||||
submitter.disabled = !accepted;
|
||||
}
|
||||
</script>
|
||||
{{end}}
|
||||
|
||||
{{define "navbar"}}
|
||||
<div class="navbar navbar-expand-lg bg-body-tertiary">
|
||||
<div class="container-fluid">
|
||||
<div class="navbar-brand">Lishwist</div>
|
||||
{{with .User}}
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarToggle"
|
||||
aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>{{if .Alert}} <span
|
||||
class="badge text-bg-danger">!</span>{{end}}
|
||||
</button>
|
||||
{{end}}
|
||||
<div class="collapse navbar-collapse" id="navbarToggle">
|
||||
{{with .Links}}
|
||||
<nav>
|
||||
<ul class="navbar-nav">
|
||||
{{range .}}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{.Href}}">{{.Label}}</a>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</nav>
|
||||
{{end}}
|
||||
<div class="flex-grow-1"></div>
|
||||
{{with .User}}
|
||||
<ul class="navbar-nav">
|
||||
{{ if .Link -}}
|
||||
<li class="nav-item">
|
||||
<button
|
||||
class="btn btn-success"
|
||||
onclick="navigator.clipboard.writeText('{{.Link.Href}}'); alert('The share link to your wishlist has been copied to your clipboard. Anyone with the link will be able to claim gifts for you. Share it with someone!');">
|
||||
{{.Link.Label}}
|
||||
</button>
|
||||
</li>
|
||||
{{ end -}}
|
||||
<li class="nav-item">
|
||||
<div class="dropdown">
|
||||
<button class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
{{.Label}}{{if .Alert}} <span class="badge text-bg-danger">!</span>{{end}}
|
||||
</button>
|
||||
<ul class="dropdown-menu">
|
||||
{{ if .Account -}}
|
||||
<li>
|
||||
<a class="dropdown-item" href="{{.Account.Href}}">{{.Account.Label}}{{if .Alert}} <span
|
||||
class="badge text-bg-danger">!</span>{{end}}</a>
|
||||
</li>
|
||||
{{ end -}}
|
||||
{{- with .Logout}}
|
||||
<li>
|
||||
<form class="d-contents" method="{{.Method}}" action="{{.Action}}">
|
||||
{{- with .Elements}}
|
||||
<button class="dropdown-item" type="{{.Type}}">{{.Label}}</button>
|
||||
{{- end}}
|
||||
</form>
|
||||
</li>
|
||||
{{- end}}
|
||||
</ul>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{define "login_prompt"}}
|
||||
<a href="/login?referrer={{.}}">Login</a> or <a href="/register?referrer={{.}}">register</a>
|
||||
{{end}}
|
||||
@ -1,17 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
<div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<p class="mb-0">{{.Message}}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,81 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
<div class="overflow-y-scroll flex-grow-1">
|
||||
<div class="container py-5">
|
||||
<section class="card">
|
||||
<div class="card-body">
|
||||
<h2>{{.Username}}'s list</h2>
|
||||
{{with .Gifts}}
|
||||
<form method="post" autocomplete="off"
|
||||
onchange="acceptNames(this, 'claimSubmit', 'claimed', 'unclaimed'); acceptNames(this, 'completeSubmit', 'claimed'); acceptAttribute(this, 'deleteSubmit', 'data-deletable')">
|
||||
<ul class="list-group mb-3">
|
||||
{{range .}}
|
||||
{{$isMine := eq .ClaimantId $.CurrentUserId}}
|
||||
{{$createdByMe := eq .CreatorId $.CurrentUserId}}
|
||||
{{$outsideIdea := ne .RecipientId .CreatorId}}
|
||||
<li class="list-group-item{{if .Sent}} list-group-item-light{{end}}">
|
||||
|
||||
<input id="foreignlist_select_{{.Id}}" class="form-check-input" type="checkbox"
|
||||
aria-describedby="wish_detail_{{.Id}}" {{if $isMine}} name="claimed" value="{{.Id}}" {{else if
|
||||
.ClaimantId}} disabled {{else}} name="unclaimed" value="{{.Id}}" {{end}} {{if .Sent}} disabled {{end}}
|
||||
{{if $createdByMe}}data-deletable{{end}}>
|
||||
|
||||
<label class="form-check-label stretched-link" for="foreignlist_select_{{.Id}}">
|
||||
{{if .Sent}}
|
||||
<s>{{.Name}}</s>
|
||||
{{else}}
|
||||
{{.Name}}
|
||||
{{end}}
|
||||
</label>
|
||||
|
||||
{{if or .ClaimantId $outsideIdea}}
|
||||
<div class="d-inline" id="wish_detail_{{.Id}}">
|
||||
{{if .ClaimantId}}
|
||||
<span style="color: {{if $isMine}}blue{{else}}red{{end}};">{{if .Sent}}Completed{{else}}Claimed{{end}}
|
||||
by
|
||||
{{if $isMine}}<em>you</em>{{else}}{{.ClaimantName}}{{end}}</span>
|
||||
{{end}}
|
||||
|
||||
{{if $outsideIdea}}
|
||||
<span style="color: green;">Added by {{if
|
||||
$createdByMe}}<em>you</em>{{else}}{{.CreatorName}}{{end}}</span>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
<button id="claimSubmit" class="btn btn-warning" type="submit" name="intent" value="claim"
|
||||
disabled>Claim/Unclaim</button>
|
||||
<button id="completeSubmit" class="btn btn-success" type="submit" name="intent" value="complete"
|
||||
disabled>Complete</button>
|
||||
<button id="deleteSubmit" class="btn btn-danger" type="submit" name="intent" value="delete"
|
||||
disabled>Delete</button>
|
||||
</form>
|
||||
{{else}}
|
||||
<p>They don't have any gift ideas. Ask them to think of something, or add an idea yourself! 👇 (everyone
|
||||
except them will be able to see it and claim it)</p>
|
||||
{{end}}
|
||||
<form method="post">
|
||||
<div class="input-group mt-3">
|
||||
<input class="form-control" name="gift_name" required aria-describedby="gift_name_help" placeholder="Write a gift idea here" autofocus>
|
||||
<button class="btn btn-primary" type="submit" name="intent" value="add">Add gift idea</button>
|
||||
</div>
|
||||
<div id="gift_name_help" class="form-text">This will be invisible to {{.Username}}, but everyone else will be
|
||||
able to see it and
|
||||
possibly claim it.</div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,32 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
<div class="overflow-y-scroll flex-grow-1">
|
||||
<div class="container py-5">
|
||||
<section class="card">
|
||||
<div class="card-body">
|
||||
<h2><em>{{.Group.Name}}</em> group members</h2>
|
||||
{{with .Group.Members}}
|
||||
<ul class="list-group">
|
||||
{{range .}}
|
||||
<li class="list-group-item">
|
||||
<a href="/lists/{{.Reference}}">{{.Name}}</a>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{else}}
|
||||
<p>There's nobody else in this group.</p>
|
||||
{{end}}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,122 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
<div class="overflow-y-scroll flex-grow-1">
|
||||
<div class="container py-5">
|
||||
{{- with .Wishlist}}
|
||||
<section class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h2>{{.Label}}</h2>
|
||||
{{with .Wishes}}
|
||||
{{with .Elements}}
|
||||
<form method="post" onchange="acceptNames(this, 'deleteSubmit', 'wish')" autocomplete="off">
|
||||
<ul class="list-group mb-3">
|
||||
{{range .Items}}
|
||||
<li class="list-group-item">
|
||||
<input id="wishlist_select_{{.Input.Value}}" class="form-check-input" type="{{.Input.Type}}" name="{{.Input.Name}}"
|
||||
value="{{.Input.Value}}">
|
||||
<label class="form-check-label stretched-link" for="wishlist_select_{{.Input.Value}}">
|
||||
{{.Input.Label}}
|
||||
</label>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
<button id="deleteSubmit" class="btn btn-danger mb-3" name="intent" value="delete"
|
||||
disabled>Delete</button>
|
||||
</form>
|
||||
{{end}}
|
||||
{{else}}
|
||||
<p>Your list is empty. Think of some things to add!</p>
|
||||
{{end}}
|
||||
{{- with .NewWish}}
|
||||
<form method="{{.Method}}">
|
||||
{{- with .Elements}}
|
||||
<div class="input-group">
|
||||
<input class="form-control" name="{{.Name}}" {{- if .Required}} required {{- end}} placeholder="Write a gift idea here" autofocus>
|
||||
<button class="btn btn-primary" type="submit" name="intent" value="add_idea">{{.Label}}</button>
|
||||
</div>
|
||||
{{- end}}
|
||||
</form>
|
||||
{{- end}}
|
||||
</div>
|
||||
</section>
|
||||
{{- end}}
|
||||
|
||||
{{- with .Todo}}
|
||||
<section class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h2>{{.Label}}</h2>
|
||||
{{with .Wishes}}
|
||||
<form method="{{.Method}}"
|
||||
onchange="acceptNames(this, 'unclaimSubmit', 'todo'); acceptNames(this, 'completeSubmit', 'todo')"
|
||||
autocomplete="off">
|
||||
{{- with .Elements}}
|
||||
<ul class="list-group mb-3">
|
||||
{{range .Items}}
|
||||
<li class="list-group-item{{if .Input.Disabled}} list-group-item-light{{end}}">
|
||||
<input id="todo_select_{{.Input.Value}}" class="form-check-input" type="{{.Input.Type}}" {{if .Input.Disabled}}
|
||||
aria-describedby="todo_detail_{{.Input.Value}}" disabled{{else}} name="{{.Input.Name}}" value="{{.Input.Value}}" {{end}}>
|
||||
<label for="todo_select_{{.Input.Value}}" class="form-check-label">
|
||||
<em>
|
||||
{{if .Input.Disabled}}
|
||||
<s>{{.Input.Label}}</s>
|
||||
{{else}}
|
||||
{{.Input.Label}}
|
||||
{{end}}
|
||||
</em>
|
||||
</label>
|
||||
<span id="todo_detail_{{.Input.Value}}">
|
||||
for <a href="{{.Recipient.Href}}">{{.Recipient.Label}}</a>
|
||||
</span>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{ range .Intent.Options -}}
|
||||
{{ if eq .Value "unclaim_todo" }}
|
||||
<button id="unclaimSubmit" class="btn btn-warning" name="intent" value="unclaim_todo"
|
||||
disabled>Unclaim</button>
|
||||
{{ else if eq .Value "complete_todo" -}}
|
||||
<button id="completeSubmit" class="btn btn-success" name="intent" value="complete_todo"
|
||||
disabled>Complete</button>
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
{{- end}}
|
||||
</form>
|
||||
{{else}}
|
||||
<p class="mb-0">When you claim gifts for others, they will appear here.</p>
|
||||
{{end}}
|
||||
</div>
|
||||
</section>
|
||||
{{- end}}
|
||||
|
||||
{{with .GroupsList}}
|
||||
<section class="card">
|
||||
<div class="card-body">
|
||||
<h2>{{.Label}}</h2>
|
||||
{{with .Links}}
|
||||
<ul class="list-group">
|
||||
{{range .}}
|
||||
<li class="list-group-item">
|
||||
<a href="{{.Href}}">{{.Label}}</a>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{else}}
|
||||
<p>You don't belong to any groups</p>
|
||||
{{end}}
|
||||
</div>
|
||||
</section>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@ -1,47 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
{{with .Form.Elements}}
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar"}}
|
||||
<div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column">
|
||||
{{if .SuccessfulRegistration}}
|
||||
<div class="alert alert-success" role="alert">
|
||||
<p class="mb-0">Registration successful. Now you can login.</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .SuccessfulSetPassword}}
|
||||
<div class="alert alert-success" role="alert">
|
||||
<p class="mb-0">Set password successfully. You can now login back in.</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{with .GeneralError}}
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<p class="mb-0">{{.}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
<form method="{{$.Form.Method}}">
|
||||
<div class="d-flex flex-column">
|
||||
<label>
|
||||
{{.Username.Label}}
|
||||
{{template "input" .Username}}
|
||||
</label>
|
||||
<label>
|
||||
{{.Password.Label}}
|
||||
{{template "input" .Password}}
|
||||
</label>
|
||||
<div class="mb-3">
|
||||
{{template "hyperlink" $.RegisterLink}}
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit">Login</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
{{end}}
|
||||
</html>
|
||||
@ -1,33 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
<div class="overflow-y-scroll flex-grow-1">
|
||||
<div class="container py-5">
|
||||
<section class="card">
|
||||
<div class="card-body">
|
||||
<h2>{{.Username}}'s list</h2>
|
||||
{{if eq .GiftCount 0}}
|
||||
<p>{{.Username}} hasn't written any gift ideas!</p>
|
||||
<p>{{template "login_prompt" .Referrer}} to add some! :^)</p>
|
||||
{{else}}
|
||||
{{if eq .GiftCount 1}}
|
||||
<p>{{.Username}} has only written one gift idea.</p>
|
||||
<p>{{template "login_prompt" .Referrer}} to claim it, or add more! :^)</p>
|
||||
{{else}}
|
||||
<p>{{.Username}} has written {{.GiftCount}} gift ideas.</p>
|
||||
<p>{{template "login_prompt" .Referrer}} to claim an idea, or add more! :^)</p>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,33 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Navbar}}
|
||||
<div class="overflow-y-scroll flex-grow-1">
|
||||
<div class="container py-5">
|
||||
<section class="card">
|
||||
<div class="card-body">
|
||||
<h2><em>{{.Group.Name}}</em> group members</h2>
|
||||
<p>{{template "login_prompt" .Referrer}} to see your groups</p>
|
||||
{{with .Group.Members}}
|
||||
<ul class="list-group">
|
||||
{{range .}}
|
||||
<li class="list-group-item">
|
||||
{{.Name}}
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{else}}
|
||||
<p>There's nobody else in this group.</p>
|
||||
{{end}}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,45 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
{{template "head"}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="height: 100svh;" class="d-flex flex-column">
|
||||
{{template "navbar" .Nav}}
|
||||
<div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column">
|
||||
{{with .Warning}}
|
||||
<div class="alert alert-warning" role="alert">
|
||||
{{.}}
|
||||
</div>
|
||||
{{end}}
|
||||
{{with .Error}}
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<p class="mb-0">{{.}}</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{with .Form}}
|
||||
<form method="{{.Method}}">
|
||||
{{with .Elements}}
|
||||
<div class="d-flex flex-column">
|
||||
<label>
|
||||
{{.Username.Label}}
|
||||
{{template "input" .Username}}
|
||||
</label>
|
||||
<label>
|
||||
{{.Password.Label}}
|
||||
{{template "input" .Password}}
|
||||
</label>
|
||||
<label>
|
||||
{{.ConfirmPassword.Label}}
|
||||
{{template "input" .ConfirmPassword}}
|
||||
</label>
|
||||
<button class="btn btn-primary" type="submit">Register</button>
|
||||
</div>
|
||||
{{end}}
|
||||
</form>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,63 +0,0 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"lishwist/http/env"
|
||||
|
||||
"github.com/Teajey/hmc"
|
||||
)
|
||||
|
||||
type NavUser struct {
|
||||
Label string `json:"label" xml:",chardata"`
|
||||
Alert bool `json:"alert,omitempty" xml:",omitempty"`
|
||||
Link *hmc.Link `json:"link,omitempty" xml:",omitempty"`
|
||||
Account *hmc.Link `json:"account,omitempty" xml:",omitempty"`
|
||||
Logout hmc.Form[hmc.Input] `json:"logout"`
|
||||
}
|
||||
|
||||
type NavCollapse struct {
|
||||
Links []hmc.Link
|
||||
User *NavUser `json:",omitempty"`
|
||||
}
|
||||
|
||||
func DefaultNavCollapse() NavCollapse {
|
||||
return NavCollapse{Links: []hmc.Link{{Href: "/", Label: "Home"}}}
|
||||
}
|
||||
|
||||
func UserNavCollapse(username, reference string, accountAlert bool) NavCollapse {
|
||||
return NavCollapse{
|
||||
Links: []hmc.Link{{Href: "/", Label: "Home"}},
|
||||
User: &NavUser{
|
||||
Label: fmt.Sprintf("Logged in as '%s'", username),
|
||||
Alert: accountAlert,
|
||||
Link: &hmc.Link{
|
||||
Label: "Share link",
|
||||
Href: fmt.Sprintf("%s/lists/%s", env.Configuration.HostUrl, reference),
|
||||
},
|
||||
Account: &hmc.Link{
|
||||
Label: "Account",
|
||||
Href: "/account",
|
||||
},
|
||||
Logout: hmc.Form[hmc.Input]{
|
||||
Method: "POST",
|
||||
Action: "/logout",
|
||||
Elements: hmc.Input{
|
||||
Label: "Logout",
|
||||
Type: "submit",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var Template *template.Template
|
||||
|
||||
func init() {
|
||||
Template = load()
|
||||
}
|
||||
|
||||
func load() *template.Template {
|
||||
t := template.Must(template.ParseGlob("templates/*.gotmpl"))
|
||||
return t
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
Just a normal login form. Takes the following form values:
|
||||
|
||||
username string
|
||||
password string
|
||||
|
||||
Upon successful login, a redirect to the same URL on the protected router is issued.
|
||||
|
||||
A registration page is also available at /register
|
||||
@ -1,11 +0,0 @@
|
||||
Register with the following form values:
|
||||
|
||||
username string
|
||||
newPassword string
|
||||
confirmPassword string
|
||||
|
||||
All must be provided, username must be unique, newPassword and confirmPassword must be identical.
|
||||
|
||||
It's worth considering that having two password form parameters isn't very helpful on the command line, it's only really useful to the browser.
|
||||
Therefore, it might be a good idea to have Javascript check these two fields are identical, and submit, rather than having this
|
||||
checked by the server. Meaning only one password field needs to be submitted to the server. That does block non-Javascript browsers tho :^P
|
||||
@ -1,16 +0,0 @@
|
||||
package text
|
||||
|
||||
import (
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var Template *template.Template
|
||||
|
||||
func init() {
|
||||
Template = load()
|
||||
}
|
||||
|
||||
func load() *template.Template {
|
||||
t := template.Must(template.ParseGlob("templates/text/*.gotmpl"))
|
||||
return t
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -x
|
||||
|
||||
git diff --exit-code $1 api.snap.txt $(find . -name '*_test.go' -type f)
|
||||
@ -1,3 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
go doc -all $1 | ./scripts/strip_godoc_comments
|
||||
@ -1,3 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo $(git rev-parse HEAD)$(test -n "$(git status --porcelain)" && echo "*")
|
||||
@ -1,24 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
APISNAP=api.snap.txt
|
||||
|
||||
./scripts/staged_files_match './scripts/**' 'core/**/*.go' && ./scripts/api_snapshot ./core > core/$APISNAP
|
||||
|
||||
if ! git diff --quiet core/$APISNAP; then
|
||||
echo "There are unstaged changes to $APISNAP"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ./scripts/staged_files_match 'assert/**' 'core/**/*'; then
|
||||
go test ./core/... || {
|
||||
echo "./core test failure"
|
||||
exit 1
|
||||
}
|
||||
fi
|
||||
|
||||
if ./scripts/staged_files_match 'assert/**' 'http/props/htmlx/**/*.go'; then
|
||||
go test ./http/props/htmlx/... || {
|
||||
echo "./http test failure"
|
||||
exit 1
|
||||
}
|
||||
fi
|
||||
15
scripts/show
15
scripts/show
@ -1,15 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "Usage: $0 <file> [file2] ..." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for file in "$@"; do
|
||||
if [ ! -f "$file" ]; then
|
||||
echo "Error: File not found: $file" >&2
|
||||
exit 1
|
||||
fi
|
||||
printf '\n\n\n ## FROM %s\n\n' "$file"
|
||||
cat "$file"
|
||||
done
|
||||
@ -1,48 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# staged_files_match - Check if any staged files match the provided glob patterns
|
||||
#
|
||||
# Usage: staged_files_match <pattern1> [pattern2] [...]
|
||||
#
|
||||
# Exits 0 (success) if at least one staged file matches any pattern
|
||||
# Exits 1 (failure) if no staged files match any pattern
|
||||
#
|
||||
# Example:
|
||||
# staged_files_match './assert/**' './core/**/*.go' && go test ./core/...
|
||||
|
||||
set -e
|
||||
|
||||
# Check if at least one argument provided
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo "Error: No glob patterns provided" >&2
|
||||
echo "Usage: $0 <pattern1> [pattern2] [...]" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# Get all staged files (including renamed files)
|
||||
staged_files=$(git diff --cached --name-only --diff-filter=ACMR)
|
||||
|
||||
# If no staged files, exit with failure
|
||||
if [[ -z "$staged_files" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check each glob pattern against each staged file
|
||||
for pattern in "$@"; do
|
||||
# Remove './' prefix if present for consistent matching
|
||||
pattern="${pattern#./}"
|
||||
|
||||
while IFS= read -r file; do
|
||||
# Remove './' prefix from file if present
|
||||
file="${file#./}"
|
||||
|
||||
# Use case for glob matching
|
||||
case "$file" in
|
||||
$pattern)
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
done <<< "$staged_files"
|
||||
done
|
||||
|
||||
exit 1
|
||||
@ -1,55 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import fileinput
|
||||
from collections import deque
|
||||
from typing import Iterator
|
||||
from typing import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
def window(it: Iterator[T], size: int = 2) -> Iterator[list[T]]:
|
||||
if size < 2:
|
||||
raise ValueError("Window size must be at least 2")
|
||||
|
||||
window: deque[T] = deque(maxlen=size)
|
||||
|
||||
for _ in range(size):
|
||||
try:
|
||||
window.append(next(it))
|
||||
except StopIteration:
|
||||
return
|
||||
|
||||
yield list(window)
|
||||
|
||||
for item in it:
|
||||
window.append(item)
|
||||
yield list(window)
|
||||
|
||||
|
||||
struct = False
|
||||
func = False
|
||||
for line, next_line in window(fileinput.input()):
|
||||
line = line.removesuffix("\n")
|
||||
|
||||
if line.startswith("type ") and line.endswith(" struct {"):
|
||||
struct = True
|
||||
elif struct and line.endswith("}"):
|
||||
struct = False
|
||||
elif line.startswith("func"):
|
||||
func = True
|
||||
elif next_line[:4].strip() != "":
|
||||
func = False
|
||||
|
||||
if struct:
|
||||
if (
|
||||
line != ""
|
||||
and not line.lstrip().startswith("//")
|
||||
or "Has unexported fields" in line
|
||||
):
|
||||
print(line)
|
||||
elif func:
|
||||
if line != "" and not line.startswith(" "):
|
||||
print(line)
|
||||
else:
|
||||
print(line)
|
||||
@ -1,30 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo "Usage: upfind <glob> [glob...]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
dir="$(pwd)"
|
||||
dirs=()
|
||||
|
||||
while true; do
|
||||
dirs+=("$dir")
|
||||
parent="$(dirname "$dir")"
|
||||
if [[ "$parent" == "$dir" ]]; then
|
||||
break
|
||||
fi
|
||||
dir="$parent"
|
||||
done
|
||||
|
||||
for (( i=${#dirs[@]}-1; i>=0; i-- )); do
|
||||
for glob in "$@"; do
|
||||
for match in "${dirs[$i]}"/$glob; do
|
||||
if [[ -e "$match" ]]; then
|
||||
echo "$match"
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
@ -1,51 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "Usage: withenv <env-file> [env-file2] ... -- <command> [args]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
script_dir=$(dirname "$0")
|
||||
|
||||
env_files=()
|
||||
cmd_args=()
|
||||
parsing_files=true
|
||||
|
||||
for arg in "$@"
|
||||
do
|
||||
if [ "$arg" = "--" ]
|
||||
then
|
||||
parsing_files=false
|
||||
continue
|
||||
fi
|
||||
|
||||
if $parsing_files
|
||||
then
|
||||
env_files+=("$arg")
|
||||
else
|
||||
cmd_args+=("$arg")
|
||||
fi
|
||||
done
|
||||
|
||||
if [ ${#env_files[@]} -eq 0 ]; then
|
||||
echo "Error: No env files provided" >&2
|
||||
echo >&2
|
||||
echo "Usage: withenv <env-file> [env-file2] ... -- <command> [args]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ${#cmd_args[@]} -eq 0 ]; then
|
||||
echo "Error: No command specified after --" >&2
|
||||
echo >&2
|
||||
echo "Usage: withenv <env-file> [env-file2] ... -- <command> [args]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
data=$($script_dir/upfind "${env_files[@]}" | xargs $script_dir/show) || exit $?
|
||||
|
||||
(
|
||||
set -a
|
||||
eval "$data"
|
||||
set +a
|
||||
exec "${cmd_args[@]}"
|
||||
)
|
||||
62
server/api/db/db.go
Normal file
62
server/api/db/db.go
Normal file
@ -0,0 +1,62 @@
|
||||
//go:generate go run gen_init_sql.go
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"lishwist/env"
|
||||
|
||||
"github.com/Teajey/sqlstore"
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
)
|
||||
|
||||
var database *sql.DB
|
||||
|
||||
func Open() error {
|
||||
db, err := sql.Open("sqlite", env.DatabaseFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
database = db
|
||||
return nil
|
||||
}
|
||||
|
||||
func Init() error {
|
||||
_, err := database.Exec(initQuery)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewSessionStore() (*sqlstore.Store, error) {
|
||||
deleteStmt, err := database.Prepare("DELETE FROM session WHERE id = ?;")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to prepare delete statement: %w", err)
|
||||
}
|
||||
|
||||
insertStmt, err := database.Prepare("INSERT INTO session (value) VALUES (?);")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to prepare insert statement: %w", err)
|
||||
}
|
||||
|
||||
selectStmt, err := database.Prepare("SELECT value FROM session WHERE id = ?;")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to prepare select statement: %w", err)
|
||||
}
|
||||
|
||||
updateStmt, err := database.Prepare("UPDATE session SET value = ?2 WHERE id = ?1;")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to prepare update statement: %w", err)
|
||||
}
|
||||
|
||||
s := sqlstore.NewSqlStore(database, sqlstore.Statements{
|
||||
Delete: deleteStmt,
|
||||
Insert: insertStmt,
|
||||
Select: selectStmt,
|
||||
Update: updateStmt,
|
||||
}, []byte(env.SessionSecret))
|
||||
|
||||
return s, nil
|
||||
}
|
||||
117
server/api/db/group.go
Normal file
117
server/api/db/group.go
Normal file
@ -0,0 +1,117 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lishwist/normalize"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
Id string
|
||||
Name string
|
||||
Reference string
|
||||
Members []User
|
||||
}
|
||||
|
||||
func (g *Group) MemberIndex(userId string) int {
|
||||
for i, u := range g.Members {
|
||||
if u.Id == userId {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func queryManyGroups(query string, args ...any) ([]Group, error) {
|
||||
groups := []Group{}
|
||||
rows, err := database.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Query failed: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var group Group
|
||||
err := rows.Scan(&group.Id, &group.Name, &group.Reference)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to scan row: %w", err)
|
||||
}
|
||||
members, err := queryManyGroupMembers(group.Id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to query for group members: %w", err)
|
||||
}
|
||||
group.Members = members
|
||||
groups = append(groups, group)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Rows error: %w", err)
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func queryOneGroup(query string, args ...any) (*Group, error) {
|
||||
groups, err := queryManyGroups(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(groups) < 1 {
|
||||
return nil, nil
|
||||
}
|
||||
return &groups[0], nil
|
||||
}
|
||||
|
||||
func queryManyGroupMembers(groupId string) ([]User, error) {
|
||||
query := "SELECT user.id, user.name, user.display_name, user.reference, user.is_admin, user.is_live FROM v_user AS user JOIN group_member ON group_member.user_id = user.id JOIN [group] ON [group].id = group_member.group_id WHERE [group].id = ? ORDER BY group_member.user_id"
|
||||
members, err := queryManyUsers(query, groupId)
|
||||
if err != nil {
|
||||
return members, fmt.Errorf("Failed to get members: %w", err)
|
||||
}
|
||||
return members, nil
|
||||
}
|
||||
|
||||
func GetGroupByReference(reference string) (*Group, error) {
|
||||
query := "SELECT [group].id, [group].name, [group].reference FROM [group] WHERE [group].reference = ?"
|
||||
return queryOneGroup(query, reference)
|
||||
}
|
||||
|
||||
func GetAllGroups() ([]Group, error) {
|
||||
query := "SELECT id, name, reference FROM [group];"
|
||||
return queryManyGroups(query)
|
||||
}
|
||||
|
||||
func CreateGroup(name string, reference string) (*Group, error) {
|
||||
name = normalize.Trim(name)
|
||||
stmt := "INSERT INTO [group] (name, reference) VALUES (?, ?)"
|
||||
result, err := database.Exec(stmt, name, reference)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
group := Group{
|
||||
Id: strconv.FormatInt(id, 10),
|
||||
Name: name,
|
||||
Reference: reference,
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
func (g *Group) AddUser(userId string) error {
|
||||
stmt := "INSERT INTO group_member (group_id, user_id) VALUES (?, ?)"
|
||||
_, err := database.Exec(stmt, g.Id, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Group) RemoveUser(userId string) error {
|
||||
stmt := "DELETE FROM group_member WHERE group_id = ? AND user_id = ?"
|
||||
_, err := database.Exec(stmt, g.Id, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user