diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go index ab34039..698cf07 100644 --- a/aggregator/aggregator.go +++ b/aggregator/aggregator.go @@ -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) +} diff --git a/cmd/root.go b/cmd/root.go index 3b37847..aae27d1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -92,6 +92,7 @@ func loadSystems(c *ctx.Ctx) []error { } c.AddSystem(&sys) + sys.SetID(c.NumSystems() - 1) } return errs diff --git a/models/reply/reply.go b/models/reply/reply.go index 9be261d..3edf367 100644 --- a/models/reply/reply.go +++ b/models/reply/reply.go @@ -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 } diff --git a/system/discourse/discourse.go b/system/discourse/discourse.go index aaf9edf..86680b8 100644 --- a/system/discourse/discourse.go +++ b/system/discourse/discourse.go @@ -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 +} diff --git a/system/hackernews/hackernews.go b/system/hackernews/hackernews.go index 3210f13..6156594 100644 --- a/system/hackernews/hackernews.go +++ b/system/hackernews/hackernews.go @@ -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 +} diff --git a/system/lemmy/lemmy.go b/system/lemmy/lemmy.go index 32e79d4..5597ed5 100644 --- a/system/lemmy/lemmy.go +++ b/system/lemmy/lemmy.go @@ -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 +} diff --git a/system/system.go b/system/system.go index 152432f..3df4bde 100644 --- a/system/system.go +++ b/system/system.go @@ -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( diff --git a/ui/ctx/ctx.go b/ui/ctx/ctx.go index c95017d..4fe0d67 100644 --- a/ui/ctx/ctx.go +++ b/ui/ctx/ctx.go @@ -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) +}