server: implements /markConversation
This commit is contained in:
@@ -151,21 +151,27 @@ func (s *Server) AppendMessageToConversation(conversation *model.Conversation, m
|
|||||||
|
|
||||||
func (s *Server) SendMessage(conversation *model.Conversation, message model.Message) {
|
func (s *Server) SendMessage(conversation *model.Conversation, message model.Message) {
|
||||||
s.AppendMessageToConversation(conversation, message)
|
s.AppendMessageToConversation(conversation, message)
|
||||||
conversation.LastMessagePreview = message.Text
|
|
||||||
conversation.Date = message.Date
|
// Update Conversation
|
||||||
|
ourConversation, _ := s.ConversationForGUID(conversation.Guid)
|
||||||
|
ourConversation.LastMessagePreview = message.Text
|
||||||
|
ourConversation.Date = message.Date
|
||||||
|
|
||||||
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) {
|
func (s *Server) ReceiveMessage(conversation *model.Conversation, message model.Message) {
|
||||||
s.AppendMessageToConversation(conversation, message)
|
s.AppendMessageToConversation(conversation, message)
|
||||||
conversation.LastMessagePreview = message.Text
|
|
||||||
conversation.Date = message.Date
|
// Update conversation
|
||||||
conversation.UnreadCount += 1
|
ourConversation, _ := s.ConversationForGUID(conversation.Guid)
|
||||||
|
ourConversation.LastMessagePreview = message.Text
|
||||||
|
ourConversation.Date = message.Date
|
||||||
|
ourConversation.UnreadCount += 1
|
||||||
|
|
||||||
// Enqueue Update
|
// Enqueue Update
|
||||||
s.EnqueueUpdateItem(model.UpdateItem{
|
s.EnqueueUpdateItem(model.UpdateItem{
|
||||||
Conversation: conversation,
|
Conversation: ourConversation,
|
||||||
Message: &message,
|
Message: &message,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -209,6 +215,15 @@ func (s *Server) FetchUpdatesBlocking(since int) []model.UpdateItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) MarkConversationAsRead(conversation *model.Conversation) {
|
||||||
|
conversation.UnreadCount = 0
|
||||||
|
|
||||||
|
// enqueue update
|
||||||
|
s.EnqueueUpdateItem(model.UpdateItem{
|
||||||
|
Conversation: conversation,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
func (s *Server) registerAuthToken(token *model.AuthToken) {
|
func (s *Server) registerAuthToken(token *model.AuthToken) {
|
||||||
|
|||||||
@@ -229,6 +229,29 @@ func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Reques
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MockHTTPServer) handleMarkConversation(w http.ResponseWriter, r *http.Request) {
|
||||||
|
guid := r.URL.Query().Get("guid")
|
||||||
|
if len(guid) == 0 {
|
||||||
|
log.Error().Msg("handleMarkConversation: Got empty guid parameter")
|
||||||
|
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find conversation
|
||||||
|
convo, err := m.Server.ConversationForGUID(guid)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msgf("handleMarkConversation: Error finding conversation (%s)", guid)
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark conversation
|
||||||
|
m.Server.MarkConversationAsRead(convo)
|
||||||
|
|
||||||
|
// Respond 200
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
m.logRequest(r, r.URL.Query().Encode())
|
m.logRequest(r, r.URL.Query().Encode())
|
||||||
m.mux.ServeHTTP(w, r)
|
m.mux.ServeHTTP(w, r)
|
||||||
@@ -248,6 +271,7 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
|
|||||||
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
|
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
|
||||||
this.mux.Handle("/pollUpdates", http.HandlerFunc(this.handlePollUpdates))
|
this.mux.Handle("/pollUpdates", http.HandlerFunc(this.handlePollUpdates))
|
||||||
this.mux.Handle("/sendMessage", http.HandlerFunc(this.handleSendMessage))
|
this.mux.Handle("/sendMessage", http.HandlerFunc(this.handleSendMessage))
|
||||||
|
this.mux.Handle("/markConversation", http.HandlerFunc(this.handleMarkConversation))
|
||||||
|
|
||||||
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))
|
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))
|
||||||
|
|
||||||
|
|||||||
@@ -299,3 +299,46 @@ func TestUpdates(t *testing.T) {
|
|||||||
t.Fatalf("Unexpected message text: %s (expected %s)", update.Message.Text, message.Text)
|
t.Fatalf("Unexpected message text: %s (expected %s)", update.Message.Text, message.Text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarkConversation(t *testing.T) {
|
||||||
|
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true})
|
||||||
|
httpServer := httptest.NewServer(s)
|
||||||
|
|
||||||
|
// Mock conversation
|
||||||
|
guid := "1234567890"
|
||||||
|
conversation := model.Conversation{
|
||||||
|
Date: time.Now(),
|
||||||
|
Participants: []string{"Alice"},
|
||||||
|
UnreadCount: 0,
|
||||||
|
Guid: guid,
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Server.AddConversation(conversation)
|
||||||
|
|
||||||
|
// Receive message to mark as unread
|
||||||
|
message := model.Message{
|
||||||
|
Text: "This is a test.",
|
||||||
|
Sender: &conversation.Participants[0],
|
||||||
|
Date: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Server.ReceiveMessage(&conversation, message)
|
||||||
|
|
||||||
|
if convo, _ := s.Server.ConversationForGUID(guid); convo.UnreadCount != 1 {
|
||||||
|
t.Fatalf("Unexpected unread count: %d (expected %d)", convo.UnreadCount, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark conversation as read
|
||||||
|
resp, err := http.Post(httpServer.URL+"/markConversation?guid="+guid, "", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("TestMarkConversation error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("Unexpected status code: %d (expected %d)", resp.StatusCode, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
if convo, _ := s.Server.ConversationForGUID(guid); convo.UnreadCount != 0 {
|
||||||
|
t.Fatalf("Unexpected unread count: %d (expected %d)", convo.UnreadCount, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user