aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-04-11 22:52:59 +0300
committerGravatar Vitaly Takmazov2018-04-11 22:52:59 +0300
commit495ee60b34991572c06732674c6e02f290cdbd55 (patch)
tree71b62ed5038f8021bf9086cd4819d66d67a18389
parent336517508441ecb9a78418065f5a6c068d15f3cb (diff)
MessagesService::getUnread
-rw-r--r--juick-common/src/main/java/com/juick/service/MessagesService.java2
-rw-r--r--juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java10
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java16
3 files changed, 28 insertions, 0 deletions
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<Integer> getPopularCandidates();
void setLastReadComment(User user, Integer mid, Integer rid);
+
+ List<Integer> 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<Integer> 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<User, Integer, Integer> 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));
+ }
}