prompt: Implements receive message
This commit is contained in:
@@ -4,7 +4,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.severnaya.net/kordophone-mock/v2/model"
|
||||||
"code.severnaya.net/kordophone-mock/v2/server"
|
"code.severnaya.net/kordophone-mock/v2/server"
|
||||||
"github.com/chzyer/readline"
|
"github.com/chzyer/readline"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -15,6 +17,15 @@ type Prompt struct {
|
|||||||
server *server.Server
|
server *server.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Prompt) conversationForGUID(guid string) (*model.Conversation, error) {
|
||||||
|
if guid == "*" {
|
||||||
|
// This means any conversation: return the first one
|
||||||
|
return &p.server.SortedConversations()[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.server.ConversationForGUID(guid)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Prompt) listConversations() {
|
func (p *Prompt) listConversations() {
|
||||||
conversations := p.server.SortedConversations()
|
conversations := p.server.SortedConversations()
|
||||||
for _, c := range conversations {
|
for _, c := range conversations {
|
||||||
@@ -28,7 +39,7 @@ func (p *Prompt) listConversations() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Prompt) listMessages(guid string) {
|
func (p *Prompt) listMessages(guid string) {
|
||||||
conversation, err := p.server.ConversationForGUID(guid)
|
conversation, err := p.conversationForGUID(guid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msgf("Error listing messages for conversation %s", guid)
|
log.Err(err).Msgf("Error listing messages for conversation %s", guid)
|
||||||
return
|
return
|
||||||
@@ -49,7 +60,7 @@ func (p *Prompt) listMessages(guid string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Prompt) markConversation(guid string, read bool) {
|
func (p *Prompt) markConversation(guid string, read bool) {
|
||||||
conversation, err := p.server.ConversationForGUID(guid)
|
conversation, err := p.conversationForGUID(guid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msgf("Error marking conversation %s as read", guid)
|
log.Err(err).Msgf("Error marking conversation %s as read", guid)
|
||||||
return
|
return
|
||||||
@@ -62,6 +73,23 @@ func (p *Prompt) markConversation(guid string, read bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Prompt) receiveMessage(guid string, text string) {
|
||||||
|
conversation, err := p.conversationForGUID(guid)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msgf("Error receiving message for conversation %s", guid)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
message := model.Message{
|
||||||
|
Guid: guid,
|
||||||
|
Sender: &conversation.Participants[0],
|
||||||
|
Text: text,
|
||||||
|
Date: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
p.server.ReceiveMessage(conversation, message)
|
||||||
|
}
|
||||||
|
|
||||||
func NewPrompt(server *server.Server) *Prompt {
|
func NewPrompt(server *server.Server) *Prompt {
|
||||||
completer := readline.NewPrefixCompleter(
|
completer := readline.NewPrefixCompleter(
|
||||||
readline.PcItem("ls"),
|
readline.PcItem("ls"),
|
||||||
@@ -69,6 +97,7 @@ func NewPrompt(server *server.Server) *Prompt {
|
|||||||
readline.PcItem("-r"),
|
readline.PcItem("-r"),
|
||||||
),
|
),
|
||||||
readline.PcItem("help"),
|
readline.PcItem("help"),
|
||||||
|
readline.PcItem("recv"),
|
||||||
readline.PcItem("exit"),
|
readline.PcItem("exit"),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -132,11 +161,31 @@ func (p *Prompt) StartInteractive() error {
|
|||||||
|
|
||||||
p.markConversation(args[1], read)
|
p.markConversation(args[1], read)
|
||||||
|
|
||||||
|
case strings.HasPrefix(line, "recv"): // Receive
|
||||||
|
args := strings.Split(line, " ")
|
||||||
|
if len(args) < 3 {
|
||||||
|
log.Info().Msgf("Usage: recv <guid> <msg>")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
body := strings.Join(args[2:], " ")
|
||||||
|
|
||||||
|
// strip quotes
|
||||||
|
if strings.HasPrefix(body, "\"") && strings.HasSuffix(body, "\"") {
|
||||||
|
body = body[1 : len(body)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
p.receiveMessage(args[1], body)
|
||||||
case line == "help": // Help
|
case line == "help": // Help
|
||||||
|
fmt.Println("Usage: <command> [args]")
|
||||||
|
fmt.Println("Where <guid> is specified, '*' can be provided as a wildcard.")
|
||||||
|
fmt.Println()
|
||||||
fmt.Println("Commands:")
|
fmt.Println("Commands:")
|
||||||
fmt.Println("\tls list conversations")
|
fmt.Println("\tls list conversations")
|
||||||
fmt.Println("\tls <guid> list messages for conversation")
|
fmt.Println("\tls <guid> list messages for conversation")
|
||||||
fmt.Println("\tmark [-r] <guid> mark conversation as unread/[r]ead")
|
fmt.Println("\tmark [-r] <guid> mark conversation as unread/[r]ead")
|
||||||
|
fmt.Println("\trecv <guid> <msg> receive a message")
|
||||||
|
fmt.Println("\thelp show this help")
|
||||||
fmt.Println("\texit exits the program")
|
fmt.Println("\texit exits the program")
|
||||||
|
|
||||||
case line == "exit": // Exit
|
case line == "exit": // Exit
|
||||||
|
|||||||
@@ -151,6 +151,15 @@ func (s *Server) SendMessage(conversation *model.Conversation, message model.Mes
|
|||||||
log.Info().EmbedObject(message).Msgf("Sent message to conversation %s", conversation.Guid)
|
log.Info().EmbedObject(message).Msgf("Sent message to conversation %s", conversation.Guid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) ReceiveMessage(conversation *model.Conversation, message model.Message) {
|
||||||
|
s.AppendMessageToConversation(conversation, message)
|
||||||
|
conversation.LastMessagePreview = message.Text
|
||||||
|
conversation.Date = message.Date
|
||||||
|
conversation.UnreadCount += 1
|
||||||
|
|
||||||
|
log.Info().EmbedObject(message).Msgf("Received message from conversation %s", conversation.Guid)
|
||||||
|
}
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
func (s *Server) registerAuthToken(token *model.AuthToken) {
|
func (s *Server) registerAuthToken(token *model.AuthToken) {
|
||||||
|
|||||||
Reference in New Issue
Block a user