Files
harmony-back/report.md
julien vdb 0bb06e0e2d
All checks were successful
Build release Docker image / Build Docker Images (push) Successful in 7m23s
0.12.18 : add create message, conversation
2026-01-16 13:23:27 +01:00

5.6 KiB

Implementation Report: Chat Messaging Feature

Summary

Successfully implemented the core chat messaging functionality with support for direct messages (1:1) and group conversations (2+). The implementation includes conversation creation with automatic member management, and message creation with permission-based access control.

What Was Implemented

1. Chat Conversation Service (src/api/chat-conversation/services/chat-conversation.ts)

  • createConversationWithMembers(userIds, creatorId, title?) service method
    • Validates all recipient user IDs exist in the database
    • Automatically includes creator in conversation members
    • Deduplicates user list to prevent duplicates
    • Determines conversation type: isGroup = totalUsers > 2
    • Sets title only for group conversations
    • Creates ChatConversationMember records with appropriate roles:
      • owner role for conversation creator
      • member role for other participants
    • Captures joinedAt timestamp for each member

2. Chat Conversation Controller (src/api/chat-conversation/controllers/chat-conversation.ts)

  • createConversation(ctx) controller method
    • Authenticates user from ctx.state.user.id
    • Validates userIds is non-empty array
    • Calls service method to create conversation with members
    • Returns created conversation with populated members
    • Proper error handling with user-friendly messages

3. Chat Message Service (src/api/chat-message/services/chat-message.ts)

  • createMessageInConversation(conversationId, senderId, content) service method
    • Validates all required parameters
    • Verifies conversation exists
    • Permission check: Verifies sender is a member of the conversation
    • Validates message content is non-empty
    • Creates ChatMessage with sender and content
    • Links message to conversation via messages relation
    • Returns created message

4. Chat Message Controller (src/api/chat-message/controllers/chat-message.ts)

  • createMessage(ctx) controller method
    • Authenticates user from ctx.state.user.id
    • Validates message content is provided
    • Workflow supports two scenarios:
      1. Existing conversation: If conversationId provided, creates message in that conversation
      2. New conversation: If conversationId is null, creates new conversation with recipientIds first, then creates message
    • Leverages service layer for all business logic
    • Proper error handling with detailed messages

Architecture Decisions

  1. Service-based approach: All business logic encapsulated in service methods for reusability and testability
  2. Permission checks: Message creation validates user membership in conversation
  3. Member creation: Automatic ChatConversationMember records ensure consistent data relationships
  4. Group detection: Automatic based on user count (>2 = group, ≤2 = direct message)
  5. Unidirectional relation: ChatMessage has no back-relation to ChatConversation (by design, avoids TypeScript type issues on client)

Workflow Diagram

createMessage(conversationId, content, recipientIds)
  ├─ If conversationId is null:
  │   └─ createConversation(recipientIds)
  │       ├─ Validate all user IDs exist
  │       ├─ Determine isGroup (>2 users)
  │       └─ Create ChatConversation + ChatConversationMembers
  │
  └─ createMessageInConversation(conversationId, userId, content)
      ├─ Verify conversation exists
      ├─ Verify user is member (permission check)
      ├─ Create ChatMessage
      └─ Link to conversation

Verification

Build: npm run build completed successfully with no TypeScript errors

  • Compilation time: 2.884s
  • Total build time: ~14s

Type Safety: Full TypeScript compilation with no errors

Edge Cases Handled:

  • Non-existent users validation
  • Permission checks for message creation
  • Empty message content validation
  • Non-existent conversation validation
  • Duplicate user removal in conversation creation

Files Modified

  1. /src/api/chat-conversation/services/chat-conversation.ts — Added createConversationWithMembers()
  2. /src/api/chat-conversation/controllers/chat-conversation.ts — Added createConversation() endpoint
  3. /src/api/chat-message/services/chat-message.ts — Added createMessageInConversation()
  4. /src/api/chat-message/controllers/chat-message.ts — Added createMessage() endpoint

Testing Recommendations

Unit Tests

  • Conversation creation with various user counts
  • Message creation in existing conversations
  • Permission validation for non-members
  • User existence validation
  • Content validation (empty/whitespace)

Integration Tests

  • End-to-end message creation flow with conversation creation
  • ChatConversationMember creation and role assignment
  • Conversation isGroup flag accuracy

API Tests

  • POST /api/chat-conversations/createConversation with valid/invalid payloads
  • POST /api/chat-messages/createMessage with conversation ID
  • POST /api/chat-messages/createMessage with recipient IDs (new conversation)

Known Limitations & Future Enhancements

  1. No bulk operations: Currently single message/conversation per request
  2. No message updates/deletion: Only creation implemented
  3. No read receipts: lastReadAt field exists but not updated
  4. No pagination: Conversation and message listing not implemented
  5. No media support: Messages are text-only

Conclusion

The messaging feature foundation is now complete with core conversation and message creation functionality. The implementation follows Strapi patterns, includes proper validation and permission checks, and is fully type-safe with TypeScript.