Private
Public Access
1
0

server: Implements sendMessage

This commit is contained in:
2023-06-23 00:44:25 -07:00
parent 06046ac266
commit 943f52ac45
4 changed files with 68 additions and 1 deletions

View File

@@ -1,6 +1,10 @@
package model
import "time"
import (
"time"
"github.com/rs/zerolog"
)
type Message struct {
Text string `json:"text"`
@@ -16,3 +20,15 @@ type AttributionInfo struct {
ThumbnailWidth int `json:"pgensw"`
ThumbnailHeight int `json:"pgensh"`
}
func (c Message) MarshalZerologObject(e *zerolog.Event) {
e.Str("guid", c.Guid)
e.Str("text", c.Text)
e.Time("date", c.Date)
if c.Sender != nil {
e.Str("sender", *c.Sender)
} else {
e.Str("sender", "(Me)")
}
}

View File

@@ -5,6 +5,7 @@ import (
"code.severnaya.net/kordophone-mock/v2/data"
"code.severnaya.net/kordophone-mock/v2/model"
"github.com/rs/zerolog/log"
)
const VERSION = "Kordophone-2.0"
@@ -142,6 +143,14 @@ func (s *Server) AppendMessageToConversation(conversation *model.Conversation, m
s.messageStore[conversation.Guid] = append(s.messageStore[conversation.Guid], message)
}
func (s *Server) SendMessage(conversation *model.Conversation, message model.Message) {
s.AppendMessageToConversation(conversation, message)
conversation.LastMessagePreview = message.Text
conversation.Date = message.Date
log.Info().EmbedObject(message).Msgf("Sent message to conversation %s", conversation.Guid)
}
// Private
func (s *Server) registerAuthToken(token *model.AuthToken) {

View File

@@ -4,3 +4,9 @@ type AuthenticationRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
type SendMessageRequest struct {
ConversationGUID string `json:"guid"`
Body string `json:"body"`
TransferGUIDs []string `json:"fileTransferGUIDs"`
}

View File

@@ -5,8 +5,11 @@ import (
"fmt"
"net/http"
"strings"
"time"
"code.severnaya.net/kordophone-mock/v2/model"
"code.severnaya.net/kordophone-mock/v2/server"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
@@ -161,6 +164,38 @@ func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Reques
w.WriteHeader(http.StatusResetContent)
}
func (m *MockHTTPServer) handleSendMessage(w http.ResponseWriter, r *http.Request) {
// Decode request body as SendMessageRequest
var sendMessageReq SendMessageRequest
err := json.NewDecoder(r.Body).Decode(&sendMessageReq)
if err != nil {
log.Error().Err(err).Msg("SendMessage: Error decoding request body")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Find conversation
conversation, err := m.Server.ConversationForGUID(sendMessageReq.ConversationGUID)
if err != nil {
log.Error().Err(err).Msgf("SendMessage: Error finding conversation (%s)", sendMessageReq.ConversationGUID)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Create Message
message := model.Message{
Guid: uuid.New().String(),
Text: sendMessageReq.Body,
Date: time.Now(),
Sender: nil, // me
}
// Send message
m.Server.SendMessage(conversation, message)
w.WriteHeader(http.StatusOK)
}
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.logRequest(r, r.URL.Query().Encode())
m.mux.ServeHTTP(w, r)
@@ -179,6 +214,7 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
this.mux.Handle("/authenticate", http.HandlerFunc(this.handleAuthenticate))
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
this.mux.Handle("/pollUpdates", http.HandlerFunc(this.handlePollUpdates))
this.mux.Handle("/sendMessage", http.HandlerFunc(this.handleSendMessage))
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))