Browse Source

fix confirmations

feature/dockerfile-arm
Evan Feenstra 5 years ago
parent
commit
2bc1e614a3
  1. 77
      api/controllers/confirmations.ts
  2. 3
      api/controllers/index.ts
  3. 14
      api/controllers/invoices.ts
  4. 13
      api/controllers/media.ts
  5. 76
      api/controllers/messages.ts
  6. 88
      dist/api/controllers/confirmations.js
  7. 1
      dist/api/controllers/confirmations.js.map
  8. 3
      dist/api/controllers/index.js
  9. 2
      dist/api/controllers/index.js.map
  10. 12
      dist/api/controllers/invoices.js
  11. 2
      dist/api/controllers/invoices.js.map
  12. 12
      dist/api/controllers/media.js
  13. 2
      dist/api/controllers/media.js.map
  14. 72
      dist/api/controllers/messages.js
  15. 2
      dist/api/controllers/messages.js.map

77
api/controllers/confirmations.ts

@ -0,0 +1,77 @@
import lock from '../utils/lock'
import {models} from '../models'
import * as socket from '../utils/socket'
import * as jsonUtils from '../utils/json'
import * as helpers from '../helpers'
const constants = require(__dirname + '/../../config/constants.json')
export function sendConfirmation({ chat, sender, msg_id }) {
helpers.sendMessage({
chat,
sender,
message: {id:msg_id},
type: constants.message_types.confirmation,
})
}
export async function receiveConfirmation(payload) {
console.log('received confirmation', { payload })
const dat = payload.content || payload
const chat_uuid = dat.chat.uuid
const msg_id = dat.message.id
const sender_pub_key = dat.sender.pub_key
const owner = await models.Contact.findOne({ where: { isOwner: true }})
const sender = await models.Contact.findOne({ where: { publicKey: sender_pub_key } })
const chat = await models.Chat.findOne({ where: { uuid: chat_uuid } })
// new confirmation logic
if(msg_id){
lock.acquire('confirmation', async function(done){
console.log("update status map")
const message = await models.Message.findOne({ where:{id:msg_id} })
if(message){
let statusMap = {}
try{
statusMap = JSON.parse(message.statusMap||'{}')
} catch(e){}
statusMap[sender.id] = constants.statuses.received
await message.update({
status: constants.statuses.received,
statusMap: JSON.stringify(statusMap)
})
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
})
}
done()
})
} else { // old logic
const messages = await models.Message.findAll({
limit: 1,
where: {
chatId: chat.id,
sender: owner.id,
type: [
constants.message_types.message,
constants.message_types.invoice,
constants.message_types.attachment,
],
status: constants.statuses.pending,
},
order: [['createdAt', 'desc']]
})
const message = messages[0]
message.update({ status: constants.statuses.received })
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
})
}
}

3
api/controllers/index.ts

