2 * Sone - AbstractSoneCommand.java - Copyright © 2011–2013 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.Reply;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
31 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
32 import net.pterodactylus.sone.freenet.fcp.Command;
33 import net.pterodactylus.sone.freenet.fcp.FcpException;
34 import net.pterodactylus.sone.template.SoneAccessor;
36 import com.google.common.base.Optional;
37 import com.google.common.collect.Collections2;
39 import freenet.node.FSParseException;
40 import freenet.support.SimpleFieldSet;
43 * Abstract base implementation of a {@link Command} with Sone-related helper
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public abstract class AbstractSoneCommand extends AbstractCommand {
51 private final Core core;
53 /** Whether this command needs write access. */
54 private final boolean writeAccess;
57 * Creates a new abstract Sone FCP command.
62 protected AbstractSoneCommand(Core core) {
67 * Creates a new abstract Sone FCP command.
72 * {@code true} if this command requires write access,
73 * {@code false} otherwise
75 protected AbstractSoneCommand(Core core, boolean writeAccess) {
77 this.writeAccess = writeAccess;
85 * Returns the Sone core.
87 * @return The Sone core
89 protected Core getCore() {
94 * Returns whether this command requires write access.
96 * @return {@code true} if this command require write access, {@code false}
99 public boolean requiresWriteAccess() {
108 * Encodes text in a way that makes it possible for the text to be stored in
109 * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
114 * @return The encoded text
116 protected static String encodeString(String text) {
117 return text.replaceAll("\\\\", "\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
121 * Returns a Sone whose ID is a parameter in the given simple field set.
123 * @param simpleFieldSet
124 * The simple field set containing the ID of the Sone
125 * @param parameterName
126 * The name under which the Sone ID is stored in the simple field
129 * {@code true} to only return local Sones, {@code false} to
132 * @throws FcpException
133 * if there is no Sone ID stored under the given parameter name,
134 * or if the Sone ID is invalid
136 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException {
137 return getSone(simpleFieldSet, parameterName, localOnly, true).get();
141 * Returns a Sone whose ID is a parameter in the given simple field set.
143 * @param simpleFieldSet
144 * The simple field set containing the ID of the Sone
145 * @param parameterName
146 * The name under which the Sone ID is stored in the simple field
149 * {@code true} to only return local Sones, {@code false} to
152 * {@code true} if a valid Sone ID is required, {@code false}
154 * @return The Sone, or {@code null} if {@code mandatory} is {@code false}
155 * and the Sone ID is invalid
156 * @throws FcpException
157 * if there is no Sone ID stored under the given parameter name,
158 * or if {@code mandatory} is {@code true} and the Sone ID is
161 protected Optional<Sone> getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException {
162 String soneId = simpleFieldSet.get(parameterName);
163 if (mandatory && (soneId == null)) {
164 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
166 Optional<Sone> sone = core.getSone(soneId);
167 if ((mandatory && !sone.isPresent()) || (mandatory && sone.isPresent() && (localOnly && !sone.get().isLocal()))) {
168 throw new FcpException("Could not load Sone from “" + soneId + "”.");
174 * Returns a post whose ID is a parameter in the given simple field set.
176 * @param simpleFieldSet
177 * The simple field set containing the ID of the post
178 * @param parameterName
179 * The name under which the post ID is stored in the simple field
182 * @throws FcpException
183 * if there is no post ID stored under the given parameter name,
184 * or if the post ID is invalid
186 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
188 String postId = simpleFieldSet.getString(parameterName);
189 Optional<Post> post = core.getPost(postId);
190 if (!post.isPresent()) {
191 throw new FcpException("Could not load post from “" + postId + "”.");
194 } catch (FSParseException fspe1) {
195 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
200 * Returns a reply whose ID is a parameter in the given simple field set.
202 * @param simpleFieldSet
203 * The simple field set containing the ID of the reply
204 * @param parameterName
205 * The name under which the reply ID is stored in the simple
208 * @throws FcpException
209 * if there is no reply ID stored under the given parameter
210 * name, or if the reply ID is invalid
212 protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
214 String replyId = simpleFieldSet.getString(parameterName);
215 Optional<PostReply> reply = core.getPostReply(replyId);
216 if (!reply.isPresent()) {
217 throw new FcpException("Could not load reply from “" + replyId + "”.");
220 } catch (FSParseException fspe1) {
221 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
226 * Creates a simple field set from the given Sone, including {@link Profile}
232 * The prefix for the field names (may be empty but not {@code
235 * An optional local Sone that is used for Sone-specific data,
236 * such as if the Sone is followed by the local Sone
237 * @return The simple field set containing the given Sone
239 protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional<Sone> localSone) {
240 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
242 soneBuilder.put(prefix + "Name", sone.getName());
243 soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
244 soneBuilder.put(prefix + "LastUpdated", sone.getTime());
245 if (localSone.isPresent()) {
246 soneBuilder.put(prefix + "Followed", String.valueOf(localSone.get().hasFriend(sone.getId())));
248 Profile profile = sone.getProfile();
249 soneBuilder.put(prefix + "Field.Count", profile.getFields().size());
251 for (Field field : profile.getFields()) {
252 soneBuilder.put(prefix + "Field." + fieldIndex + ".Name", field.getName());
253 soneBuilder.put(prefix + "Field." + fieldIndex + ".Value", field.getValue());
257 return soneBuilder.get();
261 * Creates a simple field set from the given collection of Sones.
264 * The Sones to encode
266 * The prefix for the field names (may be empty but not
268 * @return The simple field set containing the given Sones
270 protected static SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
271 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
274 soneBuilder.put(prefix + "Count", sones.size());
275 for (Sone sone : sones) {
276 String sonePrefix = prefix + soneIndex++ + ".";
277 soneBuilder.put(sonePrefix + "ID", sone.getId());
278 soneBuilder.put(sonePrefix + "Name", sone.getName());
279 soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
280 soneBuilder.put(sonePrefix + "Time", sone.getTime());
283 return soneBuilder.get();
287 * Creates a simple field set from the given post.
292 * The prefix for the field names (may be empty but not
294 * @param includeReplies
295 * {@code true} to include replies, {@code false} to not include
297 * @return The simple field set containing the post
299 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
300 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
302 postBuilder.put(prefix + "ID", post.getId());
303 postBuilder.put(prefix + "Sone", post.getSone().getId());
304 if (post.getRecipientId().isPresent()) {
305 postBuilder.put(prefix + "Recipient", post.getRecipientId().get());
307 postBuilder.put(prefix + "Time", post.getTime());
308 postBuilder.put(prefix + "Text", encodeString(post.getText()));
309 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
311 if (includeReplies) {
312 List<PostReply> replies = core.getReplies(post.getId());
313 postBuilder.put(encodeReplies(replies, prefix));
316 return postBuilder.get();
320 * Creates a simple field set from the given collection of posts.
323 * The posts to encode
325 * The prefix for the field names (may be empty but not
327 * @param includeReplies
328 * {@code true} to include the replies, {@code false} to not
329 * include the replies
330 * @return The simple field set containing the posts
332 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
333 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
336 postBuilder.put(prefix + "Count", posts.size());
337 for (Post post : posts) {
338 String postPrefix = prefix + postIndex++;
339 postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
340 if (includeReplies) {
341 postBuilder.put(encodeReplies(Collections2.filter(core.getReplies(post.getId()), Reply.FUTURE_REPLY_FILTER), postPrefix + "."));
345 return postBuilder.get();
349 * Creates a simple field set from the given collection of replies.
352 * The replies to encode
354 * The prefix for the field names (may be empty, but not
356 * @return The simple field set containing the replies
358 protected static SimpleFieldSet encodeReplies(Collection<? extends PostReply> replies, String prefix) {
359 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
362 replyBuilder.put(prefix + "Replies.Count", replies.size());
363 for (PostReply reply : replies) {
364 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
365 replyBuilder.put(replyPrefix + "ID", reply.getId());
366 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
367 replyBuilder.put(replyPrefix + "Time", reply.getTime());
368 replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
371 return replyBuilder.get();
375 * Creates a simple field set from the given collection of Sones that like
381 * The prefix for the field names (may be empty but not
383 * @return The simple field set containing the likes
385 protected static SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
386 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
389 likesBuilder.put(prefix + "Count", likes.size());
390 for (Sone sone : likes) {
391 String sonePrefix = prefix + likeIndex++ + ".";
392 likesBuilder.put(sonePrefix + "ID", sone.getId());
395 return likesBuilder.get();
406 public String toString() {
407 return getClass().getName() + "[writeAccess=" + writeAccess + "]";