2 * Sone - AbstractSoneCommand.java - Copyright © 2011–2016 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.Collection;
21 import java.util.List;
23 import net.pterodactylus.sone.core.Core;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.PostReply;
26 import net.pterodactylus.sone.data.Profile;
27 import net.pterodactylus.sone.data.Profile.Field;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
30 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
31 import net.pterodactylus.sone.freenet.fcp.Command;
32 import net.pterodactylus.sone.freenet.fcp.FcpException;
33 import net.pterodactylus.sone.template.SoneAccessor;
35 import freenet.node.FSParseException;
36 import freenet.support.SimpleFieldSet;
38 import com.google.common.base.Optional;
41 * Abstract base implementation of a {@link Command} with Sone-related helper
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public abstract class AbstractSoneCommand extends AbstractCommand {
49 private final Core core;
51 /** Whether this command needs write access. */
52 private final boolean writeAccess;
55 * Creates a new abstract Sone FCP command.
60 protected AbstractSoneCommand(Core core) {
65 * Creates a new abstract Sone FCP command.
70 * {@code true} if this command requires write access,
71 * {@code false} otherwise
73 protected AbstractSoneCommand(Core core, boolean writeAccess) {
75 this.writeAccess = writeAccess;
83 * Returns the Sone core.
85 * @return The Sone core
87 protected Core getCore() {
92 * Returns whether this command requires write access.
94 * @return {@code true} if this command require write access, {@code false}
97 public boolean requiresWriteAccess() {
106 * Encodes text in a way that makes it possible for the text to be stored in
107 * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
112 * @return The encoded text
114 protected static String encodeString(String text) {
115 return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
119 * Returns a Sone whose ID is a parameter in the given simple field set.
121 * @param simpleFieldSet
122 * The simple field set containing the ID of the Sone
123 * @param parameterName
124 * The name under which the Sone ID is stored in the simple field
127 * {@code true} to only return local Sones, {@code false} to
130 * @throws FcpException
131 * if there is no Sone ID stored under the given parameter name,
132 * or if the Sone ID is invalid
134 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException {
135 return getSone(simpleFieldSet, parameterName, localOnly, true).get();
139 * Returns a Sone whose ID is a parameter in the given simple field set.
141 * @param simpleFieldSet
142 * The simple field set containing the ID of the Sone
143 * @param parameterName
144 * The name under which the Sone ID is stored in the simple field
147 * {@code true} to only return local Sones, {@code false} to
150 * {@code true} if a valid Sone ID is required, {@code false}
152 * @return The Sone, or {@code null} if {@code mandatory} is {@code false}
153 * and the Sone ID is invalid
154 * @throws FcpException
155 * if there is no Sone ID stored under the given parameter name,
156 * or if {@code mandatory} is {@code true} and the Sone ID is
159 protected Optional<Sone> getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException {
160 String soneId = simpleFieldSet.get(parameterName);
161 if (mandatory && (soneId == null)) {
162 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
164 Optional<Sone> sone = core.getSone(soneId);
165 if ((mandatory && !sone.isPresent()) || (mandatory && sone.isPresent() && (localOnly && !sone.get().isLocal()))) {
166 throw new FcpException("Could not load Sone from “" + soneId + "”.");
172 * Returns a post whose ID is a parameter in the given simple field set.
174 * @param simpleFieldSet
175 * The simple field set containing the ID of the post
176 * @param parameterName
177 * The name under which the post ID is stored in the simple field
180 * @throws FcpException
181 * if there is no post ID stored under the given parameter name,
182 * or if the post ID is invalid
184 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
186 String postId = simpleFieldSet.getString(parameterName);
187 Optional<Post> post = core.getPost(postId);
188 if (!post.isPresent()) {
189 throw new FcpException("Could not load post from “" + postId + "”.");
192 } catch (FSParseException fspe1) {
193 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
198 * Returns a reply whose ID is a parameter in the given simple field set.
200 * @param simpleFieldSet
201 * The simple field set containing the ID of the reply
202 * @param parameterName
203 * The name under which the reply ID is stored in the simple
206 * @throws FcpException
207 * if there is no reply ID stored under the given parameter
208 * name, or if the reply ID is invalid
210 protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
212 String replyId = simpleFieldSet.getString(parameterName);
213 Optional<PostReply> reply = core.getPostReply(replyId);
214 if (!reply.isPresent()) {
215 throw new FcpException("Could not load reply from “" + replyId + "”.");
218 } catch (FSParseException fspe1) {
219 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
224 * Creates a simple field set from the given Sone, including {@link Profile}
230 * The prefix for the field names (may be empty but not {@code
233 * An optional local Sone that is used for Sone-specific data,
234 * such as if the Sone is followed by the local Sone
235 * @return The simple field set containing the given Sone
237 protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional<Sone> localSone) {
238 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
240 soneBuilder.put(prefix + "Name", sone.getName());
241 soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
242 soneBuilder.put(prefix + "LastUpdated", sone.getTime());
243 if (localSone.isPresent()) {
244 soneBuilder.put(prefix + "Followed", String.valueOf(localSone.get().hasFriend(sone.getId())));
246 Profile profile = sone.getProfile();
247 soneBuilder.put(prefix + "Field.Count", profile.getFields().size());
249 for (Field field : profile.getFields()) {
250 soneBuilder.put(prefix + "Field." + fieldIndex + ".Name", field.getName());
251 soneBuilder.put(prefix + "Field." + fieldIndex + ".Value", field.getValue());
255 return soneBuilder.get();
259 * Creates a simple field set from the given collection of Sones.
262 * The Sones to encode
264 * The prefix for the field names (may be empty but not
266 * @return The simple field set containing the given Sones
268 protected static SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
269 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
272 soneBuilder.put(prefix + "Count", sones.size());
273 for (Sone sone : sones) {
274 String sonePrefix = prefix + soneIndex++ + ".";
275 soneBuilder.put(sonePrefix + "ID", sone.getId());
276 soneBuilder.put(sonePrefix + "Name", sone.getName());
277 soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
278 soneBuilder.put(sonePrefix + "Time", sone.getTime());
281 return soneBuilder.get();
285 * Creates a simple field set from the given post.
290 * The prefix for the field names (may be empty but not
292 * @param includeReplies
293 * {@code true} to include replies, {@code false} to not include
295 * @return The simple field set containing the post
297 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
298 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
300 postBuilder.put(prefix + "ID", post.getId());
301 postBuilder.put(prefix + "Sone", post.getSone().getId());
302 if (post.getRecipientId().isPresent()) {
303 postBuilder.put(prefix + "Recipient", post.getRecipientId().get());
305 postBuilder.put(prefix + "Time", post.getTime());
306 postBuilder.put(prefix + "Text", encodeString(post.getText()));
307 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
309 if (includeReplies) {
310 List<PostReply> replies = core.getReplies(post.getId());
311 postBuilder.put(encodeReplies(replies, prefix));
314 return postBuilder.get();
318 * Creates a simple field set from the given collection of posts.
321 * The posts to encode
323 * The prefix for the field names (may be empty but not
325 * @param includeReplies
326 * {@code true} to include the replies, {@code false} to not
327 * include the replies
328 * @return The simple field set containing the posts
330 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
331 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
334 postBuilder.put(prefix + "Count", posts.size());
335 for (Post post : posts) {
336 String postPrefix = prefix + postIndex++;
337 postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
340 return postBuilder.get();
344 * Creates a simple field set from the given collection of replies.
347 * The replies to encode
349 * The prefix for the field names (may be empty, but not
351 * @return The simple field set containing the replies
353 protected SimpleFieldSet encodeReplies(Collection<? extends PostReply> replies, String prefix) {
354 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
357 replyBuilder.put(prefix + "Replies.Count", replies.size());
358 for (PostReply reply : replies) {
359 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
360 replyBuilder.put(replyPrefix + "ID", reply.getId());
361 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
362 replyBuilder.put(replyPrefix + "Time", reply.getTime());
363 replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
364 replyBuilder.put(encodeLikes(core.getLikes(reply), replyPrefix + "Likes."));
367 return replyBuilder.get();
371 * Creates a simple field set from the given collection of Sones that like
377 * The prefix for the field names (may be empty but not
379 * @return The simple field set containing the likes
381 protected static SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
382 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
385 likesBuilder.put(prefix + "Count", likes.size());
386 for (Sone sone : likes) {
387 String sonePrefix = prefix + likeIndex++ + ".";
388 likesBuilder.put(sonePrefix + "ID", sone.getId());
391 return likesBuilder.get();
402 public String toString() {
403 return getClass().getName() + "[writeAccess=" + writeAccess + "]";