File size: 3,138 Bytes
cc276cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Project Technical Specifications

This document outlines the technical details of the project's database structure and data objects, as required for developing server-side functions like push notifications.

## 1. Database Structure (JSON Structure)

### Firestore (For User Information)

User profiles, including their FCM token for notifications, are stored in Firestore.

-   **Path to Users:** `/users/{userId}`
-   **Example User Object in Firestore:**
    ```json
    {
      "uid": "user_abc_123",
      "displayName": "Ahmed",
      "publicId": "ahmed123",
      "photoURL": "https://.../avatar.png",
      "fcmToken": "c3p...very_long_and_unique_token...w9G"
    }
    ```

### Realtime Database (For Chat Messages)

Real-time messaging is handled by the Realtime Database for speed.

-   **Path to Chats:** `/chats/{chatId}/messages/{messageId}`
    -   The `chatId` for private chats is generated by combining the UIDs of the two users, sorted alphabetically (e.g., `private_user1_user2`).
    -   The `chatId` for group chats is the unique ID of the group.
-   **Example JSON structure in Realtime Database:**
    ```json
    {
      "chats": {
        "private_user1_user2": {
          "messages": {
            "msg_xyz_789": {
              "sender": "user1",
              "senderDisplayName": "Alice",
              "text": "Hello Bob!",
              "timestamp": 1678886400000,
              "deliveredTo": {
                "user1": 1678886400000
              }
            }
          },
          "participants": {
             "user1": true,
             "user2": true
          }
        }
      }
    }
    ```

---

## 2. Message Object Details

This is the structure of the message object saved under `/chats/{chatId}/messages/{messageId}`.

-   **Is there a `receiverId` field?**
    No. The recipient is inferred from the `chatId`. In a private chat like `private_user1_user2`, if the `sender` is `user1`, the recipient is `user2`.

-   **Is there a `senderName` field?**
    Yes, the field is named `senderDisplayName`.

-   **Example Message Object:**
    ```json
    {
      "sender": "user_abc_123",
      "senderDisplayName": "Ahmed",
      "senderPhotoUrl": "https://.../avatar.png",
      "text": "Hello, how are you?",
      "timestamp": { ".sv": "timestamp" },
      "deliveredTo": {
        "user_abc_123": { ".sv": "timestamp" }
      }
    }
    ```

---

## 3. Multiple Devices

-   Currently, the system saves only **one FCM token per user** as a `String` in their Firestore document.
-   This means that notifications are sent to the last device that registered its token. The system does not support sending notifications to multiple devices for the same user simultaneously.

---

## 4. Tech Stack

-   **Backend Logic (Cloud Functions):**
    We are using **TypeScript** within **Next.js API Routes** (e.g., `src/app/api/.../route.ts`). This serves as the server-side environment instead of traditional Cloud Functions.

-   **Frontend Firebase SDK:**
    The project uses the modern **Firebase SDK v9 (Modular)**. Imports follow the pattern: `import { ... } from 'firebase/database';`.