aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src')
-rw-r--r--juick-server/src/main/java/com/juick/server/TelegramBotManager.java4
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Messages.java1
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Post.java27
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java84
-rw-r--r--juick-server/src/test/resources/data.sql7
5 files changed, 111 insertions, 12 deletions
diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
index 5bbfec91..31496407 100644
--- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
@@ -358,8 +358,8 @@ public class TelegramBotManager implements NotificationListener {
.forEach(c -> telegramNotify(c, String.format("%s recommends your [post](%s)",
MessageUtils.getMarkdownUser(liker), formatUrl(message)),null));
telegramService.getTelegramIdentifiers(subscribers)
- .forEach(c -> telegramNotify(c, String.format("%s recommends you someone's [post](%s)",
- MessageUtils.getMarkdownUser(liker), formatUrl(message)), null));
+ .forEach(c -> telegramNotify(c, String.format("%s recommends you someone's [post](%s)",
+ MessageUtils.getMarkdownUser(liker), formatUrl(message)), null));
}
@Override
diff --git a/juick-server/src/main/java/com/juick/server/api/Messages.java b/juick-server/src/main/java/com/juick/server/api/Messages.java
index 8142a179..c6600e2b 100644
--- a/juick-server/src/main/java/com/juick/server/api/Messages.java
+++ b/juick-server/src/main/java/com/juick/server/api/Messages.java
@@ -90,6 +90,7 @@ public class Messages {
@RequestParam(required = false) String popular,
@RequestParam(required = false) String media,
@RequestParam(required = false) String tag) {
+
User visitor = UserUtils.getCurrentUser();
int vuid = visitor.getUid();
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 9dd42db0..5a027ece 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
@@ -22,8 +22,8 @@ import com.juick.User;
import com.juick.server.CommandsManager;
import com.juick.server.EmailManager;
import com.juick.server.XMPPConnection;
-import com.juick.server.util.*;
import com.juick.server.helpers.CommandResult;
+import com.juick.server.util.*;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.UserService;
@@ -44,6 +44,7 @@ import javax.inject.Inject;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
+import javax.validation.constraints.NotNull;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -235,6 +236,7 @@ public class Post {
throw new HttpForbiddenException();
}
}
+
@PostMapping("/like")
@ResponseStatus(value = HttpStatus.OK)
public Status doPostRecomm(@RequestParam Integer mid) throws Exception {
@@ -253,4 +255,27 @@ public class Post {
URI.create(StringUtils.EMPTY));
return Status.getStatus(status.getText());
}
+
+ @PostMapping("/react")
+ @ResponseStatus(value = HttpStatus.OK)
+ public Status doPostReact(@RequestParam Integer mid,@RequestParam @NotNull int reactionId){
+
+ logger.info("got reaction with type: {}", reactionId);
+ 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 recommendStatus = messagesService.likeMessage(mid,msg.getUser().getUid(),
+ reactionId);
+
+ return recommendStatus == MessagesService.RecommendStatus.Error ? Status.ERROR :Status.OK;
+ }
}
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 17c7e35c..6b31906d 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
@@ -19,10 +19,7 @@ package com.juick.server.tests;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.juick.ExternalToken;
-import com.juick.Message;
-import com.juick.Tag;
-import com.juick.User;
+import com.juick.*;
import com.juick.server.*;
import com.juick.server.component.MessageEvent;
import com.juick.server.helpers.AnonymousUser;
@@ -34,10 +31,9 @@ import com.juick.util.DateFormattersHolder;
import com.juick.util.MessageUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.hamcrest.Matchers;
+import org.junit.*;
+import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -77,6 +73,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
@@ -261,11 +258,14 @@ public class ServerTests {
List<Message> replies2 = messagesService.getReplies(user, msg2.getMid());
assertThat(replies2.size(), equalTo(2));
assertThat(replies2.get(1), equalTo(reply2));
+
Message msg3 = messagesService.getMessage(mid2);
+
assertEquals(2, msg3.getReplies());
assertEquals("weather", msg3.getTags().get(0).getName());
assertEquals(ugnich.getUid(), userService.checkPassword(ugnich.getName(), "x"));
assertEquals(-1, userService.checkPassword(ugnich.getName(), "xy"));
+
subscriptionService.subscribeMessage(msg, user);
subscriptionService.subscribeMessage(msg, ugnich);
int reply_id = messagesService.createReply(msg.getMid(), 0, ugnich, "comment", null);
@@ -286,6 +286,28 @@ public class ServerTests {
assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4));
messagesService.deleteMessage(user_id, mid4);
}
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ @Test
+ public void likeTypeStatsTests(){
+ int user_id = userService.createUser("dsdss", "secret");
+ final int freefdId = freefd.getUid();
+ int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
+ messagesService.likeMessage(mid, freefdId , 2);
+ messagesService.likeMessage(mid, freefdId,2);
+ messagesService.likeMessage(mid, freefdId,3);
+ exception.expect(IllegalArgumentException.class);
+ messagesService.likeMessage(mid, freefdId,1);
+
+ Message msg4 = messagesService.getMessage(mid);
+
+ Assert.assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2)
+ .findFirst().orElseThrow(IllegalStateException::new).getCount());
+ Assert.assertEquals(1,msg4.getReactions().stream().filter(r -> r.getId() == 3)
+ .findFirst().orElseThrow(IllegalStateException::new).getCount());
+ }
@Test
public void lastJidShouldNotBeDeleted() {
int ugnich_id = userService.createUser("hugnich2", "x");
@@ -405,6 +427,29 @@ public class ServerTests {
}
@Test
+ public void messagesUrlTest() throws Exception {
+ int user_id = userService.createUser("dsds4345", "secret");
+
+ String freefdHash = userService.getHashByUID(freefd.getUid());
+ System.out.println("user_id"+ user_id);
+ String userIdHash = userService.getHashByUID(user_id);
+ final int freefdId = freefd.getUid();
+ int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
+ messagesService.likeMessage(mid, freefdId, 2 );
+ messagesService.likeMessage(mid, freefdId, 2 );
+ messagesService.likeMessage(mid, freefdId, 3 );
+
+ mockMvc.perform(get("/messages?"+ "hash=" + userIdHash))
+ .andDo(print())
+ .andExpect(status().isOk())
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
+ .andExpect((jsonPath("$[0].reactions[?(@.id == 3)].count",
+ Matchers.is(Collections.singletonList(1)))))
+ .andExpect((jsonPath("$[0].reactions[?(@.id == 2)].count",
+ Matchers.is(Collections.singletonList(2)))));
+ }
+
+ @Test
public void tags() throws Exception {
Tag weather = tagService.getTag("weather", true);
Tag yo = tagService.getTag("yo", true);
@@ -745,16 +790,37 @@ public class ServerTests {
}
@Test
- public void likesTests() throws Exception {
+ public void recommendTests() 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())
.andExpect(jsonPath("$.status", is("Message is added to your recommendations")));
+
mockMvc.perform(post("/like?mid=" + freefdMid + "&hash=" + freefdHash))
.andExpect(status().isForbidden());
}
+
+ @Test
+ public void likesTests() throws Exception{
+ int user_id = userService.createUser("dsds", "secret");
+ String freefdHash = userService.getHashByUID(freefd.getUid());
+ System.out.println("user_id"+ user_id);
+ String userIdHash = userService.getHashByUID(user_id);
+ int mid1 = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
+
+ mockMvc.perform(post("/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=2"))
+ .andExpect(status().isOk());
+
+ Message msg4 = messagesService.getMessage(mid1);
+ System.out.println("msg4"+msg4);
+ Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2)
+ .findFirst().orElseThrow(IllegalStateException::new).getCount());
+ }
+
@Test
public void telegramTests() {
jdbcTemplate.update("INSERT INTO telegram(user_id, tg_id) VALUES(?,1)", ugnich.getUid());
diff --git a/juick-server/src/test/resources/data.sql b/juick-server/src/test/resources/data.sql
new file mode 100644
index 00000000..17421b16
--- /dev/null
+++ b/juick-server/src/test/resources/data.sql
@@ -0,0 +1,7 @@
+INSERT INTO reactions (like_id, description) VALUES (1, 'like');
+INSERT INTO reactions (like_id, description) VALUES (2, 'love');
+INSERT INTO reactions (like_id, description) VALUES (3, 'lol');
+INSERT INTO reactions (like_id, description) VALUES (4, 'hmm');
+INSERT INTO reactions (like_id, description) VALUES (5, 'angry');
+INSERT INTO reactions (like_id, description) VALUES (6, 'uhblya');
+INSERT INTO reactions (like_id, description) VALUES (7, 'ugh');