Skip to content

Commit c0b083c

Browse files
committed
Refactored to store JSON instead of an HTML message
This will allow the messages to be rendered differently. New CSS or HTML can be applied.
1 parent 650f4ee commit c0b083c

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ networks:
119119

120120
Persistent chat rooms are not enabled by default and are never required when entering a room. If you click the checkbox on the login page to **Create or join a persistent room** all of your messages will be logged. If you do not check the box your messages will not be logged. You can tell if a user is logging there messages when an exclamation mark is appended to their username. e.g. `@foobar!`
121121

122+
Note: There is no URL syntax for logging into a room with encrypting messages enabled. If you are logged into a room with encryption enabled and you refresh the browser, your encryption password will be lost. You will need to logout and back in manually with the same encryption password. Your encryption password is only valid until you close the browser window or tab, or refresh.
123+
122124
## Encrypted messages
123125

124126
Encryption is handled by the Stanford Javascript Crypto Library and happens entirely within your browser.

html/js/talk2me.js

+47-11
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,22 @@
9898
return;
9999
}
100100

101-
msg = "<span class=\"room-user-message\">@" + username + "</span> " + htmlspecialchars(msg)
102-
+ " <span class=\"timestamp\">" + getTimestamp() + "</span>";
103-
// strip_tags(msg, "<strong><em><table><thead><tbody><tr><th><td><img><br><br/><a><p><div><ul><li><ol><span><hr><hr/><dd><dl><dt>");
104-
var orgMsg = msg;
101+
var timestamp = getTimestamp();
102+
var jsonMessageString = JSON.stringify({ "username": username, "msg": msg, "tst": timestamp });
103+
// TODO: create a function to generate htmlMessage - this is duplicated 3 times in this code
104+
var htmlMessage = "<span class=\"room-user-message\">@" + username + "</span> " + htmlspecialchars(msg)
105+
+ " <span class=\"timestamp\">" + timestamp + "</span>";
105106

106107
if (usekey) {
107-
msg = encryptMessage(msg);
108+
jsonMessageString = encryptMessage(jsonMessageString);
108109
}
109110

110111
if (!msg) {
111112
$.jGrowl("Message not sent - could not encrypt!", { life: 8000, group: "error-encryption" });
112113
} else {
113-
var request = {"a": "message", "msg": msg, "persistent": persistent, "encrypted": usekey};
114+
var request = {"a": "message", "msg": jsonMessageString, "persistent": persistent, "encrypted": usekey};
114115
conn.send(JSON.stringify(request));
115-
appendMessage(orgMsg, usekey);
116+
appendMessage(htmlMessage, usekey);
116117
}
117118

118119
scrollToTop();
@@ -278,14 +279,32 @@
278279
$(".messages").before("<div class=\"more-messages-alert container\">"
279280
+ "Persistent messages enabled</div>");
280281
// Display all messages from room when first logging into room.
282+
console.log(jsonObj.messages);
281283
$.each(jsonObj.messages, function(k, v) {
284+
// TODO: start: create function for this (#duplicateParsedMessage)
285+
var jsonMessage = null;
282286
if (v.item.encrypted) {
283-
v.item.message = getMessageLockHTML() + " " + decryptMessage(v.item.message);
287+
jsonMessage = JSON.parse(decryptMessage(v.item.message));
288+
} else {
289+
jsonMessage = JSON.parse(v.item.message);
284290
}
285291

292+
var message = null;
293+
if (v.item.encrypted) {
294+
message = getMessageLockHTML() + " " + htmlspecialchars(jsonMessage.msg);
295+
} else {
296+
message = htmlspecialchars(jsonMessage.msg);
297+
}
298+
var username = jsonMessage.username;
299+
var timestamp = jsonMessage.tst;
300+
301+
var htmlMessage = "<span class=\"room-user-message\">@" + username + "</span> "
302+
+ message + " <span class=\"timestamp\">" + timestamp + "</span>";
303+
// TODO: end: create function for this (#duplicateParsedMessage)
304+
286305
if (v.item.message) {
287306
$(".messages").append("<div class=\"well well-sm message\">"
288-
+ Wwiki.render(linker.link(v.item.message)) + "</div>");
307+
+ Wwiki.render(linker.link(htmlMessage)) + "</div>");
289308
}
290309
});
291310
var s = $(jsonObj.messages).size();
@@ -696,12 +715,29 @@
696715
"use strict";
697716
if (persistent) {
698717
$.each(jsonObj.messages, function(k, v) {
718+
// TODO: start: create function for this (#duplicateParsedMessage)
719+
var jsonMessage = null;
720+
if (v.item.encrypted) {
721+
jsonMessage = JSON.parse(decryptMessage(v.item.message));
722+
} else {
723+
jsonMessage = JSON.parse(v.item.message);
724+
}
725+
726+
var message = null;
699727
if (v.item.encrypted) {
700-
v.item.message = getMessageLockHTML() + " " + decryptMessage(v.item.message);
728+
message = getMessageLockHTML() + " " + htmlspecialchars(jsonMessage.msg);
729+
} else {
730+
message = htmlspecialchars(jsonMessage.msg);
701731
}
732+
var username = jsonMessage.username;
733+
var timestamp = jsonMessage.tst;
734+
735+
var htmlMessage = "<span class=\"room-user-message\">@" + username + "</span> "
736+
+ message + " <span class=\"timestamp\">" + timestamp + "</span>";
737+
// TODO: end: create function for this (#duplicateParsedMessage)
702738

703739
$(".messages").append("<div class=\"well well-sm message\">"
704-
+ Wwiki.render(linker.link(v.item.message)) + "</div>");
740+
+ Wwiki.render(linker.link(htmlMessage)) + "</div>");
705741
});
706742
var s = $(jsonObj.messages).size();
707743
messagesShown += s;

0 commit comments

Comments
 (0)