feat: keep track of new messages

This commit is contained in:
Vlad Stan 2023-03-29 14:20:49 +03:00
parent 098cb5adf5
commit e04b9dc448
7 changed files with 32 additions and 1 deletions

14
crud.py
View file

@ -641,3 +641,17 @@ async def update_customer_profile(
f"UPDATE nostrmarket.customers SET event_created_at = ?, meta = ? WHERE public_key = ?", f"UPDATE nostrmarket.customers SET event_created_at = ?, meta = ? WHERE public_key = ?",
(event_created_at, json.dumps(profile.dict()), 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,),
)

View file

@ -135,6 +135,7 @@ async def m001_initial(db):
merchant_id TEXT NOT NULL, merchant_id TEXT NOT NULL,
public_key TEXT NOT NULL, public_key TEXT NOT NULL,
event_created_at INTEGER, event_created_at INTEGER,
unread_messages INTEGER NOT NULL DEFAULT 1,
meta TEXT NOT NULL DEFAULT '{}' meta TEXT NOT NULL DEFAULT '{}'
); );
""" """

View file

@ -442,6 +442,7 @@ class Customer(BaseModel):
public_key: str public_key: str
event_created_at: Optional[int] event_created_at: Optional[int]
profile: Optional[CustomerProfile] profile: Optional[CustomerProfile]
unread_messages: int = 0
@classmethod @classmethod
def from_row(cls, row: Row) -> "Customer": def from_row(cls, row: Row) -> "Customer":

View file

@ -20,6 +20,7 @@ from .crud import (
get_products_by_ids, get_products_by_ids,
get_stalls, get_stalls,
get_wallet_for_product, get_wallet_for_product,
increment_customer_unread_messages,
update_customer_profile, update_customer_profile,
update_order_paid_status, update_order_paid_status,
update_product, update_product,
@ -256,6 +257,8 @@ async def _handle_incoming_dms(
customer = await get_customer(merchant.id, event.pubkey) customer = await get_customer(merchant.id, event.pubkey)
if not customer: if not customer:
await _handle_new_customer(event, merchant) await _handle_new_customer(event, merchant)
else:
await increment_customer_unread_messages(event.pubkey)
dm_reply = await _handle_dirrect_message( dm_reply = await _handle_dirrect_message(
merchant.id, merchant.id,

View file

@ -9,7 +9,7 @@
<q-card-section> <q-card-section>
<q-select <q-select
v-model="activePublicKey" v-model="activePublicKey"
:options="customers.map(c => ({label: `${c.profile.name || 'unknown'} ${c.profile.about || ''} (${c.public_key.slice(0, 16)}...${c.public_key.slice(c.public_key.length - 16)})`, value: c.public_key}))" :options="customers.map(c => ({label: buildCustomerLabel(c), value: c.public_key}))"
label="Select Customer" label="Select Customer"
emit-value emit-value
@input="selectActiveCustomer()" @input="selectActiveCustomer()"

View file

@ -20,6 +20,16 @@ async function directMessages(path) {
}, },
methods: { methods: {
sendMessage: async function () {}, sendMessage: async function () {},
buildCustomerLabel: function (c) {
let label = `${c.profile.name || 'unknown'} ${c.profile.about || ''}`
if (c.unread_messages) {
label += `[new: ${c.unread_messages}]`
}
label += ` (${c.public_key.slice(0, 16)}...${c.public_key.slice(
c.public_key.length - 16
)}`
return label
},
getDirectMessages: async function (pubkey) { getDirectMessages: async function (pubkey) {
if (!pubkey) { if (!pubkey) {
this.messages = [] this.messages = []

View file

@ -44,6 +44,7 @@ from .crud import (
get_stalls, get_stalls,
get_zone, get_zone,
get_zones, get_zones,
update_customer_no_unread_messages,
update_merchant, update_merchant,
update_order_shipped_status, update_order_shipped_status,
update_product, update_product,
@ -738,6 +739,7 @@ async def api_get_messages(
assert merchant, f"Merchant cannot be found" assert merchant, f"Merchant cannot be found"
messages = await get_direct_messages(merchant.id, public_key) messages = await get_direct_messages(merchant.id, public_key)
await update_customer_no_unread_messages(public_key)
return messages return messages
except AssertionError as ex: except AssertionError as ex:
raise HTTPException( raise HTTPException(