2023-01-03 16:46:16 -05:00
|
|
|
package postcreate
|
|
|
|
|
|
|
|
import (
|
2023-01-05 21:19:08 -05:00
|
|
|
"net/url"
|
2023-01-05 23:11:55 -05:00
|
|
|
"time"
|
2023-01-05 21:19:08 -05:00
|
|
|
|
2023-01-03 16:46:16 -05:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2023-01-06 19:46:41 -05:00
|
|
|
"github.com/mrusme/neonmodem/models/post"
|
|
|
|
"github.com/mrusme/neonmodem/models/reply"
|
|
|
|
"github.com/mrusme/neonmodem/ui/cmd"
|
2023-01-03 16:46:16 -05:00
|
|
|
)
|
|
|
|
|
2023-01-05 19:44:14 -05:00
|
|
|
func handleTab(mi interface{}) (bool, []tea.Cmd) {
|
|
|
|
var m *Model = mi.(*Model)
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
if m.action == "reply" {
|
|
|
|
return false, cmds
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.inputFocused == 0 {
|
|
|
|
m.inputFocused = 1
|
|
|
|
m.textinput.Blur()
|
|
|
|
cmds = append(cmds, m.textarea.Focus())
|
|
|
|
} else {
|
|
|
|
m.inputFocused = 0
|
|
|
|
m.textarea.Blur()
|
|
|
|
cmds = append(cmds, m.textinput.Focus())
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, cmds
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:46:16 -05:00
|
|
|
func handleSubmit(mi interface{}) (bool, []tea.Cmd) {
|
|
|
|
var m *Model = mi.(*Model)
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
2023-01-05 21:19:08 -05:00
|
|
|
if m.action == "post" {
|
|
|
|
// --- NEW POST ---
|
|
|
|
subject := m.textinput.Value()
|
|
|
|
body := m.textarea.Value()
|
|
|
|
typ := "post"
|
|
|
|
|
|
|
|
if _, err := url.Parse(body); err == nil {
|
|
|
|
typ = "url"
|
2023-01-05 17:40:18 -05:00
|
|
|
}
|
2023-01-03 16:46:16 -05:00
|
|
|
|
2023-01-05 21:19:08 -05:00
|
|
|
x := m.iface.(*post.Post)
|
|
|
|
p := post.Post{
|
|
|
|
Subject: subject,
|
|
|
|
Body: body,
|
|
|
|
Type: typ,
|
2023-01-03 16:46:16 -05:00
|
|
|
|
2023-01-05 21:19:08 -05:00
|
|
|
Forum: x.Forum,
|
|
|
|
|
|
|
|
SysIDX: x.SysIDX,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.a.CreatePost(&p)
|
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
|
|
|
cmds = append(cmds, cmd.New(
|
|
|
|
cmd.MsgError,
|
|
|
|
WIN_ID,
|
|
|
|
cmd.Arg{Name: "error", Value: err},
|
|
|
|
).Tea())
|
|
|
|
return true, cmds
|
|
|
|
}
|
|
|
|
} else if m.action == "reply" {
|
|
|
|
// --- REPLY TO EXISTING POST ---
|
|
|
|
var r reply.Reply
|
|
|
|
if m.replyToIdx == 0 {
|
|
|
|
// No numbers were typed before hitting `r` so we're replying to the actual
|
|
|
|
// Post
|
|
|
|
x := m.iface.(post.Post)
|
|
|
|
r = reply.Reply{
|
|
|
|
ID: x.ID,
|
|
|
|
InReplyTo: "",
|
|
|
|
Index: -1,
|
|
|
|
SysIDX: x.SysIDX,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Numbers were typed before hitting `r`, so we're taking the actual reply
|
|
|
|
// here
|
|
|
|
r = m.iface.(reply.Reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Body = m.textarea.Value()
|
|
|
|
|
|
|
|
err := m.a.CreateReply(&r)
|
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
|
|
|
cmds = append(cmds, cmd.New(
|
|
|
|
cmd.MsgError,
|
|
|
|
WIN_ID,
|
|
|
|
cmd.Arg{Name: "error", Value: err},
|
|
|
|
).Tea())
|
|
|
|
return true, cmds
|
|
|
|
}
|
|
|
|
} // </IF POST || REPLY>
|
2023-01-03 16:46:16 -05:00
|
|
|
|
2023-01-05 21:19:08 -05:00
|
|
|
m.inputFocused = 0
|
|
|
|
m.textinput.Reset()
|
2023-01-03 16:46:16 -05:00
|
|
|
m.textarea.Reset()
|
|
|
|
m.replyToIdx = 0
|
|
|
|
cmds = append(cmds, cmd.New(cmd.WMCloseWin, WIN_ID).Tea())
|
2023-01-05 23:11:55 -05:00
|
|
|
cmds = append(cmds, cmd.New(cmd.WinRefreshData, "*", cmd.Arg{
|
|
|
|
Name: "delay", Value: (3 * time.Second),
|
|
|
|
}).Tea())
|
2023-01-03 16:46:16 -05:00
|
|
|
return true, cmds
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWinOpenCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
|
|
|
|
var m *Model = mi.(*Model)
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
if c.Target == WIN_ID {
|
|
|
|
m.xywh = c.GetArg("xywh").([4]int)
|
2023-01-05 19:44:14 -05:00
|
|
|
|
|
|
|
m.action = c.GetArg("action").(string)
|
|
|
|
|
|
|
|
if m.action == "post" {
|
|
|
|
m.iface = c.GetArg("post").(*post.Post)
|
|
|
|
m.inputFocused = 0
|
|
|
|
cmds = append(cmds, m.textinput.Focus())
|
|
|
|
} else if m.action == "reply" {
|
|
|
|
m.replyToIdx = c.GetArg("replyToIdx").(int)
|
|
|
|
m.replyTo = c.GetArg("replyTo").(string)
|
|
|
|
m.iface = c.GetArg(m.replyTo)
|
|
|
|
m.inputFocused = 1
|
|
|
|
cmds = append(cmds, m.textarea.Focus())
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:46:16 -05:00
|
|
|
return true, cmds
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, cmds
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWinCloseCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
|
|
|
|
var m *Model = mi.(*Model)
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
if c.Target == WIN_ID {
|
|
|
|
m.textarea.Reset()
|
|
|
|
return true, cmds
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, cmds
|
|
|
|
}
|