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 public abstract class AbstractSoneCommand extends AbstractCommand {
47 private final Core core;
49 /** Whether this command needs write access. */
50 private final boolean writeAccess;
53 * Creates a new abstract Sone FCP command.
58 protected AbstractSoneCommand(Core core) {
63 * Creates a new abstract Sone FCP command.
68 * {@code true} if this command requires write access,
69 * {@code false} otherwise
71 protected AbstractSoneCommand(Core core, boolean writeAccess) {
73 this.writeAccess = writeAccess;
81 * Returns the Sone core.
83 * @return The Sone core
85 protected Core getCore() {
90 * Returns whether this command requires write access.
92 * @return {@code true} if this command require write access, {@code false}
95 public boolean requiresWriteAccess() {
104 * Encodes text in a way that makes it possible for the text to be stored in
105 * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
110 * @return The encoded text
112 protected static String encodeString(String text) {
113 return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
117 * Returns a Sone whose ID is a parameter in the given simple field set.
119 * @param simpleFieldSet
120 * The simple field set containing the ID of the Sone
121 * @param parameterName
122 * The name under which the Sone ID is stored in the simple field
125 * {@code true} to only return local Sones, {@code false} to
128 * @throws FcpException
129 * if there is no Sone ID stored under the given parameter name,
130 * or if the Sone ID is invalid
132 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException {
133 return getSone(simpleFieldSet, parameterName, localOnly, true).get();
137 * Returns a Sone whose ID is a parameter in the given simple field set.
139 * @param simpleFieldSet
140 * The simple field set containing the ID of the Sone
141 * @param parameterName
142 * The name under which the Sone ID is stored in the simple field
145 * {@code true} to only return local Sones, {@code false} to
148 * {@code true} if a valid Sone ID is required, {@code false}
150 * @return The Sone, or {@code null} if {@code mandatory} is {@code false}
151 * and the Sone ID is invalid
152 * @throws FcpException
153 * if there is no Sone ID stored under the given parameter name,
154 * or if {@code mandatory} is {@code true} and the Sone ID is
157 protected Optional<Sone> getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException {
158 String soneId = simpleFieldSet.get(parameterName);
159 if (mandatory && (soneId == null)) {
160 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
162 Sone sone = core.getSone(soneId);
163 if ((mandatory && (sone == null)) || ((sone != null) && localOnly && !sone.isLocal())) {
164 throw new FcpException("Could not load Sone from “" + soneId + "”.");
166 return Optional.fromNullable(sone);
170 * Returns a post whose ID is a parameter in the given simple field set.
172 * @param simpleFieldSet
173 * The simple field set containing the ID of the post
174 * @param parameterName
175 * The name under which the post ID is stored in the simple field
178 * @throws FcpException
179 * if there is no post ID stored under the given parameter name,
180 * or if the post ID is invalid
182 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
184 String postId = simpleFieldSet.getString(parameterName);
185 Post post = core.getPost(postId);
187 throw new FcpException("Could not load post from “" + postId + "”.");
190 } catch (FSParseException fspe1) {
191 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
196 * Returns a reply whose ID is a parameter in the given simple field set.
198 * @param simpleFieldSet
199 * The simple field set containing the ID of the reply
200 * @param parameterName
201 * The name under which the reply ID is stored in the simple
204 * @throws FcpException
205 * if there is no reply ID stored under the given parameter
206 * name, or if the reply ID is invalid
208 protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
210 String replyId = simpleFieldSet.getString(parameterName);
211 PostReply reply = core.getPostReply(replyId);
213 throw new FcpException("Could not load reply from “" + replyId + "”.");
216 } catch (FSParseException fspe1) {
217 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
222 * Creates a simple field set from the given Sone, including {@link Profile}
228 * The prefix for the field names (may be empty but not {@code
231 * An optional local Sone that is used for Sone-specific data,
232 * such as if the Sone is followed by the local Sone
233 * @return The simple field set containing the given Sone
235 protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional<Sone> localSone) {
236 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
238 soneBuilder.put(prefix + "ID", sone.getId());
239 soneBuilder.put(prefix + "Name", sone.getName());
240 soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
241 soneBuilder.put(prefix + "LastUpdated", sone.getTime());
242 if (localSone.isPresent()) {
243 soneBuilder.put(prefix + "Followed", String.valueOf(localSone.get().hasFriend(sone.getId())));
245 Profile profile = sone.getProfile();
246 soneBuilder.put(prefix + "Field.Count", profile.getFields().size());
248 for (Field field : profile.getFields()) {
249 soneBuilder.put(prefix + "Field." + fieldIndex + ".Name", field.getName());
250 soneBuilder.put(prefix + "Field." + fieldIndex + ".Value", field.getValue());
254 return soneBuilder.get();
258 * Creates a simple field set from the given collection of Sones.
261 * The Sones to encode
263 * The prefix for the field names (may be empty but not
265 * @return The simple field set containing the given Sones
267 protected static SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
268 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
271 soneBuilder.put(prefix + "Count", sones.size());
272 for (Sone sone : sones) {
273 String sonePrefix = prefix + soneIndex++ + ".";
274 soneBuilder.put(encodeSone(sone, sonePrefix, Optional.<Sone>absent()));
277 return soneBuilder.get();
281 * Creates a simple field set from the given post.
286 * The prefix for the field names (may be empty but not
288 * @param includeReplies
289 * {@code true} to include replies, {@code false} to not include
291 * @return The simple field set containing the post
293 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
294 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
296 postBuilder.put(prefix + "ID", post.getId());
297 postBuilder.put(prefix + "Sone", post.getSone().getId());
298 if (post.getRecipientId().isPresent()) {
299 postBuilder.put(prefix + "Recipient", post.getRecipientId().get());
301 postBuilder.put(prefix + "Time", post.getTime());
302 postBuilder.put(prefix + "Text", encodeString(post.getText()));
303 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
305 if (includeReplies) {
306 List<PostReply> replies = core.getReplies(post.getId());
307 postBuilder.put(encodeReplies(replies, prefix));
310 return postBuilder.get();
314 * Creates a simple field set from the given collection of posts.
317 * The posts to encode
319 * The prefix for the field names (may be empty but not
321 * @param includeReplies
322 * {@code true} to include the replies, {@code false} to not
323 * include the replies
324 * @return The simple field set containing the posts
326 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
327 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
330 postBuilder.put(prefix + "Count", posts.size());
331 for (Post post : posts) {
332 String postPrefix = prefix + postIndex++;
333 postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
336 return postBuilder.get();
340 * Creates a simple field set from the given collection of replies.
343 * The replies to encode
345 * The prefix for the field names (may be empty, but not
347 * @return The simple field set containing the replies
349 protected SimpleFieldSet encodeReplies(Collection<? extends PostReply> replies, String prefix) {
350 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
353 replyBuilder.put(prefix + "Replies.Count", replies.size());
354 for (PostReply reply : replies) {
355 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
356 replyBuilder.put(replyPrefix + "ID", reply.getId());
357 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
358 replyBuilder.put(replyPrefix + "Time", reply.getTime());
359 replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
360 replyBuilder.put(encodeLikes(core.getLikes(reply), replyPrefix + "Likes."));
363 return replyBuilder.get();
367 * Creates a simple field set from the given collection of Sones that like
373 * The prefix for the field names (may be empty but not
375 * @return The simple field set containing the likes
377 protected static SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
378 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
381 likesBuilder.put(prefix + "Count", likes.size());
382 for (Sone sone : likes) {
383 String sonePrefix = prefix + likeIndex++ + ".";
384 likesBuilder.put(sonePrefix + "ID", sone.getId());
387 return likesBuilder.get();
398 public String toString() {
399 return getClass().getName() + "[writeAccess=" + writeAccess + "]";