diff options
author | Vitaly Takmazov | 2018-03-28 22:28:33 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-03-28 22:31:43 +0300 |
commit | f6eb11a23ab645b187c5911ec762a3d6ca58140c (patch) | |
tree | 54f79790dc11a4e94487a5fce671f0514c092ccb | |
parent | a41684e44da0021e2f8e653db28f1535e3941eb7 (diff) |
move like API to server
4 files changed, 36 insertions, 39 deletions
diff --git a/juick-server/src/main/java/com/juick/server/api/Post.java b/juick-server/src/main/java/com/juick/server/api/Post.java index e09de900..17426072 100644 --- a/juick-server/src/main/java/com/juick/server/api/Post.java +++ b/juick-server/src/main/java/com/juick/server/api/Post.java @@ -18,8 +18,8 @@ package com.juick.server.api; import com.juick.User; -import com.juick.server.ServerManager; import com.juick.server.EmailManager; +import com.juick.server.ServerManager; import com.juick.server.util.*; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; @@ -285,4 +285,25 @@ public class Post { throw new HttpForbiddenException(); } } + @PostMapping("/like") + @ResponseStatus(value = HttpStatus.OK) + public void doPostRecomm(@RequestParam Integer mid) { + com.juick.User visitor = UserUtils.getCurrentUser(); + if (visitor.getUid() == 0) { + throw new HttpForbiddenException(); + } + com.juick.Message msg = messagesService.getMessage(mid); + if (msg == null) { + throw new HttpNotFoundException(); + } + if (msg.getUser().getUid() == visitor.getUid()) { + throw new HttpForbiddenException(); + } + rocks.xmpp.core.stanza.model.Message xmsg = new rocks.xmpp.core.stanza.model.Message(); + xmsg.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT); + xmsg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "mail")); + xmsg.setTo(Jid.of("juick@juick.com/Juick")); + xmsg.setBody(String.format("! #%d", mid)); + serverManager.sendMessage(xmsg); + } } 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 52504f4a..c9556a98 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 @@ -41,7 +41,6 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.MediaType; import org.springframework.jdbc.core.JdbcTemplate; @@ -465,7 +464,7 @@ public class ServerTests { .param("mid", "1").param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isUnauthorized()); } @Test - public void topTest() throws Exception { + public void topTest() { int topmid = messagesService.createMessage(ugnich.getUid(), "top message", null, null); IntStream.rangeClosed(6, 12).forEach(i -> { messagesService.createReply(topmid, 0, i, "yo", null); @@ -654,4 +653,15 @@ public class ServerTests { Assert.assertThat(count, equalTo(0L)); mockMvc.perform(get("/ws/_all")).andExpect(status().isBadRequest()); } + + @Test + public void likesTests() throws Exception { + int mid = messagesService.createMessage(ugnich.getUid(), "to be liked", null, null); + String freefdHash = userService.getHashByUID(freefd.getUid()); + int freefdMid = messagesService.createMessage(freefd.getUid(), "to be not liked", null, null); + mockMvc.perform(post("/like?mid=" + mid + "&hash=" + freefdHash)) + .andExpect(status().isOk()); + mockMvc.perform(post("/like?mid=" + freefdMid + "&hash=" + freefdHash)) + .andExpect(status().isForbidden()); + } } diff --git a/juick-www/src/main/assets/scripts.js b/juick-www/src/main/assets/scripts.js index 9da9ce3c..57ab9ee4 100644 --- a/juick-www/src/main/assets/scripts.js +++ b/juick-www/src/main/assets/scripts.js @@ -519,7 +519,8 @@ function resultMessage(str) { function likeMessage(e, mid) { if (confirm(i18n('message.likeThisMessage?'))) { - fetch('//juick.com/like?mid=' + mid, { + fetch('//api.juick.com/like?mid=' + mid + + '&hash=' + document.getElementById('body').getAttribute('data-hash'), { method: 'POST', credentials: 'same-origin' }) diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 4192bc11..585a4906 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -289,41 +289,6 @@ public class NewMessage { return "redirect:/" + msg.getUser().getName() + "/" + mid + "#" + ridnew; } - @PostMapping("/like") - @ResponseBody - public Status doPostRecomm(@RequestParam Integer mid) throws IOException { - com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { - throw new HttpForbiddenException(); - } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { - throw new HttpNotFoundException(); - } - if (msg.getUser().getUid() == visitor.getUid()) { - throw new HttpForbiddenException(); - } - - MessagesService.RecommendStatus res = messagesService.recommendMessage(mid, visitor.getUid()); - - if (res == MessagesService.RecommendStatus.Added) { - if (xmpp.isConnected()) { - Message xmsg = new Message(); - xmsg.setFrom(Jid.of("juick@juick.com")); - xmsg.setTo(Jid.of("recomm@s2s.juick.com")); - com.juick.Message jmsg = new com.juick.Message(); - jmsg.setMid(mid); - jmsg.setUser(visitor); - xmsg.addExtension(jmsg); - xmpp.send(xmsg); - } else { - logger.warn("XMPP unavailable"); - } - } else if (res == MessagesService.RecommendStatus.Error) { - throw new HttpBadRequestException(); - } - return Status.OK; - } @PostMapping("/pm/send") public String doPostPM(@RequestParam(name = "uname", required = false) String unameParam, @RequestParam String body) throws IOException { |