From a1f9fe5ea28ec28d7cda704d8ef79ca2bd890195 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 18 Oct 2018 12:45:58 +0300 Subject: last message in pm groups --- .../src/main/java/com/juick/server/api/PM.java | 10 +++------- .../java/com/juick/service/PMQueriesServiceImpl.java | 18 ++++++++++++------ .../test/java/com/juick/server/tests/ServerTests.java | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) (limited to 'juick-server/src') diff --git a/juick-server/src/main/java/com/juick/server/api/PM.java b/juick-server/src/main/java/com/juick/server/api/PM.java index 80be92f0..0c36fe00 100644 --- a/juick-server/src/main/java/com/juick/server/api/PM.java +++ b/juick-server/src/main/java/com/juick/server/api/PM.java @@ -17,6 +17,7 @@ package com.juick.server.api; +import com.juick.Chat; import com.juick.User; import com.juick.service.component.MessageEvent; import com.juick.model.AnonymousUser; @@ -105,14 +106,9 @@ public class PM { if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } - if (cnt < 3) { - cnt = 3; - } - if (cnt > 10) { - cnt = 10; - } + // TODO: ignore cnt param for now but make sure paging param will not be cnt - List lastconv = pmQueriesService.getPMLastConversationsUsers(visitor.getUid(), cnt); + List lastconv = pmQueriesService.getLastChats(visitor); PrivateChats pms = new PrivateChats(); pms.setUsers(lastconv); return pms; diff --git a/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java index b68e0236..ee49755d 100644 --- a/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java @@ -17,6 +17,7 @@ package com.juick.service; +import com.juick.Chat; import com.juick.User; import org.springframework.dao.DuplicateKeyException; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; @@ -89,20 +90,25 @@ public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesSe @Transactional(readOnly = true) @Override - public List getPMLastConversationsUsers(final int uid, final int cnt) { + public List getLastChats(final User user) { return getJdbcTemplate().query( - "SELECT pm_streams.user_id, users.nick, pm_streams.unread FROM pm_streams " + "SELECT pm_streams.user_id, users.nick, l.last, pm.txt, pm_streams.unread FROM pm_streams " + "INNER JOIN users ON users.id = pm_streams.user_id " + + "INNER JOIN pm ON pm.user_id = pm_streams.user_id " + + "INNER JOIN (SELECT user_id, MAX(ts) AS last FROM pm " + + "WHERE user_id_to=? GROUP BY user_id) l ON l.last = pm.ts " + "WHERE pm_streams.user_id_to=? " - + "ORDER BY pm_streams.unread DESC, pm_streams.lastmessage DESC LIMIT ?", + + "ORDER BY pm_streams.unread DESC, l.last DESC", (rs, rowNum) -> { - com.juick.User u = new com.juick.User(); + com.juick.Chat u = new com.juick.Chat(); u.setUid(rs.getInt(1)); u.setName(rs.getString(2)); - u.setUnreadCount(rs.getInt(3)); + u.setLastMessageTimestamp(rs.getTimestamp(3).toInstant()); + u.setLastMessageText(rs.getString(4)); + u.setUnreadCount(rs.getInt(5)); return u; }, - uid, cnt); + user.getUid(), user.getUid()); } @Transactional 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 4d93470d..2d55b047 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 @@ -29,6 +29,7 @@ import com.jayway.jsonpath.JsonPath; import com.juick.*; import com.juick.model.AnonymousUser; import com.juick.model.CommandResult; +import com.juick.model.PrivateChats; import com.juick.model.TagStats; import com.juick.server.*; import com.juick.server.api.activity.model.Context; @@ -1730,4 +1731,19 @@ public class ServerTests { .andExpect(status().isOk()).andReturn(); String xrd = result.getResponse().getContentAsString(); } + @Test + public void pms() throws Exception { + jdbcTemplate.execute("DELETE FROM pm"); + jdbcTemplate.execute("DELETE FROM pm_streams"); + CommandResult res = commandsManager.processCommand(ugnich, "@freefd DICK", emptyUri); + assertThat(res.getNewMessage(), is(Optional.empty())); + assertThat(res.getText(), is("Private message sent")); + MvcResult result = mockMvc.perform(get("/api/groups_pms") + .with(httpBasic(freefdName, freefdPassword))) + .andExpect(status().isOk()) + .andReturn(); + PrivateChats chats = jsonMapper.readValue(result.getResponse().getContentAsString(), PrivateChats.class); + assertThat(chats.getUsers().size(), is(1)); + assertThat(chats.getUsers().get(0).getUnreadCount(), is(1)); + } } -- cgit v1.2.3