add stubs for subscription handling
parent
de17ded048
commit
a5257bb705
|
@ -11,36 +11,48 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Neighbors struct {
|
type Neighbors struct {
|
||||||
title string
|
title string
|
||||||
list ui.List
|
list ui.List
|
||||||
help string
|
help string
|
||||||
neighbors []ttbp.User
|
neighbors []ttbp.User
|
||||||
|
subscriptions *ttbp.Subscriptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNeighbors() *Neighbors {
|
func NewNeighbors() *Neighbors {
|
||||||
users := ttbp.SortUsersByRecent(ttbp.GetUsers())
|
users := ttbp.SortUsersByRecent(ttbp.GetUsers())
|
||||||
|
subscriptions := ttbp.GetSubscriptions()
|
||||||
list := []string{}
|
list := []string{}
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
publishDir := ""
|
list = append(list, formatNeighbor(user, subscriptions))
|
||||||
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,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Neighbors{
|
return &Neighbors{
|
||||||
title,
|
title,
|
||||||
ui.NewList(list),
|
ui.NewList(list),
|
||||||
"↑↓/kj move ↵ enter q return",
|
"↑↓/kj move ↵ enter s subscribe q return",
|
||||||
users,
|
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) {
|
func (neighbors *Neighbors) Event(state *ui.State, event vaxis.Event) (processed bool) {
|
||||||
if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress {
|
if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress {
|
||||||
switch key.String() {
|
switch key.String() {
|
||||||
|
@ -59,6 +71,14 @@ func (neighbors *Neighbors) Event(state *ui.State, event vaxis.Event) (processed
|
||||||
neighbors.list.SetIndex(i)
|
neighbors.list.SetIndex(i)
|
||||||
case "q", "h", "Left":
|
case "q", "h", "Left":
|
||||||
ui.ViewChange <- NewMainMenu()
|
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":
|
case "Enter", "l", "Right":
|
||||||
ui.ViewChange <- NewUserPage(neighbors.neighbors[neighbors.list.Index()].Name)
|
ui.ViewChange <- NewUserPage(neighbors.neighbors[neighbors.list.Index()].Name)
|
||||||
}
|
}
|
||||||
|
@ -79,5 +99,5 @@ func (neighbors *Neighbors) Draw(state *ui.State) {
|
||||||
Width: 80,
|
Width: 80,
|
||||||
Height: 10,
|
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})
|
||||||
}
|
}
|
||||||
|
|
32
ttbp/ttbp.go
32
ttbp/ttbp.go
|
@ -137,3 +137,35 @@ func SortPostsByRecent(posts []Post) []Post {
|
||||||
})
|
})
|
||||||
return posts
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -90,6 +90,10 @@ func (m *List) SetItems(items []string) {
|
||||||
m.index = min(len(items)-1, m.index)
|
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.
|
// Returns the index of the currently selected item.
|
||||||
func (m *List) Index() int {
|
func (m *List) Index() int {
|
||||||
return m.index
|
return m.index
|
||||||
|
|
Loading…
Reference in New Issue