Message listener in progress (along with a refactoring)
This commit is contained in:
parent
7565c4fc96
commit
bbc8121968
13 changed files with 78 additions and 45 deletions
|
@ -5,7 +5,8 @@
|
|||
<script src="js/jquery-3.1.0.js"></script>
|
||||
<script src="js/moment-with-locales.js" charset="UTF-8"></script>
|
||||
<script src="js/utils.js"></script>
|
||||
<script src="js/message.js"></script>
|
||||
<script src="js/sendmessage.js"></script>
|
||||
<script src="js/receivemessage.js"></script>
|
||||
<script src="js/login.js"></script>
|
||||
<script src="js/register.js"></script>
|
||||
<link rel="stylesheet" href="css/style.css"/>
|
||||
|
|
|
@ -18,7 +18,6 @@ $(document).ready(function () {
|
|||
console.log(ctime.innerText);
|
||||
if (ctime != null)
|
||||
ctime.innerText = moment(ctime.innerText, "YYYY-MM-DDTHH:mm:ssZ").fromNow(); //.format("lll");
|
||||
//ctime.innerText = new Date(ctime.innerText * 1).toDateString();
|
||||
}
|
||||
cmsgs.lastElementChild.scrollIntoView(false);
|
||||
}
|
||||
|
|
7
pages/js/receivemessage.js
Normal file
7
pages/js/receivemessage.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
(function poll() {
|
||||
setTimeout(function() {
|
||||
$.ajax({ url: "/receivemessage", success: function(data) {
|
||||
|
||||
}, dataType: "json", complete: poll });
|
||||
}, 100);
|
||||
})();
|
|
@ -15,9 +15,7 @@ var respfunc = function respfunc(result) {
|
|||
sendmsg(msginput);
|
||||
}
|
||||
else {
|
||||
var errormsg = document.getElementById("errormsg");
|
||||
errormsg.innerHTML = result.responseText;
|
||||
errormsg.style = "display: block"; //TODO: Hide errormsg after a while (index.js)
|
||||
showError.
|
||||
msginput.disabled = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,20 @@
|
|||
/**
|
||||
* Created by Norbi on 2016-07-27.
|
||||
*/
|
||||
function getFormData($form) {
|
||||
var unindexed_array = $form.serializeArray();
|
||||
var indexed_array = {};
|
||||
|
||||
$.map(unindexed_array, function (n, i) {
|
||||
indexed_array[n['name']] = n['value'];
|
||||
});
|
||||
|
||||
return indexed_array;
|
||||
}
|
||||
/**
|
||||
* Created by Norbi on 2016-07-27.
|
||||
*/
|
||||
function getFormData($form) {
|
||||
var unindexed_array = $form.serializeArray();
|
||||
var indexed_array = {};
|
||||
|
||||
$.map(unindexed_array, function (n, i) {
|
||||
indexed_array[n['name']] = n['value'];
|
||||
});
|
||||
|
||||
return indexed_array;
|
||||
}
|
||||
|
||||
function showError(message) {
|
||||
var errormsg = document.getElementById("errormsg");
|
||||
errormsg.innerHTML = result.responseText;
|
||||
errormsg.style = "display: block";
|
||||
setTimeout(function(){errormsg.style.display="none";}, 2000);
|
||||
}
|
|
@ -8,11 +8,10 @@ import io.github.norbipeti.chat.server.data.LoaderCollection;
|
|||
@Table(name = "CONVERSATION")
|
||||
public class Conversation extends SavedData {
|
||||
private static final long serialVersionUID = 5058682475353799722L;
|
||||
private static Long nextid = 0L;
|
||||
// @Id
|
||||
// @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
// @Column(name = "ID", unique = true, nullable = false)
|
||||
private Long id = nextid++;
|
||||
private Long id;
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "conversation")
|
||||
private LoaderCollection<MessageChunk> messsagechunks = new LoaderCollection<>(MessageChunk.class);
|
||||
|
@ -53,4 +52,8 @@ public class Conversation extends SavedData {
|
|||
|
||||
private Conversation() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,11 +10,13 @@ import io.github.norbipeti.chat.server.data.LoaderRef;
|
|||
@Entity
|
||||
@Table(name = "MESSAGE")
|
||||
public class Message implements Serializable {
|
||||
private static final int MESSAGE_LIMIT_PER_CHUNK = 50;
|
||||
private static final long serialVersionUID = 6345941601716826570L;
|
||||
private static Long nextid = 0L;
|
||||
// @Id
|
||||
// @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
// @Column(name = "ID", unique = true, nullable = false)
|
||||
// private Long id;
|
||||
private Long id = nextid++;
|
||||
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
|
||||
// @JoinTable(name="user_message")
|
||||
private LoaderRef<User> sender;
|
||||
|
@ -64,7 +66,28 @@ public class Message implements Serializable {
|
|||
this.messagechunk = new LoaderRef<MessageChunk>(messagechunk);
|
||||
}
|
||||
|
||||
/*
|
||||
* public Long getId() { return id; } public void setId(Long id) { this.id = id; }
|
||||
*/
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
private Message() {
|
||||
}
|
||||
|
||||
public static Message create(Conversation parent) {
|
||||
Message msg = new Message();
|
||||
int size = parent.getMesssageChunks().size();
|
||||
MessageChunk chunk;
|
||||
if (size == 0 || parent.getMesssageChunks().get(size - 1).getMessages().size() >= MESSAGE_LIMIT_PER_CHUNK) {
|
||||
chunk = SavedData.create(MessageChunk.class);
|
||||
chunk.setConversation(parent);
|
||||
parent.getMesssageChunks().add(chunk);
|
||||
} else
|
||||
chunk = parent.getMesssageChunks().get(size - 1);
|
||||
msg.setMessageChunk(chunk);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,11 +7,11 @@ import io.github.norbipeti.chat.server.data.LoaderRef;
|
|||
|
||||
public class MessageChunk extends SavedData {
|
||||
private static final long serialVersionUID = -1665300779209348467L;
|
||||
private static Long nextid = 0L;
|
||||
private Long id = nextid++;
|
||||
|
||||
|
||||
private Long id;
|
||||
private List<Message> messages = new ArrayList<>();
|
||||
private LoaderRef<Conversation> conversation;
|
||||
private Long nextmsgid = 0L;
|
||||
|
||||
public List<Message> getMessages() {
|
||||
return messages;
|
||||
|
|
|
@ -11,10 +11,12 @@ public abstract class SavedData implements Serializable {
|
|||
|
||||
public abstract void setId(long id);
|
||||
|
||||
protected abstract void init();
|
||||
|
||||
protected SavedData() {
|
||||
}
|
||||
|
||||
public static <T extends SavedData> T create(Class<T> cl) {
|
||||
static <T extends SavedData> T create(Class<T> cl) {
|
||||
T obj;
|
||||
try {
|
||||
Constructor<T> constructor = cl.getDeclaredConstructor(new Class<?>[0]);
|
||||
|
@ -27,6 +29,7 @@ public abstract class SavedData implements Serializable {
|
|||
}
|
||||
obj.setId(DataManager.getNextIDs().getOrDefault(obj.getClass(), 0L));
|
||||
DataManager.setNextID(obj.getClass(), obj.getId() + 1);
|
||||
obj.init();
|
||||
DataManager.save(obj);
|
||||
return obj;
|
||||
}
|
||||
|
|
|
@ -11,8 +11,7 @@ import io.github.norbipeti.chat.server.data.LoaderCollection;
|
|||
@Table(name = "\"USER\"")
|
||||
public class User extends SavedData {
|
||||
private static final long serialVersionUID = 2862762084164225666L;
|
||||
private static Long nextid = 0L;
|
||||
private Long id = nextid++;
|
||||
private Long id;
|
||||
private String name;
|
||||
private String email;
|
||||
private String password;
|
||||
|
@ -109,4 +108,8 @@ public class User extends SavedData {
|
|||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static User createUser() {
|
||||
return SavedData.create(User.class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ public class IOHelper {
|
|||
}
|
||||
|
||||
public static String ReadFile(File file) throws FileNotFoundException, IOException {
|
||||
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); // TODO: FIx UTF-8 file reading
|
||||
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
|
||||
return content;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public class IndexPage extends Page {
|
|||
if (user.getConversations().size() == 0) {
|
||||
LoaderCollection<Conversation> convs = DataManager.getAll(Conversation.class);
|
||||
if (convs.size() == 0) {
|
||||
Conversation c = SavedData.create(Conversation.class);
|
||||
Conversation c = Conversation.create();
|
||||
convs.add(c); // TODO: Handle no conversation open
|
||||
}
|
||||
user.getConversations().add(convs.get(0));
|
||||
|
|
|
@ -27,21 +27,10 @@ public class ReceiveMessageAjaxPage extends Page {
|
|||
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;
|
||||
}
|
||||
JsonObject obj = IOHelper.GetPOSTJSON(exchange);
|
||||
if (obj == null) {
|
||||
IOHelper.SendResponse(400, "JSONERROR: " + IOHelper.GetPOST(exchange), 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);
|
||||
IOHelper.SendResponse(403, "<p>Please log in to receive messages</p>", exchange);
|
||||
return;
|
||||
}
|
||||
JsonObject obj = new JsonObject();
|
||||
String message = obj.get("message").getAsString().trim();
|
||||
int conversation = obj.get("conversation").getAsInt();
|
||||
if (message.trim().length() == 0) {
|
||||
|
|
Loading…
Reference in a new issue