Conversation adding added (buggy)
This commit is contained in:
parent
0055ac8f2b
commit
660e49a9f6
9 changed files with 109 additions and 16 deletions
|
@ -12,12 +12,16 @@
|
|||
<script src="js/register.js"></script>
|
||||
<link rel="stylesheet" href="css/style.css" />
|
||||
<script src="js/index.js"></script>
|
||||
<script src="js/conversations.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="errormsg">
|
||||
</div>
|
||||
<div id="conversations">
|
||||
<div id="addconv">
|
||||
<a href="#" onclick="return addConversation();">Add conversation</a>
|
||||
</div>
|
||||
</div>
|
||||
<div id="sidebar">
|
||||
<div id="userbox">
|
||||
|
|
11
pages/js/conversations.js
Normal file
11
pages/js/conversations.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
function addConversation() {
|
||||
var json = JSON.stringify({ "action": "add" });
|
||||
$.ajax({
|
||||
url: "/conversations", data: json, method: "POST", success: function (result) {
|
||||
document.getElementById("conversations").innerHTML += result;
|
||||
}, error: function (result) {
|
||||
showError(result.responseText);
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
|
@ -29,6 +29,14 @@ function changeConversation(convid) {
|
|||
var convidp = document.getElementById("convidp");
|
||||
if (convidp.innerText == convid + "")
|
||||
return;
|
||||
if (!canswitchconversations)
|
||||
return;
|
||||
canswitchconversations = false;
|
||||
stopPoll();
|
||||
var chmsgse = document.getElementById("channelmessages");
|
||||
var chmsgs = chmsgse.getElementsByClassName("chmessage");
|
||||
for (var i = 0; i < chmsgs.length; i++)
|
||||
chmsgse.removeChild(chmsgs[i]);
|
||||
convidp.innerText = convid;
|
||||
conversationChanged();
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ var resetUnread = function resetUnread() {
|
|||
};
|
||||
|
||||
var shouldpoll = false;
|
||||
var canswitchconversations = true;
|
||||
function poll() {
|
||||
setTimeout(function () {
|
||||
if (!shouldpoll)
|
||||
|
@ -51,10 +52,12 @@ function poll() {
|
|||
msgs.innerHTML += data;
|
||||
var msgelement = msgs.children[msgs.children.length - 1];
|
||||
handlereceivedmessage(msgelement);
|
||||
canswitchconversations = true;
|
||||
if (justsentmsgread)
|
||||
justsentmsgread = false;
|
||||
else
|
||||
addUnread();
|
||||
|
||||
}, error: function (data) {
|
||||
if (data.responseText) {
|
||||
if (data.responseText.indexOf("ERROR") == -1)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
var sendmsg = function sendmsg(msginputta) {
|
||||
window.convid = document.getElementById("convidp").innerText * 1;
|
||||
window.jsonobj = JSON.stringify({ "message": msginputta.value, "conversation": window.convid });
|
||||
$.ajax({
|
||||
url: "/sendmessage", data: window.jsonobj, method: "POST", success: respfunc, error: respfunc
|
||||
|
|
|
@ -2,6 +2,8 @@ package io.github.norbipeti.chat.server.db.domain;
|
|||
|
||||
import javax.persistence.*;
|
||||
|
||||
import org.jsoup.nodes.Element;
|
||||
|
||||
import io.github.norbipeti.chat.server.data.LoaderCollection;
|
||||
|
||||
@Entity
|
||||
|
@ -56,4 +58,19 @@ public class Conversation extends SavedData {
|
|||
@Override
|
||||
protected void init() {
|
||||
}
|
||||
|
||||
public Element getAsHtml(Element conversations) {
|
||||
Element conversation = conversations.appendElement("div");
|
||||
conversation.addClass("conversation");
|
||||
String users = "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (User item : getUsers()) {
|
||||
sb.append(item.getName()).append(", ");
|
||||
}
|
||||
if (sb.length() > 2)
|
||||
sb.replace(sb.length() - 2, sb.length() - 1, "");
|
||||
users = sb.toString();
|
||||
conversation.appendElement("a").text(users).attr("href", "javascript:changeConversation(" + getId() + ")");
|
||||
return conversation;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package io.github.norbipeti.chat.server.page;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.jsoup.nodes.Document;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import io.github.norbipeti.chat.server.db.domain.Conversation;
|
||||
import io.github.norbipeti.chat.server.db.domain.ManagedData;
|
||||
import io.github.norbipeti.chat.server.db.domain.User;
|
||||
import io.github.norbipeti.chat.server.io.IOHelper;
|
||||
|
||||
public class ConversationsAjaxPage extends Page {
|
||||
|
||||
@Override
|
||||
public String GetName() {
|
||||
return "conversations";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePage(HttpExchange exchange) throws IOException {
|
||||
JsonObject post = IOHelper.GetPOSTJSON(exchange);
|
||||
User user = IOHelper.GetLoggedInUser(exchange);
|
||||
if (user == null) {
|
||||
IOHelper.SendResponse(403, "<h1>403 Login required</h1>", exchange);
|
||||
return;
|
||||
}
|
||||
if (post == null) {
|
||||
IOHelper.SendResponse(400,
|
||||
"<h1>400 Bad request</h1><p>Not a JSON string: " + IOHelper.GetPOST(exchange) + "</p>", exchange);
|
||||
return;
|
||||
}
|
||||
if (!post.has("action")) {
|
||||
IOHelper.SendResponse(400, "<h1>400 Bad request</h1><p>Action missing: " + post + "</p>", exchange);
|
||||
return;
|
||||
}
|
||||
switch (post.get("action").getAsString()) {
|
||||
case "add": {
|
||||
Conversation conv = ManagedData.create(Conversation.class);
|
||||
conv.getUsers().add(user); // TODO: Option to invite people
|
||||
user.getConversations().add(conv);
|
||||
Document doc = new Document("");
|
||||
conv.getAsHtml(doc);
|
||||
IOHelper.SendResponse(200, doc.toString(), exchange);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
IOHelper.SendResponse(400,
|
||||
"<h1>400 Bad request</h1><p>Unknown action: " + post.get("action").getAsString() + "</p>",
|
||||
exchange);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -51,22 +51,11 @@ public class IndexPage extends Page {
|
|||
cide.attr("id", "convidp");
|
||||
cide.text(Long.toString(convid));
|
||||
Element conversations = doc.getElementById("conversations");
|
||||
for (Conversation conv : user.getConversations()) {
|
||||
Element conversation = conversations.appendElement("div");
|
||||
conversation.addClass("conversation");
|
||||
String users = "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (User item : conv.getUsers()) {
|
||||
sb.append(item.getName()).append(", ");
|
||||
}
|
||||
if (sb.length() > 2)
|
||||
sb.replace(sb.length() - 2, sb.length() - 1, "");
|
||||
users = sb.toString();
|
||||
conversation.appendElement("a").text(users).attr("href",
|
||||
"javascript:changeConversation(" + conv.getId() + ")");
|
||||
}
|
||||
for (Conversation conv : user.getConversations())
|
||||
conv.getAsHtml(conversations);
|
||||
return doc;
|
||||
}, exchange);
|
||||
|
||||
} // TODO:
|
||||
// Validation
|
||||
// at
|
||||
|
|
|
@ -41,6 +41,7 @@ public class ReceiveMessageAjaxPage extends Page {
|
|||
String post = IOHelper.GetPOST(exchange);
|
||||
if (post.length() == 0) {
|
||||
IOHelper.SendResponse(400, "ERROR: Empty string", exchange);
|
||||
return;
|
||||
}
|
||||
long convid = Long.parseLong(post);
|
||||
LoaderRef<Conversation> currentconversation = convid == -1 ? null : new LoaderRef<>(Conversation.class, convid);
|
||||
|
@ -91,6 +92,7 @@ public class ReceiveMessageAjaxPage extends Page {
|
|||
LogManager.getLogger().debug("Attempting to send channel messages to user: " + user);
|
||||
if (exmap.containsKey(user)) {
|
||||
Document doc = new Document("");
|
||||
if (conv.getMesssageChunks().size() > 0)
|
||||
for (Message msg : conv.getMesssageChunks().get(conv.getMesssageChunks().size() - 1).getMessages())
|
||||
msg.getAsHTML(doc);
|
||||
IOHelper.SendResponse(200, doc.toString(), exmap.get(user));
|
||||
|
|
Loading…
Reference in a new issue