Many fixes and other things
- Fixed handling the case of no messages in conversation - Changed sending new messages to HTML instead of JSON - So the message format is at one place - Fixed new messages showing the conversation ID to the user at the top - Fixed unsent messages - Fixed unread indicator on own message - Fixed error hider timer - Fixed message receiving when not logged in
This commit is contained in:
parent
f997da810b
commit
23134e74f0
11 changed files with 83 additions and 70 deletions
|
@ -104,3 +104,4 @@ org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
|
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
|
||||||
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
|
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
|
||||||
org.eclipse.jdt.core.compiler.source=1.8
|
org.eclipse.jdt.core.compiler.source=1.8
|
||||||
|
org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter
|
||||||
|
|
2
.settings/org.eclipse.jdt.ui.prefs
Normal file
2
.settings/org.eclipse.jdt.ui.prefs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
formatter_settings_version=12
|
|
@ -1,11 +1,15 @@
|
||||||
function handlereceivedmessage(msgnode) {
|
function updatemsgtime(msgnode) {
|
||||||
var spans = msgnode.getElementsByTagName("span");
|
var spans = msgnode.getElementsByTagName("span");
|
||||||
var ctime = null;
|
var ctime = null;
|
||||||
for (var i = 0; i < spans.length; i++)
|
for (var i = 0; i < spans.length; i++)
|
||||||
if (spans[i].className.split(' ').indexOf("converttime") > -1)
|
if (spans[i].className.split(' ').indexOf("converttime") > -1)
|
||||||
ctime = spans[i];
|
ctime = spans[i];
|
||||||
if (ctime != null)
|
if (ctime != null)
|
||||||
ctime.innerText = moment(ctime.innerText, "YYYY-MM-DDTHH:mm:ssZ").fromNow();
|
ctime.innerText = moment($(ctime).data("val"), "YYYY-MM-DDTHH:mm:ssZ").fromNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handlereceivedmessage(msgnode) {
|
||||||
|
updatemsgtime(msgnode);
|
||||||
msgnode.scrollIntoView(false);
|
msgnode.scrollIntoView(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,12 +20,13 @@ var updateUnreadCount = function () {
|
||||||
document.title = "(" + unreadCount + ") Chat";
|
document.title = "(" + unreadCount + ") Chat";
|
||||||
else
|
else
|
||||||
document.title = "Chat";
|
document.title = "Chat";
|
||||||
var msgs = document.getElementById("channelmessages").children;
|
var msgs = document.getElementById("channelmessages").getElementsByClassName("chmessage");
|
||||||
for (var i = msgs.length - 1; i >= 0; i--) {
|
for (var i = msgs.length - 1; i >= 0; i--) {
|
||||||
if (i >= msgs.length - unreadCount)
|
if (i >= msgs.length - unreadCount)
|
||||||
msgs[i].style.backgroundColor = "darkgray";
|
msgs[i].style.backgroundColor = "darkgray";
|
||||||
else
|
else
|
||||||
msgs[i].style = "";
|
msgs[i].style = "";
|
||||||
|
updatemsgtime(msgs[i]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -35,27 +40,29 @@ var resetUnread = function resetUnread() {
|
||||||
updateUnreadCount();
|
updateUnreadCount();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var readTimer = null;
|
||||||
|
$(document).ready(function () {
|
||||||
|
$('#msginput').on("focus", function () { readTimer == null ? readTimer = setTimeout(function () { resetUnread(); }, 3000) : readTimer; });
|
||||||
|
$('#msginput').on("keydown", resetUnread);
|
||||||
|
$('#msginput').on("blur", function () { readTimer != null ? clearTimeout(readTimer) : readTimer; });
|
||||||
|
|
||||||
|
if (isLoggedIn())
|
||||||
(function poll() {
|
(function poll() {
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "/receivemessage", success: function (data) {
|
url: "/receivemessage", success: function (data) {
|
||||||
console.log(data);
|
var msgs = document.getElementById("channelmessages");
|
||||||
var msgelement = document.getElementById("channelmessages").appendChild(document.createElement("div"));
|
msgs.innerHTML += data;
|
||||||
var header = msgelement.appendChild(document.createElement("p"));
|
var msgelement = msgs.children[msgs.children.length - 1];
|
||||||
header.innerText = data.sender.name + " - ";
|
|
||||||
var span = header.appendChild(document.createElement("span"));
|
|
||||||
span.className = "converttime";
|
|
||||||
span.innerText = data.time;
|
|
||||||
var body = msgelement.appendChild(document.createElement("p"));
|
|
||||||
body.innerText = data.message;
|
|
||||||
handlereceivedmessage(msgelement);
|
handlereceivedmessage(msgelement);
|
||||||
|
if (justsentmsgread)
|
||||||
|
justsentmsgread = false;
|
||||||
|
else
|
||||||
addUnread();
|
addUnread();
|
||||||
}, dataType: "json", complete: poll
|
}, error: function (data) {
|
||||||
|
showError(data.responseText);
|
||||||
|
}, dataType: "text", complete: poll
|
||||||
});
|
});
|
||||||
}, 100);
|
}, 100);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
$(document).ready(function () {
|
|
||||||
$('#msginput').on("focus", resetUnread);
|
|
||||||
$('#msginput').on("keydown", resetUnread);
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
var sendmsg = function sendmsg(msginputta) {
|
var sendmsg = function sendmsg(msginputta) {
|
||||||
window.jsonobj = JSON.stringify({ "message": msginputta.value, "conversation": window.convid });
|
window.jsonobj = JSON.stringify({ "message": msginputta.value, "conversation": window.convid });
|
||||||
console.log(window.jsonobj);
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "/sendmessage", data: window.jsonobj, method: "POST", success: respfunc, error: respfunc
|
url: "/sendmessage", data: window.jsonobj, method: "POST", success: respfunc, error: respfunc
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var justsentmsgread = false;
|
||||||
var respfunc = function respfunc(result) {
|
var respfunc = function respfunc(result) {
|
||||||
var msginput = document.getElementById("msginput");
|
var msginput = document.getElementById("msginput");
|
||||||
if (result != "Success") { //on success result is string
|
if (result != "Success") { //on success result is string
|
||||||
|
@ -18,12 +18,14 @@ var respfunc = function respfunc(result) {
|
||||||
showError(result.responseText);
|
showError(result.responseText);
|
||||||
msginput.disabled = false;
|
msginput.disabled = false;
|
||||||
msginput.focus();
|
msginput.focus();
|
||||||
|
resetUnread();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
msginput.value = "";
|
msginput.value = "";
|
||||||
msginput.disabled = false;
|
msginput.disabled = false;
|
||||||
msginput.focus();
|
msginput.focus();
|
||||||
|
justsentmsgread = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,16 @@ function getFormData($form) {
|
||||||
return indexed_array;
|
return indexed_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errorcleartimer = null;
|
||||||
function showError(message) {
|
function showError(message) {
|
||||||
|
if (errorcleartimer != null)
|
||||||
|
clearInterval(errorcleartimer);
|
||||||
var errormsg = document.getElementById("errormsg");
|
var errormsg = document.getElementById("errormsg");
|
||||||
errormsg.innerHTML = message;
|
errormsg.innerHTML = message;
|
||||||
errormsg.style = "display: block";
|
errormsg.style = "display: block";
|
||||||
setTimeout(function () { errormsg.style.display = "none"; }, 2000);
|
errorcleartimer = setTimeout(function () { errormsg.style.display = "none"; }, 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isLoggedIn() {
|
||||||
|
return document.getElementById("usercontent") != null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ import io.github.norbipeti.chat.server.data.*;
|
||||||
import io.github.norbipeti.chat.server.db.domain.*;
|
import io.github.norbipeti.chat.server.db.domain.*;
|
||||||
import io.github.norbipeti.chat.server.io.DataType;
|
import io.github.norbipeti.chat.server.io.DataType;
|
||||||
import io.github.norbipeti.chat.server.page.*;
|
import io.github.norbipeti.chat.server.page.*;
|
||||||
import io.vertx.core.Vertx;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static Gson gson;
|
public static Gson gson;
|
||||||
|
@ -31,7 +30,6 @@ public class Main {
|
||||||
LogManager.getLogger().log(Level.INFO, "Loading files...");
|
LogManager.getLogger().log(Level.INFO, "Loading files...");
|
||||||
DataManager.init();
|
DataManager.init();
|
||||||
final GsonBuilder saveGsonBuilder = new GsonBuilder();
|
final GsonBuilder saveGsonBuilder = new GsonBuilder();
|
||||||
final GsonBuilder exchangeGsonBuilder = new GsonBuilder();
|
|
||||||
Reflections rf = new Reflections(new ConfigurationBuilder()
|
Reflections rf = new Reflections(new ConfigurationBuilder()
|
||||||
.setUrls(ClasspathHelper.forClassLoader(ManagedData.class.getClassLoader()))
|
.setUrls(ClasspathHelper.forClassLoader(ManagedData.class.getClassLoader()))
|
||||||
.addClassLoader(ManagedData.class.getClassLoader()).addScanners(new SubTypesScanner())
|
.addClassLoader(ManagedData.class.getClassLoader()).addScanners(new SubTypesScanner())
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package io.github.norbipeti.chat.server.data;
|
package io.github.norbipeti.chat.server.data;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
@ -40,7 +39,6 @@ public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<?>>
|
||||||
in.beginObject();
|
in.beginObject();
|
||||||
in.nextName();
|
in.nextName();
|
||||||
List<Long> list;
|
List<Long> list;
|
||||||
LoaderCollection<? extends ManagedData> itemcol;
|
|
||||||
list = new Gson().fromJson(in, new TypeToken<List<Long>>() {
|
list = new Gson().fromJson(in, new TypeToken<List<Long>>() {
|
||||||
}.getType());
|
}.getType());
|
||||||
if (!in.nextName().equals("class"))
|
if (!in.nextName().equals("class"))
|
||||||
|
|
|
@ -103,14 +103,20 @@ public class Message extends ManagedData {
|
||||||
|
|
||||||
public Element getAsHTML(Element channelmessages) {
|
public Element getAsHTML(Element channelmessages) {
|
||||||
Element msgelement = channelmessages.appendElement("div");
|
Element msgelement = channelmessages.appendElement("div");
|
||||||
|
msgelement.addClass("chmessage");
|
||||||
Element header = msgelement.appendElement("p");
|
Element header = msgelement.appendElement("p");
|
||||||
header.text(getSender().get().getName() + " - ");
|
header.text(getSender().get().getName() + " - ");
|
||||||
header.appendElement("span").addClass("converttime").text(formatDate());
|
header.appendElement("span").addClass("converttime").attr("data-val", formatDate());
|
||||||
Element body = msgelement.appendElement("p");
|
Element body = msgelement.appendElement("p");
|
||||||
body.text(getMessage());
|
body.text(getMessage());
|
||||||
return msgelement;
|
return msgelement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @Deprecated Why send it as JSON then convert it to HTML?
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public JsonObject getAsJson() {
|
public JsonObject getAsJson() {
|
||||||
JsonObject msgobj = new JsonObject();
|
JsonObject msgobj = new JsonObject();
|
||||||
msgobj.add("sender", Main.gson.toJsonTree(getSender().get()));
|
msgobj.add("sender", Main.gson.toJsonTree(getSender().get()));
|
||||||
|
|
|
@ -4,7 +4,6 @@ import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
|
||||||
import io.github.norbipeti.chat.server.data.DataManager;
|
|
||||||
import io.github.norbipeti.chat.server.data.LoaderCollection;
|
import io.github.norbipeti.chat.server.data.LoaderCollection;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
package io.github.norbipeti.chat.server.page;
|
package io.github.norbipeti.chat.server.page;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.TimeZone;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Level;
|
import org.apache.logging.log4j.Level;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
|
@ -57,11 +53,13 @@ public class IndexPage extends Page {
|
||||||
cide.attr("style", "display: none");
|
cide.attr("style", "display: none");
|
||||||
cide.attr("id", "convidp");
|
cide.attr("id", "convidp");
|
||||||
cide.text(Long.toString(conv.getId()));
|
cide.text(Long.toString(conv.getId()));
|
||||||
LogManager.getLogger().log(Level.DEBUG, "Messages: " + conv.getMesssageChunks().size());
|
LogManager.getLogger().log(Level.DEBUG, "Messagechunks: " + conv.getMesssageChunks().size());
|
||||||
|
if (conv.getMesssageChunks().size() > 0) {
|
||||||
MessageChunk chunk = conv.getMesssageChunks().get(conv.getMesssageChunks().size() - 1);
|
MessageChunk chunk = conv.getMesssageChunks().get(conv.getMesssageChunks().size() - 1);
|
||||||
for (Message message : chunk.getMessages()) {
|
for (Message message : chunk.getMessages()) {
|
||||||
message.getAsHTML(channelmessages);
|
message.getAsHTML(channelmessages);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return doc;
|
return doc;
|
||||||
}, exchange);
|
}, exchange);
|
||||||
} // TODO: Validation at registration (no special chars, etc.)
|
} // TODO: Validation at registration (no special chars, etc.)
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
package io.github.norbipeti.chat.server.page;
|
package io.github.norbipeti.chat.server.page;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map.Entry;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import javax.swing.text.html.HTMLDocument;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
|
||||||
import io.github.norbipeti.chat.server.Main;
|
|
||||||
import io.github.norbipeti.chat.server.data.LoaderRef;
|
|
||||||
import io.github.norbipeti.chat.server.db.domain.Conversation;
|
import io.github.norbipeti.chat.server.db.domain.Conversation;
|
||||||
import io.github.norbipeti.chat.server.db.domain.Message;
|
import io.github.norbipeti.chat.server.db.domain.Message;
|
||||||
import io.github.norbipeti.chat.server.db.domain.User;
|
import io.github.norbipeti.chat.server.db.domain.User;
|
||||||
|
@ -26,7 +28,7 @@ public class ReceiveMessageAjaxPage extends Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap<User, HttpExchange> exmap = new HashMap<>();
|
public static HashMap<User, HttpExchange> exmap = new HashMap<>();
|
||||||
public static HashMap<User, Message> unsentmessages = new HashMap<>();
|
public static HashMap<User, ArrayList<Message>> unsentmessages = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePage(HttpExchange exchange) throws IOException {
|
public void handlePage(HttpExchange exchange) throws IOException {
|
||||||
|
@ -36,42 +38,35 @@ public class ReceiveMessageAjaxPage extends Page {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
exmap.put(user, exchange);
|
exmap.put(user, exchange);
|
||||||
/*
|
|
||||||
* String message = obj.get("message").getAsString().trim(); int conversation = obj.get("conversation").getAsInt(); if (message.trim().length() == 0) { IOHelper.SendResponse(400,
|
|
||||||
* "<h1>400 Bad request</h1><p>The message cannot be empty.</p>", exchange); return; } LoaderCollection<Conversation> convos = user.getConversations(); Conversation conv = null;
|
|
||||||
* LogManager.getLogger().log(Level.DEBUG, "Len: " + convos.size()); for (Conversation con : convos) { LogManager.getLogger().log(Level.DEBUG, con.getId()); if (con.getId() == conversation) {
|
|
||||||
* conv = con; break; } } if (conv == null) { IOHelper.SendResponse(400, "<h1>400 Conversation not found</h1><p>The conversation with the id " + conversation + " is not found.</p>", exchange);
|
|
||||||
* return; } MessageChunk chunk = ManagedData.create(MessageChunk.class); chunk.setConversation(conv); Message msg = new Message(); msg.setSender(user); msg.setMessage(message);
|
|
||||||
* msg.setTime(new Date()); msg.setMessageChunk(chunk); chunk.getMessages().add(msg); conv.getMesssageChunks().add(chunk); DataManager.save(conv); LogManager.getLogger().log(Level.DEBUG,
|
|
||||||
* "Added conversation's message count: " + conv.getMesssageChunks().size());
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendMessageBack(Message msg, Conversation conv) throws IOException {
|
public static void sendMessageBack(Message msg, Conversation conv) throws IOException {
|
||||||
for (User user : conv.getUsers()) { // TODO: Load older messages when scrolling up
|
for (User user : conv.getUsers()) { // TODO: Load older messages when scrolling up
|
||||||
if (unsentmessages.size() > 10)
|
if (unsentmessages.containsKey(user) && unsentmessages.get(user).size() > 10) {
|
||||||
unsentmessages.clear();
|
unsentmessages.get(user).clear();
|
||||||
if (exmap.containsKey(user)) { // TODO: Save new messages if not listening - If message count is bigger than 10, remove (the user is probably offline)
|
}
|
||||||
unsentmessages.put(user, msg);
|
if (!unsentmessages.containsKey(user))
|
||||||
for (Entry<User, Message> entry : unsentmessages.) { //TODO: Only one key allowed, fix
|
unsentmessages.put(user, new ArrayList<Message>());
|
||||||
JsonObject msgobj = entry.getValue().getAsJson(); // TODO: Only send messages if the user's current conversation matches
|
unsentmessages.get(user).add(msg);
|
||||||
|
if (exmap.containsKey(user)) {
|
||||||
|
Iterator<Message> it = unsentmessages.get(user).iterator();
|
||||||
|
String finalmsghtml = "";
|
||||||
|
while (it.hasNext()) {
|
||||||
|
Message entry = it.next();
|
||||||
|
Element msgobj = entry.getAsHTML(new Document("")); // TODO: Only send messages if the user's current conversation matches
|
||||||
|
finalmsghtml += msgobj.toString() + "\n";
|
||||||
try {
|
try {
|
||||||
IOHelper.SendResponse(200, msgobj.toString(), exmap.get(user));
|
it.remove(); // Remove sent message
|
||||||
} catch (IOException e) { // Remove users even if an error occurs (otherwise they may not be able to send a new message due to "headers already sent")
|
} catch (Exception e) { // Remove users even if an error occurs (otherwise they may not be able to send a new/ message due to "headers already sent")
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonObject msgobj = msg.getAsJson(); // TODO: Only send messages if the user's current conversation matches
|
IOHelper.SendResponse(200, finalmsghtml, exmap.get(user));
|
||||||
try {
|
|
||||||
IOHelper.SendResponse(200, msgobj.toString(), exmap.get(user));
|
|
||||||
} catch (IOException e) { // Remove users even if an error occurs (otherwise they may not be able to send a new message due to "headers already sent")
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
exmap.remove(user);
|
exmap.remove(user);
|
||||||
|
if (unsentmessages.get(user).size() == 0)
|
||||||
|
unsentmessages.remove(user);
|
||||||
} else {
|
} else {
|
||||||
LogManager.getLogger().warn("User is not listening: " + user);
|
LogManager.getLogger().warn("User is not listening: " + user);
|
||||||
unsentmessages.put(user, msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue