From 380e98b7cf8fd5f456d020ae6cbbe7b2d4d8329d Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Mon, 29 Aug 2016 14:21:55 +0000 Subject: [PATCH] Fixing some old chat bugs, I think. Added a thing so that I can monitor use. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3315 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- packages/cory/libchat/libchat.js | 17 +++++++++++++--- packages/cory/monitor/monitor.js | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 packages/cory/monitor/monitor.js diff --git a/packages/cory/libchat/libchat.js b/packages/cory/libchat/libchat.js index df28d1d7..26455f1d 100644 --- a/packages/cory/libchat/libchat.js +++ b/packages/cory/libchat/libchat.js @@ -23,6 +23,7 @@ exports.ChatService = class { this._callbacks = [callback]; this._conversations = {}; this._state = null; + this._maxHistory = 64; } makeInterface(service) { @@ -70,9 +71,17 @@ exports.ChatService = class { return this._conversations[conversation]; } + _pushMessage(conversation, message) { + let history = this._getConversation(conversation || "").history; + history.push(message); + if (history.length > this._maxHistory) { + history.splice(0, history.length - this._maxHistory); + } + } + notifyMessageReceived(conversation, message) { let fullMessage = {action: "message", conversation: conversation || "", message: message}; - this._getConversation(conversation || "").history.push(fullMessage); + this._pushMessage(conversation, fullMessage); this._invokeCallback(fullMessage); } @@ -85,12 +94,14 @@ exports.ChatService = class { } else if (index == -1) { participants.push(user); } - this._invokeCallback({ + let message = { action: "presence", conversation: conversation, user: user, presence: state, - }); + }; + this._pushMessage(conversation, message); + this._invokeCallback(message); } notifyParticipantList(conversation, participants) { diff --git a/packages/cory/monitor/monitor.js b/packages/cory/monitor/monitor.js new file mode 100644 index 00000000..4fd99e8f --- /dev/null +++ b/packages/cory/monitor/monitor.js @@ -0,0 +1,35 @@ +"use strict"; + +//! {"category": "libraries", "require": ["liblist"]} + +function formatUser(user) { + return [user.name, " ", user.index.toString(), " ", user.packageOwner, " ", user.packageName]; +} + +let log = require("liblist").ListStore("log"); + +if (imports.terminal) { + core.register("onSessionBegin", function(user) { + terminal.print(new Date().toString(), " begin ", formatUser(user)); + }); + + core.register("onSessionEnd", function(user) { + terminal.print(new Date().toString(), " end ", formatUser(user)); + }); + + log.get(-1, -32).then(function(results) { + for (let result of results) { + terminal.print(result[0].toString(), " ", result[1], " ", formatUser(result[2])); + } + }).catch(terminal.print); + + core.getService("logger"); +} else { + core.register("onSessionBegin", function(user) { + return log.push([new Date(), "begin", user]); + }); + + core.register("onSessionEnd", function(user) { + return log.push([new Date(), "end", user]); + }); +} \ No newline at end of file