Fixed some errors
This commit is contained in:
parent
0f54d6ddb8
commit
24059da654
3 changed files with 92 additions and 2 deletions
|
@ -11,7 +11,7 @@
|
|||
<script>
|
||||
$(document).ready(function () {
|
||||
var cmsgs = document.getElementById("channelmessages");
|
||||
if (cmsgs.elements.length > 0)
|
||||
if (cmsgs != null && cmsgs.childElementCount > 0)
|
||||
cmsgs.lastElementChild.scrollIntoView(false);
|
||||
}); //TODO: Move to an index.js
|
||||
</script>
|
||||
|
|
|
@ -19,8 +19,10 @@ public final class DataManager {
|
|||
public static <T extends SavedData> void save(T object) {
|
||||
try {
|
||||
File file = new File(object.getClass().getName() + "-" + object.getId());
|
||||
while (file.exists())
|
||||
while (file.exists()) {
|
||||
object.setId(object.getId() + 1);
|
||||
file = new File(object.getClass().getName() + "-" + object.getId());
|
||||
}
|
||||
Files.write(Main.gson.toJson(object), file, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
package io.github.norbipeti.chat.server.page;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import io.github.norbipeti.chat.server.data.DataManager;
|
||||
import io.github.norbipeti.chat.server.data.LoaderCollection;
|
||||
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.MessageChunk;
|
||||
import io.github.norbipeti.chat.server.db.domain.User;
|
||||
import io.github.norbipeti.chat.server.io.IOHelper;
|
||||
|
||||
public class ReceiveMessageAjaxPage extends Page {
|
||||
|
||||
@Override
|
||||
public String GetName() { //TODO
|
||||
return "message"; //TODO: Update cookie every once in a while
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePage(HttpExchange exchange) throws IOException {
|
||||
User user = IOHelper.GetLoggedInUser(exchange);
|
||||
if (user == null) {
|
||||
IOHelper.SendResponse(403, "<p>Please log in to send messages</p>", exchange);
|
||||
return; // TODO: Fix sending messages
|
||||
}
|
||||
JsonObject obj = IOHelper.GetPOSTJSON(exchange);
|
||||
if (obj == null) {
|
||||
/*
|
||||
* IOHelper.SendResponse(400,
|
||||
* "<h1>400 Bad request</h1><p>Not a JSON string!</p><p>" +
|
||||
* IOHelper.GetPOST(exchange) + "</p>", exchange);
|
||||
*/
|
||||
IOHelper.SendResponse(400, "JSONERROR", exchange);
|
||||
return;
|
||||
}
|
||||
if (!obj.has("message") || !obj.has("conversation")) {
|
||||
IOHelper.SendResponse(400,
|
||||
"<h1>400 Bad request</h1><p>Message or conversation not found in JSON response.</p><p>"
|
||||
+ IOHelper.GetPOST(exchange) + "</p>",
|
||||
exchange);
|
||||
return;
|
||||
}
|
||||
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 = new MessageChunk(); // TODO: Automatize
|
||||
chunk.setConversation(conv);
|
||||
Message msg = new Message();
|
||||
msg.setSender(user);
|
||||
msg.setMessage(message);
|
||||
msg.setTime(new Date());
|
||||
msg.setMessageChunk(chunk); // TODO: Store relations at one side or
|
||||
// both);
|
||||
chunk.getMessages().add(msg);
|
||||
//DataManager.save(chunk); - TODO
|
||||
conv.getMesssageChunks().add(chunk);
|
||||
DataManager.save(conv);
|
||||
LogManager.getLogger().log(Level.DEBUG,
|
||||
"Added conversation's message count: " + conv.getMesssageChunks().size());
|
||||
|
||||
IOHelper.SendResponse(200, "Success", exchange);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue