aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/www/rss
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/www/rss')
-rw-r--r--src/main/java/com/juick/www/rss/Feeds.java76
-rw-r--r--src/main/java/com/juick/www/rss/MessagesView.java158
-rw-r--r--src/main/java/com/juick/www/rss/RepliesView.java111
-rw-r--r--src/main/java/com/juick/www/rss/extension/JuickModule.java33
-rw-r--r--src/main/java/com/juick/www/rss/extension/JuickModuleGenerator.java66
-rw-r--r--src/main/java/com/juick/www/rss/extension/JuickModuleImpl.java54
-rw-r--r--src/main/java/com/juick/www/rss/extension/JuickModuleParser.java42
7 files changed, 540 insertions, 0 deletions
diff --git a/src/main/java/com/juick/www/rss/Feeds.java b/src/main/java/com/juick/www/rss/Feeds.java
new file mode 100644
index 00000000..42cadc5d
--- /dev/null
+++ b/src/main/java/com/juick/www/rss/Feeds.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2008-2019, 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.www.rss;
+
+import com.juick.model.User;
+import com.juick.server.util.HttpNotFoundException;
+import com.juick.service.MessagesService;
+import com.juick.service.UserService;
+import com.juick.service.security.annotation.Visitor;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+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;
+
+ @GetMapping("/rss/{userName}/blog")
+ public ModelAndView getBlog(@Visitor User visitor, @PathVariable String userName) {
+ User user = userService.getUserByName(userName);
+ if (!user.isAnonymous() && !user.isBanned()) {
+ List<Integer> mids = messagesService.getUserBlog(user.getUid(), 0, 0);
+ ModelAndView modelAndView = new ModelAndView();
+ modelAndView.setViewName("messagesView");
+ modelAndView.addObject("user", user);
+ modelAndView.addObject("messages", messagesService.getMessages(visitor, mids));
+ return modelAndView;
+ }
+ throw new HttpNotFoundException();
+ }
+
+ @GetMapping("/rss/")
+ public ModelAndView getLast(
+ @Visitor User visitor,
+ @RequestParam(value = "hours", required = false, defaultValue = "0") Integer hours) {
+ List<Integer> mids = messagesService.getLastMessages(hours);
+ ModelAndView modelAndView = new ModelAndView();
+ modelAndView.setViewName("messagesView");
+ modelAndView.addObject("messages", messagesService.getMessages(visitor, mids));
+ return modelAndView;
+ }
+ @GetMapping("/rss/comments")
+ public ModelAndView getLastReplies(@RequestParam(value = "hours", required = false, defaultValue = "0") Integer hours) {
+ ModelAndView modelAndView = new ModelAndView();
+ modelAndView.setViewName("repliesView");
+ modelAndView.addObject("messages", messagesService.getLastReplies(hours));
+ return modelAndView;
+ }
+}
diff --git a/src/main/java/com/juick/www/rss/MessagesView.java b/src/main/java/com/juick/www/rss/MessagesView.java
new file mode 100644
index 00000000..d9c5a6b7
--- /dev/null
+++ b/src/main/java/com/juick/www/rss/MessagesView.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2008-2019, 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.www.rss;
+
+import com.juick.model.Message;
+import com.juick.model.User;
+import com.juick.www.rss.extension.JuickModule;
+import com.juick.www.rss.extension.JuickModuleImpl;
+import com.juick.www.WebApp;
+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.annotation.Nonnull;
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+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);
+
+ @Inject
+ private WebApp webApp;
+
+ @PostConstruct
+ public void init() {
+ setContentType("application/rss+xml;charset=UTF-8");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected List<Item> buildFeedItems(@Nonnull Map<String, Object> model,
+ @Nonnull HttpServletRequest request,
+ @Nonnull HttpServletResponse response) {
+ 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(webApp.getAvatarUrl(user));
+ 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(), MessageUtils.getTagsString(msg));
+ 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.getCreated()));
+ 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(MessageUtils.attachmentUrl(msg));
+ 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/src/main/java/com/juick/www/rss/RepliesView.java b/src/main/java/com/juick/www/rss/RepliesView.java
new file mode 100644
index 00000000..2e5b9f67
--- /dev/null
+++ b/src/main/java/com/juick/www/rss/RepliesView.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2008-2019, 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.www.rss;
+
+import com.juick.model.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.annotation.Nonnull;
+import javax.annotation.PostConstruct;
+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);
+
+ @PostConstruct
+ public void init() {
+ setContentType("application/rss+xml;charset=UTF-8");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected @Nonnull List<Item> buildFeedItems(@Nonnull Map<String, Object> model,
+ @Nonnull HttpServletRequest request,
+ @Nonnull HttpServletResponse response) {
+ 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/m/%d#%d", msg.getMid(), msg.getRid());
+ String messageTitle = String.format("@%s:", msg.getUname());
+ String messageDescription = msg.isHtml() ? msg.getDescription() : 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/src/main/java/com/juick/www/rss/extension/JuickModule.java b/src/main/java/com/juick/www/rss/extension/JuickModule.java
new file mode 100644
index 00000000..6b24390a
--- /dev/null
+++ b/src/main/java/com/juick/www/rss/extension/JuickModule.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2008-2019, 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.www.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/src/main/java/com/juick/www/rss/extension/JuickModuleGenerator.java b/src/main/java/com/juick/www/rss/extension/JuickModuleGenerator.java
new file mode 100644
index 00000000..61368785
--- /dev/null
+++ b/src/main/java/com/juick/www/rss/extension/JuickModuleGenerator.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2008-2019, 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.www.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.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<Namespace> NAMESPACES;
+
+ static {
+ NAMESPACES = Set.of(JUICK_NS);
+ }
+
+ @Override
+ public Set<Namespace> 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.getParentElement();
+ }
+ 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/src/main/java/com/juick/www/rss/extension/JuickModuleImpl.java b/src/main/java/com/juick/www/rss/extension/JuickModuleImpl.java
new file mode 100644
index 00000000..ea6c4a66
--- /dev/null
+++ b/src/main/java/com/juick/www/rss/extension/JuickModuleImpl.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2008-2019, 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.www.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/src/main/java/com/juick/www/rss/extension/JuickModuleParser.java b/src/main/java/com/juick/www/rss/extension/JuickModuleParser.java
new file mode 100644
index 00000000..fbf8c8bb
--- /dev/null
+++ b/src/main/java/com/juick/www/rss/extension/JuickModuleParser.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2008-2019, 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.www.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;
+ }
+}