From f2804c1fc9c4ea81d9554b17da1439aa9a1bc817 Mon Sep 17 00:00:00 2001 From: nbsp Date: Wed, 8 Jan 2025 03:43:27 +0200 Subject: [PATCH] start work on user management --- app/management.go | 29 ++++++++++++++++++++++++----- app/neighbors.go | 2 +- app/subscriptions.go | 2 +- app/user.go | 10 ++++++++-- main.go | 2 -- ttbp/ttbp.go | 11 +++++++++++ 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/app/management.go b/app/management.go index 730110e..26928df 100644 --- a/app/management.go +++ b/app/management.go @@ -2,9 +2,12 @@ package app import ( "fmt" + "os" + "os/user" "strconv" "git.sr.ht/~rockorager/vaxis" + "git.tilde.town/nbsp/neofeels/ttbp" "git.tilde.town/nbsp/neofeels/ui" ) @@ -18,14 +21,14 @@ func NewManagement() *Management { return &Management{ title, ui.NewList([]string{ - "read over feels", // TODO + "read over feels", "modify feels publishing", // TODO "backup your feels", // TODO "import a feels backup", // TODO "bury some feels", // TODO "delete feels by day", // TODO - "purge all feels", // TODO - "wipe feels account", // TODO + "purge all feels", + "wipe feels account", }), "↑↓/kj move ↵ enter q return", } @@ -51,10 +54,13 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process ui.ViewChange <- NewMainMenu() case "Enter", "l", "Right": switch management.list.Index() { + case 0: + user, _ := user.Current() + ui.ViewChange <- NewUserPage(user.Username, true) case 6: - management.Confirmation(state, func() {}, "feels purged") + management.Confirmation(state, purgeFeels, "feels purged") case 7: - management.Confirmation(state, func() {}, "feels account wiped") + management.Confirmation(state, wipeAccount, "feels account wiped") } } processed = true @@ -77,6 +83,19 @@ func (management *Management) Draw(state *ui.State) { win.New(win.Width/2-15, win.Height/2+7, 30, 1).Print(vaxis.Segment{Text: management.help}) } +func purgeFeels() { + ttbp.Unpublish() + os.RemoveAll(ttbp.PathUserEntries) + os.MkdirAll(ttbp.PathUserEntries, 0700) +} + +func wipeAccount() { + // TODO: edit users.txt + ttbp.Unpublish() + os.RemoveAll(ttbp.PathUserFeels) + close(ui.Quit) +} + // TODO: abstract this to our own List type // TODO: figure out a less janky way instead of nesting event channels func (management *Management) Confirmation(state *ui.State, action func(), message string) { diff --git a/app/neighbors.go b/app/neighbors.go index 5a8cd33..b3078b9 100644 --- a/app/neighbors.go +++ b/app/neighbors.go @@ -81,7 +81,7 @@ func (neighbors *Neighbors) Event(state *ui.State, event vaxis.Event) (processed neighbors.subscriptions.Write() neighbors.list.SetItem(neighbors.list.Index(), formatNeighbor(user, neighbors.subscriptions)) case "Enter", "l", "Right": - ui.ViewChange <- NewUserPage(neighbors.neighbors[neighbors.list.Index()].Name) + ui.ViewChange <- NewUserPage(neighbors.neighbors[neighbors.list.Index()].Name, false) } processed = true } diff --git a/app/subscriptions.go b/app/subscriptions.go index c6812c0..d853887 100644 --- a/app/subscriptions.go +++ b/app/subscriptions.go @@ -69,7 +69,7 @@ func (subscriptions *Subscriptions) Event(state *ui.State, event vaxis.Event) (p case "Enter", "l", "Right": if len(subscriptions.list.Items()) > 0 { subscriptions.subscriptions.Write() - ui.ViewChange <- NewUserPage(subscriptions.neighbors[subscriptions.list.Index()].Name) + ui.ViewChange <- NewUserPage(subscriptions.neighbors[subscriptions.list.Index()].Name, false) } } processed = true diff --git a/app/user.go b/app/user.go index 891730c..929effd 100644 --- a/app/user.go +++ b/app/user.go @@ -18,9 +18,10 @@ type UserPage struct { list ui.List help string posts []ttbp.Post + self bool } -func NewUserPage(user string) *UserPage { +func NewUserPage(user string, self bool) *UserPage { posts := ttbp.GetPostsForUser(user) list := []string{} for _, post := range posts { @@ -36,6 +37,7 @@ func NewUserPage(user string) *UserPage { ui.NewList(list), "↑↓/kj move ↵ enter q return", posts, + self, } } @@ -56,7 +58,11 @@ func (user *UserPage) Event(state *ui.State, event vaxis.Event) (processed bool) i, _ := strconv.Atoi(key.String()) user.list.SetIndex(i) case "q", "h", "Left": - ui.ViewChange <- NewNeighbors() + if user.self { + ui.ViewChange <- NewManagement() + } else { + ui.ViewChange <- NewNeighbors() + } case "Enter", "l", "Right": showPost(state, user.posts[user.list.Index()]) } diff --git a/main.go b/main.go index ab94c50..8745207 100644 --- a/main.go +++ b/main.go @@ -36,8 +36,6 @@ press ↵ to set up an account, or Ctrl+c to quit. you can always come back late input := bufio.NewScanner(os.Stdin) input.Scan() - println(ttbp.PathUserConfig, ttbp.PathUserFeels) - user, _ := user.Current() header := strings.ReplaceAll(header, "%USER%", user.Username) header = strings.ReplaceAll(header, "%DATETIME%", time.Now().Format(time.DateTime)) diff --git a/ttbp/ttbp.go b/ttbp/ttbp.go index 0172e44..703b246 100644 --- a/ttbp/ttbp.go +++ b/ttbp/ttbp.go @@ -296,6 +296,17 @@ func Publish(t time.Time) { // TODO: gopher } +func Unpublish() { + cfg, err := config.Read() + if err != nil { + return // TODO: expose + } + if cfg.Publishing { + os.RemoveAll(PathUserWWW) + os.RemoveAll(path.Join(PathUserHTML, cfg.PublishDir)) + } +} + func writePage(post Post, header, footer []byte) { dateString := post.Date.Format("20060102.html") file, err := os.Create(path.Join(PathUserWWW, dateString))