aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-10-02 00:24:57 +0300
committerGravatar Vitaly Takmazov2019-10-02 00:24:57 +0300
commitae550feea0394bfff147fa1946cfa8c357eacb8a (patch)
treefe352d7702e921afa247ef0c5f59d7fde1d0b7f9 /src
parent0ad4a26ab6b78b8bb3123faed6fcfaff807f49f3 (diff)
Fill Message::unread in single query
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/juick/server/www/controllers/MessagesWWW.java3
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java11
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java1
3 files changed, 8 insertions, 7 deletions
diff --git a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
index 501c0d82..d845aac6 100644
--- a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
+++ b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
@@ -168,7 +168,6 @@ public class MessagesWWW {
msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList()));
msgs.forEach(m -> {
m.ReadOnly |= blUIDs.contains(m.getUser().getUid());
- m.setUnread(unread.contains(m.getMid()));
});
}
model.addAttribute("msgs", msgs);
@@ -287,7 +286,6 @@ public class MessagesWWW {
msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList()));
msgs.forEach(m -> {
m.ReadOnly |= blUIDs.contains(m.getUser().getUid());
- m.setUnread(unread.contains(m.getMid()));
});
}
model.addAttribute("msgs", msgs);
@@ -428,7 +426,6 @@ public class MessagesWWW {
);
msgs.forEach(m -> {
m.ReadOnly |= blUIDs.contains(m.getUser().getUid());
- m.setUnread(unread.contains(m.getMid()));
});
fillUserModel(model, visitor, visitor);
}
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java
index 95d5d1bf..a190a112 100644
--- a/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -116,6 +116,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
msg.setUpdatedAt(rs.getTimestamp(21).toInstant());
msg.setReplyUri(URI.create(Optional.ofNullable(rs.getString(24)).orElse(StringUtils.EMPTY)));
msg.setHtml(rs.getBoolean(25));
+ msg.setUnread(rs.getBoolean(26));
if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
try {
imagesService.setAttachmentMetadata(baseImagesUrl, msg);
@@ -380,7 +381,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "messages.readonly, messages.privacy, messages.replies,"
+ "messages.attach, COUNT(DISTINCT favorites.user_id) as likes, messages.hidden,"
+ "GROUP_CONCAT(tags.name SEPARATOR ' '), txt.repliesby, txt.txt, '' as q, messages.updated as updated, 0 as to_uid, "
- + "NULL as to_name, txt.updated_at, '' as reply_user_uri, '' as to_uri, '' as reply_uri, 0 as html FROM messages "
+ + "NULL as to_name, txt.updated_at, '' as reply_user_uri, '' as to_uri, '' as reply_uri, 0 as html, 0 as unread FROM messages "
+ "INNER JOIN users ON messages.user_id = users.id "
+ "INNER JOIN messages_txt AS txt "
+ "ON messages.message_id = txt.message_id "
@@ -390,7 +391,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "LEFT JOIN tags ON tags.tag_id=messages_tags.tag_id "
+ "WHERE messages.message_id = ? AND users.banned = 0 "
+ "GROUP BY mid, rid, replyto, uid, nick, banned, messages.ts, readonly, "
- + "privacy, replies, attach, repliesby, q, updated_at, reply_user_uri, to_uri, reply_uri, html",
+ + "privacy, replies, attach, repliesby, q, updated_at, reply_user_uri, to_uri, reply_uri, html, unread",
new MessageMapper(),
mid);
if (!list.isEmpty()) {
@@ -895,10 +896,12 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "messages.attach,COUNT(DISTINCT favorites.user_id) AS likes,messages.hidden,"
+ "GROUP_CONCAT(tags.name SEPARATOR ' '), messages_txt.repliesby, messages_txt.txt, '' as q, "
+ "messages.updated, 0 as to_uid, NULL as to_name, messages_txt.updated_at, '' as m_user_uri, "
- + "'' as to_uri, '' as msg_reply_uri, 0 as html "
+ + "'' as to_uri, '' as msg_reply_uri, 0 as html, messages.replies-subscr_messages.last_read_rid>0 as unread "
+ "FROM (messages INNER JOIN messages_txt "
+ "ON messages.message_id=messages_txt.message_id) "
+ "INNER JOIN users ON messages.user_id=users.id "
+ + "LEFT JOIN subscr_messages "
+ + "ON messages.message_id=subscr_messages.message_id AND subscr_messages.suser_id=:uid "
+ "LEFT JOIN favorites "
+ "ON messages.message_id = favorites.message_id AND favorites.like_id=1 "
+ "LEFT JOIN banned "
@@ -988,7 +991,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
"NOW(), " +
"COALESCE(qw.user_id, m.user_id) as to_uid, COALESCE(qu.nick, mu.nick) as to_name, " +
"replies.updated_at, replies.user_uri as uri, " +
- "qw.user_uri as to_uri, replies.reply_uri, replies.html " +
+ "qw.user_uri as to_uri, replies.reply_uri, replies.html, 0 as unread " +
"FROM replies LEFT JOIN users " +
"ON replies.user_id = users.id " +
"LEFT JOIN replies qw ON replies.message_id = qw.message_id and replies.replyto = qw.reply_id " +
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 51be7092..d896565c 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -1000,6 +1000,7 @@ public class ServerTests {
assertThat(lastRead.apply(ugnich, mid), is(0));
assertThat(messagesService.getUnread(ugnich).size(), is(1));
assertThat(messagesService.getUnread(ugnich).get(0), is(mid));
+ assertThat(messagesService.getMessages(ugnich, Collections.singletonList(mid)).get(0).isUnread(), is(true));
messagesService.getReplies(ugnich, mid);
assertThat(lastRead.apply(ugnich, mid), is(1));
assertThat(messagesService.getUnread(ugnich).size(), is(0));