diff --git a/helpers/transactionTypes.js b/helpers/transactionTypes.js index c5ec427c..03b81ab7 100644 --- a/helpers/transactionTypes.js +++ b/helpers/transactionTypes.js @@ -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 + } }; diff --git a/modules/chatrooms.js b/modules/chatrooms.js index 4ec096fb..82a512a6 100644 --- a/modules/chatrooms.js +++ b/modules/chatrooms.js @@ -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; @@ -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); } @@ -106,9 +106,7 @@ __private.listChats = function (filter, cb) { } const orderBy = OrderBy( - filter.orderBy, { - sortFields: sql.sortFields - } + filter.orderBy, sql.chatroomsSortDefaults ); if (orderBy.error) { @@ -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, @@ -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) { @@ -203,9 +204,7 @@ __private.listMessages = function (filter, cb) { } const orderBy = OrderBy( - filter.orderBy, { - sortFields: sql.sortFields - } + filter.orderBy, sql.chatroomsSortDefaults ); if (orderBy.error) { @@ -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 }; @@ -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; @@ -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 }); } @@ -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; @@ -336,4 +339,4 @@ Chatrooms.prototype.internal = { Chatrooms.prototype.shared = {}; -module.exports = Chatrooms; \ No newline at end of file +module.exports = Chatrooms; diff --git a/package.json b/package.json index 000c58d3..435ae309 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adamant", - "version": "0.5.0", + "version": "0.5.1", "private": true, "scripts": { "start": "node app.js", diff --git a/sql/chats.js b/sql/chats.js index 01700b5a..1222b21b 100644 --- a/sql/chats.js +++ b/sql/chats.js @@ -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}', @@ -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"', @@ -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; }, diff --git a/test/api/chatrooms.js b/test/api/chatrooms.js index 8b0a9bf1..e990b0ff 100644 --- a/test/api/chatrooms.js +++ b/test/api/chatrooms.js @@ -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(); }); @@ -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'}); @@ -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(); }); @@ -470,4 +473,4 @@ describe('GET /api/chatrooms/:ID/:ID', function () { orderBy: 'timestamp:asc' }); }); -}); \ No newline at end of file +});