Skip to content

Commit

Permalink
Merge pull request #33 from Adamant-im/dev
Browse files Browse the repository at this point in the history
Pushing release 0.5.1
  • Loading branch information
zyuhel authored May 15, 2019
2 parents 1de8ca7 + 18b549c commit c944ad8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 64 deletions.
8 changes: 7 additions & 1 deletion helpers/transactionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ module.exports = {
IN_TRANSFER: 6,
OUT_TRANSFER: 7,
CHAT_MESSAGE: 8,
STATE: 9
STATE: 9,
CHAT_MESSAGE_TYPES: {
LEGACY_MESSAGE: 0,
ORDINARY_MESSAGE: 1,
RICH_TEXT_MESSAGE: 2,
SIGNAL_MESSAGE: 3
}
};
57 changes: 30 additions & 27 deletions modules/chatrooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ __private.listChats = function (filter, cb) {
where.push('"c_type" = ${type}');
params.type = filter.type;
}
if (filter.withPayments) {
where.push(`("t_type" = ${transactionTypes.CHAT_MESSAGE} OR "t_type" = ${transactionTypes.SEND})`);
if (filter.withoutDirectTransfers) {
where.push('"t_type" = ' + transactionTypes.CHAT_MESSAGE);
} else {
where.push('"t_type" = '+ transactionTypes.CHAT_MESSAGE);
where.push(`("t_type" = ${transactionTypes.CHAT_MESSAGE} OR "t_type" = ${transactionTypes.SEND})`);
}

where.push(`(NOT("c_type" = ${transactionTypes.CHAT_MESSAGE_TYPES.SIGNAL_MESSAGE}) OR c_type IS NULL) `);
if (filter.senderId) {
where.push('"t_senderId" = ${name}');
params.name = filter.senderId;
Expand All @@ -90,7 +90,7 @@ __private.listChats = function (filter, cb) {
}

if (!filter.limit) {
params.limit = 100;
params.limit = 25;
} else {
params.limit = Math.abs(filter.limit);
}
Expand All @@ -106,9 +106,7 @@ __private.listChats = function (filter, cb) {
}

const orderBy = OrderBy(
filter.orderBy, {
sortFields: sql.sortFields
}
filter.orderBy, sql.chatroomsSortDefaults
);

if (orderBy.error) {
Expand All @@ -127,19 +125,20 @@ __private.listChats = function (filter, cb) {
}), params).then(function (rows) {
let transactions = [], chats = {};
for (let i = 0; i < rows.length; i++) {
const trs = library.logic.transaction.dbRead(rows[i]);
let trs = {};
trs.lastTransaction = library.logic.transaction.dbRead(rows[i]);
trs.participants = [
{address: trs.senderId, publicKey: trs.senderPublicKey},
{address: trs.recipientId, publicKey: trs.recipientPublicKey}
{address: trs.lastTransaction.senderId, publicKey: trs.lastTransaction.senderPublicKey},
{address: trs.lastTransaction.recipientId, publicKey: trs.lastTransaction.recipientPublicKey}
];
const uid = trs.senderId !== filter.userId ? trs.senderId : trs.recipientId;
const uid = trs.lastTransaction.senderId !== filter.userId ? trs.lastTransaction.senderId : trs.lastTransaction.recipientId;
if (!chats[uid]) {
chats[uid] = [];
}
chats[uid].push(trs);
}
for (const uid in chats) {
transactions.push(chats[uid].sort((x, y) => x.timestamp - y.timestamp)[0]);
transactions.push(chats[uid].sort((x, y) => x.lastTransaction.timestamp - y.lastTransaction.timestamp)[0]);
}
const data = {
chats: transactions,
Expand All @@ -162,11 +161,13 @@ __private.listMessages = function (filter, cb) {
if (filter.type >= 0) {
where.push('"c_type" = ${type}');
params.type = filter.type;
} else {
where.push(`(NOT("c_type" = ${transactionTypes.CHAT_MESSAGE_TYPES.SIGNAL_MESSAGE}) OR c_type IS NULL)`);
}
if (filter.withPayments) {
where.push(`("t_type" = ${transactionTypes.CHAT_MESSAGE} OR "t_type" = ${transactionTypes.SEND})`);
if (filter.withoutDirectTransfers) {
where.push('"t_type" = ' + transactionTypes.CHAT_MESSAGE);
} else {
where.push('"t_type" = '+ transactionTypes.CHAT_MESSAGE);
where.push(`("t_type" = ${transactionTypes.CHAT_MESSAGE} OR "t_type" = ${transactionTypes.SEND})`);
}

if (filter.senderId) {
Expand Down Expand Up @@ -203,9 +204,7 @@ __private.listMessages = function (filter, cb) {
}

const orderBy = OrderBy(
filter.orderBy, {
sortFields: sql.sortFields
}
filter.orderBy, sql.chatroomsSortDefaults
);

if (orderBy.error) {
Expand All @@ -230,8 +229,8 @@ __private.listMessages = function (filter, cb) {
const data = {
messages: transactions,
participants: transactions.length ? [
{address: transactions[0].senderId,publicKey: transactions[0].senderPublicKey},
{address: transactions[0].recipientId,publicKey: transactions[0].recipientPublicKey}
{address: transactions[0].senderId, publicKey: transactions[0].senderPublicKey},
{address: transactions[0].recipientId, publicKey: transactions[0].recipientPublicKey}
] : [],
count: count
};
Expand Down Expand Up @@ -270,8 +269,10 @@ Chatrooms.prototype.isLoaded = function () {
Chatrooms.prototype.internal = {
getChats: function (req, cb) {
let validRequest;
[validRequest, req.body.userId,req.body.companionId] = req.path.match(/(U[0-9]+)\/?(U[0-9]+)?/);
if (!validRequest) { return setImmediate(cb, 'Invalid Request path'); }
[validRequest, req.body.userId, req.body.companionId] = req.path.match(/(U[0-9]+)\/?(U[0-9]+)?/);
if (!validRequest) {
return setImmediate(cb, 'Invalid Request path');
}
async.waterfall([
function (waterCb) {
const params = req.body;
Expand All @@ -290,7 +291,7 @@ Chatrooms.prototype.internal = {
return setImmediate(waterCb, 'Failed to get transactions: ' + err);
} else {
return setImmediate(waterCb, null, {
chats: _.uniqBy(data.chats, (x) => x.id),
chats: _.uniqBy(data.chats, (x) => x.lastTransaction.id),
count: data.count
});
}
Expand All @@ -302,8 +303,10 @@ Chatrooms.prototype.internal = {
},
getMessages: function (req, cb) {
let validRequest;
[validRequest, req.body.userId,req.body.companionId] = req.path.match(/(U[0-9]+)\/?(U[0-9]+)?/);
if (!validRequest) { return setImmediate(cb, 'Invalid Request path'); }
[validRequest, req.body.userId, req.body.companionId] = req.path.match(/(U[0-9]+)\/?(U[0-9]+)?/);
if (!validRequest) {
return setImmediate(cb, 'Invalid Request path');
}
async.waterfall([
function (waterCb) {
const params = req.body;
Expand Down Expand Up @@ -336,4 +339,4 @@ Chatrooms.prototype.internal = {

Chatrooms.prototype.shared = {};

module.exports = Chatrooms;
module.exports = Chatrooms;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adamant",
"version": "0.5.0",
"version": "0.5.1",
"private": true,
"scripts": {
"start": "node app.js",
Expand Down
39 changes: 19 additions & 20 deletions sql/chats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

var ChatsSql = {
sortFields: ['type','timestamp'],

chatroomsSortDefaults: {
sortField: 'timestamp',
sortMethod: 'desc'
},
countByTransactionId: 'SELECT COUNT(*)::int AS "count" FROM chats WHERE "transactionId" = ${id}',


Expand Down Expand Up @@ -89,19 +92,21 @@ var ChatsSql = {
let y = [
'SELECT',
'CONCAT(LEAST("t_senderId", "t_recipientId"), GREATEST("t_senderId", "t_recipientId")) as "srt",',
'first("t_id") as "t_id",',
'first("t_senderPublicKey") as "t_senderPublicKey",',
'first("m_recipientPublicKey") as "m_recipientPublicKey",',
'first("t_senderId") as "t_senderId",',
'first("t_recipientId") as "t_recipientId",',
'first("t_timestamp") as "t_timestamp",',
'first("t_timestamp") as "timestamp",',
'first("t_amount") as "t_amount",',
'first("t_fee") as "t_fee",',
'first("c_message") as "c_message",',
'first("c_own_message") as "c_own_message",',
'first("c_type") as "c_type",',
'first("t_type") as "t_type"',
'first("t_id" ORDER BY b_height DESC, t_timestamp DESC) as "t_id",',
'first("t_senderPublicKey" ORDER BY b_height DESC, t_timestamp DESC) as "t_senderPublicKey",',
'first("m_recipientPublicKey" ORDER BY b_height DESC, t_timestamp DESC) as "m_recipientPublicKey",',
'first("t_senderId" ORDER BY b_height DESC, t_timestamp DESC) as "t_senderId",',
'first("t_recipientId" ORDER BY b_height DESC, t_timestamp DESC) as "t_recipientId",',
'first("t_timestamp" ORDER BY b_height DESC, t_timestamp DESC) as "t_timestamp",',
'first("t_timestamp" ORDER BY b_height DESC, t_timestamp DESC) as "timestamp",',
'first("t_amount" ORDER BY b_height DESC, t_timestamp DESC) as "t_amount",',
'first("t_fee" ORDER BY b_height DESC, t_timestamp DESC) as "t_fee",',
'first("c_message" ORDER BY b_height DESC, t_timestamp DESC) as "c_message",',
'first("c_own_message" ORDER BY b_height DESC, t_timestamp DESC) as "c_own_message",',
'first("c_type" ORDER BY b_height DESC, t_timestamp DESC) as "c_type",',
'first("t_type" ORDER BY b_height DESC, t_timestamp DESC) as "t_type",',
'first("b_height" ORDER BY b_height DESC, t_timestamp DESC) as "b_height",',
'first("b_id" ORDER BY b_height DESC, t_timestamp DESC) as "b_id"',
'FROM ( SELECT *, t_timestamp as timestamp, ENCODE("publicKey", \'hex\') as "m_recipientPublicKey"',
'FROM full_blocks_list',
'LEFT OUTER JOIN mem_accounts ON address = "t_recipientId"',
Expand All @@ -111,12 +116,6 @@ var ChatsSql = {
') as foo GROUP by srt',
(params.sortField ? 'ORDER BY ' + [params.sortField, params.sortMethod].join(' ') : ''),
'LIMIT ${limit} OFFSET ${offset}'
//
//
// 'WHERE "t_type" = 8',
// 'AND ("t_senderId" = \'U1283640763437948723\'',
// 'OR "t_recipientId" = \'U1020291227689695733\')',
// 'ORDER BY "t_timestamp" DESC) as foo GROUP by srt'
].filter(Boolean).join(' ');
return y;
},
Expand Down
33 changes: 18 additions & 15 deletions test/api/chatrooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ describe('GET /api/chatrooms/:ID/:ID', function () {
node.expect(res.body.chats[i].participants[0].address).to.equal(sender.address);
node.expect(res.body.chats[i].participants[0].publicKey).to.equal(sender.publicKey.toString('hex'));
node.expect(res.body.chats[i].participants[1].publicKey).to.not.equal(null);
node.expect(res.body.chats[i].timestamp).to.not.equal(null);
node.expect(res.body.chats[i].fee).to.not.equal(null);
node.expect(res.body.chats[i].amount).to.not.equal(null);
node.expect(res.body.chats[i]).to.have.property('asset').to.be.an('object');
node.expect(res.body.chats[i].asset).to.have.property('chat').to.be.an('object');
node.expect(res.body.chats[i].asset.chat).to.have.property('message').to.not.equal(null);
node.expect(res.body.chats[i].asset.chat).to.have.property('own_message').to.not.equal(null);
node.expect(res.body.chats[i].asset.chat).to.have.property('type').to.not.equal(null);
node.expect(res.body.chats[i]).to.have.property('lastTransaction').to.be.an('object');
node.expect(res.body.chats[i].lastTransaction.timestamp).to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.fee).to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.amount).to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction).to.have.property('asset').to.be.an('object');
node.expect(res.body.chats[i].lastTransaction.asset).to.have.property('chat').to.be.an('object');
node.expect(res.body.chats[i].lastTransaction.asset.chat).to.have.property('message').to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.asset.chat).to.have.property('own_message').to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.asset.chat).to.have.property('type').to.not.equal(null);
}
done();
});
Expand All @@ -209,9 +210,10 @@ describe('GET /api/chatrooms/:ID/:ID', function () {
node.expect(res.body.chats[i]).to.have.property('participants').to.have.lengthOf(2);
node.expect(res.body.chats[i].participants[0].publicKey).to.not.equal(null);
node.expect(res.body.chats[i].participants[1].publicKey).to.not.equal(null);
node.expect(res.body.chats[i].timestamp).to.not.equal(null);
node.expect(res.body.chats[i].fee).to.not.equal(null);
node.expect(res.body.chats[i].amount).to.not.equal(null);
node.expect(res.body.chats[i]).to.have.property('lastTransaction').to.be.an('object');
node.expect(res.body.chats[i].lastTransaction.timestamp).to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.fee).to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.amount).to.not.equal(null);
}
done();
}, { orderBy: 'timestamp:desc'});
Expand All @@ -226,9 +228,10 @@ describe('GET /api/chatrooms/:ID/:ID', function () {
node.expect(res.body.chats[i]).to.have.property('participants').to.have.lengthOf(2);
node.expect(res.body.chats[i].participants[0].publicKey).to.not.equal(null);
node.expect(res.body.chats[i].participants[1].publicKey).to.not.equal(null);
node.expect(res.body.chats[i].timestamp).to.not.equal(null);
node.expect(res.body.chats[i].fee).to.not.equal(null);
node.expect(res.body.chats[i].amount).to.not.equal(null);
node.expect(res.body.chats[i]).to.have.property('lastTransaction').to.be.an('object');
node.expect(res.body.chats[i].lastTransaction.timestamp).to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.fee).to.not.equal(null);
node.expect(res.body.chats[i].lastTransaction.amount).to.not.equal(null);
}
done();
});
Expand Down Expand Up @@ -470,4 +473,4 @@ describe('GET /api/chatrooms/:ID/:ID', function () {
orderBy: 'timestamp:asc'
});
});
});
});

0 comments on commit c944ad8

Please sign in to comment.