# 21nayan/chatsphere-backend architecture

> Architecture diagram of the GitHub repository 21nayan/chatsphere-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/21nayan/chatsphere-backend
- Repository: https://github.com/21nayan/chatsphere-backend

## Overview

ChatSphere is a Spring backend for authenticated direct and group chat. Clients use REST endpoints for account, profile, contacts, rooms, and message operations; chat services persist and format conversation data, while a WebSocket endpoint supports realtime delivery. The code also includes email-based account recovery, AI responses, pinned chats, and uploaded attachments. Source excerpts establish several controller-to-service and service-to-repository calls; unsampled subsystems are represented where the source index identifies them, and uncertain wiring is omitted.

Main entry points: [`src/main/java/com/chatsphere/service/ChatService.java`](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/ChatService.java) (Chat service), [`src/main/java/com/chatsphere/service/AuthService.java`](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/AuthService.java) (Authentication service), [`src/main/java/com/chatsphere/repository`](https://github.com/21nayan/chatsphere-backend/tree/main/src/main/java/com/chatsphere/repository) (Data repositories), [`src/main/java/com/chatsphere/controller/ChatController.java`](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/ChatController.java) (Room chat API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API endpoints"]
  node_auth_api["Authentication API"]
  node_chat_api["Room chat API"]
  node_dm_api["Direct message API<br/>[DMController.java]"]
  node_user_api["User and contacts API"]
  node_pinned_api["Pinned chat API"]
  node_email_api["Email API"]
end

subgraph group_identity["Identity and security"]
  node_auth_service["Authentication service<br/>[AuthService.java]"]
  node_jwt_service["JWT service<br/>[JwtService.java]"]
  node_jwt_filter["JWT request filter<br/>[JwtAuthFilter.java]"]
  node_security_config["Security configuration"]
end

subgraph group_chat["Conversation services"]
  node_chat_service["Chat service<br/>[ChatService.java]"]
  node_websocket["WebSocket handler"]
  node_ai_service["AI response service<br/>[AiService.java]"]
end

subgraph group_persistence["Persistence"]
  node_entities["Chat and user entities"]
  node_repositories["Data repositories"]
end

subgraph group_integrations["External integrations"]
  node_email_service["Email service<br/>[EmailService.java]"]
end

node_client(("Chat client"))
node_mail_provider(("Mail provider"))
node_attachment_store[("Attachment files")]
node_ai_provider(("AI provider"))

node_client -->|"authenticates"| node_auth_api
node_client -->|"sends requests"| node_chat_api
node_client -->|"sends requests"| node_dm_api
node_client -->|"manages profile"| node_user_api
node_client -->|"manages pins"| node_pinned_api
node_client -->|"requests email"| node_email_api
node_auth_api -->|"delegates to"| node_auth_service
node_auth_service -->|"reads and writes"| node_repositories
node_auth_service -->|"issues tokens"| node_jwt_service
node_auth_service -->|"sends recovery"| node_email_service
node_security_config -->|"configures"| node_jwt_filter
node_jwt_filter -->|"validates tokens"| node_jwt_service
node_chat_api -->|"delegates to"| node_chat_service
node_dm_api -->|"delegates to"| node_chat_service
node_chat_service -->|"reads and writes"| node_repositories
node_chat_service -->|"requests responses"| node_ai_service
node_websocket -->|"delegates to"| node_chat_service
node_user_api -->|"reads and writes"| node_repositories
node_user_api -->|"formats user data"| node_chat_service
node_pinned_api -->|"reads and writes"| node_repositories
node_email_api -->|"delegates to"| node_email_service
node_email_service -->|"sends mail"| node_mail_provider
node_chat_api -->|"writes uploads"| node_attachment_store
node_ai_service -.->|"requests completion"| node_ai_provider

click node_auth_api "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/AuthController.java"
click node_chat_api "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/ChatController.java"
click node_dm_api "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/DMController.java"
click node_user_api "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/UserController.java"
click node_pinned_api "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/PinnedChatController.java"
click node_email_api "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/EmailController.java"
click node_auth_service "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/AuthService.java"
click node_jwt_service "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/JwtService.java"
click node_jwt_filter "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/security/JwtAuthFilter.java"
click node_security_config "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/config/SecurityConfig.java"
click node_chat_service "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/ChatService.java"
click node_websocket "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/websocket/WebSocketController.java"
click node_ai_service "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/AiService.java"
click node_entities "https://github.com/21nayan/chatsphere-backend/tree/main/src/main/java/com/chatsphere/model"
click node_repositories "https://github.com/21nayan/chatsphere-backend/tree/main/src/main/java/com/chatsphere/repository"
click node_email_service "https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/EmailService.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_auth_api,node_chat_api,node_dm_api,node_user_api,node_pinned_api,node_email_api,node_client toneBlue
class node_auth_service,node_jwt_service,node_jwt_filter,node_security_config,node_attachment_store toneAmber
class node_chat_service,node_websocket,node_ai_service toneMint
class node_entities,node_repositories toneRose
class node_email_service,node_mail_provider,node_ai_provider toneIndigo
```

## Components

### API endpoints

- [Authentication API](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/AuthController.java): `src/main/java/com/chatsphere/controller/AuthController.java`
- [Room chat API](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/ChatController.java): `src/main/java/com/chatsphere/controller/ChatController.java`
- [Direct message API](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/DMController.java): `src/main/java/com/chatsphere/controller/DMController.java`
- [User and contacts API](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/UserController.java): `src/main/java/com/chatsphere/controller/UserController.java`
- [Pinned chat API](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/PinnedChatController.java): `src/main/java/com/chatsphere/controller/PinnedChatController.java`
- [Email API](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/controller/EmailController.java): `src/main/java/com/chatsphere/controller/EmailController.java`

### Identity and security

- [Authentication service](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/AuthService.java): `src/main/java/com/chatsphere/service/AuthService.java`
- [JWT service](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/JwtService.java): `src/main/java/com/chatsphere/service/JwtService.java`
- [JWT request filter](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/security/JwtAuthFilter.java): `src/main/java/com/chatsphere/security/JwtAuthFilter.java`
- [Security configuration](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/config/SecurityConfig.java): `src/main/java/com/chatsphere/config/SecurityConfig.java`

### Conversation services

- [Chat service](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/ChatService.java): `src/main/java/com/chatsphere/service/ChatService.java`
- [WebSocket handler](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/websocket/WebSocketController.java): `src/main/java/com/chatsphere/websocket/WebSocketController.java`
- [AI response service](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/AiService.java): `src/main/java/com/chatsphere/service/AiService.java`

### Persistence

- [Chat and user entities](https://github.com/21nayan/chatsphere-backend/tree/main/src/main/java/com/chatsphere/model): `src/main/java/com/chatsphere/model`
- [Data repositories](https://github.com/21nayan/chatsphere-backend/tree/main/src/main/java/com/chatsphere/repository): `src/main/java/com/chatsphere/repository`

### External integrations

- [Email service](https://github.com/21nayan/chatsphere-backend/blob/main/src/main/java/com/chatsphere/service/EmailService.java): `src/main/java/com/chatsphere/service/EmailService.java`

### Other components

- **Chat client**
- **Mail provider**
- **Attachment files**
- **AI provider**

## Connections

- Chat client → Authentication API: authenticates
- Chat client → Room chat API: sends requests
- Chat client → Direct message API: sends requests
- Chat client → User and contacts API: manages profile
- Chat client → Pinned chat API: manages pins
- Chat client → Email API: requests email
- Authentication API → Authentication service: delegates to (evidence: `src/main/java/com/chatsphere/controller/AuthController.java`)
- Authentication service → Data repositories: reads and writes (evidence: `src/main/java/com/chatsphere/service/AuthService.java`)
- Authentication service → JWT service: issues tokens (evidence: `src/main/java/com/chatsphere/service/AuthService.java`)
- Authentication service → Email service: sends recovery (evidence: `src/main/java/com/chatsphere/service/AuthService.java`)
- Security configuration → JWT request filter: configures (evidence: `src/main/java/com/chatsphere/config/SecurityConfig.java`)
- JWT request filter → JWT service: validates tokens (evidence: `src/main/java/com/chatsphere/security/JwtAuthFilter.java`)
- Room chat API → Chat service: delegates to (evidence: `src/main/java/com/chatsphere/controller/ChatController.java`)
- Direct message API → Chat service: delegates to (evidence: `src/main/java/com/chatsphere/controller/DMController.java`)
- Chat service → Data repositories: reads and writes (evidence: `src/main/java/com/chatsphere/service/ChatService.java`)
- Chat service → AI response service: requests responses (evidence: `src/main/java/com/chatsphere/service/ChatService.java`)
- WebSocket handler → Chat service: delegates to (evidence: `src/main/java/com/chatsphere/websocket/WebSocketController.java`)
- User and contacts API → Data repositories: reads and writes (evidence: `src/main/java/com/chatsphere/controller/UserController.java`)
- User and contacts API → Chat service: formats user data (evidence: `src/main/java/com/chatsphere/controller/UserController.java`)
- Pinned chat API → Data repositories: reads and writes (evidence: `src/main/java/com/chatsphere/controller/PinnedChatController.java`)
- Email API → Email service: delegates to (evidence: `src/main/java/com/chatsphere/controller/EmailController.java`)
- Email service → Mail provider: sends mail (evidence: `src/main/java/com/chatsphere/service/EmailService.java`)
- Room chat API → Attachment files: writes uploads (evidence: `src/main/java/com/chatsphere/controller/ChatController.java`)
- AI response service → AI provider: requests completion
