1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-16 06:25:23 +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(
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{

View File

@ -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

View File

@ -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)

View File

@ -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":

View File

@ -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(