diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/juick/www/controllers/X.java | 82 | ||||
-rw-r--r-- | src/test/java/com/juick/server/tests/ServerTests.java | 10 |
2 files changed, 92 insertions, 0 deletions
diff --git a/src/main/java/com/juick/www/controllers/X.java b/src/main/java/com/juick/www/controllers/X.java new file mode 100644 index 00000000..b1b25dbc --- /dev/null +++ b/src/main/java/com/juick/www/controllers/X.java @@ -0,0 +1,82 @@ +package com.juick.www.controllers; + +import java.util.List; +import java.util.Locale; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.juick.model.Message; +import com.juick.model.User; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import com.juick.util.HttpNotFoundException; +import com.juick.www.WebApp; + +import io.swagger.v3.oas.annotations.Parameter; + +@RestController +public class X { + + private MessagesService messagesService; + + private User serviceUser; + + private UserService userService; + + private WebApp webApp; + + public X(MessagesService messagesService, User serviceUser, UserService userService, WebApp webApp) { + this.messagesService = messagesService; + this.serviceUser = serviceUser; + this.userService = userService; + this.webApp = webApp; + } + + @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE) + protected List<Message> doGet(@Parameter(hidden = true) User visitor, Locale locale, + @RequestParam(name = "show", required = false) String paramShow, + @RequestParam(name = "search", required = false) String paramSearch, + @RequestParam(name = "before", required = false, defaultValue = "0") Integer paramBefore, + @RequestParam(name = "to", required = false, defaultValue = "0") Long paramTo, + @RequestParam(name = "page", required = false, defaultValue = "0") Integer page) { + visitor.setAvatar(webApp.getAvatarWebPath(visitor)); + + if (paramSearch != null && paramSearch.length() > 64) { + paramSearch = null; + } + + List<Integer> mids; + + if (paramSearch != null) { + mids = messagesService.getSearch(visitor, paramSearch, page); + } else if (paramShow == null) { + mids = messagesService.getDiscussions(visitor.getUid(), paramTo); + } else if (paramShow.equals("top")) { + mids = messagesService.getUserBlogWithRecommendations(serviceUser, visitor, 0, paramBefore); + } else if (paramShow.equals("my") && !visitor.isAnonymous()) { + mids = messagesService.getMyFeed(visitor.getUid(), paramBefore, true); + } else if (paramShow.equals("private") && !visitor.isAnonymous()) { + mids = messagesService.getPrivate(visitor.getUid(), paramBefore); + } else if (paramShow.equals("recommended") && !visitor.isAnonymous()) { + mids = messagesService.getRecommended(visitor.getUid(), paramBefore); + } else if (paramShow.equals("photos")) { + mids = messagesService.getPhotos(visitor.getUid(), paramBefore); + } else if (paramShow.equals("all")) { + mids = messagesService.getAll(visitor.getUid(), paramBefore); + } else { + throw new HttpNotFoundException(); + } + List<Message> msgs = messagesService.getMessages(visitor.getUid(), mids); + msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); + if (!visitor.isAnonymous()) { + List<Integer> unread = messagesService.getUnread(visitor); + visitor.setUnreadCount(unread.size()); + List<Integer> blUIDs = userService.checkBL(visitor.getUid(), + msgs.stream().map(m -> m.getUser().getUid()).toList()); + msgs.forEach(m -> m.ReadOnly |= blUIDs.contains(m.getUser().getUid())); + } + return msgs; + } +} diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index ce9b49d0..5a32c9ef 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -2534,6 +2534,16 @@ public class ServerTests { mockMvc.perform(get(htmlUri)).andExpect(status().isOk()); mockMvc.perform(get(ldUri)).andExpect(status().isOk()); } + @Test + public void jsonRequestToRootShouldRespondWithJsonContent() throws Exception { + var jsonHeader = "application/json"; + mockMvc.perform(get("/").accept(jsonHeader)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)); + HtmlPage htmlPage = webClient.getPage("/"); + assertThat(htmlPage.isHtmlPage(), is(true)); + assertThat(htmlPage.getTitleText(), is("Discuss")); + } @Test @Transactional |