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 static com.google.common.collect.FluentIterable.from;
21 import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER;
23 import java.util.Collection;
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.PostReply;
29 import net.pterodactylus.sone.data.Profile;
30 import net.pterodactylus.sone.data.Profile.Field;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
33 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
34 import net.pterodactylus.sone.freenet.fcp.Command;
35 import net.pterodactylus.sone.freenet.fcp.FcpException;
36 import net.pterodactylus.sone.template.SoneAccessor;
38 import freenet.node.FSParseException;
39 import freenet.support.SimpleFieldSet;
41 import com.google.common.base.Optional;
44 * Abstract base implementation of a {@link Command} with Sone-related helper
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public abstract class AbstractSoneCommand extends AbstractCommand {
52 private final Core core;
54 /** Whether this command needs write access. */
55 private final boolean writeAccess;
58 * Creates a new abstract Sone FCP command.
63 protected AbstractSoneCommand(Core core) {
68 * Creates a new abstract Sone FCP command.
73 * {@code true} if this command requires write access,
74 * {@code false} otherwise
76 protected AbstractSoneCommand(Core core, boolean writeAccess) {
78 this.writeAccess = writeAccess;
86 * Returns the Sone core.
88 * @return The Sone core
90 protected Core getCore() {
95 * Returns whether this command requires write access.
97 * @return {@code true} if this command require write access, {@code false}
100 public boolean requiresWriteAccess() {
109 * Encodes text in a way that makes it possible for the text to be stored in
110 * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
115 * @return The encoded text
117 protected static String encodeString(String text) {
118 return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
121 protected Optional<Sone> getOptionalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
122 String soneId = getMandatoryParameter(simpleFieldSet, parameterName);
123 return core.getSone(soneId);
126 protected Sone getMandatoryLocalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
127 Sone sone = getMandatorySone(simpleFieldSet, parameterName);
128 if (!sone.isLocal()) {
129 throw new FcpException("Could not load Sone from “" + sone.getId() + "”.");
134 protected Sone getMandatorySone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
135 String soneId = getMandatoryParameter(simpleFieldSet, parameterName);
136 Optional<Sone> sone = getMandatorySone(soneId);
140 private Optional<Sone> getMandatorySone(String soneId) throws FcpException {
141 Optional<Sone> sone = core.getSone(soneId);
142 if (!sone.isPresent()) {
143 throw new FcpException("Could not load Sone from “" + soneId + "”.");
148 private String getMandatoryParameter(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
149 String soneId = simpleFieldSet.get(parameterName);
150 if (soneId == null) {
151 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
157 * Returns a post whose ID is a parameter in the given simple field set.
159 * @param simpleFieldSet
160 * The simple field set containing the ID of the post
161 * @param parameterName
162 * The name under which the post ID is stored in the simple field
165 * @throws FcpException
166 * if there is no post ID stored under the given parameter name,
167 * or if the post ID is invalid
169 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
171 String postId = simpleFieldSet.getString(parameterName);
172 Optional<Post> post = core.getDatabase().getPost(postId);
173 if (!post.isPresent()) {
174 throw new FcpException("Could not load post from “" + postId + "”.");
177 } catch (FSParseException fspe1) {
178 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
183 * Returns a reply whose ID is a parameter in the given simple field set.
185 * @param simpleFieldSet
186 * The simple field set containing the ID of the reply
187 * @param parameterName
188 * The name under which the reply ID is stored in the simple
191 * @throws FcpException
192 * if there is no reply ID stored under the given parameter
193 * name, or if the reply ID is invalid
195 protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
197 String replyId = simpleFieldSet.getString(parameterName);
198 Optional<PostReply> reply = core.getDatabase().getPostReply(replyId);
199 if (!reply.isPresent()) {
200 throw new FcpException("Could not load reply from “" + replyId + "”.");
203 } catch (FSParseException fspe1) {
204 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
209 * Creates a simple field set from the given Sone, including {@link Profile}
215 * The prefix for the field names (may be empty but not {@code
218 * An optional local Sone that is used for Sone-specific data,
219 * such as if the Sone is followed by the local Sone
220 * @return The simple field set containing the given Sone
222 protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional<Sone> localSone) {
223 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
225 soneBuilder.put(prefix + "Name", sone.getName());
226 soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
227 soneBuilder.put(prefix + "LastUpdated", sone.getTime());
228 if (localSone.isPresent()) {
229 soneBuilder.put(prefix + "Followed", String.valueOf(localSone.get().hasFriend(sone.getId())));
231 Profile profile = sone.getProfile();
232 soneBuilder.put(prefix + "Field.Count", profile.getFields().size());
234 for (Field field : profile.getFields()) {
235 soneBuilder.put(prefix + "Field." + fieldIndex + ".Name", field.getName());
236 soneBuilder.put(prefix + "Field." + fieldIndex + ".Value", field.getValue());
240 return soneBuilder.get();
244 * Creates a simple field set from the given collection of Sones.
247 * The Sones to encode
249 * The prefix for the field names (may be empty but not
251 * @return The simple field set containing the given Sones
253 protected static SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
254 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
257 soneBuilder.put(prefix + "Count", sones.size());
258 for (Sone sone : sones) {
259 String sonePrefix = prefix + soneIndex++ + ".";
260 soneBuilder.put(sonePrefix + "ID", sone.getId());
261 soneBuilder.put(sonePrefix + "Name", sone.getName());
262 soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
263 soneBuilder.put(sonePrefix + "Time", sone.getTime());
266 return soneBuilder.get();
269 protected SimpleFieldSet encodePost(Post post, String prefix) {
270 return createPostBuilderFromPost(post, prefix).get();
273 protected SimpleFieldSet encodePostWithReplies(Post post, String prefix) {
274 SimpleFieldSetBuilder postBuilder = createPostBuilderFromPost(post, prefix);
276 List<PostReply> replies = post.getReplies();
277 postBuilder.put(encodeReplies(replies, prefix));
279 return postBuilder.get();
282 private SimpleFieldSetBuilder createPostBuilderFromPost(Post post, String prefix) {
283 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
285 postBuilder.put(prefix + "ID", post.getId());
286 postBuilder.put(prefix + "Sone", post.getSone().getId());
287 if (post.getRecipientId().isPresent()) {
288 postBuilder.put(prefix + "Recipient", post.getRecipientId().get());
290 postBuilder.put(prefix + "Time", post.getTime());
291 postBuilder.put(prefix + "Text", encodeString(post.getText()));
292 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
297 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix) {
298 SimpleFieldSetBuilder postBuilder = createPostBuilderFromPosts(posts, prefix);
300 return postBuilder.get();
303 private SimpleFieldSetBuilder createPostBuilderFromPosts(Collection<? extends Post> posts, String prefix) {
304 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
307 postBuilder.put(prefix + "Count", posts.size());
308 for (Post post : posts) {
309 String postPrefix = prefix + postIndex++;
310 postBuilder.put(encodePost(post, postPrefix + "."));
316 protected SimpleFieldSet encodePostsWithReplies(Collection<? extends Post> posts, String prefix) {
317 SimpleFieldSetBuilder postBuilder = createPostBuilderFromPosts(posts, prefix);
320 for (Post post : posts) {
321 String postPrefix = prefix + postIndex++;
322 postBuilder.put(encodeReplies(from(post.getReplies()).filter(FUTURE_REPLY_FILTER).toList(), postPrefix + "."));
325 return postBuilder.get();
329 * Creates a simple field set from the given collection of replies.
332 * The replies to encode
334 * The prefix for the field names (may be empty, but not
336 * @return The simple field set containing the replies
338 protected static SimpleFieldSet encodeReplies(Collection<? extends PostReply> replies, String prefix) {
339 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
342 replyBuilder.put(prefix + "Replies.Count", replies.size());
343 for (PostReply reply : replies) {
344 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
345 replyBuilder.put(replyPrefix + "ID", reply.getId());
346 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
347 replyBuilder.put(replyPrefix + "Time", reply.getTime());
348 replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
351 return replyBuilder.get();
355 * Creates a simple field set from the given collection of Sones that like
361 * The prefix for the field names (may be empty but not
363 * @return The simple field set containing the likes
365 protected static SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
366 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
369 likesBuilder.put(prefix + "Count", likes.size());
370 for (Sone sone : likes) {
371 String sonePrefix = prefix + likeIndex++ + ".";
372 likesBuilder.put(sonePrefix + "ID", sone.getId());
375 return likesBuilder.get();
383 public String toString() {
384 return getClass().getName() + "[writeAccess=" + writeAccess + "]";