diff --git a/CHANGELOG.md b/CHANGELOG.md index 2505519..e826afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ +## v0.2.11 +- archive-conversation payload now publishes on MQTT the full conversation data, not only the conversation patch + ## v0.2.10 - added test #17 - conversation/archivedConversation detail diff --git a/benchmarks/performance_test.js b/benchmarks/performance_test.js index b8abefd..529e3a7 100644 --- a/benchmarks/performance_test.js +++ b/benchmarks/performance_test.js @@ -39,7 +39,7 @@ const user1 = { fullname: 'User 1', firstname: 'User', lastname: '1', - token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2NzI5NDc4ZS1mOWIwLTRiODctYjNhYS03ZjU1OWExNzc5YjIiLCJzdWIiOiJVU0VSMSIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjEuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIxLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjEiLCJjaWQiOiJVU0VSMSIsImF6cCI6IlVTRVIxIiwidXNlcl9pZCI6IlVTRVIxIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjEiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.r-GBXo1fIUtl1QjOkXxcRaenVNQBElRkus3omh9YtjQ' + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2OGFkODJjYi1lODE2LTRkYWEtYjljYi0wM2NiZmFjMDY1OGQiLCJzdWIiOiJVU0VSMSIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2Fw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ToqLyovYXBwcy50aWxlY2hhdC5vdXRnb2luZy51c2Vycy5VU0VSMi4qIiwicmFiYml0bXEuY29uZmlndXJlOiovKi8qIl0sImNsaWVudF9pZCI6IlVTRVIyIiwiY2lkIjoiVVNFUjIiLCJhenAiOiJVU0VSMiIsInVzZXJfaWQiOiJVU0VSMiIsImFwcF9pZCI6InRpbGVjaGF0IiwiaWF0IjoxNjQ0Njc1NzcxLCJleHAiOjE5NTU3MTU3NzEsImF1ZCI6WyJyYWJiaXRtcSIsIlVTRVIyIl0sImtpZCI6InRpbGVkZXNrLWtleSIsInRpbGVkZXNrX2FwaV9yb2xlcyI6InVzZXIifQ.NQsVvyrwGaCz9W6vS1-QSPRxBL1b2mPz1ntLtEFJm_A' }; let chatClient1 = new Chat21Client( @@ -131,7 +123,6 @@ describe("Performance Test", function() { ); }); }); - }); after(function(done) { @@ -144,7 +135,6 @@ describe("Performance Test", function() { it("Benchmark for direct messages", function(done) { this.timeout(1000 * 70); - async function benchmark() { console.log("\n\n*********************************************"); console.log("********* Direct messages benchmark *********"); @@ -168,6 +158,7 @@ describe("Performance Test", function() { for (let c = 0; c < config.CONCURRENCY; c++) { let recipient_id = user2.userid; let recipient_fullname = user2.fullname; + // console.log("sending...",i,c); sendMessage(i, c, recipient_id, recipient_fullname, async function(latency, iteration, concurrent_iteration) { // console.log("Direct - latency:", latency) if (iteration == total_iterations - 1 && concurrent_iteration == config.CONCURRENCY - 1) { @@ -183,6 +174,7 @@ describe("Performance Test", function() { console.log("End 'Direct' benchmark iterations."); function endCallback(latency) { + console.log("\n\n********* Direct - Benchmark results *********"); console.log("Direct - Final latency:", latency.meanLatencyMs); let test_duration = Math.round(current / 1000) console.log("Direct - Test duration:", test_duration + " seconds" + " (" + current + ") ms"); @@ -241,6 +233,7 @@ describe("Performance Test", function() { } function endCallback(latency) { + console.log("\n\n********* Group - Benchmark results *********"); console.log("Group - Final latency:", latency.meanLatencyMs); console.log("Group - Expected max average latency:", config.EXPECTED_AVG_GROUP_MESSAGE_DELAY); let test_duration = Math.round(current / 1000) @@ -286,7 +279,7 @@ function sendMessage(iteration, concurrent_iteration, recipient_id, recipient_fu total_delay += delay; // console.log("total:", total_delay) let mean = total_delay / total_messages - // console.log("message N:", messages, "currentTimeMs:", current, "meanMs:", Math.floor(mean)); + // console.log("total_messages N:", total_messages, "currentTimeMs:", current, "meanMs:", Math.floor(mean)); let latency_info = { totalMessages: total_messages, latencyMs: delay, @@ -311,7 +304,12 @@ function sendMessage(iteration, concurrent_iteration, recipient_id, recipient_fu null, recipient_id.startsWith("group-") ? 'group' : 'direct', //user2.userid.startsWith("group-") ? 'group' : 'direct', (err, msg) => { - // console.log("Message sent", msg.text); + if (err) { + console.error("Error send:", err); + } + // else { + // console.log("Message sent:", msg.text); + // } } ); } diff --git a/chatdb/index.js b/chatdb/index.js index 50ec0b3..864d560 100644 --- a/chatdb/index.js +++ b/chatdb/index.js @@ -118,8 +118,25 @@ class ChatDB { }); } - getConversation(timelineOf, conversWith, callback) { - this.db.collection(this.conversations_collection).findOne( { timelineOf: timelineOf, conversWith: conversWith }, function(err, doc) { + // DEPRECATED? // + // getConversation(timelineOf, conversWith, callback) { + // this.db.collection(this.conversations_collection).findOne( { timelineOf: timelineOf, conversWith: conversWith }, function(err, doc) { + // if (err) { + // if (callback) { + // callback(err, null) + // } + // } + // else { + // if (callback) { + // callback(null, doc) + // } + // } + // }); + // } + + conversationDetail(appid, timelineOf, conversWith, archived, callback) { + logger.debug("DB. app: "+ appid+ " user: " + timelineOf + " conversWith: "+ conversWith); + this.db.collection(this.conversations_collection).find( { timelineOf: timelineOf, app_id: appid, conversWith: conversWith, archived: archived } ).limit(1).toArray(function(err, docs) { if (err) { if (callback) { callback(err, null) @@ -127,7 +144,7 @@ class ChatDB { } else { if (callback) { - callback(null, doc) + callback(null, docs) } } }); diff --git a/observer.js b/observer.js index 38f9a34..b5bbcd7 100644 --- a/observer.js +++ b/observer.js @@ -842,28 +842,42 @@ function process_archive(topic, payload, callback) { callback(false) return } - const conversation_deleted_topic = 'apps.tilechat.users.' + user_id + '.conversations.' + convers_with + '.clientdeleted' - logger.debug(">>> NOW PUBLISHING... CONVERSATION ARCHIVED (DELETED) TOPIC " + conversation_deleted_topic) - const payload = JSON.stringify(conversation_archive_patch) - publish(exchange, conversation_deleted_topic, Buffer.from(payload), function(err) { - logger.debug(">>> PUBLISHED!!!! CONVERSATION ON TOPIC: " + conversation_deleted_topic + " ARCHIVED (DELETED). Payload: " + payload + " buffered:" + Buffer.from(payload)) + chatdb.conversationDetail(app_id, me, convers_with, true, (err, convs) => { if (err) { - logger.error("error",err); - callback(false) + logger.error("Error getting conversationDetail()", err); + callback(true); + } + else if (convs && convs.length < 1) { + logger.error("Error getting conversationDetail(): convs[].length < 1"); + callback(true); } else { - // now publish new archived conversation added - const archived_conversation_added_topic = 'apps.tilechat.users.' + user_id + '.archived_conversations.' + convers_with + '.clientadded' - logger.debug(">>> NOW PUBLISHING... CONVERSATION ARCHIVED (ADDED) TOPIC: "+ archived_conversation_added_topic) - // const success_payload = JSON.stringify({"success": true}) - publish(exchange, archived_conversation_added_topic, Buffer.from(payload), function(err) { - logger.debug(">>> PUBLISHED!!!! ARCHIVED (DELETED) CONVERSATION ON TOPIC: " + conversation_deleted_topic) + const conversation_archived = convs[0]; + logger.debug("got archived conversation detail:", conversation_archived); + const conversation_deleted_topic = 'apps.tilechat.users.' + user_id + '.conversations.' + convers_with + '.clientdeleted' + logger.debug(">>> NOW PUBLISHING... CONVERSATION ARCHIVED (DELETED) TOPIC " + conversation_deleted_topic) + const payload = JSON.stringify(conversation_archived); + publish(exchange, conversation_deleted_topic, Buffer.from(payload), function(err) { + logger.debug(">>> PUBLISHED!!!! CONVERSATION ON TOPIC: " + conversation_deleted_topic + " ARCHIVED (DELETED). Payload: " + payload + " buffered:" + Buffer.from(payload)) if (err) { logger.error("error",err); - callback(false) + callback(false); } else { - callback(true) + // now publish new archived conversation added + const archived_conversation_added_topic = 'apps.tilechat.users.' + user_id + '.archived_conversations.' + convers_with + '.clientadded' + logger.debug(">>> NOW PUBLISHING... CONVERSATION ARCHIVED (ADDED) TOPIC: " + archived_conversation_added_topic) + // const success_payload = JSON.stringify({"success": true}) + publish(exchange, archived_conversation_added_topic, Buffer.from(payload), function(err) { + logger.debug(">>> PUBLISHED!!!! ARCHIVED (DELETED) CONVERSATION ON TOPIC: " + conversation_deleted_topic) + if (err) { + logger.error("error",err); + callback(false); + } + else { + callback(true); + } + }); } }); } diff --git a/package.json b/package.json index 68d13cc..52f4a2b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@chat21/chat21-server", - "version": "0.2.10", - "description": "Chat21 AMQP messages dispatcher", + "version": "0.2.11", + "description": "Chat21 AMQP/MQTT messages dispatcher", "dependencies": { "aedes": "^0.40.1", "amqplib": "^0.7.1", diff --git a/test/test.js b/test/test.js index dec26bb..aafe55f 100644 --- a/test/test.js +++ b/test/test.js @@ -1583,11 +1583,11 @@ REUSE SHARED CHAT CLIENTS', function(done) { topic.conversWith === user1.userid) { // chatClient2.removeOnConversationAddedHandler(handler); chatClient2.conversationDetail(topic.conversWith, (err, conv) => { - console.log("conv detail:", conv); + logger.log("conv detail:", conv); assert(err == null); assert(conv != null); chatClient2.archivedConversationDetail(topic.conversWith, (err, conv) => { - console.log("archived conv detail:", conv); + logger.log("archived conv detail:", conv); assert(err == null); assert(conv == null); chatClient2.archiveConversation(topic.conversWith, (err) => { @@ -1600,6 +1600,7 @@ REUSE SHARED CHAT CLIENTS', function(done) { let onArchivedConversationAddedHandler = chatClient2.onArchivedConversationAdded((archived_conv, topic) => { console.log("conv was archived:", archived_conv, topic); assert(archived_conv != null); + assert(archived_conv.last_message_text != null); // checking archived-conversation is metadata-full chatClient2.conversationDetail(topic.conversWith, (err, conv) => { console.log("conv detail:", conv); assert(err == null);