From ef6f2ebe337779201dd14e1445dffe41265c710e Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 8 Mar 2023 09:52:01 +0200 Subject: [PATCH] feat: store `event_created_at` for some events --- crud.py | 21 +++++++++++----- migrations.py | 2 ++ models.py | 2 ++ .../direct-messages/direct-messages.js | 14 +++++++---- tasks.py | 8 +++--- templates/nostrmarket/index.html | 25 ++----------------- views_api.py | 2 ++ 7 files changed, 37 insertions(+), 37 deletions(-) diff --git a/crud.py b/crud.py index be4d627..2d89175 100644 --- a/crud.py +++ b/crud.py @@ -318,13 +318,14 @@ async def delete_product(user_id: str, product_id: str) -> None: async def create_order(user_id: str, o: Order) -> Order: await db.execute( f""" - INSERT INTO nostrmarket.orders (user_id, id, event_id, pubkey, address, contact_data, extra_data, order_items, stall_id, invoice_id, total) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO nostrmarket.orders (user_id, id, event_id, event_created_at, pubkey, address, contact_data, extra_data, order_items, stall_id, invoice_id, total) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( user_id, o.id, o.event_id, + o.event_created_at, o.pubkey, o.address, json.dumps(o.contact.dict() if o.contact else {}), @@ -418,10 +419,18 @@ async def create_direct_message( dm_id = urlsafe_short_hash() await db.execute( f""" - INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, message, public_key, incoming) - VALUES (?, ?, ?, ?, ?, ?) + INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, event_created_at, message, public_key, incoming) + VALUES (?, ?, ?, ?, ?, ?, ?) """, - (merchant_id, dm_id, dm.event_id, dm.message, dm.public_key, dm.incoming), + ( + merchant_id, + dm_id, + dm.event_id, + dm.event_created_at, + dm.message, + dm.public_key, + dm.incoming, + ), ) msg = await get_direct_message(merchant_id, dm_id) @@ -442,7 +451,7 @@ async def get_direct_message(merchant_id: str, dm_id: str) -> Optional[DirectMes async def get_direct_messages(merchant_id: str, public_key: str) -> List[DirectMessage]: rows = await db.fetchall( - "SELECT * FROM nostrmarket.direct_messages WHERE merchant_id = ? AND public_key = ? ORDER BY time DESC", + "SELECT * FROM nostrmarket.direct_messages WHERE merchant_id = ? AND public_key = ? ORDER BY event_created_at", (merchant_id, public_key), ) return [DirectMessage.from_row(row) for row in rows] diff --git a/migrations.py b/migrations.py index e9ec647..6b3c3c5 100644 --- a/migrations.py +++ b/migrations.py @@ -78,6 +78,7 @@ async def m001_initial(db): user_id TEXT NOT NULL, id TEXT PRIMARY KEY, event_id TEXT, + event_created_at INTEGER NOT NULL, pubkey TEXT NOT NULL, contact_data TEXT NOT NULL DEFAULT '{empty_object}', extra_data TEXT NOT NULL DEFAULT '{empty_object}', @@ -102,6 +103,7 @@ async def m001_initial(db): merchant_id TEXT NOT NULL, id TEXT PRIMARY KEY, event_id TEXT, + event_created_at INTEGER NOT NULL, message TEXT NOT NULL, public_key TEXT NOT NULL, incoming BOOLEAN NOT NULL DEFAULT false, diff --git a/models.py b/models.py index 09d753a..c76d25b 100644 --- a/models.py +++ b/models.py @@ -281,6 +281,7 @@ class OrderExtra(BaseModel): class PartialOrder(BaseModel): id: str event_id: Optional[str] + event_created_at: Optional[int] pubkey: str items: List[OrderItem] contact: Optional[OrderContact] @@ -366,6 +367,7 @@ class PaymentRequest(BaseModel): class PartialDirectMessage(BaseModel): event_id: Optional[str] + event_created_at: Optional[int] message: str public_key: str incoming: bool = False diff --git a/static/components/direct-messages/direct-messages.js b/static/components/direct-messages/direct-messages.js index 4e14431..7a4299a 100644 --- a/static/components/direct-messages/direct-messages.js +++ b/static/components/direct-messages/direct-messages.js @@ -26,7 +26,10 @@ async function directMessages(path) { this.inkey ) this.messages = data - console.log('### this.messages', this.messages) + console.log( + '### this.messages', + this.messages.map(m => m.message) + ) } catch (error) { LNbits.utils.notifyApiError(error) } @@ -38,16 +41,17 @@ async function directMessages(path) { '/nostrmarket/api/v1/message', this.adminkey, { - message: this.newMessage, - public_key: this.activePublicKey + message: this.newMessage, + public_key: this.activePublicKey } ) - this.messages.push(data) + this.messages = this.messages.concat([data]) + console.log('### this.messages', this.messages) this.newMessage = '' } catch (error) { LNbits.utils.notifyApiError(error) } - }, + } }, created: async function () { await this.getDirectMessages() diff --git a/tasks.py b/tasks.py index c9eb535..06e6a87 100644 --- a/tasks.py +++ b/tasks.py @@ -127,7 +127,7 @@ async def handle_nip04_message(public_key: str, event: NostrEvent): clear_text_msg = merchant.decrypt_message(event.content, event.pubkey) dm_content = await handle_dirrect_message( - merchant.id, event.pubkey, event.id, clear_text_msg + merchant.id, event.pubkey, event.id, event.created_at, clear_text_msg ) if dm_content: dm_event = merchant.build_dm_event(dm_content, event.pubkey) @@ -135,18 +135,20 @@ async def handle_nip04_message(public_key: str, event: NostrEvent): async def handle_dirrect_message( - merchant_id: str, from_pubkey: str, event_id: str, msg: str + merchant_id: str, from_pubkey: str, event_id: str, event_created_at: int, msg: str ) -> Optional[str]: order, text_msg = order_from_json(msg) try: if order: order["pubkey"] = from_pubkey order["event_id"] = event_id + order["event_created_at"] = event_created_at return await handle_new_order(PartialOrder(**order)) else: print("### text_msg", text_msg) dm = PartialDirectMessage( event_id=event_id, + event_created_at=event_created_at, message=text_msg, public_key=from_pubkey, incoming=True, @@ -158,7 +160,7 @@ async def handle_dirrect_message( return None -async def handle_new_order(order: PartialOrder): +async def handle_new_order(order: PartialOrder) -> Optional[str]: ### todo: check that event_id not parsed already order.validate_order() diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index ec96692..fe7c6b0 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -114,7 +114,7 @@ -
+
{% endblock%}{% block scripts %} {{ window_vars(user) }} - @@ -201,5 +181,4 @@ - {% endblock %} diff --git a/views_api.py b/views_api.py index 6434332..d3cca1e 100644 --- a/views_api.py +++ b/views_api.py @@ -600,6 +600,7 @@ async def api_get_messages( detail="Cannot get zone", ) + @nostrmarket_ext.post("/api/v1/message") async def api_create_message( data: PartialDirectMessage, wallet: WalletTypeInfo = Depends(require_admin_key) @@ -610,6 +611,7 @@ async def api_create_message( dm_event = merchant.build_dm_event(data.message, data.public_key) data.event_id = dm_event.id + data.event_created_at = dm_event.created_at dm = await create_direct_message(merchant.id, data) await publish_nostr_event(dm_event)