From e04b9dc4481f17943845846bec3382eefeaa980d Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 29 Mar 2023 14:20:49 +0300 Subject: [PATCH] feat: keep track of new messages --- crud.py | 14 ++++++++++++++ migrations.py | 1 + models.py | 1 + services.py | 3 +++ .../direct-messages/direct-messages.html | 2 +- .../components/direct-messages/direct-messages.js | 10 ++++++++++ views_api.py | 2 ++ 7 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crud.py b/crud.py index 196a973..131bd2e 100644 --- a/crud.py +++ b/crud.py @@ -641,3 +641,17 @@ async def update_customer_profile( f"UPDATE nostrmarket.customers SET event_created_at = ?, meta = ? WHERE public_key = ?", (event_created_at, json.dumps(profile.dict()), public_key), ) + + +async def increment_customer_unread_messages(public_key: str): + await db.execute( + f"UPDATE nostrmarket.customers SET unread_messages = unread_messages + 1 WHERE public_key = ?", + (public_key,), + ) + + +async def update_customer_no_unread_messages(public_key: str): + await db.execute( + f"UPDATE nostrmarket.customers SET unread_messages = 0 WHERE public_key = ?", + (public_key,), + ) diff --git a/migrations.py b/migrations.py index 9edc050..27d4e21 100644 --- a/migrations.py +++ b/migrations.py @@ -135,6 +135,7 @@ async def m001_initial(db): merchant_id TEXT NOT NULL, public_key TEXT NOT NULL, event_created_at INTEGER, + unread_messages INTEGER NOT NULL DEFAULT 1, meta TEXT NOT NULL DEFAULT '{}' ); """ diff --git a/models.py b/models.py index fbb43de..e7565eb 100644 --- a/models.py +++ b/models.py @@ -442,6 +442,7 @@ class Customer(BaseModel): public_key: str event_created_at: Optional[int] profile: Optional[CustomerProfile] + unread_messages: int = 0 @classmethod def from_row(cls, row: Row) -> "Customer": diff --git a/services.py b/services.py index f06fa1c..46a3aba 100644 --- a/services.py +++ b/services.py @@ -20,6 +20,7 @@ from .crud import ( get_products_by_ids, get_stalls, get_wallet_for_product, + increment_customer_unread_messages, update_customer_profile, update_order_paid_status, update_product, @@ -256,6 +257,8 @@ async def _handle_incoming_dms( customer = await get_customer(merchant.id, event.pubkey) if not customer: await _handle_new_customer(event, merchant) + else: + await increment_customer_unread_messages(event.pubkey) dm_reply = await _handle_dirrect_message( merchant.id, diff --git a/static/components/direct-messages/direct-messages.html b/static/components/direct-messages/direct-messages.html index 5d4c678..409ea3a 100644 --- a/static/components/direct-messages/direct-messages.html +++ b/static/components/direct-messages/direct-messages.html @@ -9,7 +9,7 @@