2022-12-30 03:22:54 -05:00
|
|
|
package aggregator
|
|
|
|
|
|
|
|
import (
|
2022-12-31 18:55:02 -05:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2022-12-30 03:22:54 -05:00
|
|
|
"sort"
|
2023-01-05 11:43:52 -05:00
|
|
|
"strings"
|
2022-12-30 03:22:54 -05:00
|
|
|
|
2023-01-06 19:46:41 -05:00
|
|
|
"github.com/mrusme/neonmodem/models/forum"
|
|
|
|
"github.com/mrusme/neonmodem/models/post"
|
|
|
|
"github.com/mrusme/neonmodem/models/reply"
|
|
|
|
"github.com/mrusme/neonmodem/ui/ctx"
|
2022-12-30 03:22:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Aggregator struct {
|
|
|
|
ctx *ctx.Ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(c *ctx.Ctx) (*Aggregator, error) {
|
|
|
|
a := new(Aggregator)
|
|
|
|
a.ctx = c
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:43:52 -05:00
|
|
|
func (a *Aggregator) ListForums() ([]forum.Forum, []error) {
|
|
|
|
var errs []error = make([]error, len(a.ctx.Systems))
|
|
|
|
var forums []forum.Forum
|
|
|
|
|
|
|
|
for idx, sys := range a.ctx.Systems {
|
|
|
|
if curSysIDX := a.ctx.GetCurrentSystem(); curSysIDX != -1 {
|
|
|
|
if idx != curSysIDX {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sysForums, err := (*sys).ListForums()
|
|
|
|
if err != nil {
|
|
|
|
errs[idx] = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
forums = append(forums, sysForums...)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.SliceStable(forums, func(i, j int) bool {
|
2023-01-05 20:47:20 -05:00
|
|
|
return strings.Compare(forums[i].Title(), forums[j].Title()) == -1
|
2023-01-05 11:43:52 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return forums, errs
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-30 03:22:54 -05:00
|
|
|
func (a *Aggregator) ListPosts() ([]post.Post, []error) {
|
|
|
|
var errs []error = make([]error, len(a.ctx.Systems))
|
|
|
|
var posts []post.Post
|
|
|
|
|
2022-12-31 18:55:02 -05:00
|
|
|
// TODO: Clean up implementation
|
2023-01-06 23:48:21 -05:00
|
|
|
if os.Getenv("NEONMODEM_TEST") == "true" {
|
2022-12-31 18:55:02 -05:00
|
|
|
jsonPosts, err := os.ReadFile("posts.db")
|
|
|
|
if err == nil {
|
|
|
|
err = json.Unmarshal(jsonPosts, &posts)
|
|
|
|
if err == nil {
|
|
|
|
return posts, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 03:22:54 -05:00
|
|
|
for idx, sys := range a.ctx.Systems {
|
2023-01-05 11:43:52 -05:00
|
|
|
if curSysIDX := a.ctx.GetCurrentSystem(); curSysIDX != -1 {
|
|
|
|
if idx != curSysIDX {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sysPosts, err := (*sys).ListPosts(a.ctx.GetCurrentForum().ID)
|
2022-12-30 03:22:54 -05:00
|
|
|
if err != nil {
|
2023-01-05 21:44:34 -05:00
|
|
|
a.ctx.Logger.Errorf("aggregator error: %v\n", err)
|
2022-12-30 03:22:54 -05:00
|
|
|
errs[idx] = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
posts = append(posts, sysPosts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.SliceStable(posts, func(i, j int) bool {
|
|
|
|
return posts[i].CreatedAt.After(posts[j].CreatedAt)
|
|
|
|
})
|
|
|
|
|
2022-12-31 18:55:02 -05:00
|
|
|
// TODO: Clean up implementation
|
2023-01-06 23:48:21 -05:00
|
|
|
if os.Getenv("NEONMODEM_TEST") == "true" {
|
|
|
|
jsonPosts, err := json.Marshal(posts)
|
|
|
|
if err == nil {
|
|
|
|
os.WriteFile("posts.db", jsonPosts, 0600)
|
|
|
|
}
|
2022-12-31 18:55:02 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 03:22:54 -05:00
|
|
|
return posts, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Aggregator) LoadPost(p *post.Post) error {
|
|
|
|
return (*a.ctx.Systems[p.SysIDX]).LoadPost(p)
|
|
|
|
}
|
2023-01-01 22:51:16 -05:00
|
|
|
|
|
|
|
func (a *Aggregator) CreatePost(p *post.Post) error {
|
|
|
|
return (*a.ctx.Systems[p.SysIDX]).CreatePost(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Aggregator) CreateReply(r *reply.Reply) error {
|
|
|
|
return (*a.ctx.Systems[r.SysIDX]).CreateReply(r)
|
|
|
|
}
|