DM o'qilgan/o'qilmagan¶
1. Nima va nega¶
Har bir xodim o'z unread sonini ko'radi: bir xodim chatni ochsa, boshqalarda bu chat o'qilmagan bo'lib qoladi. Mijoz xabarlari va mijozga yuborilgan AI javoblari sanaladi. Xodim yoki egasi o'zi yozgan javoblar sanalmaydi. Mijoz "Seen" belgisini ko'rmaydi, hammasi faqat Gapex ichida.
2. Oqim (sequence)¶
sequenceDiagram
participant App as Mobil ilova
participant API as inbox.py
participant R as dm_reads.py
participant DB
Note over App,DB: Ro'yxat va badge
App->>API: GET /conversations
API->>R: unread_count_sq(user)
R->>DB: watermark'dan keyingi sanaladigan xabarlar
DB-->>API: har chat uchun unread_count
API-->>App: ro'yxat (har chatda unread_count)
App->>API: GET /conversations/unread-count
API-->>App: {count: N} (jami badge)
Note over App,DB: Chat o'qildi
App->>API: POST /conversations/{id}/read
API->>R: mark_read(user, conv)
R->>DB: UPSERT last_read_at = GREATEST(eski, yangi)
API-->>App: yangi unread_count (odatda 0)
Note over App,DB: "O'qilmagan qilish"
App->>API: POST /conversations/{id}/unread
API->>R: mark_unread(user, conv)
R->>DB: last_read_at = oxirgi sanaladigan xabar − 1 µs
API-->>App: unread_count = 1
3. Qarorlar (flowchart)¶
Bitta xabar shu user uchun sanaladimi:
flowchart TD
A[Xabar] --> B{inbound?}
B -- ha --> W
B -- yo'q --> C{AI yozganmi?}
C -- "yo'q (human/external)" --> N[Sanalmaydi]
C -- ha --> D{published?}
D -- "yo'q (draft)" --> N
D -- ha --> E{Shu user tasdiqlaganmi?}
E -- ha --> N
E -- yo'q --> W{"Vaqti > watermark?"}
W -- ha --> U[Unread]
W -- yo'q --> N
Watermark qayerdan olinadi (xabar vaqti = published_at, bo'lmasa created_at):
flowchart TD
X{"conversation_reads<br/>qatori bormi?"} -- ha --> Y[last_read_at]
X -- yo'q --> Z["GREATEST(membership.created_at,<br/>conversation.created_at)"]
4. Edge case'lar¶
| # | Holat | Nima bo'ladi | Tekshirilgan |
|---|---|---|---|
| 1 | A chatni o'qidi | Faqat A'da 0 bo'ladi, B'da o'zgarmaydi | test DB, 09-29 |
| 2 | Eskiroq xabargacha read yuborildi |
Watermark orqaga ketmaydi, hech narsa o'zgarmaydi | test DB, 09-29 |
| 3 | Deploy (hali hech kim o'qimagan) | Eski chatlar unread bo'lib qolmaydi: default watermark ishlaydi | test DB, 09-29 |
| 4 | Yangi xodim org'ga qo'shildi | Qo'shilishidan oldingi tarix unread emas | test DB, 09-29 |
| 5 | AI draft'ni A tasdiqladi | A uchun sanalmaydi, B uchun sanaladi | test DB, 09-29 |
| 6 | Membership'i yo'q user | Chat yaratilgan vaqtdan keyingi xabarlar sanaladi | test DB, 09-29 |
| 7 | Operator chatga javob yozdi | Shu operator uchun chat o'qilgan bo'ladi | kod bo'yicha |
| 8 | Connect paytida import qilingan tarix | Xabar vaqti import vaqti bo'lsa, unread ko'rinishi mumkin | tekshirilmagan |
| 9 | SSE/feed event'lari | unread_count = null (per-user son), ilova o'zi +1 qiladi yoki qayta so'raydi |
kod bo'yicha |
5. Bug bo'lsa qayerga qarash¶
Simptomlar¶
| Simptom | Qayerga qarash | Ehtimoliy sabab |
|---|---|---|
| Badge 0 ga tushmaydi | Ilova so'rovlari | Ilova chat ochilganda POST /readni chaqirmayapti |
/unread-count 422 qaytaradi |
api/routes/inbox.pydagi route tartibi |
/unread-count /{conversation_id}dan keyin e'lon qilingan |
| Yangi account'da hamma chat unread | dm_messages.created_at va conversations.created_at |
Import qilingan tarix import vaqti bilan saqlangan (edge case 8) |
| Bir xodim o'qisa, boshqasida ham 0 | conversation_reads.user_id |
Noto'g'ri user bilan yozilgan |
| AI javobi unread'da ko'rinmaydi | dm_messages.status, generated_by |
Xabar hali published emas yoki shu user tasdiqlagan |
Buyruqlar¶
-- user'ning watermark'lari
select conversation_id, last_read_at, updated_at
from conversation_reads
where user_id = '<user_id>'
order by updated_at desc limit 20;
-- chatdagi sanaladigan xabarlar
select id, direction, generated_by, status, approved_by,
coalesce(published_at, created_at) as at
from dm_messages
where conversation_id = '<conversation_id>'
order by at desc limit 20;
Kod joylari¶
| Qism | Fayl |
|---|---|
| Endpoint'lar | api/routes/inbox.py (list_conversations, conversations_unread_count, mark_conversation_read, mark_conversation_unread) |
| Asosiy mantiq | processing/dm_reads.py (counted, watermark, mark_read, mark_unread) |
| Jadval | db/models.py (ConversationRead), migration c9e2a7f4b1d8 |
| Testlar | tests/api/test_dm_unread.py |