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;
22 import java.util.Collection;
23 import java.util.List;
25 import net.pterodactylus.sone.core.Core;
26 import net.pterodactylus.sone.data.Post;
27 import net.pterodactylus.sone.data.PostReply;
28 import net.pterodactylus.sone.data.Profile;
29 import net.pterodactylus.sone.data.Profile.Field;
30 import net.pterodactylus.sone.data.Reply;
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();
270 * Creates a simple field set from the given post.
275 * The prefix for the field names (may be empty but not
277 * @param includeReplies
278 * {@code true} to include replies, {@code false} to not include
280 * @return The simple field set containing the post
282 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
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."));
294 if (includeReplies) {
295 List<PostReply> replies = post.getReplies();
296 postBuilder.put(encodeReplies(replies, prefix));
299 return postBuilder.get();
303 * Creates a simple field set from the given collection of posts.
306 * The posts to encode
308 * The prefix for the field names (may be empty but not
310 * @param includeReplies
311 * {@code true} to include the replies, {@code false} to not
312 * include the replies
313 * @return The simple field set containing the posts
315 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
316 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
319 postBuilder.put(prefix + "Count", posts.size());
320 for (Post post : posts) {
321 String postPrefix = prefix + postIndex++;
322 postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
323 if (includeReplies) {
324 postBuilder.put(encodeReplies(from(post.getReplies()).filter(Reply.FUTURE_REPLY_FILTER).toList(), postPrefix + "."));
328 return postBuilder.get();
332 * Creates a simple field set from the given collection of replies.
335 * The replies to encode
337 * The prefix for the field names (may be empty, but not
339 * @return The simple field set containing the replies
341 protected static SimpleFieldSet encodeReplies(Collection<? extends PostReply> replies, String prefix) {
342 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
345 replyBuilder.put(prefix + "Replies.Count", replies.size());
346 for (PostReply reply : replies) {
347 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
348 replyBuilder.put(replyPrefix + "ID", reply.getId());
349 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
350 replyBuilder.put(replyPrefix + "Time", reply.getTime());
351 replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
354 return replyBuilder.get();
358 * Creates a simple field set from the given collection of Sones that like
364 * The prefix for the field names (may be empty but not
366 * @return The simple field set containing the likes
368 protected static SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
369 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
372 likesBuilder.put(prefix + "Count", likes.size());
373 for (Sone sone : likes) {
374 String sonePrefix = prefix + likeIndex++ + ".";
375 likesBuilder.put(sonePrefix + "ID", sone.getId());
378 return likesBuilder.get();
386 public String toString() {
387 return getClass().getName() + "[writeAccess=" + writeAccess + "]";