From d74825eadcca62bcd431f3c654cbea761d9dee10 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Mon, 29 Aug 2016 15:17:36 +0000 Subject: [PATCH] More chat fixes. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3316 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- packages/cory/chat/chat.js | 39 ++++++++++++++++---------------- packages/cory/libchat/libchat.js | 7 +++++- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/cory/chat/chat.js b/packages/cory/chat/chat.js index a5521bf3..0384e336 100644 --- a/packages/cory/chat/chat.js +++ b/packages/cory/chat/chat.js @@ -242,26 +242,22 @@ function updateConversation() { gCurrentConversation.session.getHistory(gCurrentConversation.name), gCurrentConversation.session.getParticipants(gCurrentConversation.name), ]).then(function(data) { - let history = data[0]; - let participants = data[1]; - gCurrentConversation.messages = history || []; - if (gCurrentConversation.messages.length > kMaxHistory) { - gCurrentConversation.messages.splice(0, gCurrentConversation.messages.length - kMaxHistory); - } + let [history, participants] = data; gCurrentConversation.participants = participants || []; - terminal.cork(); - terminal.select("terminal"); - terminal.clear(); - for (var i in gCurrentConversation.messages) { - let message = gCurrentConversation.messages[i]; - if (message.action == "message") { - printMessage(message.message); - } else { - terminal.print(message.message); + try { + terminal.cork(); + terminal.select("terminal"); + terminal.clear(); + printToConversation(gCurrentConversation, ["[", gCurrentConversation.name, "]"]); + if (history) { + for (let message of history) { + printToConversation(gCurrentConversation, message); + } } + updateUsers(); + } finally { + terminal.uncork(); } - updateUsers(); - terminal.uncork(); }).catch(function(error) { terminal.print(error); }); @@ -334,13 +330,16 @@ function printToConversation(conversation, message, notify) { if (conversation == gCurrentConversation) { if (message.action == "message") { printMessage(message.message); + } else if (message.action == "presence") { + if (message.presence == "unavailable") { + terminal.print(new Date().toString(), ": ", message.user, " has left the room."); + } else { + terminal.print(new Date().toString(), ": ", message.user, " has joined the room."); + } } else { terminal.print(message); } } - if (conversation) { - conversation.messages.push(message); - } if (notify && !gFocus) { gUnread++; updateTitle(); diff --git a/packages/cory/libchat/libchat.js b/packages/cory/libchat/libchat.js index 26455f1d..285a347f 100644 --- a/packages/cory/libchat/libchat.js +++ b/packages/cory/libchat/libchat.js @@ -89,10 +89,13 @@ exports.ChatService = class { let leaving = state == "unavailable"; let participants = this._getConversation(conversation).participants; let index = participants.indexOf(user); + let different = true; if (leaving) { participants.splice(index, 1); } else if (index == -1) { participants.push(user); + } else { + different = false; } let message = { action: "presence", @@ -100,7 +103,9 @@ exports.ChatService = class { user: user, presence: state, }; - this._pushMessage(conversation, message); + if (different) { + this._pushMessage(conversation, message); + } this._invokeCallback(message); }