@ -19,6 +19,7 @@ let controllers = {
chats: require('./chats'),
subcriptions: require('./subscriptions'),
media: require('./media'),
confirmations: require('./confirmations')
}
async function iniGrpcSubscriptions() {
@ -31,7 +32,7 @@ async function iniGrpcSubscriptions() {
[types.message]: controllers.messages.receiveMessage,
[types.invoice]: controllers.invoices.receiveInvoice,
[types.direct_payment]: controllers.payments.receivePayment,
[types.confirmation]: controllers.messages.receiveConfirmation,
[types.confirmation]: controllers.confirmations.receiveConfirmation,
[types.attachment]: controllers.media.receiveAttachment,
[types.purchase]: controllers.media.receivePurchase,
[types.purchase_accept]: controllers.media.receivePurchaseAccept,

14
api/controllers/invoices.ts

@ -6,6 +6,7 @@ import * as decodeUtils from '../utils/decode'
import * as helpers from '../helpers'
import { sendNotification } from '../hub'
import { success } from '../utils/res'
import {sendConfirmation} from './confirmations'
const constants = require(__dirname + '/../../config/constants.json');
@ -216,19 +217,10 @@ const receiveInvoice = async (payload) => {
sendNotification(chat, sender.alias, 'message')
sendConfirmation({ chat, sender: owner, msg_id })
const theChat = {...chat.dataValues, contactIds:[sender.id]}
sendConfirmation({ chat:theChat, sender: owner, msg_id })
}
const sendConfirmation = ({ chat, sender, msg_id }) => {
helpers.sendMessage({
chat,
sender,
message: { id: msg_id },
type: constants.message_types.confirmation,
})
}
export {
listInvoices,
payInvoice,

13
api/controllers/media.ts

@ -11,6 +11,7 @@ import {parseLDAT, tokenFromTerms, urlBase64FromBytes, testLDAT} from '../utils/
import {CronJob} from 'cron'
import * as zbase32 from '../utils/zbase32'
import * as schemas from './schemas'
import {sendConfirmation} from './confirmations'
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../../config/app.json')[env];
@ -382,16 +383,8 @@ const receiveAttachment = async (payload) => {
sendNotification(chat, sender.alias, 'message')
sendConfirmation({ chat, sender: owner, msg_id })
}
const sendConfirmation = ({ chat, sender, msg_id }) => {
helpers.sendMessage({
chat,
sender,
message: {id:msg_id},
type: constants.message_types.confirmation,
})
const theChat = {...chat.dataValues, contactIds:[sender.id]}
sendConfirmation({ chat:theChat, sender: owner, msg_id })
}
async function signer(req, res) {

76
api/controllers/messages.ts

@ -6,7 +6,7 @@ import * as socket from '../utils/socket'
import * as jsonUtils from '../utils/json'
import * as helpers from '../helpers'
import { success } from '../utils/res'
import lock from '../utils/lock'
import {sendConfirmation} from './confirmations'
const constants = require(__dirname + '/../../config/constants.json')
@ -171,77 +171,8 @@ const receiveMessage = async (payload) => {
sendNotification(chat, sender.alias, 'message')
sendConfirmation({ chat, sender: owner, msg_id })
}
const sendConfirmation = ({ chat, sender, msg_id }) => {
helpers.sendMessage({
chat,
sender,
message: {id:msg_id},
type: constants.message_types.confirmation,
})
}
const receiveConfirmation = async (payload) => {
console.log('received confirmation', { payload })
const dat = payload.content || payload
const chat_uuid = dat.chat.uuid
const msg_id = dat.message.id
const sender_pub_key = dat.sender.pub_key
const owner = await models.Contact.findOne({ where: { isOwner: true }})
const sender = await models.Contact.findOne({ where: { publicKey: sender_pub_key } })
const chat = await models.Chat.findOne({ where: { uuid: chat_uuid } })
// new confirmation logic
if(msg_id){
lock.acquire('confirmation', async function(done){
console.log("update status map")
const message = await models.Message.findOne({ where:{id:msg_id} })
if(message){
let statusMap = {}
try{
statusMap = JSON.parse(message.statusMap||'{}')
} catch(e){}
statusMap[sender.id] = constants.statuses.received
await message.update({
status: constants.statuses.received,
statusMap: JSON.stringify(statusMap)
})
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
})
}
done()
})
} else { // old logic
const messages = await models.Message.findAll({
limit: 1,
where: {
chatId: chat.id,
sender: owner.id,
type: [
constants.message_types.message,
constants.message_types.invoice,
constants.message_types.attachment,
],
status: constants.statuses.pending,
},
order: [['createdAt', 'desc']]
})
const message = messages[0]
message.update({ status: constants.statuses.received })
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
})
}
const theChat = {...chat.dataValues, contactIds:[sender.id]}
sendConfirmation({ chat:theChat, sender: owner, msg_id })
}
const readMessages = async (req, res) => {
@ -271,7 +202,6 @@ export {
getMessages,
sendMessage,
receiveMessage,
receiveConfirmation,
clearMessages,
readMessages
}

88
dist/api/controllers/confirmations.js

@ -0,0 +1,88 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const lock_1 = require("../utils/lock");
const models_1 = require("../models");
const socket = require("../utils/socket");
const jsonUtils = require("../utils/json");
const helpers = require("../helpers");
const constants = require(__dirname + '/../../config/constants.json');
function sendConfirmation({ chat, sender, msg_id }) {
helpers.sendMessage({
chat,
sender,
message: { id: msg_id },
type: constants.message_types.confirmation,
});
}
exports.sendConfirmation = sendConfirmation;
function receiveConfirmation(payload) {
return __awaiter(this, void 0, void 0, function* () {
console.log('received confirmation', { payload });
const dat = payload.content || payload;
const chat_uuid = dat.chat.uuid;
const msg_id = dat.message.id;
const sender_pub_key = dat.sender.pub_key;
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
const sender = yield models_1.models.Contact.findOne({ where: { publicKey: sender_pub_key } });
const chat = yield models_1.models.Chat.findOne({ where: { uuid: chat_uuid } });
// new confirmation logic
if (msg_id) {
lock_1.default.acquire('confirmation', function (done) {
return __awaiter(this, void 0, void 0, function* () {
console.log("update status map");
const message = yield models_1.models.Message.findOne({ where: { id: msg_id } });
if (message) {
let statusMap = {};
try {
statusMap = JSON.parse(message.statusMap || '{}');
}
catch (e) { }
statusMap[sender.id] = constants.statuses.received;
yield message.update({
status: constants.statuses.received,
statusMap: JSON.stringify(statusMap)
});
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
});
}
done();
});
});
}
else { // old logic
const messages = yield models_1.models.Message.findAll({
limit: 1,
where: {
chatId: chat.id,
sender: owner.id,
type: [
constants.message_types.message,
constants.message_types.invoice,
constants.message_types.attachment,
],
status: constants.statuses.pending,
},
order: [['createdAt', 'desc']]
});
const message = messages[0];
message.update({ status: constants.statuses.received });
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
});
}
});
}
exports.receiveConfirmation = receiveConfirmation;
//# sourceMappingURL=confirmations.js.map

1
dist/api/controllers/confirmations.js.map

@ -0,0 +1 @@
{"version":3,"file":"confirmations.js","sourceRoot":"","sources":["../../../api/controllers/confirmations.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,wCAAgC;AAChC,sCAAgC;AAChC,0CAAyC;AACzC,2CAA0C;AAC1C,sCAAqC;AAErC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,GAAG,8BAA8B,CAAC,CAAA;AAErE,SAAgB,gBAAgB,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE;IACxD,OAAO,CAAC,WAAW,CAAC;QACnB,IAAI;QACJ,MAAM;QACN,OAAO,EAAE,EAAC,EAAE,EAAC,MAAM,EAAC;QACpB,IAAI,EAAE,SAAS,CAAC,aAAa,CAAC,YAAY;KAC1C,CAAC,CAAA;AACH,CAAC;AAPD,4CAOC;AAED,SAAsB,mBAAmB,CAAC,OAAO;;QAChD,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAA;QAEjD,MAAM,GAAG,GAAG,OAAO,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E,SAAS,CAAC,QAAQ,CAAC,QAAQ;4BACnC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC;yBACpC,CAAC,CAAA;wBACF,MAAM,CAAC,QAAQ,CAAC;4BACf,IAAI,EAAE,cAAc;4BACpB,QAAQ,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC;yBAChD,CAAC,CAAA;qBACF;oBACD,IAAI,EAAE,CAAA;gBACP,CAAC;aAAA,CAAC,CAAA;SACF;aAAM,EAAE,YAAY;YACpB,MAAM,QAAQ,GAAG,MAAM,eAAM,CAAC,OAAO,CAAC,OAAO,CAAC;gBAC7C,KAAK,EAAE,CAAC;gBACR,KAAK,EAAE;oBACN,MAAM,EAAE,IAAI,CAAC,EAAE;oBACf,MAAM,EAAE,KAAK,CAAC,EAAE;oBAChB,IAAI,EAAE;wBACL,SAAS,CAAC,aAAa,CAAC,OAAO;wBAC/B,SAAS,CAAC,aAAa,CAAC,OAAO;wBAC/B,SAAS,CAAC,aAAa,CAAC,UAAU;qBAClC;oBACD,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC,OAAO;iBAClC;gBACD,KAAK,EAAE,CAAC,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;aAC9B,CAAC,CAAA;YAEF,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;YAC3B,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAA;YAEvD,MAAM,CAAC,QAAQ,CAAC;gBACf,IAAI,EAAE,cAAc;gBACpB,QAAQ,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC;aAChD,CAAC,CAAA;SACF;IACF,CAAC;CAAA;AA3DD,kDA2DC"}

3
dist/api/controllers/index.js

@ -27,6 +27,7 @@ let controllers = {
chats: require('./chats'),
subcriptions: require('./subscriptions'),
media: require('./media'),
confirmations: require('./confirmations')
};
function iniGrpcSubscriptions() {
return __awaiter(this, void 0, void 0, function* () {
@ -39,7 +40,7 @@ function iniGrpcSubscriptions() {
[types.message]: controllers.messages.receiveMessage,
[types.invoice]: controllers.invoices.receiveInvoice,
[types.direct_payment]: controllers.payments.receivePayment,
[types.confirmation]: controllers.messages.receiveConfirmation,
[types.confirmation]: controllers.confirmations.receiveConfirmation,
[types.attachment]: controllers.media.receiveAttachment,
[types.purchase]: controllers.media.receivePurchase,
[types.purchase_accept]: controllers.media.receivePurchaseAccept,

2
dist/api/controllers/index.js.map

File diff suppressed because one or more lines are too long

12
dist/api/controllers/invoices.js

@ -17,6 +17,7 @@ const decodeUtils = require("../utils/decode");
const helpers = require("../helpers");
const hub_1 = require("../hub");
const res_1 = require("../utils/res");
const confirmations_1 = require("./confirmations");
const constants = require(__dirname + '/../../config/constants.json');
const payInvoice = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const lightning = yield lightning_1.loadLightning();
@ -196,17 +197,10 @@ const receiveInvoice = (payload) => __awaiter(void 0, void 0, void 0, function*
response: jsonUtils.messageToJson(message, chat)
});
hub_1.sendNotification(chat, sender.alias, 'message');
sendConfirmation({ chat, sender: owner, msg_id });
const theChat = Object.assign(Object.assign({}, chat.dataValues), { contactIds: [sender.id] });
confirmations_1.sendConfirmation({ chat: theChat, sender: owner, msg_id });
});
exports.receiveInvoice = receiveInvoice;
const sendConfirmation = ({ chat, sender, msg_id }) => {
helpers.sendMessage({
chat,
sender,
message: { id: msg_id },
type: constants.message_types.confirmation,
});
};
// lnd invoice stuff
function decodePaymentRequest(paymentRequest) {
var decodedPaymentRequest = decodeUtils.decode(paymentRequest);

2
dist/api/controllers/invoices.js.map

File diff suppressed because one or more lines are too long

12
dist/api/controllers/media.js

@ -22,6 +22,7 @@ const ldat_1 = require("../utils/ldat");
const cron_1 = require("cron");
const zbase32 = require("../utils/zbase32");
const schemas = require("./schemas");
const confirmations_1 = require("./confirmations");
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../../config/app.json')[env];
const constants = require(__dirname + '/../../config/constants.json');
@ -347,17 +348,10 @@ const receiveAttachment = (payload) => __awaiter(void 0, void 0, void 0, functio
response: jsonUtils.messageToJson(message, chat)
});
hub_1.sendNotification(chat, sender.alias, 'message');
sendConfirmation({ chat, sender: owner, msg_id });
const theChat = Object.assign(Object.assign({}, chat.dataValues), { contactIds: [sender.id] });
confirmations_1.sendConfirmation({ chat: theChat, sender: owner, msg_id });
});
exports.receiveAttachment = receiveAttachment;
const sendConfirmation = ({ chat, sender, msg_id }) => {
helpers.sendMessage({
chat,
sender,
message: { id: msg_id },
type: constants.message_types.confirmation,
});
};
function signer(req, res) {
return __awaiter(this, void 0, void 0, function* () {
if (!req.params.challenge)

2
dist/api/controllers/media.js.map

File diff suppressed because one or more lines are too long

72
dist/api/controllers/messages.js

@ -17,7 +17,7 @@ const socket = require("../utils/socket");
const jsonUtils = require("../utils/json");
const helpers = require("../helpers");
const res_1 = require("../utils/res");
const lock_1 = require("../utils/lock");
const confirmations_1 = require("./confirmations");
const constants = require(__dirname + '/../../config/constants.json');
const getMessages = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const dateToReturn = req.query.date;
@ -147,76 +147,10 @@ const receiveMessage = (payload) => __awaiter(void 0, void 0, void 0, function*
response: jsonUtils.messageToJson(message, chat)
});
hub_1.sendNotification(chat, sender.alias, 'message');
sendConfirmation({ chat, sender: owner, msg_id });
const theChat = Object.assign(Object.assign({}, chat.dataValues), { contactIds: [sender.id] });
confirmations_1.sendConfirmation({ chat: theChat, sender: owner, msg_id });
});
exports.receiveMessage = receiveMessage;
const sendConfirmation = ({ chat, sender, msg_id }) => {
helpers.sendMessage({
chat,
sender,
message: { id: msg_id },
type: constants.message_types.confirmation,
});
};
const receiveConfirmation = (payload) => __awaiter(void 0, void 0, void 0, function* () {
console.log('received confirmation', { payload });
const dat = payload.content || payload;
const chat_uuid = dat.chat.uuid;
const msg_id = dat.message.id;
const sender_pub_key = dat.sender.pub_key;
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
const sender = yield models_1.models.Contact.findOne({ where: { publicKey: sender_pub_key } });
const chat = yield models_1.models.Chat.findOne({ where: { uuid: chat_uuid } });
// new confirmation logic
if (msg_id) {
lock_1.default.acquire('confirmation', function (done) {
return __awaiter(this, void 0, void 0, function* () {
console.log("update status map");
const message = yield models_1.models.Message.findOne({ where: { id: msg_id } });
if (message) {
let statusMap = {};
try {
statusMap = JSON.parse(message.statusMap || '{}');
}
catch (e) { }
statusMap[sender.id] = constants.statuses.received;
yield message.update({
status: constants.statuses.received,
statusMap: JSON.stringify(statusMap)
});
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
});
}
done();
});
});
}
else { // old logic
const messages = yield models_1.models.Message.findAll({
limit: 1,
where: {
chatId: chat.id,
sender: owner.id,
type: [
constants.message_types.message,
constants.message_types.invoice,
constants.message_types.attachment,
],
status: constants.statuses.pending,
},
order: [['createdAt', 'desc']]
});
const message = messages[0];
message.update({ status: constants.statuses.received });
socket.sendJson({
type: 'confirmation',
response: jsonUtils.messageToJson(message, chat)
});
}
});
exports.receiveConfirmation = receiveConfirmation;
const readMessages = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const chat_id = req.params.chat_id;
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });

2
dist/api/controllers/messages.js.map

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save