add stubs for subscription handling

trunk
nbsp 2025-01-07 13:50:02 +02:00
parent de17ded048
commit a5257bb705
No known key found for this signature in database
GPG Key ID: 7184AC1C9835CE48
3 changed files with 72 additions and 16 deletions

View File

@ -15,32 +15,44 @@ type Neighbors struct {
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})
}

View File

@ -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
}
}
}

View File

@ -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