From 69e7a6af07742a075716fa9c30087c68170f6dc6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 18 Feb 2018 01:07:19 +0300 Subject: server: merge rss --- juick-rss/build.gradle | 28 --- .../src/main/java/com/juick/rss/MessagesView.java | 143 --------------- .../src/main/java/com/juick/rss/RepliesView.java | 101 ----------- .../rss/configuration/RssAppConfiguration.java | 34 ---- .../juick/rss/configuration/RssInitializer.java | 56 ------ .../rss/configuration/RssMvcConfiguration.java | 47 ----- .../com/juick/rss/controllers/FeedsController.java | 74 -------- .../java/com/juick/rss/extension/JuickModule.java | 33 ---- .../juick/rss/extension/JuickModuleGenerator.java | 70 -------- .../com/juick/rss/extension/JuickModuleImpl.java | 54 ------ .../com/juick/rss/extension/JuickModuleParser.java | 42 ----- juick-rss/src/main/resources/rome.properties | 2 - juick-rss/src/main/webapp/WEB-INF/web.xml | 4 - .../test/java/com/juick/rss/tests/RSSTests.java | 165 ----------------- juick-server/build.gradle | 2 + .../main/java/com/juick/server/api/rss/Feeds.java | 74 ++++++++ .../com/juick/server/api/rss/MessagesView.java | 143 +++++++++++++++ .../java/com/juick/server/api/rss/RepliesView.java | 101 +++++++++++ .../server/api/rss/extension/JuickModule.java | 33 ++++ .../api/rss/extension/JuickModuleGenerator.java | 70 ++++++++ .../server/api/rss/extension/JuickModuleImpl.java | 54 ++++++ .../api/rss/extension/JuickModuleParser.java | 42 +++++ .../server/configuration/ApiAppConfiguration.java | 16 ++ juick-server/src/main/resources/rome.properties | 2 + .../test/java/com/juick/server/tests/RSSTests.java | 198 +++++++++++++++++++++ settings.gradle | 2 +- 26 files changed, 736 insertions(+), 854 deletions(-) delete mode 100644 juick-rss/build.gradle delete mode 100644 juick-rss/src/main/java/com/juick/rss/MessagesView.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/RepliesView.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/configuration/RssAppConfiguration.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/configuration/RssMvcConfiguration.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/controllers/FeedsController.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/extension/JuickModule.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/extension/JuickModuleGenerator.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/extension/JuickModuleImpl.java delete mode 100644 juick-rss/src/main/java/com/juick/rss/extension/JuickModuleParser.java delete mode 100644 juick-rss/src/main/resources/rome.properties delete mode 100644 juick-rss/src/main/webapp/WEB-INF/web.xml delete mode 100644 juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java create mode 100644 juick-server/src/main/java/com/juick/server/api/rss/Feeds.java create mode 100644 juick-server/src/main/java/com/juick/server/api/rss/MessagesView.java create mode 100644 juick-server/src/main/java/com/juick/server/api/rss/RepliesView.java create mode 100644 juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModule.java create mode 100644 juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleGenerator.java create mode 100644 juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleImpl.java create mode 100644 juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleParser.java create mode 100644 juick-server/src/main/resources/rome.properties create mode 100644 juick-server/src/test/java/com/juick/server/tests/RSSTests.java diff --git a/juick-rss/build.gradle b/juick-rss/build.gradle deleted file mode 100644 index 44f2334e..00000000 --- a/juick-rss/build.gradle +++ /dev/null @@ -1,28 +0,0 @@ -apply plugin: 'java' -apply plugin: 'war' -apply plugin: 'org.akhikhl.gretty' - -dependencies { - compile project(':juick-server-jdbc') - compile project(':juick-server-web') - compile 'com.rometools:rome:1.9.0' - compile 'com.rometools:rome-modules:1.9.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:${rootProject.mockitoVersion}" - testCompile "org.springframework:spring-test:${rootProject.springFrameworkVersion}" -} - -compileJava.options.encoding = 'UTF-8' - -gretty { - httpPort = 8080 - contextPath = '/' - servletContainer = 'tomcat8' -} - -configurations { - all*.exclude module: 'commons-logging' -} diff --git a/juick-rss/src/main/java/com/juick/rss/MessagesView.java b/juick-rss/src/main/java/com/juick/rss/MessagesView.java deleted file mode 100644 index d560b4e9..00000000 --- a/juick-rss/src/main/java/com/juick/rss/MessagesView.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -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.Collections; -import java.util.Date; -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 buildFeedItems(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { - List msgs = (List)model.get("messages"); - return msgs.stream().map(this::createRssItem).collect(Collectors.toList()); - } - - @Override - protected void buildFeedMetadata(Map 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.setLinks(Collections.singletonList(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(StringUtils.defaultString(msg.getText())) - : MessageUtils.formatMessage(StringUtils.defaultString(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(Date.from(msg.getTimestamp())); - 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.getAttachment().getUrl()); - 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 deleted file mode 100644 index 9db61049..00000000 --- a/juick-rss/src/main/java/com/juick/rss/RepliesView.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -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 buildFeedItems(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { - List msgs = (List)model.get("messages"); - return msgs.stream().map(this::createRssItem).collect(Collectors.toList()); - } - - @Override - protected void buildFeedMetadata(Map 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 deleted file mode 100644 index 2069b14e..00000000 --- a/juick-rss/src/main/java/com/juick/rss/configuration/RssAppConfiguration.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -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 deleted file mode 100644 index 617da288..00000000 --- a/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.juick.rss.configuration; - -import org.apache.commons.codec.CharEncoding; -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}; - } - - @Override - protected Class[] getServletConfigClasses() { - return new Class[]{RssMvcConfiguration.class}; - } - - @Override - protected String[] getServletMappings() { - return new String[]{"/"}; - } - - @Override - protected Filter[] getServletFilters() { - CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(CharEncoding.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 deleted file mode 100644 index c9d69f99..00000000 --- a/juick-rss/src/main/java/com/juick/rss/configuration/RssMvcConfiguration.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.juick.rss.configuration; - -import com.juick.rss.MessagesView; -import com.juick.rss.RepliesView; -import com.juick.server.configuration.BaseWebConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.view.BeanNameViewResolver; -import org.springframework.web.servlet.view.feed.AbstractRssFeedView; - -/** - * Created by vitalyster on 28.06.2016. - */ -@Configuration -@ComponentScan(basePackages = {"com.juick.rss"}) -public class RssMvcConfiguration extends BaseWebConfiguration { - @Bean - public BeanNameViewResolver beanNameViewResolver() { - return new BeanNameViewResolver(); - } - @Bean - AbstractRssFeedView messages() { - return new MessagesView(); - } - @Bean - AbstractRssFeedView replies() { - return 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 deleted file mode 100644 index 7555f128..00000000 --- a/juick-rss/src/main/java/com/juick/rss/controllers/FeedsController.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -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 - private MessagesService messagesService; - @Inject - private 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 mids = messagesService.getUserBlog(user.getUid(), 0, 0); - ModelAndView modelAndView = new ModelAndView(); - modelAndView.setViewName("messages"); - 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 mids = messagesService.getLastMessages(hours); - ModelAndView modelAndView = new ModelAndView(); - modelAndView.setViewName("messages"); - 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.setViewName("replies"); - 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 deleted file mode 100644 index d8c1ef1e..00000000 --- a/juick-rss/src/main/java/com/juick/rss/extension/JuickModule.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -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/JuickModuleGenerator.java b/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleGenerator.java deleted file mode 100644 index b38c2ab8..00000000 --- a/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleGenerator.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.juick.rss.extension; - -import com.rometools.rome.feed.module.Module; -import com.rometools.rome.io.ModuleGenerator; -import org.jdom2.Element; -import org.jdom2.Namespace; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -/** - * Created by vt on 13/12/2016. - */ -public class JuickModuleGenerator implements ModuleGenerator { - - private static final Namespace JUICK_NS = Namespace.getNamespace("juick", JuickModule.URI); - - @Override - public String getNamespaceUri() { - return JuickModule.URI; - } - - private static final Set NAMESPACES; - - static { - Set nss = new HashSet(); - nss.add(JUICK_NS); - NAMESPACES = Collections.unmodifiableSet(nss); - } - - @Override - public Set getNamespaces() { - return NAMESPACES; - } - - @Override - public void generate(Module module, Element element) { - // this is not necessary, it is done to avoid the namespace definition in every item. - Element root = element; - while (root.getParent()!=null && root.getParent() instanceof Element) { - root = (Element) element.getParent(); - } - root.addNamespaceDeclaration(JUICK_NS); - - JuickModule juickModule = (JuickModule) module; - if (juickModule.getUid() > 0) { - Element user = new Element("user", JUICK_NS); - user.setAttribute("uid", String.valueOf(juickModule.getUid()), JUICK_NS); - element.addContent(user); - } - } -} 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 deleted file mode 100644 index c2ae1fbe..00000000 --- a/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleImpl.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -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 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 deleted file mode 100644 index a11e50b5..00000000 --- a/juick-rss/src/main/java/com/juick/rss/extension/JuickModuleParser.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -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/main/resources/rome.properties b/juick-rss/src/main/resources/rome.properties deleted file mode 100644 index e57f6391..00000000 --- a/juick-rss/src/main/resources/rome.properties +++ /dev/null @@ -1,2 +0,0 @@ -rss_2.0.item.ModuleParser.classes=com.juick.rss.extension.JuickModuleParser -rss_2.0.item.ModuleGenerator.classes=com.juick.rss.extension.JuickModuleGenerator \ No newline at end of file diff --git a/juick-rss/src/main/webapp/WEB-INF/web.xml b/juick-rss/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 7e1c30d0..00000000 --- a/juick-rss/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - 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 deleted file mode 100644 index 7c356771..00000000 --- a/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.juick.rss.tests; - -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; -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.text.RandomStringGenerator; -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.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.*; - -/** - * Created by vitalyster on 13.12.2016. - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -@WebAppConfiguration -public class RSSTests { - @Configuration - @Import(value = {RssMvcConfiguration.class, RssAppConfiguration.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; - - final static RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('a', 'z').build(); - - private static Message getMessage(final User user, final String messageText) { - Message msg = new Message(); - - msg.setMid(1); - msg.setUser(user); - msg.setText(messageText == null ? generator.generate(24) : messageText); - msg.setTags(Collections.singletonList(new Tag(generator.generate(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 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, false)) - .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")) - .andExpect(xpath("/rss/channel/description").string("The latest messages at Juick")); - } -} diff --git a/juick-server/build.gradle b/juick-server/build.gradle index 8d894913..2642e9ee 100644 --- a/juick-server/build.gradle +++ b/juick-server/build.gradle @@ -24,6 +24,8 @@ dependencies { exclude group: 'xmlpull' } providedCompile 'xpp3:xpp3:1.1.4c' + compile 'com.rometools:rome:1.9.0' + compile 'com.rometools:rome-modules:1.9.0' testCompile project(path: ':juick-core', configuration: 'testArtifacts') testCompile project(path: ':juick-server-web', configuration: 'testArtifacts') diff --git a/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java b/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java new file mode 100644 index 00000000..51c32cac --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.api.rss; + +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 Feeds { + + @Inject + private MessagesService messagesService; + @Inject + private UserService userService; + + @RequestMapping(value = "/rss/{userName}/blog", method = RequestMethod.GET) + public ModelAndView getBlog(@PathVariable String userName) { + User user = userService.getUserByName(userName); + if (user.getUid() > 0) { + List mids = messagesService.getUserBlog(user.getUid(), 0, 0); + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("messages"); + modelAndView.addObject("user", user); + modelAndView.addObject("messages", messagesService.getMessages(mids)); + return modelAndView; + } + throw new HttpBadRequestException(); + } + + @RequestMapping(value = "/rss/", method = RequestMethod.GET) + public ModelAndView getLast(@RequestParam(value = "hours", required = false, defaultValue = "0") Integer hours) { + List mids = messagesService.getLastMessages(hours); + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("messages"); + modelAndView.addObject("messages", messagesService.getMessages(mids)); + return modelAndView; + } + @RequestMapping(value = "/rss/comments", method = RequestMethod.GET) + public ModelAndView getLastReplies(@RequestParam(value = "hours", required = false, defaultValue = "0") Integer hours) { + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("replies"); + modelAndView.addObject("messages", messagesService.getLastReplies(hours)); + return modelAndView; + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/rss/MessagesView.java b/juick-server/src/main/java/com/juick/server/api/rss/MessagesView.java new file mode 100644 index 00000000..f61757a6 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/rss/MessagesView.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.api.rss; + +import com.juick.Message; +import com.juick.User; +import com.juick.server.api.rss.extension.JuickModule; +import com.juick.server.api.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.Collections; +import java.util.Date; +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 buildFeedItems(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { + List msgs = (List)model.get("messages"); + return msgs.stream().map(this::createRssItem).collect(Collectors.toList()); + } + + @Override + protected void buildFeedMetadata(Map 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.setLinks(Collections.singletonList(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(StringUtils.defaultString(msg.getText())) + : MessageUtils.formatMessage(StringUtils.defaultString(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(Date.from(msg.getTimestamp())); + 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.getAttachment().getUrl()); + 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-server/src/main/java/com/juick/server/api/rss/RepliesView.java b/juick-server/src/main/java/com/juick/server/api/rss/RepliesView.java new file mode 100644 index 00000000..b53e9750 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/rss/RepliesView.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.api.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 buildFeedItems(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { + List msgs = (List)model.get("messages"); + return msgs.stream().map(this::createRssItem).collect(Collectors.toList()); + } + + @Override + protected void buildFeedMetadata(Map 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-server/src/main/java/com/juick/server/api/rss/extension/JuickModule.java b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModule.java new file mode 100644 index 00000000..a4198518 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModule.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.api.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-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleGenerator.java b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleGenerator.java new file mode 100644 index 00000000..eb103136 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleGenerator.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.api.rss.extension; + +import com.rometools.rome.feed.module.Module; +import com.rometools.rome.io.ModuleGenerator; +import org.jdom2.Element; +import org.jdom2.Namespace; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** + * Created by vt on 13/12/2016. + */ +public class JuickModuleGenerator implements ModuleGenerator { + + private static final Namespace JUICK_NS = Namespace.getNamespace("juick", JuickModule.URI); + + @Override + public String getNamespaceUri() { + return JuickModule.URI; + } + + private static final Set NAMESPACES; + + static { + Set nss = new HashSet(); + nss.add(JUICK_NS); + NAMESPACES = Collections.unmodifiableSet(nss); + } + + @Override + public Set getNamespaces() { + return NAMESPACES; + } + + @Override + public void generate(Module module, Element element) { + // this is not necessary, it is done to avoid the namespace definition in every item. + Element root = element; + while (root.getParent()!=null && root.getParent() instanceof Element) { + root = (Element) element.getParent(); + } + root.addNamespaceDeclaration(JUICK_NS); + + JuickModule juickModule = (JuickModule) module; + if (juickModule.getUid() > 0) { + Element user = new Element("user", JUICK_NS); + user.setAttribute("uid", String.valueOf(juickModule.getUid()), JUICK_NS); + element.addContent(user); + } + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleImpl.java b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleImpl.java new file mode 100644 index 00000000..dbdd8c85 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleImpl.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.api.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 getInterface() { + return JuickModule.class; + } + + @Override + public void copyFrom(CopyFrom obj) { + JuickModule juickModule = (JuickModule) obj; + setUid(juickModule.getUid()); + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleParser.java b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleParser.java new file mode 100644 index 00000000..a3d0e175 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/rss/extension/JuickModuleParser.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.api.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-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java index 55050f7e..ad417807 100644 --- a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java +++ b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java @@ -18,6 +18,8 @@ package com.juick.server.configuration; import com.juick.server.WebsocketManager; +import com.juick.server.api.rss.MessagesView; +import com.juick.server.api.rss.RepliesView; import com.juick.server.component.JuickServerComponent; import com.juick.server.component.JuickServerReconnectManager; import com.juick.service.UserService; @@ -32,6 +34,8 @@ import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.view.BeanNameViewResolver; +import org.springframework.web.servlet.view.feed.AbstractRssFeedView; import org.springframework.web.socket.client.WebSocketConnectionManager; import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.config.annotation.EnableWebSocket; @@ -149,6 +153,18 @@ public class ApiAppConfiguration extends BaseWebConfiguration implements WebSock cs.addConverter(new JidConverter()); return cs; } + @Bean + public BeanNameViewResolver beanNameViewResolver() { + return new BeanNameViewResolver(); + } + @Bean + AbstractRssFeedView messages() { + return new MessagesView(); + } + @Bean + AbstractRssFeedView replies() { + return new RepliesView(); + } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); diff --git a/juick-server/src/main/resources/rome.properties b/juick-server/src/main/resources/rome.properties new file mode 100644 index 00000000..fdb9aaa2 --- /dev/null +++ b/juick-server/src/main/resources/rome.properties @@ -0,0 +1,2 @@ +rss_2.0.item.ModuleParser.classes=com.juick.server.api.rss.extension.JuickModuleParser +rss_2.0.item.ModuleGenerator.classes=com.juick.server.api.rss.extension.JuickModuleGenerator \ No newline at end of file diff --git a/juick-server/src/test/java/com/juick/server/tests/RSSTests.java b/juick-server/src/test/java/com/juick/server/tests/RSSTests.java new file mode 100644 index 00000000..a5e05a08 --- /dev/null +++ b/juick-server/src/test/java/com/juick/server/tests/RSSTests.java @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.tests; + +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +import com.juick.server.configuration.ApiAppConfiguration; +import com.juick.service.*; +import org.apache.commons.text.RandomStringGenerator; +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.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.*; + +/** + * Created by vitalyster on 13.12.2016. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@WebAppConfiguration +public class RSSTests { + @Configuration + @Import(value = {ApiAppConfiguration.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); + } + @Bean + CrosspostService crosspostService() { + return Mockito.mock(CrosspostService.class); + } + @Bean + EmailService emailService() { + return Mockito.mock(EmailService.class); + } + @Bean + SubscriptionService subscriptionService() { + return Mockito.mock(SubscriptionService.class); + } + @Bean + MessengerService messengerService() { + return Mockito.mock(MessengerService.class); + } + @Bean + TelegramService telegramService() { + return Mockito.mock(TelegramService.class); + } + @Bean + PMQueriesService pmQueriesService() { + return Mockito.mock(PMQueriesService.class); + } + @Bean + ShowQueriesService showQueriesService() { + return Mockito.mock(ShowQueriesService.class); + } + @Bean + PrivacyQueriesService privacyQueriesService() { + return Mockito.mock(PrivacyQueriesService.class); + } + @Bean + PushQueriesService pushQueriesService() { + return Mockito.mock(PushQueriesService.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; + + final static RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('a', 'z').build(); + + private static Message getMessage(final User user, final String messageText) { + Message msg = new Message(); + + msg.setMid(1); + msg.setUser(user); + msg.setText(messageText == null ? generator.generate(24) : messageText); + msg.setTags(Collections.singletonList(new Tag(generator.generate(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 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, false)) + .thenReturn(Collections.singletonList(1)); + when(messagesService.getMessages(Collections.singletonList(1))) + .thenReturn(Collections.singletonList(msg)); + + mockMvc.perform( + get("/rss/")) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/rss+xml")) + .andExpect(xpath("/rss/channel/description").string("The latest messages at Juick")); + } +} diff --git a/settings.gradle b/settings.gradle index 005d592b..1dc8f277 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ rootProject.name = "Juick" -include ':juick-core', ':juick-server-core', ':juick-server-jdbc', ':juick-server-web', ':juick-server', ':juick-www', ':juick-rss', ':juick-notifications', ':juick-xmpp-wip' +include ':juick-core', ':juick-server-core', ':juick-server-jdbc', ':juick-server-web', ':juick-server', ':juick-www', ':juick-notifications', ':juick-xmpp-wip' -- cgit v1.2.3