feat: store event_created_at for some events
This commit is contained in:
parent
240da5277f
commit
ef6f2ebe33
7 changed files with 37 additions and 37 deletions
21
crud.py
21
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:
|
async def create_order(user_id: str, o: Order) -> Order:
|
||||||
await db.execute(
|
await db.execute(
|
||||||
f"""
|
f"""
|
||||||
INSERT INTO nostrmarket.orders (user_id, id, event_id, pubkey, address, contact_data, extra_data, order_items, stall_id, invoice_id, total)
|
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
user_id,
|
user_id,
|
||||||
o.id,
|
o.id,
|
||||||
o.event_id,
|
o.event_id,
|
||||||
|
o.event_created_at,
|
||||||
o.pubkey,
|
o.pubkey,
|
||||||
o.address,
|
o.address,
|
||||||
json.dumps(o.contact.dict() if o.contact else {}),
|
json.dumps(o.contact.dict() if o.contact else {}),
|
||||||
|
|
@ -418,10 +419,18 @@ async def create_direct_message(
|
||||||
dm_id = urlsafe_short_hash()
|
dm_id = urlsafe_short_hash()
|
||||||
await db.execute(
|
await db.execute(
|
||||||
f"""
|
f"""
|
||||||
INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, message, public_key, incoming)
|
INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, event_created_at, message, public_key, incoming)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
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)
|
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]:
|
async def get_direct_messages(merchant_id: str, public_key: str) -> List[DirectMessage]:
|
||||||
rows = await db.fetchall(
|
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),
|
(merchant_id, public_key),
|
||||||
)
|
)
|
||||||
return [DirectMessage.from_row(row) for row in rows]
|
return [DirectMessage.from_row(row) for row in rows]
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ async def m001_initial(db):
|
||||||
user_id TEXT NOT NULL,
|
user_id TEXT NOT NULL,
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
event_id TEXT,
|
event_id TEXT,
|
||||||
|
event_created_at INTEGER NOT NULL,
|
||||||
pubkey TEXT NOT NULL,
|
pubkey TEXT NOT NULL,
|
||||||
contact_data TEXT NOT NULL DEFAULT '{empty_object}',
|
contact_data TEXT NOT NULL DEFAULT '{empty_object}',
|
||||||
extra_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,
|
merchant_id TEXT NOT NULL,
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
event_id TEXT,
|
event_id TEXT,
|
||||||
|
event_created_at INTEGER NOT NULL,
|
||||||
message TEXT NOT NULL,
|
message TEXT NOT NULL,
|
||||||
public_key TEXT NOT NULL,
|
public_key TEXT NOT NULL,
|
||||||
incoming BOOLEAN NOT NULL DEFAULT false,
|
incoming BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
|
|
||||||
|
|
@ -281,6 +281,7 @@ class OrderExtra(BaseModel):
|
||||||
class PartialOrder(BaseModel):
|
class PartialOrder(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
event_id: Optional[str]
|
event_id: Optional[str]
|
||||||
|
event_created_at: Optional[int]
|
||||||
pubkey: str
|
pubkey: str
|
||||||
items: List[OrderItem]
|
items: List[OrderItem]
|
||||||
contact: Optional[OrderContact]
|
contact: Optional[OrderContact]
|
||||||
|
|
@ -366,6 +367,7 @@ class PaymentRequest(BaseModel):
|
||||||
|
|
||||||
class PartialDirectMessage(BaseModel):
|
class PartialDirectMessage(BaseModel):
|
||||||
event_id: Optional[str]
|
event_id: Optional[str]
|
||||||
|
event_created_at: Optional[int]
|
||||||
message: str
|
message: str
|
||||||
public_key: str
|
public_key: str
|
||||||
incoming: bool = False
|
incoming: bool = False
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,10 @@ async function directMessages(path) {
|
||||||
this.inkey
|
this.inkey
|
||||||
)
|
)
|
||||||
this.messages = data
|
this.messages = data
|
||||||
console.log('### this.messages', this.messages)
|
console.log(
|
||||||
|
'### this.messages',
|
||||||
|
this.messages.map(m => m.message)
|
||||||
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
LNbits.utils.notifyApiError(error)
|
LNbits.utils.notifyApiError(error)
|
||||||
}
|
}
|
||||||
|
|
@ -38,16 +41,17 @@ async function directMessages(path) {
|
||||||
'/nostrmarket/api/v1/message',
|
'/nostrmarket/api/v1/message',
|
||||||
this.adminkey,
|
this.adminkey,
|
||||||
{
|
{
|
||||||
message: this.newMessage,
|
message: this.newMessage,
|
||||||
public_key: this.activePublicKey
|
public_key: this.activePublicKey
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
this.messages.push(data)
|
this.messages = this.messages.concat([data])
|
||||||
|
console.log('### this.messages', this.messages)
|
||||||
this.newMessage = ''
|
this.newMessage = ''
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
LNbits.utils.notifyApiError(error)
|
LNbits.utils.notifyApiError(error)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
created: async function () {
|
created: async function () {
|
||||||
await this.getDirectMessages()
|
await this.getDirectMessages()
|
||||||
|
|
|
||||||
8
tasks.py
8
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)
|
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
|
||||||
dm_content = await handle_dirrect_message(
|
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:
|
if dm_content:
|
||||||
dm_event = merchant.build_dm_event(dm_content, event.pubkey)
|
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(
|
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]:
|
) -> Optional[str]:
|
||||||
order, text_msg = order_from_json(msg)
|
order, text_msg = order_from_json(msg)
|
||||||
try:
|
try:
|
||||||
if order:
|
if order:
|
||||||
order["pubkey"] = from_pubkey
|
order["pubkey"] = from_pubkey
|
||||||
order["event_id"] = event_id
|
order["event_id"] = event_id
|
||||||
|
order["event_created_at"] = event_created_at
|
||||||
return await handle_new_order(PartialOrder(**order))
|
return await handle_new_order(PartialOrder(**order))
|
||||||
else:
|
else:
|
||||||
print("### text_msg", text_msg)
|
print("### text_msg", text_msg)
|
||||||
dm = PartialDirectMessage(
|
dm = PartialDirectMessage(
|
||||||
event_id=event_id,
|
event_id=event_id,
|
||||||
|
event_created_at=event_created_at,
|
||||||
message=text_msg,
|
message=text_msg,
|
||||||
public_key=from_pubkey,
|
public_key=from_pubkey,
|
||||||
incoming=True,
|
incoming=True,
|
||||||
|
|
@ -158,7 +160,7 @@ async def handle_dirrect_message(
|
||||||
return None
|
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
|
### todo: check that event_id not parsed already
|
||||||
|
|
||||||
order.validate_order()
|
order.validate_order()
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-12">
|
<div v-if="merchant && merchant.id" class="col-12">
|
||||||
<direct-messages
|
<direct-messages
|
||||||
:inkey="g.user.wallets[0].inkey"
|
:inkey="g.user.wallets[0].inkey"
|
||||||
:adminkey="g.user.wallets[0].adminkey"
|
:adminkey="g.user.wallets[0].adminkey"
|
||||||
|
|
@ -152,39 +152,19 @@
|
||||||
</div>
|
</div>
|
||||||
{% endblock%}{% block scripts %} {{ window_vars(user) }}
|
{% endblock%}{% block scripts %} {{ window_vars(user) }}
|
||||||
|
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.q-field__native span {
|
|
||||||
white-space: nowrap;
|
|
||||||
overflow: hidden;
|
|
||||||
text-overflow: ellipsis;
|
|
||||||
}
|
|
||||||
.chat-container {
|
.chat-container {
|
||||||
position: relative;
|
position: relative;
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-rows: 1fr auto;
|
grid-template-rows: 1fr auto;
|
||||||
/*height: calc(100vh - 200px);*/
|
|
||||||
height: 50vh;
|
height: 50vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
.chat-box {
|
.chat-box {
|
||||||
display: flex;
|
|
||||||
flex-direction: column-reverse;
|
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
width: 50%;
|
width: 100%;
|
||||||
}
|
|
||||||
|
|
||||||
.chat-messages {
|
|
||||||
width: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-input {
|
|
||||||
position: relative;
|
|
||||||
display: flex;
|
|
||||||
align-items: end;
|
|
||||||
margin-top: 1rem;
|
|
||||||
}
|
}
|
||||||
</style>
|
</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='components/direct-messages/direct-messages.js') }}"></script>
|
||||||
<script src="{{ url_for('nostrmarket_static', path='js/index.js') }}"></script>
|
<script src="{{ url_for('nostrmarket_static', path='js/index.js') }}"></script>
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -600,6 +600,7 @@ async def api_get_messages(
|
||||||
detail="Cannot get zone",
|
detail="Cannot get zone",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@nostrmarket_ext.post("/api/v1/message")
|
@nostrmarket_ext.post("/api/v1/message")
|
||||||
async def api_create_message(
|
async def api_create_message(
|
||||||
data: PartialDirectMessage, wallet: WalletTypeInfo = Depends(require_admin_key)
|
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)
|
dm_event = merchant.build_dm_event(data.message, data.public_key)
|
||||||
data.event_id = dm_event.id
|
data.event_id = dm_event.id
|
||||||
|
data.event_created_at = dm_event.created_at
|
||||||
|
|
||||||
dm = await create_direct_message(merchant.id, data)
|
dm = await create_direct_message(merchant.id, data)
|
||||||
await publish_nostr_event(dm_event)
|
await publish_nostr_event(dm_event)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue