Evan Feenstra
5 years ago
committed by
GitHub
15 changed files with 192 additions and 187 deletions
@ -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) |
|||
}) |
|||
} |
|||
} |
@ -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
|
@ -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,O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AAC,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"} |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue