aboutsummaryrefslogtreecommitdiff
path: root/juick-rss/src/main/java/com/juick/rss/RepliesView.java
blob: bdbab240a4c3676df9c6b1867af718ce34cd1234 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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;
    }
}