2 * Sone - GetPostFeedCommand.java - Copyright © 2011–2019 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
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;
31 import com.google.common.collect.Collections2;
33 import freenet.support.SimpleFieldSet;
36 * Implementation of an FCP interface for other clients or plugins to
37 * communicate with Sone.
39 public class GetPostFeedCommand extends AbstractSoneCommand {
42 * Creates a new “GetPostFeed” command.
47 public GetPostFeedCommand(Core core) {
55 public Response execute(SimpleFieldSet parameters) throws FcpException {
56 Sone sone = getSone(parameters, "Sone", true);
57 int startPost = getInt(parameters, "StartPost", 0);
58 int maxPosts = getInt(parameters, "MaxPosts", -1);
60 Collection<Post> allPosts = new HashSet<>();
61 allPosts.addAll(sone.getPosts());
62 for (String friendSoneId : sone.getFriends()) {
63 Sone friendSone = getCore().getSone(friendSoneId);
64 if (friendSone == null) {
67 allPosts.addAll(friendSone.getPosts());
69 allPosts.addAll(getCore().getDirectedPosts(sone.getId()));
70 allPosts = Collections2.filter(allPosts, Post.FUTURE_POSTS_FILTER);
72 List<Post> sortedPosts = new ArrayList<>(allPosts);
73 Collections.sort(sortedPosts, Post.NEWEST_FIRST);
75 if (sortedPosts.size() < startPost) {
76 return new Response("PostFeed", encodePosts(Collections.<Post> emptyList(), "Posts.", false));
79 return new Response("PostFeed", encodePosts(sortedPosts.subList(startPost, (maxPosts == -1) ? sortedPosts.size() : Math.min(startPost + maxPosts, sortedPosts.size())), "Posts.", true));