From 9c546bde3974cc08b88d9013d24e16b1979d444e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Mon, 2 Jan 2023 01:57:28 -0500 Subject: [PATCH] Implemented first working reply draft --- system/discourse/api/client.go | 5 ++++ system/discourse/api/posts.go | 25 ++++++++++-------- system/discourse/discourse.go | 17 ++++++++---- ui/views/posts/posts.go | 47 ++++++++++++++++++++++++++++++++++ ui/views/posts/view.go | 4 ++- 5 files changed, 81 insertions(+), 17 deletions(-) diff --git a/system/discourse/api/client.go b/system/discourse/api/client.go index a7d06d9..f5eb58b 100644 --- a/system/discourse/api/client.go +++ b/system/discourse/api/client.go @@ -126,6 +126,8 @@ func (c *Client) NewRequest( } } + c.logger.Debug(buffer.String()) + if req, err = http.NewRequest( method, parsedURL.String(), @@ -173,6 +175,9 @@ func (c *Client) Do( } } + c.logger.Debug(res) + c.logger.Debug(string(body)) + if res.StatusCode < http.StatusOK || res.StatusCode > http.StatusNoContent { return &RequestError{ diff --git a/system/discourse/api/posts.go b/system/discourse/api/posts.go index 2751bad..3b2001f 100644 --- a/system/discourse/api/posts.go +++ b/system/discourse/api/posts.go @@ -8,15 +8,16 @@ import ( const PostsBaseURL = "/posts" type CreatePostModel struct { - Title string `json:"title,omitempty"` - Raw string `json:"raw"` - TopicID int `json:"topic_id,omitempty"` - Category int `json:"category,omitempty"` - TargetRecipients string `json:"targe_recipients,omitempty"` - Archetype string `json:"archetype,omitempty"` - CreatedAt string `json:"created_at"` - EmbedURL string `json:"embed_url,omitempty"` - ExternalID string `json:"external_id,omitempty"` + Title string `json:"title,omitempty"` + Raw string `json:"raw"` + TopicID int `json:"topic_id,omitempty"` + ReplyToPostNumber int `json:"reply_to_post_number,omitempty"` + Category int `json:"category,omitempty"` + TargetRecipients string `json:"targe_recipients,omitempty"` + Archetype string `json:"archetype,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + EmbedURL string `json:"embed_url,omitempty"` + ExternalID string `json:"external_id,omitempty"` } type PostModel struct { @@ -107,12 +108,14 @@ func (a *PostServiceHandler) Create( return PostModel{}, err } - response := new(Response) + response := new(PostModel) if err = a.client.Do(ctx, req, response); err != nil { return PostModel{}, err } - return response.Post, nil + a.client.logger.Debug(response) + + return *response, nil } // Show diff --git a/system/discourse/discourse.go b/system/discourse/discourse.go index 86680b8..ffbfbd0 100644 --- a/system/discourse/discourse.go +++ b/system/discourse/discourse.go @@ -183,7 +183,8 @@ func (sys *System) LoadPost(p *post.Post) error { createdAt = time.Now() // TODO: Errrrrr } p.Replies = append(p.Replies, reply.Reply{ - ID: strconv.Itoa(i.ID), + ID: strconv.Itoa(i.ID), + InReplyTo: p.ID, Body: cookedMd, @@ -211,7 +212,7 @@ func (sys *System) CreatePost(p *post.Post) error { Title: p.Subject, Raw: p.Body, Category: categoryID, - CreatedAt: time.Now().Format(time.RFC3339), + CreatedAt: strconv.FormatInt(time.Now().Unix(), 10), } cp, err := sys.client.Posts.Create(context.Background(), &ap) @@ -224,15 +225,21 @@ func (sys *System) CreatePost(p *post.Post) error { } func (sys *System) CreateReply(r *reply.Reply) error { + sys.logger.Debugf("%v", r) + ID, err := strconv.Atoi(r.ID) + if err != nil { + return err + } 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), + Raw: r.Body, + TopicID: inReplyTo, + ReplyToPostNumber: ID, + CreatedAt: strconv.FormatInt(time.Now().Unix(), 10), } cp, err := sys.client.Posts.Create(context.Background(), &ap) diff --git a/ui/views/posts/posts.go b/ui/views/posts/posts.go index afc98bc..5bcddc8 100644 --- a/ui/views/posts/posts.go +++ b/ui/views/posts/posts.go @@ -13,6 +13,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/mrusme/gobbs/aggregator" "github.com/mrusme/gobbs/models/post" + "github.com/mrusme/gobbs/models/reply" "github.com/mrusme/gobbs/ui/ctx" ) @@ -31,6 +32,7 @@ type KeyMap struct { Select key.Binding Esc key.Binding Quit key.Binding + Reply key.Binding } var DefaultKeyMap = KeyMap{ @@ -49,6 +51,10 @@ var DefaultKeyMap = KeyMap{ Quit: key.NewBinding( key.WithKeys("q"), ), + Reply: key.NewBinding( + key.WithKeys("ctrl+s"), + key.WithHelp("ctrl+s", "reply"), + ), } type Model struct { @@ -67,6 +73,10 @@ type Model struct { buffer string replyIDs []string + activePost *post.Post + allReplies []*reply.Reply + activeReply *reply.Reply + viewcache string viewcacheTextareaXY []int } @@ -175,6 +185,43 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } + case key.Matches(msg, m.keymap.Reply): + if m.WMisFocused("reply") { + replyToIdx, _ := strconv.Atoi(m.buffer) + + m.ctx.Logger.Debugf("replyToIdx: %d", replyToIdx) + + var irtID string = "" + var irtIRT string = "" + var irtSysIDX int = 0 + + if replyToIdx == 0 { + irtID = m.activePost.ID + irtSysIDX = m.activePost.SysIDX + } else { + irt := m.allReplies[(replyToIdx - 1)] + irtID = strconv.Itoa(replyToIdx + 1) + irtIRT = irt.InReplyTo + irtSysIDX = irt.SysIDX + } + + r := reply.Reply{ + ID: irtID, + InReplyTo: irtIRT, + Body: m.textarea.Value(), + SysIDX: irtSysIDX, + } + err := m.a.CreateReply(&r) + if err != nil { + m.ctx.Logger.Error(err) + } + + m.textarea.Reset() + m.buffer = "" + m.WMClose("reply") + return m, nil + } + default: switch msg.String() { case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0": diff --git a/ui/views/posts/view.go b/ui/views/posts/view.go index a3f5df6..a885400 100644 --- a/ui/views/posts/view.go +++ b/ui/views/posts/view.go @@ -158,6 +158,7 @@ func (m *Model) renderViewport(p *post.Post) string { ) m.replyIDs = []string{p.ID} + m.activePost = p out += m.renderReplies(0, p.Author.Name, &p.Replies) return out @@ -174,7 +175,7 @@ func (m *Model) renderReplies( return "" } - for _, re := range *replies { + for ri, re := range *replies { var err error = nil var body string = "" var author string = "" @@ -193,6 +194,7 @@ func (m *Model) renderReplies( } m.replyIDs = append(m.replyIDs, re.ID) + m.allReplies = append(m.allReplies, &(*replies)[ri]) idx := len(m.replyIDs) - 1 out += fmt.Sprintf(