aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--juick-rss/build.gradle8
-rw-r--r--juick-rss/src/main/java/com/juick/rss/MessagesView.java124
-rw-r--r--juick-rss/src/main/java/com/juick/rss/RepliesView.java84
-rw-r--r--juick-rss/src/main/java/com/juick/rss/configuration/RssAppConfiguration.java17
-rw-r--r--juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java40
-rw-r--r--juick-rss/src/main/java/com/juick/rss/configuration/RssMvcConfiguration.java20
-rw-r--r--juick-rss/src/main/java/com/juick/rss/controllers/FeedsController.java54
-rw-r--r--juick-rss/src/main/java/com/juick/rss/extension/JuickModule.java16
-rw-r--r--juick-rss/src/main/java/com/juick/rss/extension/JuickModuleImpl.java37
-rw-r--r--juick-rss/src/main/java/com/juick/rss/extension/JuickModuleParser.java25
-rw-r--r--juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java148
-rw-r--r--juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java72
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesService.java6
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java31
14 files changed, 682 insertions, 0 deletions
diff --git a/juick-rss/build.gradle b/juick-rss/build.gradle
index 88df407b..0d9cc4c4 100644
--- a/juick-rss/build.gradle
+++ b/juick-rss/build.gradle
@@ -5,8 +5,16 @@ apply plugin: 'com.github.ben-manes.versions'
dependencies {
compile project(':juick-server')
+ compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}"
+ compile 'com.rometools:rome:1.7.0'
+ compile 'com.rometools:rome-modules:1.7.0'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
providedRuntime 'mysql:mysql-connector-java:5.1.40'
+
+ testCompile "junit:junit:${rootProject.junitVersion}"
+ testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}"
+ testCompile "org.mockito:mockito-core:1.+"
+ testCompile "org.springframework:spring-test:${rootProject.springFrameworkVersion}"
}
compileJava.options.encoding = 'UTF-8'
diff --git a/juick-rss/src/main/java/com/juick/rss/MessagesView.java b/juick-rss/src/main/java/com/juick/rss/MessagesView.java
new file mode 100644
index 00000000..3b6b48a6
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/MessagesView.java
@@ -0,0 +1,124 @@
+package com.juick.rss;
+
+import com.juick.Message;
+import com.juick.User;
+import com.juick.rss.extension.JuickModule;
+import com.juick.rss.extension.JuickModuleImpl;
+import com.juick.util.MessageUtils;
+import com.rometools.modules.atom.modules.AtomLinkModule;
+import com.rometools.modules.atom.modules.AtomLinkModuleImpl;
+import com.rometools.modules.mediarss.MediaEntryModuleImpl;
+import com.rometools.modules.mediarss.MediaModule;
+import com.rometools.modules.mediarss.MediaModuleImpl;
+import com.rometools.modules.mediarss.types.MediaContent;
+import com.rometools.modules.mediarss.types.Metadata;
+import com.rometools.modules.mediarss.types.Thumbnail;
+import com.rometools.modules.mediarss.types.UrlReference;
+import com.rometools.rome.feed.atom.Link;
+import com.rometools.rome.feed.rss.*;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+public class MessagesView extends AbstractRssFeedView {
+
+ private static final Logger logger = LoggerFactory.getLogger(MessagesView.class);
+
+ @Override
+ protected List<Item> buildFeedItems(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ List<Message> msgs = (List<Message>)model.get("messages");
+ return msgs.stream().map(this::createRssItem).collect(Collectors.toList());
+ }
+
+ @Override
+ protected void buildFeedMetadata(Map<String, Object> model, Channel feed, HttpServletRequest request) {
+ Object userObj = model.get("user");
+ if (userObj != null) {
+ User user = (User) userObj;
+ feed.setDescription(String.format("The latest messages by @%s at Juick", user.getName()));
+ String title = String.format("%s - Juick", user.getName());
+ feed.setTitle(title);
+ String link = String.format("http://juick.com/%s/", user.getName());
+ feed.setLink(link);
+ Image rssImage = new Image();
+ rssImage.setUrl(String.format("http://juick.com/a/%d.png", user.getUid()));
+ rssImage.setTitle(title);
+ rssImage.setLink(link);
+ feed.setImage(rssImage);
+ String href = String.format("http://rss.juick.com/%s/blog", user.getName());
+ AtomLinkModule atomLinkModule = new AtomLinkModuleImpl();
+ Link atomLink = new Link();
+ atomLink.setHref(href);
+ atomLink.setType("application/rss+xml");
+ atomLink.setRel("self");
+ atomLinkModule.setLink(atomLink);
+
+ feed.getModules().add(atomLinkModule);
+ } else {
+ feed.setDescription("The latest messages at Juick");
+ feed.setLink("http://juick.com/");
+ feed.setTitle("Juick");
+ }
+
+ MediaModule mediaModule = new MediaModuleImpl();
+ feed.getModules().add(mediaModule);
+
+
+ }
+
+ private Item createRssItem(Message msg) {
+ Item item = new Item();
+ String messageUrl = String.format("http://juick.com/%s/%d", msg.getUser().getName(), msg.getMid());
+ String messageTitle = String.format("@%s: %s", msg.getUser().getName(), msg.getTagsString());
+ boolean isCode = msg.getTags().stream().anyMatch(t -> t.getName().equals("code"));
+ String messageDescription = isCode ? MessageUtils.formatMessageCode(msg.getText())
+ : MessageUtils.formatMessage(msg.getText());
+ item.setLink(messageUrl);
+ //item.setGuid(messageUrl);
+ item.setTitle(messageTitle);
+ Description description = new Description();
+ description.setType("text/html");
+ description.setValue(messageDescription);
+ item.setDescription(description);
+ item.setPubDate(msg.getDate());
+ item.setComments(messageUrl);
+ msg.getTags().stream().map(t -> {
+ Category category = new Category();
+ category.setValue(t.getName());
+ return category;
+ }).forEach(c -> item.getCategories().add(c));
+ JuickModule juickModule = new JuickModuleImpl();
+ juickModule.setUid(msg.getUser().getUid());
+ item.getModules().add(juickModule);
+ if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
+ String type = msg.getAttachmentType().equals("jpg") ? "image/jpeg" : "image/png";
+ MediaEntryModuleImpl module = new MediaEntryModuleImpl();
+ try {
+ UrlReference reference = new UrlReference(msg.getAttachmentURL());
+ MediaContent mediaContent = new MediaContent(reference);
+ mediaContent.setType(type);
+ Metadata metadata = new Metadata();
+ metadata.setThumbnail(new Thumbnail[]{new Thumbnail(new URI(msg.getPhoto().getThumbnail()))});
+ module.setMetadata(metadata);
+ module.setMediaContents(new MediaContent[]{mediaContent});
+ item.getModules().add(module);
+ } catch (URISyntaxException e) {
+ logger.error("Invalid URI", e);
+ }
+
+ }
+ return item;
+ }
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/RepliesView.java b/juick-rss/src/main/java/com/juick/rss/RepliesView.java
new file mode 100644
index 00000000..bdbab240
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/RepliesView.java
@@ -0,0 +1,84 @@
+package com.juick.rss;
+
+import com.juick.server.helpers.ResponseReply;
+import com.juick.util.MessageUtils;
+import com.rometools.modules.mediarss.MediaEntryModuleImpl;
+import com.rometools.modules.mediarss.MediaModule;
+import com.rometools.modules.mediarss.MediaModuleImpl;
+import com.rometools.modules.mediarss.types.MediaContent;
+import com.rometools.modules.mediarss.types.Metadata;
+import com.rometools.modules.mediarss.types.Thumbnail;
+import com.rometools.modules.mediarss.types.UrlReference;
+import com.rometools.rome.feed.rss.Channel;
+import com.rometools.rome.feed.rss.Description;
+import com.rometools.rome.feed.rss.Item;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+public class RepliesView extends AbstractRssFeedView {
+
+ private static final Logger logger = LoggerFactory.getLogger(RepliesView.class);
+
+ @Override
+ protected List<Item> buildFeedItems(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ List<ResponseReply> msgs = (List<ResponseReply>)model.get("messages");
+ return msgs.stream().map(this::createRssItem).collect(Collectors.toList());
+ }
+
+ @Override
+ protected void buildFeedMetadata(Map<String, Object> model, Channel feed, HttpServletRequest request) {
+ feed.setTitle("Juick");
+ feed.setLink("http://juick.com/");
+ feed.setDescription("The latest comments at Juick");
+ MediaModule mediaModule = new MediaModuleImpl();
+ feed.getModules().add(mediaModule);
+ }
+
+ private Item createRssItem(ResponseReply msg) {
+ Item item = new Item();
+ String messageUrl = String.format("http://juick.com/%d#%d", msg.getMid(), msg.getRid());
+ String messageTitle = String.format("@%s:", msg.getUname());
+ String messageDescription = MessageUtils.formatMessage(msg.getDescription());
+ item.setLink(messageUrl);
+ //item.setGuid(messageUrl);
+ item.setTitle(messageTitle);
+ Description description = new Description();
+ description.setType("text/html");
+ description.setValue(messageDescription);
+ item.setDescription(description);
+ item.setPubDate(msg.getPubDate());
+ if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
+ String type = msg.getAttachmentType().equals("jpg") ? "image/jpeg" : "image/png";
+ MediaEntryModuleImpl module = new MediaEntryModuleImpl();
+ try {
+ UrlReference reference = new UrlReference(
+ String.format("http://i.juick.com/photos-1024/%d-%d.%s", msg.getMid(), msg.getRid(), type));
+ MediaContent mediaContent = new MediaContent(reference);
+ mediaContent.setType(type);
+ Metadata metadata = new Metadata();
+ metadata.setThumbnail(new Thumbnail[]{new Thumbnail(
+ new URI(String.format("http://i.juick.com/ps/%d-%d.%s", msg.getMid(), msg.getRid(), type)))});
+ module.setMetadata(metadata);
+ module.setMediaContents(new MediaContent[]{mediaContent});
+ item.getModules().add(module);
+ } catch (URISyntaxException e) {
+ logger.error("Invalid URI", e);
+ }
+
+ }
+ return item;
+ }
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/configuration/RssAppConfiguration.java b/juick-rss/src/main/java/com/juick/rss/configuration/RssAppConfiguration.java
new file mode 100644
index 00000000..15195aba
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/configuration/RssAppConfiguration.java
@@ -0,0 +1,17 @@
+package com.juick.rss.configuration;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.core.env.Environment;
+
+import javax.inject.Inject;
+
+/**
+ * Created by aalexeev on 11/12/16.
+ */
+@Configuration
+@PropertySource("classpath:juick.conf")
+public class RssAppConfiguration {
+ @Inject
+ private Environment env;
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java b/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java
new file mode 100644
index 00000000..a66fcca7
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java
@@ -0,0 +1,40 @@
+package com.juick.rss.configuration;
+
+import com.juick.configuration.DataConfiguration;
+import org.springframework.web.filter.CharacterEncodingFilter;
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+import javax.servlet.Filter;
+
+/**
+ * Created by vt on 09/02/16.
+ */
+public class RssInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
+
+ @Override
+ protected Class<?>[] getRootConfigClasses() {
+ return new Class<?>[]{RssAppConfiguration.class, DataConfiguration.class};
+ }
+
+ @Override
+ protected Class<?>[] getServletConfigClasses() {
+ return new Class<?>[]{RssMvcConfiguration.class};
+ }
+
+ @Override
+ protected String[] getServletMappings() {
+ return new String[]{"/"};
+ }
+
+ @Override
+ protected Filter[] getServletFilters() {
+ CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
+ characterEncodingFilter.setEncoding("UTF-8");
+ return new Filter[]{characterEncodingFilter};
+ }
+
+ @Override
+ protected String getServletName() {
+ return "RSS dispatcher servlet";
+ }
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/configuration/RssMvcConfiguration.java b/juick-rss/src/main/java/com/juick/rss/configuration/RssMvcConfiguration.java
new file mode 100644
index 00000000..f975d87b
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/configuration/RssMvcConfiguration.java
@@ -0,0 +1,20 @@
+package com.juick.rss.configuration;
+
+import com.juick.rss.MessagesView;
+import com.juick.rss.RepliesView;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
+
+/**
+ * Created by vitalyster on 28.06.2016.
+ */
+@Configuration
+@ComponentScan(basePackages = {"com.juick.rss.controllers"})
+public class RssMvcConfiguration extends WebMvcConfigurationSupport {
+ @Override
+ protected void configureViewResolvers(ViewResolverRegistry registry) {
+ registry.enableContentNegotiation(new MessagesView(), new RepliesView());
+ }
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/controllers/FeedsController.java b/juick-rss/src/main/java/com/juick/rss/controllers/FeedsController.java
new file mode 100644
index 00000000..9c3dc787
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/controllers/FeedsController.java
@@ -0,0 +1,54 @@
+package com.juick.rss.controllers;
+
+import com.juick.User;
+import com.juick.server.util.HttpBadRequestException;
+import com.juick.service.MessagesService;
+import com.juick.service.UserService;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.inject.Inject;
+import java.util.List;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+@Controller
+public class FeedsController {
+
+ @Inject
+ MessagesService messagesService;
+ @Inject
+ UserService userService;
+
+ @RequestMapping(value = "/{userName}/blog", method = RequestMethod.GET)
+ public ModelAndView getBlog(@PathVariable String userName) {
+ User user = userService.getUserByName(userName);
+ if (user.getUid() > 0) {
+ List<Integer> mids = messagesService.getUserBlog(user.getUid(), 0, 0);
+ ModelAndView modelAndView = new ModelAndView();
+ modelAndView.addObject("user", user);
+ modelAndView.addObject("messages", messagesService.getMessages(mids));
+ return modelAndView;
+ }
+ throw new HttpBadRequestException();
+ }
+
+ @RequestMapping(value = "/", method = RequestMethod.GET)
+ public ModelAndView getLast(@RequestParam(value = "hours", required = false, defaultValue = "0") Integer hours) {
+ List<Integer> mids = messagesService.getLastMessages(hours);
+ ModelAndView modelAndView = new ModelAndView();
+ modelAndView.addObject("messages", messagesService.getMessages(mids));
+ return modelAndView;
+ }
+ @RequestMapping(value = "/comments", method = RequestMethod.GET)
+ public ModelAndView getLastReplies(@RequestParam(value = "hours", required = false, defaultValue = "0") Integer hours) {
+ ModelAndView modelAndView = new ModelAndView();
+ modelAndView.addObject("messages", messagesService.getLastReplies(hours));
+ return modelAndView;
+ }
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/extension/JuickModule.java b/juick-rss/src/main/java/com/juick/rss/extension/JuickModule.java
new file mode 100644
index 00000000..b1a44067
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/extension/JuickModule.java
@@ -0,0 +1,16 @@
+package com.juick.rss.extension;
+
+import com.rometools.rome.feed.module.Module;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+public interface JuickModule extends Module {
+
+ String URI = "http://juick.com/";
+
+ Integer getUid();
+
+ void setUid(Integer uid);
+
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleImpl.java b/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleImpl.java
new file mode 100644
index 00000000..3f60ed76
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleImpl.java
@@ -0,0 +1,37 @@
+package com.juick.rss.extension;
+
+import com.rometools.rome.feed.CopyFrom;
+import com.rometools.rome.feed.module.ModuleImpl;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+public class JuickModuleImpl extends ModuleImpl implements JuickModule {
+
+ private Integer uid;
+
+ public JuickModuleImpl() {
+ super(JuickModule.class, JuickModule.URI);
+ }
+
+ @Override
+ public Integer getUid() {
+ return uid;
+ }
+
+ @Override
+ public void setUid(Integer uid) {
+ this.uid = uid;
+ }
+
+ @Override
+ public Class<? extends CopyFrom> getInterface() {
+ return JuickModule.class;
+ }
+
+ @Override
+ public void copyFrom(CopyFrom obj) {
+ JuickModule juickModule = (JuickModule) obj;
+ setUid(juickModule.getUid());
+ }
+}
diff --git a/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleParser.java b/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleParser.java
new file mode 100644
index 00000000..7d6ba8cc
--- /dev/null
+++ b/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleParser.java
@@ -0,0 +1,25 @@
+package com.juick.rss.extension;
+
+import com.rometools.rome.feed.module.Module;
+import com.rometools.rome.io.ModuleParser;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.jdom2.Element;
+
+import java.util.Locale;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+public class JuickModuleParser implements ModuleParser {
+ @Override
+ public String getNamespaceUri() {
+ return JuickModule.URI;
+ }
+
+ @Override
+ public Module parse(Element element, Locale locale) {
+ JuickModuleImpl juickModule = new JuickModuleImpl();
+ juickModule.setUid(NumberUtils.toInt(element.getAttributeValue("uid", JuickModule.URI), 0));
+ return juickModule;
+ }
+}
diff --git a/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java b/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java
new file mode 100644
index 00000000..8f10f9c3
--- /dev/null
+++ b/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java
@@ -0,0 +1,148 @@
+package com.juick.rss.tests;
+
+import com.juick.Message;
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.configuration.DataConfiguration;
+import com.juick.rss.configuration.RssAppConfiguration;
+import com.juick.rss.configuration.RssMvcConfiguration;
+import com.juick.service.MessagesService;
+import com.juick.service.TagService;
+import com.juick.service.UserService;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.Primary;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
+import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.mockito.Mockito.when;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
+@WebAppConfiguration
+public class RSSTests {
+ @Configuration
+ @Import(value = {RssMvcConfiguration.class, RssAppConfiguration.class, DataConfiguration.class})
+ static class Config {
+ @Bean
+ @Primary
+ MessagesService messagesService() {
+ return Mockito.mock(MessagesService.class);
+ }
+
+ @Bean
+ @Primary
+ UserService userService() {
+ return Mockito.mock(UserService.class);
+ }
+
+ @Bean
+ @Primary
+ TagService tagService() {
+ return Mockito.mock(TagService.class);
+ }
+ }
+
+ private MockMvc mockMvc;
+ @Inject
+ private WebApplicationContext webApplicationContext;
+
+ @Inject
+ private MessagesService messagesService;
+ @Inject
+ private UserService userService;
+ @Inject
+ private TagService tagService;
+
+ private User ugnich, freefd;
+ String ugnichName, ugnichPassword, freefdName, freefdPassword;
+
+ private static Message getMessage(final User user, final String messageText) {
+ Message msg = new Message();
+
+ msg.setMid(1);
+ msg.setUser(user);
+ msg.setText(messageText == null ? RandomStringUtils.randomAlphanumeric(24) : messageText);
+ msg.setTags(Collections.singletonList(new Tag(RandomStringUtils.randomAlphabetic(4))));
+
+ return msg;
+ }
+
+ private static User getUser(final int uid, final String name, final String password) {
+ User user = new User();
+
+ user.setName(name);
+ user.setUid(uid);
+ user.setCredentials(password);
+ user.setBanned(false);
+
+ return user;
+ }
+
+ @Before
+ public void setUp() {
+ mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
+ .dispatchOptions(true)
+ .build();
+ ugnichName = "ugnich";
+ ugnichPassword = "MyPassw0rd!";
+ freefdName = "freefd";
+ freefdPassword = "MyPassw0rd!";
+
+ ugnich = getUser(1, ugnichName, ugnichPassword);
+ freefd = getUser(2, freefdName, freefdPassword);
+
+ List<String> users = new ArrayList<>(2);
+ users.add(ugnichName);
+ users.add(freefdName);
+
+ when(userService.getUsersByName(users))
+ .thenReturn(Arrays.asList(ugnich, freefd));
+ when(userService.getUserByName(ugnichName))
+ .thenReturn(ugnich);
+ when(userService.getFullyUserByName(ugnichName))
+ .thenReturn(ugnich);
+ when(userService.getUserByName(null))
+ .thenReturn(new User());
+ }
+
+ @Test
+ public void lastMessagesTest() throws Exception {
+ String msgText = "Привет, я - Угнич";
+
+ Message msg = getMessage(ugnich, msgText);
+
+ when(messagesService.getMyFeed(1, 0))
+ .thenReturn(Collections.singletonList(1));
+ when(messagesService.getMessages(Collections.singletonList(1)))
+ .thenReturn(Collections.singletonList(msg));
+
+ mockMvc.perform(
+ get("/"))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType("application/rss+xml"));
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java b/juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java
new file mode 100644
index 00000000..f941c743
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java
@@ -0,0 +1,72 @@
+package com.juick.server.helpers;
+
+import java.util.Date;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+public class ResponseReply {
+ private String muname;
+ private int mid;
+ private int rid;
+ private String uname;
+ private String description;
+ private Date pubDate;
+ private String attachmentType;
+
+ public String getMuname() {
+ return muname;
+ }
+
+ public void setMuname(String muname) {
+ this.muname = muname;
+ }
+
+ public int getMid() {
+ return mid;
+ }
+
+ public void setMid(int mid) {
+ this.mid = mid;
+ }
+
+ public int getRid() {
+ return rid;
+ }
+
+ public void setRid(int rid) {
+ this.rid = rid;
+ }
+
+ public String getUname() {
+ return uname;
+ }
+
+ public void setUname(String uname) {
+ this.uname = uname;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public Date getPubDate() {
+ return pubDate;
+ }
+
+ public void setPubDate(Date pubDate) {
+ this.pubDate = pubDate;
+ }
+
+ public String getAttachmentType() {
+ return attachmentType;
+ }
+
+ public void setAttachmentType(String attachmentType) {
+ this.attachmentType = attachmentType;
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/service/MessagesService.java b/juick-server/src/main/java/com/juick/service/MessagesService.java
index f7175967..8cb0782f 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesService.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesService.java
@@ -1,6 +1,8 @@
package com.juick.service;
+import com.juick.Message;
import com.juick.User;
+import com.juick.server.helpers.ResponseReply;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Collection;
@@ -75,4 +77,8 @@ public interface MessagesService {
boolean setMessagePrivacy(int mid);
boolean deleteMessage(int uid, int mid);
+
+ List<Integer> getLastMessages(int hours);
+
+ List<ResponseReply> getLastReplies(int hours);
}
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
index d7c1d51b..88167be8 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -4,6 +4,7 @@ import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.server.helpers.PrivacyOpts;
+import com.juick.server.helpers.ResponseReply;
import com.juick.service.search.SearchService;
import com.juick.util.MessageUtils;
import org.apache.commons.collections4.CollectionUtils;
@@ -739,4 +740,34 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
}
return false;
}
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getLastMessages(int hours) {
+ return getJdbcTemplate().queryForList("SELECT message_id FROM messages WHERE messages.ts>TIMESTAMPADD(HOUR,?,NOW())",
+ Integer.class, -hours);
+
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<ResponseReply> getLastReplies(int hours) {
+ return getJdbcTemplate().query("SELECT users2.nick,replies.message_id,replies.reply_id," +
+ "users.nick,replies.txt," +
+ "replies.ts,replies.attach,replies.ts+0 " +
+ "FROM ((replies INNER JOIN users ON replies.user_id=users.id) " +
+ "INNER JOIN messages ON replies.message_id=messages.message_id) " +
+ "INNER JOIN users AS users2 ON messages.user_id=users2.id " +
+ "WHERE replies.ts>TIMESTAMPADD(HOUR,?,NOW()) AND messages.privacy>0", (rs, rowNum) -> {
+ ResponseReply reply = new ResponseReply();
+ reply.setMuname(rs.getString(1));
+ reply.setMid(rs.getInt(2));
+ reply.setRid(rs.getInt(3));
+ reply.setUname(rs.getString(4));
+ reply.setDescription(rs.getString(5));
+ reply.setPubDate(rs.getTimestamp(6));
+ reply.setAttachmentType(rs.getString(7));
+ return reply;
+ }, -hours);
+ }
}