From 495ee60b34991572c06732674c6e02f290cdbd55 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 11 Apr 2018 22:52:59 +0300 Subject: MessagesService::getUnread --- .../src/main/java/com/juick/service/MessagesService.java | 2 ++ .../main/java/com/juick/service/MessagesServiceImpl.java | 10 ++++++++++ .../test/java/com/juick/server/tests/ServerTests.java | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/juick-common/src/main/java/com/juick/service/MessagesService.java b/juick-common/src/main/java/com/juick/service/MessagesService.java index 968e64be..34c15271 100644 --- a/juick-common/src/main/java/com/juick/service/MessagesService.java +++ b/juick-common/src/main/java/com/juick/service/MessagesService.java @@ -112,4 +112,6 @@ public interface MessagesService { List getPopularCandidates(); void setLastReadComment(User user, Integer mid, Integer rid); + + List getUnread(User user); } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java index 5370295a..1401cfce 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -930,4 +930,14 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ jdbcTemplate.update("UPDATE subscr_messages SET last_read_rid=? WHERE message_id=? AND suser_id=?", rid, mid, user.getUid()); } + + @Override + public List getUnread(User user) { + return jdbcTemplate.queryForList( + "select subscr_messages.message_id " + + "from subscr_messages inner join messages on subscr_messages.message_id=messages.message_id " + + "where subscr_messages.suser_id=? and " + + "messages.replies>subscr_messages.last_read_rid", + Integer.class, user.getUid()); + } } diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index 095566ec..a7701511 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -66,6 +66,7 @@ import java.nio.file.Paths; import java.sql.Timestamp; import java.time.Instant; import java.util.*; +import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.IntStream; @@ -747,4 +748,19 @@ public class ServerTests { jdbcTemplate.update("INSERT INTO telegram_chats(chat_id) VALUES(1)"); telegramBotManager.processUpdate("{\"message\":{\"chat\":{\"id\":1},\"text\":\"yo\",\"from\":{\"id\":1,\"username\":\"teleugnich\"}}}"); } + @Test + public void lastReadTests() { + int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, null); + subscriptionService.subscribeMessage(mid, ugnich.getUid()); + messagesService.createReply(mid, 0, freefd, "new reply", null); + BiFunction lastRead = (user, m) -> jdbcTemplate.queryForObject( + "SELECT last_read_rid FROM subscr_messages WHERE suser_id=? AND message_id=?", + Integer.class, user.getUid(), m); + assertThat(lastRead.apply(ugnich, mid), is(0)); + assertThat(messagesService.getUnread(ugnich).size(), is(1)); + assertThat(messagesService.getUnread(ugnich).get(0), is(mid)); + messagesService.getReplies(ugnich, mid); + assertThat(lastRead.apply(ugnich, mid), is(1)); + assertThat(messagesService.getUnread(ugnich).size(), is(0)); + } } -- cgit v1.2.3