Merge branch 'next' into dev/image
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / GetPostFeedCommand.java
1 /*
2  * Sone - GetPostFeedCommand.java - Copyright © 2011 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.fcp;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import net.pterodactylus.sone.core.Core;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.freenet.fcp.FcpException;
30 import net.pterodactylus.util.filter.Filters;
31 import freenet.support.SimpleFieldSet;
32 import freenet.support.api.Bucket;
33
34 /**
35  * Implementation of an FCP interface for other clients or plugins to
36  * communicate with Sone.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class GetPostFeedCommand extends AbstractSoneCommand {
41
42         /**
43          * Creates a new “GetPostFeed” command.
44          *
45          * @param core
46          *            The core
47          */
48         public GetPostFeedCommand(Core core) {
49                 super(core);
50         }
51
52         /**
53          * {@inheritDoc}
54          */
55         @Override
56         public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
57                 Sone sone = getSone(parameters, "Sone", true);
58                 int startPost = getInt(parameters, "StartPost", 0);
59                 int maxPosts = getInt(parameters, "MaxPosts", -1);
60
61                 Set<Post> allPosts = new HashSet<Post>();
62                 allPosts.addAll(sone.getPosts());
63                 for (String friendSoneId : sone.getFriends()) {
64                         if (!getCore().hasSone(friendSoneId)) {
65                                 continue;
66                         }
67                         allPosts.addAll(getCore().getSone(friendSoneId).getPosts());
68                 }
69                 allPosts.addAll(getCore().getDirectedPosts(sone));
70                 allPosts = Filters.filteredSet(allPosts, Post.FUTURE_POSTS_FILTER);
71
72                 List<Post> sortedPosts = new ArrayList<Post>(allPosts);
73                 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
74
75                 if (sortedPosts.size() < startPost) {
76                         return new Response("PostFeed", encodePosts(Collections.<Post> emptyList(), "Posts.", false));
77                 }
78
79                 return new Response("PostFeed", encodePosts(sortedPosts.subList(startPost, (maxPosts == -1) ? sortedPosts.size() : Math.min(startPost + maxPosts, sortedPosts.size())), "Posts.", true));
80         }
81
82 }