server: Implements sendMessage
This commit is contained in:
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user