retrieve chat messages from db
This commit is contained in:
parent
57bb9665c6
commit
db2ffadb43
3 changed files with 65 additions and 6 deletions
|
|
@ -9,6 +9,7 @@ from lnbits.settings import WALLET
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from .models import (
|
from .models import (
|
||||||
|
ChatMessage,
|
||||||
CreateChatMessage,
|
CreateChatMessage,
|
||||||
CreateMarket,
|
CreateMarket,
|
||||||
CreateMarketStalls,
|
CreateMarketStalls,
|
||||||
|
|
@ -420,3 +421,18 @@ async def create_chat_message(data: CreateChatMessage):
|
||||||
data.room_name,
|
data.room_name,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def get_diagonalley_latest_chat_messages(room_name: str):
|
||||||
|
rows = await db.fetchall(
|
||||||
|
"SELECT * FROM diagonalley.messages WHERE id_conversation = ? ORDER BY timestamp DESC LIMIT 20", (room_name,)
|
||||||
|
)
|
||||||
|
|
||||||
|
return [ChatMessage(**row) for row in rows]
|
||||||
|
|
||||||
|
async def get_diagonalley_chat_messages(room_name: str):
|
||||||
|
rows = await db.fetchall(
|
||||||
|
"SELECT * FROM diagonalley.messages WHERE id_conversation = ? ORDER BY timestamp DESC", (room_name,)
|
||||||
|
)
|
||||||
|
|
||||||
|
return [ChatMessage(**row) for row in rows]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -125,8 +125,13 @@
|
||||||
</div>
|
</div>
|
||||||
{% endblock %} {% block scripts %}
|
{% endblock %} {% block scripts %}
|
||||||
<script>
|
<script>
|
||||||
const mapChatMsg = obj => {
|
const mapChatMsg = msg => {
|
||||||
return
|
let obj = {}
|
||||||
|
obj.timestamp = {
|
||||||
|
msg: msg,
|
||||||
|
pubkey: pubkey
|
||||||
|
}
|
||||||
|
return obj
|
||||||
}
|
}
|
||||||
Vue.component(VueQrcode.name, VueQrcode)
|
Vue.component(VueQrcode.name, VueQrcode)
|
||||||
new Vue({
|
new Vue({
|
||||||
|
|
@ -186,6 +191,29 @@
|
||||||
LNbits.utils.notifyApiError(error)
|
LNbits.utils.notifyApiError(error)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
async getMessages(room_name, all = false) {
|
||||||
|
await LNbits.api
|
||||||
|
.request(
|
||||||
|
'GET',
|
||||||
|
`/diagonalley/api/v1/chat/messages/${room_name}${
|
||||||
|
all ? '?all_messages=true' : ''
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
.then(response => {
|
||||||
|
if (response.data) {
|
||||||
|
console.log(response.data)
|
||||||
|
response.data.reverse().map(m => {
|
||||||
|
this.$set(this.messages, m.timestamp * 1000, {
|
||||||
|
msg: m.msg,
|
||||||
|
pubkey: m.pubkey
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
LNbits.utils.notifyApiError(error)
|
||||||
|
})
|
||||||
|
},
|
||||||
changeOrder() {
|
changeOrder() {
|
||||||
console.log(this.selectedOrder)
|
console.log(this.selectedOrder)
|
||||||
},
|
},
|
||||||
|
|
@ -212,7 +240,6 @@
|
||||||
let event_data = JSON.parse(event.data)
|
let event_data = JSON.parse(event.data)
|
||||||
|
|
||||||
this.$set(this.messages, Date.now(), event_data)
|
this.$set(this.messages, Date.now(), event_data)
|
||||||
console.log(this.messages)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws = ws
|
this.ws = ws
|
||||||
|
|
@ -230,8 +257,8 @@
|
||||||
if (data) {
|
if (data) {
|
||||||
this.user = data
|
this.user = data
|
||||||
//add chat key (merchant pubkey) if not set
|
//add chat key (merchant pubkey) if not set
|
||||||
if (!this.user.chats[`${this.stall.publickey}`]) {
|
if (!this.user.chats[`${order_id}`]) {
|
||||||
this.$set(this.user.chats, this.stall.publickey, [])
|
this.$set(this.user.chats, order_id, [])
|
||||||
}
|
}
|
||||||
//this.$q.localStorage.set(`lnbits.diagonalley.data`, this.user)
|
//this.$q.localStorage.set(`lnbits.diagonalley.data`, this.user)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -239,7 +266,7 @@
|
||||||
await this.generateKeys()
|
await this.generateKeys()
|
||||||
// populate user data
|
// populate user data
|
||||||
this.user.chats = {
|
this.user.chats = {
|
||||||
[`${this.stall.publickey}`]: []
|
[`${order_id}`]: []
|
||||||
}
|
}
|
||||||
this.user.orders = []
|
this.user.orders = []
|
||||||
}
|
}
|
||||||
|
|
@ -248,6 +275,8 @@
|
||||||
this.user.orders = [...new Set([...this.user.orders, order_id])]
|
this.user.orders = [...new Set([...this.user.orders, order_id])]
|
||||||
this.selectedOrder = order_id
|
this.selectedOrder = order_id
|
||||||
|
|
||||||
|
await this.getMessages(order_id)
|
||||||
|
|
||||||
this.$q.localStorage.set(`lnbits.diagonalley.data`, this.user)
|
this.$q.localStorage.set(`lnbits.diagonalley.data`, this.user)
|
||||||
this.startChat(order_id)
|
this.startChat(order_id)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,8 @@ from .crud import (
|
||||||
delete_diagonalley_product,
|
delete_diagonalley_product,
|
||||||
delete_diagonalley_stall,
|
delete_diagonalley_stall,
|
||||||
delete_diagonalley_zone,
|
delete_diagonalley_zone,
|
||||||
|
get_diagonalley_chat_messages,
|
||||||
|
get_diagonalley_latest_chat_messages,
|
||||||
get_diagonalley_market,
|
get_diagonalley_market,
|
||||||
get_diagonalley_market_stalls,
|
get_diagonalley_market_stalls,
|
||||||
get_diagonalley_markets,
|
get_diagonalley_markets,
|
||||||
|
|
@ -481,3 +483,15 @@ async def api_diagonalley_generate_keys():
|
||||||
private_key = PrivateKey()
|
private_key = PrivateKey()
|
||||||
public_key = private_key.pubkey.serialize().hex()
|
public_key = private_key.pubkey.serialize().hex()
|
||||||
return {"privkey": private_key.serialize(), "pubkey": public_key[2:]}
|
return {"privkey": private_key.serialize(), "pubkey": public_key[2:]}
|
||||||
|
|
||||||
|
|
||||||
|
## MESSAGES/CHAT
|
||||||
|
|
||||||
|
@diagonalley_ext.get("/api/v1/chat/messages/{room_name}")
|
||||||
|
async def api_get_latest_chat_msg(room_name: str, all_messages: bool = Query(False)):
|
||||||
|
if(all_messages):
|
||||||
|
messages = await get_diagonalley_chat_messages(room_name)
|
||||||
|
else:
|
||||||
|
messages = await get_diagonalley_latest_chat_messages(room_name)
|
||||||
|
|
||||||
|
return messages
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue