1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00

Implemented LoadPost, minor fixes, restructuring

This commit is contained in:
マリウス 2022-12-30 02:48:53 -05:00
parent 6ed8922303
commit e0539b4954
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
5 changed files with 112 additions and 35 deletions

View File

@ -79,7 +79,7 @@ var rootCmd = &cobra.Command{
c := ctx.New(&CFG, LOG) c := ctx.New(&CFG, LOG)
_ = loadSystems(&c) // TODO: Handle errs _ = loadSystems(&c) // TODO: Handle errs
posts, err := (*c.Systems[1]).ListPosts() posts, err := (*c.Systems[0]).ListPosts()
fmt.Println("-----------------------") fmt.Println("-----------------------")
fmt.Printf("%v\n", posts) fmt.Printf("%v\n", posts)
fmt.Printf("%v\n", err) fmt.Printf("%v\n", err)

View File

@ -1,11 +1,17 @@
package reply package reply
import "github.com/mrusme/gobbs/models/author" import (
"time"
"github.com/mrusme/gobbs/models/author"
)
type Reply struct { type Reply struct {
ID string ID string
Body string Body string
CreatedAt time.Time
Author author.Author Author author.Author
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/mrusme/gobbs/models/author" "github.com/mrusme/gobbs/models/author"
"github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/system/adapter" "github.com/mrusme/gobbs/system/adapter"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -31,6 +32,25 @@ func (sys *System) SetLogger(logger *zap.SugaredLogger) {
sys.logger = logger sys.logger = logger
} }
func (sys *System) GetCapabilities() []adapter.Capability {
var caps []adapter.Capability
caps = append(caps, adapter.Capability{
ID: "posts",
Name: "Posts",
})
caps = append(caps, adapter.Capability{
ID: "groups",
Name: "Groups",
})
caps = append(caps, adapter.Capability{
ID: "search",
Name: "Search",
})
return caps
}
func (sys *System) Load() error { func (sys *System) Load() error {
url := sys.config["url"] url := sys.config["url"]
if url == nil { if url == nil {
@ -52,25 +72,6 @@ func (sys *System) Load() error {
return nil return nil
} }
func (sys *System) GetCapabilities() []adapter.Capability {
var caps []adapter.Capability
caps = append(caps, adapter.Capability{
ID: "posts",
Name: "Posts",
})
caps = append(caps, adapter.Capability{
ID: "groups",
Name: "Groups",
})
caps = append(caps, adapter.Capability{
ID: "search",
Name: "Search",
})
return caps
}
func (sys *System) ListPosts() ([]post.Post, error) { func (sys *System) ListPosts() ([]post.Post, error) {
items, err := sys.client.Topics.ListLatest(context.Background()) items, err := sys.client.Topics.ListLatest(context.Background())
if err != nil { if err != nil {
@ -118,3 +119,36 @@ func (sys *System) ListPosts() ([]post.Post, error) {
return models, nil return models, nil
} }
func (sys *System) LoadPost(p *post.Post) error {
item, err := sys.client.Topics.Show(context.Background(), p.ID)
if err != nil {
return nil
}
for idx, i := range item.PostStream.Posts {
if idx == 0 {
p.Body = i.Cooked // TODO: Clean Cooked
continue
}
createdAt, err := dateparse.ParseAny(i.CreatedAt)
if err != nil {
createdAt = time.Now() // TODO: Errrrrr
}
p.Replies = append(p.Replies, reply.Reply{
ID: strconv.Itoa(i.ID),
Body: i.Cooked, // TODO: Clean Cooked
CreatedAt: createdAt,
Author: author.Author{
ID: strconv.Itoa(i.UserID),
Name: i.Name,
},
})
}
return nil
}

View File

@ -8,6 +8,7 @@ import (
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/mrusme/gobbs/models/author" "github.com/mrusme/gobbs/models/author"
"github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/system/adapter" "github.com/mrusme/gobbs/system/adapter"
"go.arsenm.dev/go-lemmy" "go.arsenm.dev/go-lemmy"
"go.arsenm.dev/go-lemmy/types" "go.arsenm.dev/go-lemmy/types"
@ -32,6 +33,25 @@ func (sys *System) SetLogger(logger *zap.SugaredLogger) {
sys.logger = logger sys.logger = logger
} }
func (sys *System) GetCapabilities() []adapter.Capability {
var caps []adapter.Capability
caps = append(caps, adapter.Capability{
ID: "posts",
Name: "Posts",
})
caps = append(caps, adapter.Capability{
ID: "groups",
Name: "Groups",
})
caps = append(caps, adapter.Capability{
ID: "search",
Name: "Search",
})
return caps
}
func (sys *System) Load() error { func (sys *System) Load() error {
var err error var err error
@ -111,21 +131,37 @@ func (sys *System) ListPosts() ([]post.Post, error) {
return models, nil return models, nil
} }
func (sys *System) GetCapabilities() []adapter.Capability { func (sys *System) LoadPost(p *post.Post) error {
var caps []adapter.Capability pid, err := strconv.Atoi(p.ID)
if err != nil {
caps = append(caps, adapter.Capability{ return err
ID: "posts", }
Name: "Posts",
}) resp, err := sys.client.Comments(context.Background(), types.GetComments{
caps = append(caps, adapter.Capability{ PostID: types.NewOptional(pid),
ID: "groups", })
Name: "Groups", if err != nil {
}) return err
caps = append(caps, adapter.Capability{ }
ID: "search",
Name: "Search", for _, i := range resp.Comments {
}) createdAt, err := dateparse.ParseAny(i.Comment.Published)
if err != nil {
return caps createdAt = time.Now() // TODO: Errrrr
}
p.Replies = append(p.Replies, reply.Reply{
ID: strconv.Itoa(i.Comment.ID),
Body: i.Comment.Content,
CreatedAt: createdAt,
Author: author.Author{
ID: strconv.Itoa(i.Comment.CreatorID),
Name: i.Creator.Name,
},
})
}
return nil
} }

View File

@ -20,6 +20,7 @@ type System interface {
Load() error Load() error
ListPosts() ([]post.Post, error) ListPosts() ([]post.Post, error)
LoadPost(p *post.Post) error
} }
func New( func New(