1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-16 06:25:23 +00:00

Implemented Aggregator

This commit is contained in:
マリウス 2022-12-30 03:22:54 -05:00
parent b78f1d0c46
commit 7e94fcd909
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
7 changed files with 64 additions and 9 deletions

43
aggregator/aggregator.go Normal file
View File

@ -0,0 +1,43 @@
package aggregator
import (
"sort"
"github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/ui/ctx"
)
type Aggregator struct {
ctx *ctx.Ctx
}
func New(c *ctx.Ctx) (*Aggregator, error) {
a := new(Aggregator)
a.ctx = c
return a, nil
}
func (a *Aggregator) ListPosts() ([]post.Post, []error) {
var errs []error = make([]error, len(a.ctx.Systems))
var posts []post.Post
for idx, sys := range a.ctx.Systems {
sysPosts, err := (*sys).ListPosts(idx)
if err != nil {
errs[idx] = err
continue
}
posts = append(posts, sysPosts...)
}
sort.SliceStable(posts, func(i, j int) bool {
return posts[i].CreatedAt.After(posts[j].CreatedAt)
})
return posts, errs
}
func (a *Aggregator) LoadPost(p *post.Post) error {
return (*a.ctx.Systems[p.SysIDX]).LoadPost(p)
}

View File

@ -5,6 +5,7 @@ import (
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/aggregator"
"github.com/mrusme/gobbs/config"
"github.com/mrusme/gobbs/system"
"github.com/mrusme/gobbs/ui"
@ -79,12 +80,15 @@ var rootCmd = &cobra.Command{
c := ctx.New(&CFG, LOG)
_ = loadSystems(&c) // TODO: Handle errs
posts, err := (*c.Systems[0]).ListPosts()
a, _ := aggregator.New(&c)
posts, errs := a.ListPosts()
// posts, err := (*c.Systems[0]).ListPosts()
fmt.Println("-----------------------")
fmt.Printf("%v\n", posts)
fmt.Printf("%v\n", err)
fmt.Printf("%v\n", errs)
err = (*c.Systems[0]).LoadPost(&posts[4])
// err = s(*c.Systems[0]).LoadPost(&posts[4])
err := a.LoadPost(&posts[4])
fmt.Printf("%v\n", posts[4].Replies[2])
fmt.Printf("%v\n", err)
os.Exit(0)

View File

@ -23,6 +23,8 @@ type Post struct {
Author author.Author
Replies []reply.Reply
SysIDX int
}
func (post Post) FilterValue() string {

View File

@ -72,7 +72,7 @@ func (sys *System) Load() error {
return nil
}
func (sys *System) ListPosts() ([]post.Post, error) {
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
items, err := sys.client.Topics.ListLatest(context.Background())
if err != nil {
return []post.Post{}, err
@ -114,6 +114,8 @@ func (sys *System) ListPosts() ([]post.Post, error) {
ID: strconv.Itoa(i.Posters[0].UserID),
Name: userName,
},
SysIDX: sysIdx,
})
}

View File

@ -80,7 +80,7 @@ func (sys *System) Load() error {
return nil
}
func (sys *System) ListPosts() ([]post.Post, error) {
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
resp, err := sys.client.Posts(context.Background(), types.GetPosts{
Type: types.NewOptional(types.ListingLocal),
Sort: types.NewOptional(types.New),
@ -125,6 +125,8 @@ func (sys *System) ListPosts() ([]post.Post, error) {
ID: strconv.Itoa(i.Post.CreatorID),
Name: i.Creator.Name,
},
SysIDX: sysIdx,
})
}

View File

@ -19,7 +19,7 @@ type System interface {
Connect(sysURL string) error
Load() error
ListPosts() ([]post.Post, error)
ListPosts(sysIdx int) ([]post.Post, error)
LoadPost(p *post.Post) error
}

View File

@ -9,6 +9,7 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mrusme/gobbs/aggregator"
"github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/ui/ctx"
)
@ -171,9 +172,10 @@ func (m *Model) refresh() tea.Cmd {
return func() tea.Msg {
var items []list.Item
posts, err := (*m.ctx.Systems[0]).ListPosts()
if err != nil {
fmt.Printf("%s", err) // TODO: Implement error message
a, _ := aggregator.New(m.ctx)
posts, errs := a.ListPosts()
if len(errs) > 0 {
fmt.Printf("%s", errs) // TODO: Implement error message
}
for _, post := range posts {
items = append(items, post)