feat: store event_created_at for some events

This commit is contained in:
Vlad Stan 2023-03-08 09:52:01 +02:00
parent 240da5277f
commit ef6f2ebe33
7 changed files with 37 additions and 37 deletions

21
crud.py
View file

@ -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]

View file

@ -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,

View file

@ -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

View file

@ -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)
}
@ -42,12 +45,13 @@ async function directMessages(path) {
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()

View file

@ -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()

View file

@ -114,7 +114,7 @@
</q-card-section>
</q-card>
</div>
<div class="col-12">
<div v-if="merchant && merchant.id" class="col-12">
<direct-messages
:inkey="g.user.wallets[0].inkey"
:adminkey="g.user.wallets[0].adminkey"
@ -152,39 +152,19 @@
</div>
{% endblock%}{% block scripts %} {{ window_vars(user) }}
<style scoped>
.q-field__native span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.chat-container {
position: relative;
display: grid;
grid-template-rows: 1fr auto;
/*height: calc(100vh - 200px);*/
height: 50vh;
}
.chat-box {
display: flex;
flex-direction: column-reverse;
padding: 1rem;
overflow-y: auto;
margin-left: auto;
width: 50%;
}
.chat-messages {
width: auto;
}
.chat-input {
position: relative;
display: flex;
align-items: end;
margin-top: 1rem;
width: 100%;
}
</style>
@ -201,5 +181,4 @@
<script src="{{ url_for('nostrmarket_static', path='components/direct-messages/direct-messages.js') }}"></script>
<script src="{{ url_for('nostrmarket_static', path='js/index.js') }}"></script>
{% endblock %}

View file

@ -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)