Fix chat history sql query

This commit is contained in:
Arnaud Vergnet 2021-01-06 09:05:52 +01:00
parent 63cb83916a
commit 46cf6f40aa
3 changed files with 60 additions and 42 deletions

View file

@ -1,6 +1,7 @@
package fr.insa.clavardator.chat;
import fr.insa.clavardator.db.DatabaseController;
import fr.insa.clavardator.users.CurrentUser;
import fr.insa.clavardator.users.PeerUser;
import fr.insa.clavardator.users.UserInformation;
import fr.insa.clavardator.util.ErrorCallback;
@ -50,7 +51,12 @@ public class ChatHistory {
final Date from = new Date();
// Load whole history
from.setTime(0);
db.getChatHistory(new UserInformation(user), from, new Date(), this::onLoaded, errorCallback);
db.getChatHistory(new UserInformation(user),
new UserInformation(CurrentUser.getInstance()),
from,
new Date(),
this::onLoaded,
errorCallback);
} else {
notifyHistoryLoaded();
}

View file

@ -2,7 +2,6 @@ package fr.insa.clavardator.db;
import fr.insa.clavardator.chat.FileMessage;
import fr.insa.clavardator.chat.Message;
import fr.insa.clavardator.users.CurrentUser;
import fr.insa.clavardator.users.User;
import fr.insa.clavardator.users.UserInformation;
import fr.insa.clavardator.util.ErrorCallback;
@ -314,21 +313,24 @@ public class DatabaseController {
/**
* Gets the chat history for a given time frame
*
* @param user the user for which to retrieve the history
* @param user1 the user for which to retrieve the history
* @param user2 the user for which to retrieve the history
* @param from the starting date
* @param to the ending date
* @param callback Function called when the request is done
* @param errorCallback The function to call on error
*/
public void getChatHistory(UserInformation user, Date from, Date to, HistoryCallback callback, ErrorCallback errorCallback) {
public void getChatHistory(UserInformation user1, UserInformation user2, Date from, Date to, HistoryCallback callback, ErrorCallback errorCallback) {
// TODO update to search in current_user table as well
@Language("SQL") String sql =
"SELECT timestamp, s.id AS sender_id, s.username AS sender_username, " +
"r.id AS recipient_id, r.username AS recipient_username, text, file_path " +
"FROM message JOIN user AS s ON sender = s.id " +
" JOIN user AS r ON recipient = r.id " +
"WHERE (s.id = '" + user.id + "' OR r.id = '" + user.id + "') AND " +
"timestamp > " + from.getTime() + " AND timestamp < " + to.getTime() + " " +
"SELECT timestamp," +
" sender," +
" recipient," +
" text," +
" file_path " +
"FROM message WHERE (sender = '" + user1.id + "'" +
" OR recipient = '" + user1.id + "')" +
" AND timestamp > " + from.getTime() + " AND timestamp < " + to.getTime() + " " +
"ORDER BY timestamp";
System.out.println(sql);
@ -336,15 +338,23 @@ public class DatabaseController {
QueryExecutor executor = new QueryExecutor(sql, res -> {
ArrayList<Message> chatHistory = new ArrayList<>();
while (res.next()) {
String sId = res.getString("sender_id");
String sUsername = res.getString("sender_username");
String rId = res.getString("recipient_id");
String rUsername = res.getString("recipient_username");
Date date = new Date(res.getTimestamp("timestamp").getTime());
String text = res.getString("text");
String filePath = res.getString("file_path");
String sId = res.getString("sender");
final UserInformation sender;
final UserInformation recipient;
if (user1.id.equals(sId)) {
sender = user1;
recipient = user2;
} else {
sender = user2;
recipient = user1;
}
if (filePath == null) {
chatHistory.add(new Message(new UserInformation(sId, sUsername), new UserInformation(rId, rUsername), date, text));
chatHistory.add(new Message(sender, recipient, date, text));
} else {
// TODO
// chatHistory.add(new FileMessage(new UserInformation(sId, sUsername), new UserInformation(rId, rUsername), date, text, filePath));
@ -369,6 +379,31 @@ public class DatabaseController {
executor.start();
}
public interface UsersCallback {
void onUsersFetched(ArrayList<User> users);
}
public interface CurrentUserCallback {
void onFetched(UserInformation user);
}
public interface UsernameCallback {
void onUsernameFetched(String username);
}
public interface HistoryCallback {
void onHistoryFetched(ArrayList<Message> history);
}
public interface UpdateCallback {
void onUpdateExecuted();
}
private interface QueryCallback {
void onQueryExecuted(ResultSet resultSet) throws SQLException;
}
private class UpdateExecutor extends Thread {
private final String sqlQuery;
private final UpdateCallback callback;
@ -438,29 +473,4 @@ public class DatabaseController {
}
}
}
public interface UsersCallback {
void onUsersFetched(ArrayList<User> users);
}
public interface CurrentUserCallback {
void onFetched(UserInformation user);
}
public interface UsernameCallback {
void onUsernameFetched(String username);
}
public interface HistoryCallback {
void onHistoryFetched(ArrayList<Message> history);
}
public interface UpdateCallback {
void onUpdateExecuted();
}
private interface QueryCallback {
void onQueryExecuted(ResultSet resultSet) throws SQLException;
}
}

View file

@ -28,7 +28,8 @@ public class DatabaseTest {
latch2.countDown();
}, Assertions::fail);
db.getChatHistory(new UserInformation("1", "Yohan"), new Date(0), new Date(), history -> {
db.getChatHistory(new UserInformation("1", "Yohan"),
new UserInformation("0", "Arnaud"), new Date(0), new Date(), history -> {
assertEquals(history.size(), 0);
latch2.countDown();
}, Assertions::fail);
@ -64,7 +65,8 @@ public class DatabaseTest {
latch4.countDown();
}, Assertions::fail);
db.getChatHistory(new UserInformation("1", "Yohan"), new Date(0), new Date(), history -> {
db.getChatHistory(new UserInformation("1", "Yohan"),
new UserInformation("2", "Arnaud"), new Date(0), new Date(), history -> {
assertEquals(5, history.size());
assertEquals("Coucou Arnaud !", history.get(0).getText());
assertEquals("1", history.get(0).getSender().id);