1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-07-21 03:14:14 -04:00

Impl CreatePost, CreateReply, refactored sysID

This commit is contained in:
マリウス 2023-01-01 22:51:16 -05:00
parent 73862a7c8f
commit 46343177c3
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
8 changed files with 124 additions and 9 deletions

View File

@ -6,6 +6,7 @@ import (
"sort"
"github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/ui/ctx"
)
@ -36,7 +37,7 @@ func (a *Aggregator) ListPosts() ([]post.Post, []error) {
}
for idx, sys := range a.ctx.Systems {
sysPosts, err := (*sys).ListPosts(idx)
sysPosts, err := (*sys).ListPosts()
if err != nil {
errs[idx] = err
continue
@ -60,3 +61,11 @@ func (a *Aggregator) ListPosts() ([]post.Post, []error) {
func (a *Aggregator) LoadPost(p *post.Post) error {
return (*a.ctx.Systems[p.SysIDX]).LoadPost(p)
}
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)
}

View File

@ -92,6 +92,7 @@ func loadSystems(c *ctx.Ctx) []error {
}
c.AddSystem(&sys)
sys.SetID(c.NumSystems() - 1)
}
return errs

View File

@ -7,7 +7,8 @@ import (
)
type Reply struct {
ID string
ID string
InReplyTo string
Body string
@ -18,4 +19,6 @@ type Reply struct {
Author author.Author
Replies []Reply
SysIDX int
}

View File

@ -18,11 +18,20 @@ import (
)
type System struct {
ID int
config map[string]interface{}
logger *zap.SugaredLogger
client *api.Client
}
func (sys *System) GetID() int {
return sys.ID
}
func (sys *System) SetID(id int) {
sys.ID = id
}
func (sys *System) GetConfig() map[string]interface{} {
return sys.config
}
@ -75,7 +84,7 @@ func (sys *System) Load() error {
return nil
}
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
func (sys *System) ListPosts() ([]post.Post, error) {
cats, err := sys.client.Categories.List(context.Background())
if err != nil {
return []post.Post{}, err
@ -143,7 +152,7 @@ func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
Name: forumName,
},
SysIDX: sysIdx,
SysIDX: sys.ID,
})
}
@ -184,8 +193,54 @@ func (sys *System) LoadPost(p *post.Post) error {
ID: strconv.Itoa(i.UserID),
Name: i.Name,
},
SysIDX: sys.ID,
})
}
return nil
}
func (sys *System) CreatePost(p *post.Post) error {
categoryID, err := strconv.Atoi(p.Forum.ID)
if err != nil {
return err
}
ap := api.CreatePostModel{
Title: p.Subject,
Raw: p.Body,
Category: categoryID,
CreatedAt: time.Now().Format(time.RFC3339),
}
cp, err := sys.client.Posts.Create(context.Background(), &ap)
if err != nil {
return err
}
p.ID = strconv.Itoa(cp.ID)
return nil
}
func (sys *System) CreateReply(r *reply.Reply) error {
inReplyTo, err := strconv.Atoi(r.InReplyTo)
if err != nil {
return err
}
ap := api.CreatePostModel{
Raw: r.Body,
TopicID: inReplyTo,
CreatedAt: time.Now().Format(time.RFC3339),
}
cp, err := sys.client.Posts.Create(context.Background(), &ap)
if err != nil {
return err
}
r.ID = strconv.Itoa(cp.ID)
return nil
}

View File

@ -16,11 +16,20 @@ import (
)
type System struct {
ID int
config map[string]interface{}
logger *zap.SugaredLogger
client *hn.Client
}
func (sys *System) GetID() int {
return sys.ID
}
func (sys *System) SetID(id int) {
sys.ID = id
}
func (sys *System) GetConfig() map[string]interface{} {
return sys.config
}
@ -57,7 +66,7 @@ func (sys *System) Load() error {
return nil
}
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
func (sys *System) ListPosts() ([]post.Post, error) {
stories, err := sys.client.TopStories(context.Background())
if err != nil {
return []post.Post{}, err
@ -121,7 +130,7 @@ func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
Replies: replies,
SysIDX: sysIdx,
SysIDX: sys.ID,
})
}
@ -168,6 +177,8 @@ func (sys *System) loadReplies(replies *[]reply.Reply) error {
Name: i.By,
}
re.SysIDX = sys.ID
for _, commentID := range i.Kids {
re.Replies = append(re.Replies, reply.Reply{
ID: strconv.Itoa(commentID),
@ -181,3 +192,11 @@ func (sys *System) loadReplies(replies *[]reply.Reply) error {
return nil
}
func (sys *System) CreatePost(p *post.Post) error {
return nil
}
func (sys *System) CreateReply(r *reply.Reply) error {
return nil
}

View File

@ -17,11 +17,20 @@ import (
)
type System struct {
ID int
config map[string]interface{}
logger *zap.SugaredLogger
client *lemmy.Client
}
func (sys *System) GetID() int {
return sys.ID
}
func (sys *System) SetID(id int) {
sys.ID = id
}
func (sys *System) GetConfig() map[string]interface{} {
return sys.config
}
@ -81,7 +90,7 @@ func (sys *System) Load() error {
return nil
}
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
func (sys *System) ListPosts() ([]post.Post, error) {
resp, err := sys.client.Posts(context.Background(), types.GetPosts{
Type: types.NewOptional(types.ListingSubscribed),
Sort: types.NewOptional(types.New),
@ -133,7 +142,7 @@ func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
Name: i.Community.Name,
},
SysIDX: sysIdx,
SysIDX: sys.ID,
})
}
@ -177,7 +186,17 @@ func (sys *System) LoadPost(p *post.Post) error {
ID: strconv.Itoa(i.Comment.CreatorID),
Name: i.Creator.Name,
},
SysIDX: sys.ID,
})
}
return nil
}
func (sys *System) CreatePost(p *post.Post) error {
return nil
}
func (sys *System) CreateReply(r *reply.Reply) error {
return nil
}

View File

@ -4,6 +4,7 @@ import (
"errors"
"github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/system/adapter"
"github.com/mrusme/gobbs/system/discourse"
"github.com/mrusme/gobbs/system/hackernews"
@ -12,6 +13,8 @@ import (
)
type System interface {
SetID(id int)
GetID() int
GetConfig() map[string]interface{}
SetConfig(cfg *map[string]interface{})
SetLogger(logger *zap.SugaredLogger)
@ -20,8 +23,10 @@ type System interface {
Connect(sysURL string) error
Load() error
ListPosts(sysIdx int) ([]post.Post, error)
ListPosts() ([]post.Post, error)
LoadPost(p *post.Post) error
CreatePost(p *post.Post) error
CreateReply(r *reply.Reply) error
}
func New(

View File

@ -35,3 +35,7 @@ func (c *Ctx) AddSystem(sys *system.System) error {
c.Systems = append(c.Systems, sys)
return nil
}
func (c *Ctx) NumSystems() int {
return len(c.Systems)
}