diff --git a/prompt/prompt.go b/prompt/prompt.go index 1ec014f..12822ec 100644 --- a/prompt/prompt.go +++ b/prompt/prompt.go @@ -4,7 +4,9 @@ import ( "fmt" "io" "strings" + "time" + "code.severnaya.net/kordophone-mock/v2/model" "code.severnaya.net/kordophone-mock/v2/server" "github.com/chzyer/readline" "github.com/rs/zerolog/log" @@ -15,6 +17,15 @@ type Prompt struct { 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() { conversations := p.server.SortedConversations() for _, c := range conversations { @@ -28,7 +39,7 @@ func (p *Prompt) listConversations() { } func (p *Prompt) listMessages(guid string) { - conversation, err := p.server.ConversationForGUID(guid) + conversation, err := p.conversationForGUID(guid) if err != nil { log.Err(err).Msgf("Error listing messages for conversation %s", guid) return @@ -49,7 +60,7 @@ func (p *Prompt) listMessages(guid string) { } func (p *Prompt) markConversation(guid string, read bool) { - conversation, err := p.server.ConversationForGUID(guid) + conversation, err := p.conversationForGUID(guid) if err != nil { log.Err(err).Msgf("Error marking conversation %s as read", guid) 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 { completer := readline.NewPrefixCompleter( readline.PcItem("ls"), @@ -69,6 +97,7 @@ func NewPrompt(server *server.Server) *Prompt { readline.PcItem("-r"), ), readline.PcItem("help"), + readline.PcItem("recv"), readline.PcItem("exit"), ) @@ -132,11 +161,31 @@ func (p *Prompt) StartInteractive() error { p.markConversation(args[1], read) + case strings.HasPrefix(line, "recv"): // Receive + args := strings.Split(line, " ") + if len(args) < 3 { + log.Info().Msgf("Usage: recv ") + 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 + fmt.Println("Usage: [args]") + fmt.Println("Where is specified, '*' can be provided as a wildcard.") + fmt.Println() fmt.Println("Commands:") fmt.Println("\tls list conversations") fmt.Println("\tls list messages for conversation") fmt.Println("\tmark [-r] mark conversation as unread/[r]ead") + fmt.Println("\trecv receive a message") + fmt.Println("\thelp show this help") fmt.Println("\texit exits the program") case line == "exit": // Exit diff --git a/server/server.go b/server/server.go index dbca998..f8ee62b 100644 --- a/server/server.go +++ b/server/server.go @@ -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) } +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 func (s *Server) registerAuthToken(token *model.AuthToken) {