#pragma once // Spec: INV-social 12 (ConversationView.ViewModel): messages, draft validation, send flow with client ids and // optimistic bubbles, live websocket events, typing sender, read receipts, unsent attachment cleanup. #include #include #include #include #include #include #include "api/DevPlaceApi.hpp" #include "api/MessagesSocket.hpp" #include "core/AlertMessage.hpp" #include "model/MessagesInbox.hpp" #include "model/UploadResponse.hpp" #include "net/HttpTypes.hpp" namespace dp { class ConversationModel { public: using AlertSink = std::function; using SocketFactory = std::function(const std::string& otherUserId)>; using Clock = std::function; // seconds, monotonic ConversationModel(User otherUser, DevPlaceApi& api, AlertSink alertSink = nullptr, SocketFactory socketFactory = nullptr, Clock clock = nullptr); ~ConversationModel(); ConversationModel(const ConversationModel&) = delete; ConversationModel& operator=(const ConversationModel&) = delete; const User& otherUser() const { return otherUser_; } const std::vector& messages() const { return messages_; } const std::optional& inbox() const { return inbox_; } const std::string& draft() const { return draft_; } const std::vector& attachments() const { return attachments_; } bool isReloading() const { return isReloading_; } bool isSending() const { return isSending_; } bool isOtherTyping() const { return isOtherTyping_; } bool isSocketConnected() const { return isSocketConnected_; } bool hasSocket() const { return socket_ != nullptr; } bool isBusy() const { return pending_ > 0 || isSending_; } // Draft edits fire userIsTyping() like onChange(of: draft). void setDraft(const std::string& text); void appendToDraft(const std::string& text) { setDraft(draft_ + text); } void setAttachments(std::vector attachments) { attachments_ = std::move(attachments); } int draftCount() const; // UTF-16 code units int minCharacterCount() const { return attachments_.empty() ? 1 : 0; } bool canSend() const; // `done` runs after the request and the unread counts refresh finished (the Swift `await load()`). void load(std::function done = nullptr); void reload(); // Calls `done(true)` when the message left the composer (socket or REST), false when refused or failed. void send(std::function done = nullptr); void userIsTyping(); void deleteUnsubmittedAttachments(); void startRealtime(); void stopRealtime(); // Feed one socket event (used by the socket handler and by tests). void handle(const ChatSocketEvent& event); private: struct PendingSend { std::string content; std::vector attachments; }; void fetch(bool reloading, std::function done); void upsertIncoming(const ChatSocketIncomingMessage& incoming); void showOtherTyping(); void markMyMessagesRead(); void handleSendFailure(const std::optional& clientId, const std::string& text); void sendRead(); void seedMessages(const MessagesInbox& inbox); void appendOptimistic(const std::string& content, const std::vector& attachments, const std::string& clientId); void removeOptimistic(const std::string& clientId); Message makeMessage(const ChatSocketIncomingMessage& incoming) const; void sortMessages(); void restFallback(const std::string& content, const std::vector& attachments, const std::string& clientId, std::function done); User otherUser_; DevPlaceApi& api_; AlertSink alertSink_; SocketFactory socketFactory_; Clock clock_; std::optional inbox_; std::vector messages_; std::string draft_; std::vector attachments_; bool isReloading_ = false; bool isSending_ = false; bool isOtherTyping_ = false; bool isSocketConnected_ = false; int pending_ = 0; std::optional lastTypingSentAt_; std::map pendingSends_; std::unique_ptr socket_; CancelPtr typingTimer_; CancelPtr restTimer_; std::shared_ptr alive_ = std::make_shared(true); }; } // namespace dp