Added check for empty messages

This commit is contained in:
Norbi Peti 2016-08-01 12:16:54 +02:00
parent d64b615020
commit 3efa16fe0a
9 changed files with 51 additions and 17 deletions

View file

@ -10,7 +10,7 @@
</File>
</Appenders>
<Loggers>
<Root level="info">
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile" />
</Root>

View file

@ -6,6 +6,7 @@
<script src="js/message.js"></script>
<script src="js/login.js"></script>
<script src="js/register.js"></script>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
@ -81,6 +82,6 @@
</div>
<div id="usercontent">
<div id="channelmessages"></div>
<textarea id="msginput"></textarea>
<textarea id="msginput" autofocus="autofocus"></textarea>
</div>
</body>

View file

@ -10,10 +10,14 @@ var respfunc = function respfunc(result) {
var sendmsgonenter = function sendmsgonenter(e) { //TODO: Detect Enter
var code = e.keyCode || e.which;
if (code != 13) { //Enter keycode
if (code != 13 || e.shiftKey) { //Enter keycode
return;
}
e.preventDefault();
var textarea = event.target;
if (textarea.value.trim().length == 0)
return;
textarea.disabled = true;
window.convid = 1;
var json = JSON.stringify({"message": textarea.value, "conversation": window.convid});
$.ajax({
@ -22,7 +26,5 @@ var sendmsgonenter = function sendmsgonenter(e) { //TODO: Detect Enter
};
$(document).ready(function () {
$('#msginput').on("keypress", sendmsgonenter);
$('#msginput').on("keydown", sendmsgonenter);
});
$('#msginput').on("keypress", sendmsgonenter);

View file

@ -1,5 +1,7 @@
package io.github.norbipeti.chat.server;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -29,7 +31,15 @@ import io.github.norbipeti.chat.server.page.Page;
public class IOHelper {
public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(code, content.length());
IOUtils.write(content, exchange.getResponseBody(), StandardCharsets.UTF_8);
try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) {
try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes())) {
byte[] buffer = new byte[512];
int count;
while ((count = bis.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
}
}
exchange.getResponseBody().close();
}

View file

@ -50,10 +50,10 @@ public class Main {
LogManager.getLogger().log(Level.DEBUG, "1st's contact: " + user.getContacts().get(0));
LogManager.getLogger().log(Level.DEBUG, "2nd's contact: " + user2.getContacts().get(0));
Conversation convo = new Conversation();
convo.getUsers().add(user);
// user.getConversations().add(convo);
convo.getUsers().add(user2);
// user2.getConversations().add(convo); - TODO: Fix duplicate
//convo.getUsers().add(user);
//user.getConversations().add(convo);
//convo.getUsers().add(user2);
//user2.getConversations().add(convo); //TODO: Fix duplicate
// key constraint
Message msg = new Message();
msg.setSender(user);

View file

@ -17,7 +17,12 @@ public class Conversation extends ChatDatabaseEntity {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "conversation")
private List<Message> messsages;
@ElementCollection(fetch = FetchType.EAGER)
@ManyToMany(cascade = CascadeType.ALL)
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
// @JoinTable(name = "User_Conversation", joinColumns = @JoinColumn(name =
// "conversation_id", referencedColumnName = "id", unique = false),
// inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName =
// "id", unique = false), uniqueConstraints = @UniqueConstraint(name =
// "USER_CONV_UN", columnNames = {"user_id", "conversation_id" }))
@JoinTable(name = "User_Conversation", joinColumns = @JoinColumn(name = "conversation_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Set<User> users;

View file

@ -10,12 +10,12 @@ public class Message extends ChatDatabaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@ManyToOne(cascade=CascadeType.ALL)
@ManyToOne
//@JoinTable(name="user_message")
private User sender;
private Date time;
private String message;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@ManyToOne(fetch = FetchType.EAGER)
//@JoinTable(name="conversation_message")
private Conversation conversation;

View file

@ -25,7 +25,9 @@ public class User extends ChatDatabaseEntity {
private String sessionid;
@ElementCollection(fetch = FetchType.EAGER)
// @ManyToMany(fetch = FetchType.EAGER, mappedBy = "users")
@ManyToMany(mappedBy = "users", fetch = FetchType.EAGER)
@ManyToMany(mappedBy = "users", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
// @ManyToMany(mappedBy = "users")
//@JoinTable(name = "User_Conversation", joinColumns = @JoinColumn(referencedColumnName = "id", unique = false), inverseJoinColumns = @JoinColumn(referencedColumnName = "id", unique = false))
private Set<Conversation> conversations;
public List<User> getContacts() {

View file

@ -35,11 +35,24 @@ public class MessageAjaxPage extends Page {
}
JSONObject obj = IOHelper.GetPOSTJSON(exchange);
if (obj == null) {
IOHelper.SendResponse(400, "<h1>400 Bad request</h1><p>Not a JSON string!</p>", exchange);
IOHelper.SendResponse(400,
"<h1>400 Bad request</h1><p>Not a JSON string!</p><p>" + IOHelper.GetPOST(exchange) + "</p>",
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.getString("message");
int conversation = obj.getInt("conversation");
if (message.trim().length() == 0) {
IOHelper.SendResponse(400, "<h1>400 Bad request</h1><p>The message cannot be empty,</p>", exchange);
return;
}
Set<Conversation> convos = user.getConversations();
Conversation conv = null;
LogManager.getLogger().log(Level.DEBUG, "Len: " + convos.size());
@ -64,7 +77,8 @@ public class MessageAjaxPage extends Page {
provider.save(msg);
conv.getMesssages().add(msg);
provider.save(conv);
LogManager.getLogger().log(Level.DEBUG, "Added conversation's message count: " + conv.getMesssages().size());
LogManager.getLogger().log(Level.DEBUG,
"Added conversation's message count: " + conv.getMesssages().size());
} catch (Exception e) {
throw e;
}