1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-30 06:45:24 +00:00

Implemented first working reply draft

This commit is contained in:
マリウス 2023-01-02 01:57:28 -05:00
parent 46343177c3
commit 9c546bde39
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
5 changed files with 81 additions and 17 deletions

View File

@ -126,6 +126,8 @@ func (c *Client) NewRequest(
} }
} }
c.logger.Debug(buffer.String())
if req, err = http.NewRequest( if req, err = http.NewRequest(
method, method,
parsedURL.String(), parsedURL.String(),
@ -173,6 +175,9 @@ func (c *Client) Do(
} }
} }
c.logger.Debug(res)
c.logger.Debug(string(body))
if res.StatusCode < http.StatusOK || if res.StatusCode < http.StatusOK ||
res.StatusCode > http.StatusNoContent { res.StatusCode > http.StatusNoContent {
return &RequestError{ return &RequestError{

View File

@ -11,10 +11,11 @@ type CreatePostModel struct {
Title string `json:"title,omitempty"` Title string `json:"title,omitempty"`
Raw string `json:"raw"` Raw string `json:"raw"`
TopicID int `json:"topic_id,omitempty"` TopicID int `json:"topic_id,omitempty"`
ReplyToPostNumber int `json:"reply_to_post_number,omitempty"`
Category int `json:"category,omitempty"` Category int `json:"category,omitempty"`
TargetRecipients string `json:"targe_recipients,omitempty"` TargetRecipients string `json:"targe_recipients,omitempty"`
Archetype string `json:"archetype,omitempty"` Archetype string `json:"archetype,omitempty"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at,omitempty"`
EmbedURL string `json:"embed_url,omitempty"` EmbedURL string `json:"embed_url,omitempty"`
ExternalID string `json:"external_id,omitempty"` ExternalID string `json:"external_id,omitempty"`
} }
@ -107,12 +108,14 @@ func (a *PostServiceHandler) Create(
return PostModel{}, err return PostModel{}, err
} }
response := new(Response) response := new(PostModel)
if err = a.client.Do(ctx, req, response); err != nil { if err = a.client.Do(ctx, req, response); err != nil {
return PostModel{}, err return PostModel{}, err
} }
return response.Post, nil a.client.logger.Debug(response)
return *response, nil
} }
// Show // Show

View File

@ -184,6 +184,7 @@ func (sys *System) LoadPost(p *post.Post) error {
} }
p.Replies = append(p.Replies, reply.Reply{ p.Replies = append(p.Replies, reply.Reply{
ID: strconv.Itoa(i.ID), ID: strconv.Itoa(i.ID),
InReplyTo: p.ID,
Body: cookedMd, Body: cookedMd,
@ -211,7 +212,7 @@ func (sys *System) CreatePost(p *post.Post) error {
Title: p.Subject, Title: p.Subject,
Raw: p.Body, Raw: p.Body,
Category: categoryID, Category: categoryID,
CreatedAt: time.Now().Format(time.RFC3339), CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
} }
cp, err := sys.client.Posts.Create(context.Background(), &ap) cp, err := sys.client.Posts.Create(context.Background(), &ap)
@ -224,6 +225,11 @@ func (sys *System) CreatePost(p *post.Post) error {
} }
func (sys *System) CreateReply(r *reply.Reply) 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) inReplyTo, err := strconv.Atoi(r.InReplyTo)
if err != nil { if err != nil {
return err return err
@ -232,7 +238,8 @@ func (sys *System) CreateReply(r *reply.Reply) error {
ap := api.CreatePostModel{ ap := api.CreatePostModel{
Raw: r.Body, Raw: r.Body,
TopicID: inReplyTo, TopicID: inReplyTo,
CreatedAt: time.Now().Format(time.RFC3339), ReplyToPostNumber: ID,
CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
} }
cp, err := sys.client.Posts.Create(context.Background(), &ap) cp, err := sys.client.Posts.Create(context.Background(), &ap)

View File

@ -13,6 +13,7 @@ import (
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/mrusme/gobbs/aggregator" "github.com/mrusme/gobbs/aggregator"
"github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/ui/ctx" "github.com/mrusme/gobbs/ui/ctx"
) )
@ -31,6 +32,7 @@ type KeyMap struct {
Select key.Binding Select key.Binding
Esc key.Binding Esc key.Binding
Quit key.Binding Quit key.Binding
Reply key.Binding
} }
var DefaultKeyMap = KeyMap{ var DefaultKeyMap = KeyMap{
@ -49,6 +51,10 @@ var DefaultKeyMap = KeyMap{
Quit: key.NewBinding( Quit: key.NewBinding(
key.WithKeys("q"), key.WithKeys("q"),
), ),
Reply: key.NewBinding(
key.WithKeys("ctrl+s"),
key.WithHelp("ctrl+s", "reply"),
),
} }
type Model struct { type Model struct {
@ -67,6 +73,10 @@ type Model struct {
buffer string buffer string
replyIDs []string replyIDs []string
activePost *post.Post
allReplies []*reply.Reply
activeReply *reply.Reply
viewcache string viewcache string
viewcacheTextareaXY []int 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: default:
switch msg.String() { switch msg.String() {
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0": case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":

View File

@ -158,6 +158,7 @@ func (m *Model) renderViewport(p *post.Post) string {
) )
m.replyIDs = []string{p.ID} m.replyIDs = []string{p.ID}
m.activePost = p
out += m.renderReplies(0, p.Author.Name, &p.Replies) out += m.renderReplies(0, p.Author.Name, &p.Replies)
return out return out
@ -174,7 +175,7 @@ func (m *Model) renderReplies(
return "" return ""
} }
for _, re := range *replies { for ri, re := range *replies {
var err error = nil var err error = nil
var body string = "" var body string = ""
var author string = "" var author string = ""
@ -193,6 +194,7 @@ func (m *Model) renderReplies(
} }
m.replyIDs = append(m.replyIDs, re.ID) m.replyIDs = append(m.replyIDs, re.ID)
m.allReplies = append(m.allReplies, &(*replies)[ri])
idx := len(m.replyIDs) - 1 idx := len(m.replyIDs) - 1
out += fmt.Sprintf( out += fmt.Sprintf(