aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-10-18 12:45:58 +0300
committerGravatar Vitaly Takmazov2018-10-18 12:45:58 +0300
commita1f9fe5ea28ec28d7cda704d8ef79ca2bd890195 (patch)
tree04475b630931d61d1fff0b9f455ad51bfc09c0fa
parent4c3dbf0fbe903cd9d4b6c54a20958e56e1a77a4d (diff)
last message in pm groups
-rw-r--r--juick-common/src/main/java/com/juick/Chat.java24
-rw-r--r--juick-common/src/main/java/com/juick/User.java1
-rw-r--r--juick-common/src/main/java/com/juick/model/PrivateChats.java8
-rw-r--r--juick-common/src/main/java/com/juick/service/PMQueriesService.java3
-rw-r--r--juick-server/src/main/java/com/juick/server/api/PM.java10
-rw-r--r--juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java18
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java16
7 files changed, 61 insertions, 19 deletions
diff --git a/juick-common/src/main/java/com/juick/Chat.java b/juick-common/src/main/java/com/juick/Chat.java
new file mode 100644
index 00000000..c4f105dc
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/Chat.java
@@ -0,0 +1,24 @@
+package com.juick;
+
+import java.time.Instant;
+
+public class Chat extends User {
+ private Instant lastMessageTimestamp;
+ private String lastMessageText;
+
+ public Instant getLastMessageTimestamp() {
+ return lastMessageTimestamp;
+ }
+
+ public void setLastMessageTimestamp(Instant lastMessageTimestamp) {
+ this.lastMessageTimestamp = lastMessageTimestamp;
+ }
+
+ public String getLastMessageText() {
+ return lastMessageText;
+ }
+
+ public void setLastMessageText(String lastMessageText) {
+ this.lastMessageText = lastMessageText;
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/User.java b/juick-common/src/main/java/com/juick/User.java
index 8228230d..765b5abb 100644
--- a/juick-common/src/main/java/com/juick/User.java
+++ b/juick-common/src/main/java/com/juick/User.java
@@ -26,7 +26,6 @@ import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
-import java.io.Serializable;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
diff --git a/juick-common/src/main/java/com/juick/model/PrivateChats.java b/juick-common/src/main/java/com/juick/model/PrivateChats.java
index ad04e649..b6bb48ab 100644
--- a/juick-common/src/main/java/com/juick/model/PrivateChats.java
+++ b/juick-common/src/main/java/com/juick/model/PrivateChats.java
@@ -18,7 +18,7 @@
package com.juick.model;
import com.fasterxml.jackson.annotation.JsonProperty;
-import com.juick.User;
+import com.juick.Chat;
import java.util.List;
@@ -26,14 +26,14 @@ import java.util.List;
* Created by vt on 24/11/2016.
*/
public class PrivateChats {
- private List<User> users;
+ private List<Chat> users;
@JsonProperty("pms")
- public List<User> getUsers() {
+ public List<Chat> getUsers() {
return users;
}
- public void setUsers(List<User> users) {
+ public void setUsers(List<Chat> users) {
this.users = users;
}
}
diff --git a/juick-common/src/main/java/com/juick/service/PMQueriesService.java b/juick-common/src/main/java/com/juick/service/PMQueriesService.java
index 4c70eece..d9e1f18f 100644
--- a/juick-common/src/main/java/com/juick/service/PMQueriesService.java
+++ b/juick-common/src/main/java/com/juick/service/PMQueriesService.java
@@ -17,6 +17,7 @@
package com.juick.service;
+import com.juick.Chat;
import com.juick.User;
import java.util.List;
@@ -35,7 +36,7 @@ public interface PMQueriesService {
String getLastView(int uidFrom, int uidTo);
- List<User> getPMLastConversationsUsers(int uid, int cnt);
+ List<Chat> getLastChats(User user);
List<com.juick.Message> getPMMessages(int uid, int uidTo);
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<User> lastconv = pmQueriesService.getPMLastConversationsUsers(visitor.getUid(), cnt);
+ List<Chat> 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<User> getPMLastConversationsUsers(final int uid, final int cnt) {
+ public List<Chat> 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));
+ }
}