diff --git a/crud.py b/crud.py index 8a5f474..be4d627 100644 --- a/crud.py +++ b/crud.py @@ -418,10 +418,10 @@ 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, incomming) + INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, message, public_key, incoming) VALUES (?, ?, ?, ?, ?, ?) """, - (merchant_id, dm_id, dm.event_id, dm.message, dm.public_key, dm.incomming), + (merchant_id, dm_id, dm.event_id, dm.message, dm.public_key, dm.incoming), ) msg = await get_direct_message(merchant_id, dm_id) @@ -442,7 +442,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.zones WHERE merchant_id = ? AND public_ley = ?", + "SELECT * FROM nostrmarket.direct_messages WHERE merchant_id = ? AND public_key = ? ORDER BY time DESC", (merchant_id, public_key), ) return [DirectMessage.from_row(row) for row in rows] diff --git a/migrations.py b/migrations.py index 08105fa..e9ec647 100644 --- a/migrations.py +++ b/migrations.py @@ -104,7 +104,7 @@ async def m001_initial(db): event_id TEXT, message TEXT NOT NULL, public_key TEXT NOT NULL, - incomming BOOLEAN NOT NULL DEFAULT false, + incoming BOOLEAN NOT NULL DEFAULT false, time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now} ); """ diff --git a/models.py b/models.py index 47220a9..09d753a 100644 --- a/models.py +++ b/models.py @@ -368,11 +368,11 @@ class PartialDirectMessage(BaseModel): event_id: Optional[str] message: str public_key: str - incomming: bool = False + incoming: bool = False time: Optional[int] -class DirectMessage(BaseModel): +class DirectMessage(PartialDirectMessage): id: str @classmethod diff --git a/static/components/direct-messages/direct-messages.html b/static/components/direct-messages/direct-messages.html index 159da03..de7665e 100644 --- a/static/components/direct-messages/direct-messages.html +++ b/static/components/direct-messages/direct-messages.html @@ -1 +1,58 @@ -
dmmms
+
+ + +
Messages
+
+ + + + + + + +
+
+
+ +
+
+ + + + + + + +
+
+
+
diff --git a/static/components/direct-messages/direct-messages.js b/static/components/direct-messages/direct-messages.js index 02f3fd9..4e14431 100644 --- a/static/components/direct-messages/direct-messages.js +++ b/static/components/direct-messages/direct-messages.js @@ -6,9 +6,51 @@ async function directMessages(path) { template, data: function () { - return {} + return { + activePublicKey: + '83d07a79496f4cbdc50ca585741a79a2df1fd938cfa449f0fccb0ab7352115dd', + messages: [], + newMessage: '' + } }, - methods: {}, - created: async function () {} + methods: { + sendMessage: async function () {}, + getDirectMessages: async function () { + if (!this.activePublicKey) { + return + } + try { + const {data} = await LNbits.api.request( + 'GET', + '/nostrmarket/api/v1/message/' + this.activePublicKey, + this.inkey + ) + this.messages = data + console.log('### this.messages', this.messages) + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, + sendDirectMesage: async function () { + try { + const {data} = await LNbits.api.request( + 'POST', + '/nostrmarket/api/v1/message', + this.adminkey, + { + message: this.newMessage, + public_key: this.activePublicKey + } + ) + this.messages.push(data) + this.newMessage = '' + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, + }, + created: async function () { + await this.getDirectMessages() + } }) } diff --git a/tasks.py b/tasks.py index 278e72a..c9eb535 100644 --- a/tasks.py +++ b/tasks.py @@ -149,7 +149,7 @@ async def handle_dirrect_message( event_id=event_id, message=text_msg, public_key=from_pubkey, - incomming=True, + incoming=True, ) await create_direct_message(merchant_id, dm) return None diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index a6fa53d..ec96692 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -101,7 +101,6 @@
-
@@ -115,7 +114,13 @@
-
x111
+
+ + +
@@ -146,6 +151,43 @@
{% endblock%}{% block scripts %} {{ window_vars(user) }} + + + + @@ -159,4 +201,5 @@ + {% endblock %} diff --git a/views_api.py b/views_api.py index 8dc1810..6434332 100644 --- a/views_api.py +++ b/views_api.py @@ -18,6 +18,7 @@ from lnbits.utils.exchange_rates import currencies from . import nostrmarket_ext, scheduled_tasks from .crud import ( + create_direct_message, create_merchant, create_order, create_product, @@ -26,6 +27,7 @@ from .crud import ( delete_product, delete_stall, delete_zone, + get_direct_messages, get_merchant_for_user, get_order, get_order_by_event_id, @@ -45,11 +47,13 @@ from .crud import ( update_zone, ) from .models import ( + DirectMessage, Merchant, Nostrable, Order, OrderExtra, OrderStatusUpdate, + PartialDirectMessage, PartialMerchant, PartialOrder, PartialProduct, @@ -576,6 +580,49 @@ async def api_update_order_status( ) +######################################## DIRECT MESSAGES ######################################## + + +@nostrmarket_ext.get("/api/v1/message/{public_key}") +async def api_get_messages( + public_key: str, wallet: WalletTypeInfo = Depends(get_key_type) +) -> List[DirectMessage]: + try: + merchant = await get_merchant_for_user(wallet.wallet.user) + assert merchant, f"Merchant cannot be found" + + messages = await get_direct_messages(merchant.id, public_key) + return messages + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot get zone", + ) + +@nostrmarket_ext.post("/api/v1/message") +async def api_create_message( + data: PartialDirectMessage, wallet: WalletTypeInfo = Depends(require_admin_key) +) -> DirectMessage: + try: + merchant = await get_merchant_for_user(wallet.wallet.user) + assert merchant, f"Merchant cannot be found" + + dm_event = merchant.build_dm_event(data.message, data.public_key) + data.event_id = dm_event.id + + dm = await create_direct_message(merchant.id, data) + await publish_nostr_event(dm_event) + + return dm + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot create message", + ) + + ######################################## OTHER ########################################