🔀 Merge branch 'release/v82'
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / GetPostFeedCommand.java
1 /*
2  * Sone - GetPostFeedCommand.java - Copyright Â© 2011–2020 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.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
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
31 import com.google.common.collect.Collections2;
32
33 import freenet.support.SimpleFieldSet;
34
35 import static net.pterodactylus.sone.data.PostKt.newestPostFirst;
36 import static net.pterodactylus.sone.data.PostKt.noFuturePost;
37
38 /**
39  * Implementation of an FCP interface for other clients or plugins to
40  * communicate with Sone.
41  */
42 public class GetPostFeedCommand extends AbstractSoneCommand {
43
44         /**
45          * Creates a new â€śGetPostFeed” command.
46          *
47          * @param core
48          *            The core
49          */
50         public GetPostFeedCommand(Core core) {
51                 super(core);
52         }
53
54         /**
55          * {@inheritDoc}
56          */
57         @Override
58         public Response execute(SimpleFieldSet parameters) throws FcpException {
59                 Sone sone = getSone(parameters, "Sone", true);
60                 int startPost = getInt(parameters, "StartPost", 0);
61                 int maxPosts = getInt(parameters, "MaxPosts", -1);
62
63                 Collection<Post> allPosts = new HashSet<>();
64                 allPosts.addAll(sone.getPosts());
65                 for (String friendSoneId : sone.getFriends()) {
66                         Sone friendSone = getCore().getSone(friendSoneId);
67                         if (friendSone == null) {
68                                 continue;
69                         }
70                         allPosts.addAll(friendSone.getPosts());
71                 }
72                 allPosts.addAll(getCore().getDirectedPosts(sone.getId()));
73                 allPosts = Collections2.filter(allPosts, noFuturePost()::invoke);
74
75                 List<Post> sortedPosts = new ArrayList<>(allPosts);
76                 sortedPosts.sort(newestPostFirst());
77
78                 if (sortedPosts.size() < startPost) {
79                         return new Response("PostFeed", encodePosts(Collections.<Post> emptyList(), "Posts.", false));
80                 }
81
82                 return new Response("PostFeed", encodePosts(sortedPosts.subList(startPost, (maxPosts == -1) ? sortedPosts.size() : Math.min(startPost + maxPosts, sortedPosts.size())), "Posts.", true));
83         }
84
85 }