From a5257bb70514bcb021140affae818cb21da817cb Mon Sep 17 00:00:00 2001 From: nbsp Date: Tue, 7 Jan 2025 13:50:02 +0200 Subject: [PATCH] add stubs for subscription handling --- app/neighbors.go | 52 +++++++++++++++++++++++++++++++++--------------- ttbp/ttbp.go | 32 +++++++++++++++++++++++++++++ ui/list.go | 4 ++++ 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/app/neighbors.go b/app/neighbors.go index bb03b0e..d2635de 100644 --- a/app/neighbors.go +++ b/app/neighbors.go @@ -11,36 +11,48 @@ import ( ) type Neighbors struct { - title string - list ui.List - help string - neighbors []ttbp.User + title string + list ui.List + help string + neighbors []ttbp.User + subscriptions *ttbp.Subscriptions } func NewNeighbors() *Neighbors { users := ttbp.SortUsersByRecent(ttbp.GetUsers()) + subscriptions := ttbp.GetSubscriptions() list := []string{} for _, user := range users { - publishDir := "" - if user.Publishing { - publishDir = fmt.Sprintf("%s%s/%s", ttbp.PathLive, user.Name, user.PublishDir) - } - list = append(list, fmt.Sprintf( - "~%-14s %-15s %-46s", - user.Name, - "("+humanize.Time(user.LastPublished)+")", - publishDir, - )) + list = append(list, formatNeighbor(user, subscriptions)) } return &Neighbors{ title, ui.NewList(list), - "↑↓/kj move ↵ enter q return", + "↑↓/kj move ↵ enter s subscribe q return", users, + subscriptions, } } +func formatNeighbor(user ttbp.User, subscriptions *ttbp.Subscriptions) string { + publishDir := "" + if user.Publishing { + publishDir = fmt.Sprintf("%s%s/%s", ttbp.PathLive, user.Name, user.PublishDir) + } + subscribed := " " + if subscriptions.IsSubscribed(user) { + subscribed = "+" + } + return fmt.Sprintf( + " %s ~%-14s %-15s %-43s", + subscribed, + user.Name, + "("+humanize.Time(user.LastPublished)+")", + publishDir, + ) +} + func (neighbors *Neighbors) Event(state *ui.State, event vaxis.Event) (processed bool) { if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress { switch key.String() { @@ -59,6 +71,14 @@ func (neighbors *Neighbors) Event(state *ui.State, event vaxis.Event) (processed neighbors.list.SetIndex(i) case "q", "h", "Left": ui.ViewChange <- NewMainMenu() + case "s": + user := neighbors.neighbors[neighbors.list.Index()] + if neighbors.subscriptions.IsSubscribed(user) { + neighbors.subscriptions.Unsubscribe(user) + } else { + neighbors.subscriptions.Subscribe(user) + } + neighbors.list.SetItem(neighbors.list.Index(), formatNeighbor(user, neighbors.subscriptions)) case "Enter", "l", "Right": ui.ViewChange <- NewUserPage(neighbors.neighbors[neighbors.list.Index()].Name) } @@ -79,5 +99,5 @@ func (neighbors *Neighbors) Draw(state *ui.State) { Width: 80, Height: 10, }) - win.New(win.Width/2-15, win.Height/2+9, 30, 1).Print(vaxis.Segment{Text: neighbors.help}) + win.New(win.Width/2-22, win.Height/2+9, 44, 1).Print(vaxis.Segment{Text: neighbors.help}) } diff --git a/ttbp/ttbp.go b/ttbp/ttbp.go index 4834a84..8853cbc 100644 --- a/ttbp/ttbp.go +++ b/ttbp/ttbp.go @@ -137,3 +137,35 @@ func SortPostsByRecent(posts []Post) []Post { }) return posts } + +type Subscriptions struct { + users []User +} + +func GetSubscriptions() *Subscriptions { + return &Subscriptions{ + users: []User{}, + } +} + +func (subscriptions *Subscriptions) IsSubscribed(user User) bool { + for _, sub := range subscriptions.users { + if sub.Name == user.Name { + return true + } + } + return false +} + +func (subscriptions *Subscriptions) Subscribe(user User) { + subscriptions.users = append(subscriptions.users, user) +} + +func (subscriptions *Subscriptions) Unsubscribe(user User) { + for i, sub := range subscriptions.users { + if sub.Name == user.Name { + subscriptions.users = append(subscriptions.users[:i], subscriptions.users[i+1:]...) + return + } + } +} diff --git a/ui/list.go b/ui/list.go index 0a83f1f..a042982 100644 --- a/ui/list.go +++ b/ui/list.go @@ -90,6 +90,10 @@ func (m *List) SetItems(items []string) { m.index = min(len(items)-1, m.index) } +func (m *List) SetItem(index int, item string) { + m.items[index] = item +} + // Returns the index of the currently selected item. func (m *List) Index() int { return m.index