From b77020e23cc2f93aef1b5e90082f6eec203c3102 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Wed, 19 Jul 2023 12:10:25 -0600 Subject: [PATCH] server: implements /markConversation --- server/server.go | 27 +++++++++++++++++++++------ web/server.go | 24 ++++++++++++++++++++++++ web/server_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/server/server.go b/server/server.go index 726bf26..4588037 100644 --- a/server/server.go +++ b/server/server.go @@ -151,21 +151,27 @@ func (s *Server) AppendMessageToConversation(conversation *model.Conversation, m func (s *Server) SendMessage(conversation *model.Conversation, message model.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) } 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 + + // Update conversation + ourConversation, _ := s.ConversationForGUID(conversation.Guid) + ourConversation.LastMessagePreview = message.Text + ourConversation.Date = message.Date + ourConversation.UnreadCount += 1 // Enqueue Update s.EnqueueUpdateItem(model.UpdateItem{ - Conversation: conversation, + Conversation: ourConversation, 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 func (s *Server) registerAuthToken(token *model.AuthToken) { diff --git a/web/server.go b/web/server.go index ac42d62..c6fd1c2 100644 --- a/web/server.go +++ b/web/server.go @@ -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) { m.logRequest(r, r.URL.Query().Encode()) 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("/pollUpdates", http.HandlerFunc(this.handlePollUpdates)) this.mux.Handle("/sendMessage", http.HandlerFunc(this.handleSendMessage)) + this.mux.Handle("/markConversation", http.HandlerFunc(this.handleMarkConversation)) this.mux.Handle("/", http.HandlerFunc(this.handleNotFound)) diff --git a/web/server_test.go b/web/server_test.go index 2052383..589beac 100644 --- a/web/server_test.go +++ b/web/server_test.go @@ -299,3 +299,46 @@ func TestUpdates(t *testing.T) { 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) + } +}