Merge pull request #36 from lnbits/filter_chat_messages
Filter JSON from chat
This commit is contained in:
commit
05a0519dfe
2 changed files with 78 additions and 40 deletions
|
|
@ -41,10 +41,16 @@
|
||||||
:name="message.sender"
|
:name="message.sender"
|
||||||
:text="[message.msg]"
|
:text="[message.msg]"
|
||||||
:sent="message.sender == 'Me'"
|
:sent="message.sender == 'Me'"
|
||||||
:bg-color="message.sender == 'Me' ? 'white' : 'light-green-2'"
|
:bg-color="message.json ? 'purple-2' : message.sender == 'Me' ? 'white' : 'light-green-2'"
|
||||||
:stamp="message.timestamp"
|
:stamp="message.timestamp"
|
||||||
size="6"
|
size="6"
|
||||||
/>
|
><template v-slot:avatar v-if="message.json">
|
||||||
|
<q-icon
|
||||||
|
color="secondary"
|
||||||
|
name="smart_toy"
|
||||||
|
class="q-message-avatar q-message-avatar--received"
|
||||||
|
/> </template
|
||||||
|
></q-chat-message>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
<q-card-actions>
|
<q-card-actions>
|
||||||
<q-form @submit="sendMessage" class="full-width chat-input">
|
<q-form @submit="sendMessage" class="full-width chat-input">
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ async function chatDialog(path) {
|
||||||
dialog: false,
|
dialog: false,
|
||||||
isChat: true,
|
isChat: true,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
messagesMap: new Map(),
|
||||||
nostrMessages: [],
|
nostrMessages: [],
|
||||||
newMessage: '',
|
newMessage: '',
|
||||||
ordersTable: {
|
ordersTable: {
|
||||||
|
|
@ -63,14 +64,15 @@ async function chatDialog(path) {
|
||||||
},
|
},
|
||||||
ordersList() {
|
ordersList() {
|
||||||
let orders = this.nostrMessages
|
let orders = this.nostrMessages
|
||||||
|
.filter(o => o.json)
|
||||||
.sort((a, b) => b.created_at - a.created_at)
|
.sort((a, b) => b.created_at - a.created_at)
|
||||||
.filter(o => isJson(o.msg))
|
|
||||||
.reduce((acc, cur) => {
|
.reduce((acc, cur) => {
|
||||||
const obj = JSON.parse(cur.msg)
|
const obj = cur.json
|
||||||
const key = obj.id
|
const key = obj.id
|
||||||
const curGroup = acc[key] ?? {created_at: cur.timestamp}
|
const curGroup = acc[key] ?? {created_at: cur.timestamp}
|
||||||
return {...acc, [key]: {...curGroup, ...obj}}
|
return {...acc, [key]: {...curGroup, ...obj}}
|
||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
return Object.values(orders)
|
return Object.values(orders)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -85,8 +87,8 @@ async function chatDialog(path) {
|
||||||
},
|
},
|
||||||
async startPool() {
|
async startPool() {
|
||||||
this.loading = true
|
this.loading = true
|
||||||
let messagesMap = new Map()
|
let relays = Array.from(this.relays)
|
||||||
let sub = this.pool.sub(Array.from(this.relays), [
|
let filters = [
|
||||||
{
|
{
|
||||||
kinds: [4],
|
kinds: [4],
|
||||||
authors: [this.account.pubkey]
|
authors: [this.account.pubkey]
|
||||||
|
|
@ -95,45 +97,75 @@ async function chatDialog(path) {
|
||||||
kinds: [4],
|
kinds: [4],
|
||||||
'#p': [this.account.pubkey]
|
'#p': [this.account.pubkey]
|
||||||
}
|
}
|
||||||
])
|
]
|
||||||
|
let events = await this.pool.list(relays, filters)
|
||||||
|
|
||||||
sub.on('eose', () => {
|
for (const event of events) {
|
||||||
this.loading = false
|
await this.processMessage(event)
|
||||||
this.nostrMessages = Array.from(messagesMap.values())
|
}
|
||||||
})
|
|
||||||
|
this.nostrMessages = Array.from(this.messagesMap.values())
|
||||||
|
this.loading = false
|
||||||
|
|
||||||
|
let sub = this.pool.sub(
|
||||||
|
relays,
|
||||||
|
filters.map(f => ({...f, since: Date.now() / 1000}))
|
||||||
|
)
|
||||||
sub.on('event', async event => {
|
sub.on('event', async event => {
|
||||||
let mine = event.pubkey == this.account.pubkey
|
await this.processMessage(event)
|
||||||
let sender = mine ? this.merchant : event.pubkey
|
this.nostrMessages = Array.from(this.messagesMap.values())
|
||||||
|
|
||||||
try {
|
|
||||||
let plaintext
|
|
||||||
if (this.account.privkey) {
|
|
||||||
plaintext = await NostrTools.nip04.decrypt(
|
|
||||||
this.account.privkey,
|
|
||||||
sender,
|
|
||||||
event.content
|
|
||||||
)
|
|
||||||
} else if (this.account.useExtension && this.hasNip07) {
|
|
||||||
plaintext = await window.nostr.nip04.decrypt(
|
|
||||||
sender,
|
|
||||||
event.content
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (plaintext) {
|
|
||||||
messagesMap.set(event.id, {
|
|
||||||
created_at: event.created_at,
|
|
||||||
msg: plaintext,
|
|
||||||
timestamp: timeFromNow(event.created_at * 1000),
|
|
||||||
sender: `${mine ? 'Me' : 'Merchant'}`
|
|
||||||
})
|
|
||||||
this.nostrMessages = Array.from(messagesMap.values())
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
console.debug('Unable to decrypt message! Not for us...')
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
this.sub = sub
|
this.sub = sub
|
||||||
},
|
},
|
||||||
|
async processMessage(event) {
|
||||||
|
let mine = event.pubkey == this.account.pubkey
|
||||||
|
let sender = mine ? this.merchant : event.pubkey
|
||||||
|
|
||||||
|
try {
|
||||||
|
let plaintext
|
||||||
|
if (this.account.privkey) {
|
||||||
|
plaintext = await NostrTools.nip04.decrypt(
|
||||||
|
this.account.privkey,
|
||||||
|
sender,
|
||||||
|
event.content
|
||||||
|
)
|
||||||
|
} else if (this.account.useExtension && this.hasNip07) {
|
||||||
|
plaintext = await window.nostr.nip04.decrypt(sender, event.content)
|
||||||
|
}
|
||||||
|
if (plaintext) {
|
||||||
|
let {text, json} = this.filterJsonMsg(plaintext)
|
||||||
|
this.messagesMap.set(event.id, {
|
||||||
|
created_at: event.created_at,
|
||||||
|
msg: text,
|
||||||
|
timestamp: timeFromNow(event.created_at * 1000),
|
||||||
|
sender: `${mine ? 'Me' : 'Merchant'}`,
|
||||||
|
json
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
} catch {
|
||||||
|
console.debug('Unable to decrypt message! Not for us...')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
filterJsonMsg(text) {
|
||||||
|
let json = null
|
||||||
|
|
||||||
|
if (!isJson(text)) return {text, json}
|
||||||
|
|
||||||
|
json = JSON.parse(text)
|
||||||
|
if (json.message) {
|
||||||
|
text = json.message
|
||||||
|
} else if (json.items) {
|
||||||
|
text = `Order placed!<br />OrderID: ${json.id}`
|
||||||
|
} else if (json.payment_options) {
|
||||||
|
text = `Invoice for order: ${json.id} <br /><a href="lightning:${
|
||||||
|
json.payment_options.find(o => o.type == 'ln')?.link
|
||||||
|
}" target="_blank" class="text-secondary">LN ⚡︎</a>`
|
||||||
|
}
|
||||||
|
|
||||||
|
return {text, json}
|
||||||
|
},
|
||||||
async sendMessage() {
|
async sendMessage() {
|
||||||
if (this.newMessage && this.newMessage.length < 1) return
|
if (this.newMessage && this.newMessage.length < 1) return
|
||||||
let event = {
|
let event = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue