You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.2 KiB
91 lines
2.2 KiB
5 years ago
|
import { models } from '../models'
|
||
|
import * as LND from '../utils/lightning'
|
||
|
import {personalizeMessage} from '../utils/msg'
|
||
5 years ago
|
|
||
|
// const constants = require('../config/constants.json');
|
||
|
|
||
|
export function signAndSend(opts){
|
||
|
return new Promise(async function(resolve, reject) {
|
||
|
if(!opts.data || typeof opts.data!=='object') {
|
||
|
return reject('object plz')
|
||
|
}
|
||
|
let data = JSON.stringify(opts.data)
|
||
|
// SIGN HERE and append sig
|
||
|
const sig = await LND.signAscii(data)
|
||
|
data = data + sig
|
||
|
|
||
5 years ago
|
console.log("DATA")
|
||
|
console.log(opts.data)
|
||
5 years ago
|
// if tribe
|
||
|
// if owner pub to mqtt
|
||
|
// else keysend to owner ONLY
|
||
|
// else:
|
||
|
LND.keysendMessage({...opts,data})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
export async function sendMessage(params) {
|
||
|
const { type, chat, message, sender, amount, success, failure } = params
|
||
|
const m = newmsg(type, chat, sender, message)
|
||
|
|
||
|
const contactIds = typeof chat.contactIds==='string' ? JSON.parse(chat.contactIds) : chat.contactIds
|
||
|
|
||
|
let yes:any = null
|
||
|
let no:any = null
|
||
|
console.log('all contactIds',contactIds)
|
||
|
await asyncForEach(contactIds, async contactId => {
|
||
|
if (contactId == sender.id) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
console.log('-> sending to contact #', contactId)
|
||
|
|
||
|
const contact = await models.Contact.findOne({ where: { id: contactId } })
|
||
|
const destkey = contact.publicKey
|
||
|
|
||
|
const finalMsg = await personalizeMessage(m, contactId, destkey)
|
||
|
|
||
|
const opts = {
|
||
|
dest: destkey,
|
||
|
data: finalMsg,
|
||
|
amt: Math.max(amount, 3)
|
||
|
}
|
||
|
try {
|
||
|
const r = await signAndSend(opts)
|
||
|
yes = r
|
||
|
} catch (e) {
|
||
|
console.log("KEYSEND ERROR", e)
|
||
|
no = e
|
||
|
}
|
||
|
})
|
||
|
if(yes){
|
||
|
if(success) success(yes)
|
||
|
} else {
|
||
|
if(failure) failure(no)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function newmsg(type, chat, sender, message){
|
||
|
return {
|
||
|
type: type,
|
||
|
chat: {
|
||
|
uuid: chat.uuid,
|
||
|
...chat.name && { name: chat.name },
|
||
5 years ago
|
...(chat.type||chat.type===0) && { type: chat.type },
|
||
5 years ago
|
...chat.members && { members: chat.members },
|
||
|
...chat.groupKey && { groupKey: chat.groupKey },
|
||
|
...chat.host && { host: chat.host }
|
||
|
},
|
||
|
message: message,
|
||
|
// sender: {
|
||
|
// pub_key: sender.publicKey,
|
||
|
// // ...sender.contactKey && {contact_key: sender.contactKey}
|
||
|
// }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function asyncForEach(array, callback) {
|
||
|
for (let index = 0; index < array.length; index++) {
|
||
|
await callback(array[index], index, array);
|
||
|
}
|
||
|
}
|