Refactoring.
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / GetPostFeedCommand.java
1 /*
2  * Sone - GetPostFeedCommand.java - Copyright © 2011–2013 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 static com.google.common.base.Optional.presentInstances;
21 import static com.google.common.collect.FluentIterable.from;
22 import static net.pterodactylus.sone.data.Sone.TO_POSTS;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28
29 import net.pterodactylus.sone.core.Core;
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.freenet.fcp.FcpException;
33
34 import freenet.support.SimpleFieldSet;
35 import freenet.support.api.Bucket;
36
37 /**
38  * Implementation of an FCP interface for other clients or plugins to
39  * communicate with Sone.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class GetPostFeedCommand extends AbstractSoneCommand {
44
45         /**
46          * Creates a new “GetPostFeed” command.
47          *
48          * @param core
49          *            The core
50          */
51         public GetPostFeedCommand(Core core) {
52                 super(core);
53         }
54
55         @Override
56         public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
57                 Sone sone = getMandatoryLocalSone(parameters, "Sone");
58                 int startPost = getInt(parameters, "StartPost", 0);
59                 int maxPosts = getInt(parameters, "MaxPosts", -1);
60
61                 List<Post> sortedPosts = from(collectAllPostsForSone(sone)).filter(Post.FUTURE_POSTS_FILTER).toSortedList(Post.TIME_COMPARATOR);
62
63                 if (sortedPosts.size() < startPost) {
64                         return new Response("PostFeed", encodePosts(Collections.<Post>emptyList(), "Posts."));
65                 }
66
67                 return new Response("PostFeed", encodePostsWithReplies(sortedPosts.subList(startPost, (maxPosts == -1) ? sortedPosts.size() : Math.min(startPost + maxPosts, sortedPosts.size())), "Posts."));
68         }
69
70         private Collection<Post> collectAllPostsForSone(Sone sone) {
71                 Collection<Post> allPosts = new HashSet<Post>();
72                 allPosts.addAll(sone.getPosts());
73                 allPosts.addAll(from(presentInstances(from(sone.getFriends()).transform(getCore().getDatabase().getSone()))).transformAndConcat(TO_POSTS).toList());
74                 allPosts.addAll(getCore().getDatabase().getDirectedPosts(sone.getId()));
75                 return allPosts;
76         }
77
78 }