--- /dev/null
+package net.pterodactylus.sone.core;
+
+import net.pterodactylus.sone.data.Post;
+
+/**
+ * Sone compatibility modes.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public enum CompatibilityMode {
+
+ /**
+ * This mode causes Sone to use a post’s {@link Post#getInternalId() internal ID} to locate posts when parsing links.
+ */
+ oldElementIds,
+
+}
.setHeight(height)
.update();
album.addImage(image);
- images.put(image.getId(), image);
+ images.put(imageId, image);
}
}
import java.util.ArrayList;
import java.util.Collection;
+import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.AlbumProvider;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
import net.pterodactylus.sone.database.ImageBuilder;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
@Singleton
-public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
+public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider, AlbumProvider {
/** The logger. */
private static final Logger logger = getLogger(Core.class.getName());
/** The trust updater. */
private final WebOfTrustUpdater webOfTrustUpdater;
+ private final Set<CompatibilityMode> compatibilityModes = EnumSet.noneOf(CompatibilityMode.class);
+
/** The times Sones were followed. */
private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
return updateChecker;
}
+ public boolean isCompatibilityMode(CompatibilityMode compatibilityMode) {
+ return compatibilityModes.contains(compatibilityMode);
+ }
+
+ public void setCompatibilityMode(CompatibilityMode compatibilityMode) {
+ compatibilityModes.add(compatibilityMode);
+ }
+
+ public void clearCompatibilityMod(CompatibilityMode compatibilityMode) {
+ compatibilityModes.remove(compatibilityMode);
+ }
+
/**
* Returns the Sone rescuer for the given local Sone.
*
* {@inheritDoc}
*/
@Override
- public Optional<Post> getPost(String postId) {
- return database.getPost(postId);
+ public Optional<Post> getPost(final String postId) {
+ Optional<Post> post = database.getPost(postId);
+ if (post.isPresent() || !isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+ return post;
+ }
+ return FluentIterable.from(getSones()).transformAndConcat(Sone.toAllPosts).filter(new Predicate<Post>() {
+ @Override
+ public boolean apply(Post input) {
+ return (input != null) && input.getInternalId().equals(postId);
+ }
+ }).first();
}
/**
* @return The album with the given ID, or {@code null} if no album with the
* given ID exists
*/
- public Album getAlbum(String albumId) {
- return database.getAlbum(albumId).orNull();
+ public Optional<Album> getAlbum(String albumId) {
+ return database.getAlbum(albumId);
}
public ImageBuilder imageBuilder() {
int postCounter = 0;
for (Post post : sone.getPosts()) {
String postPrefix = sonePrefix + "/Posts/" + postCounter++;
- configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
+ configuration.getStringValue(postPrefix + "/ID").setValue(post.getInternalId());
configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
int replyCounter = 0;
for (PostReply reply : sone.getReplies()) {
String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
- configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
+ configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getInternalId());
configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
int albumCounter = 0;
for (Album album : albums) {
String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
- configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
+ configuration.getStringValue(albumPrefix + "/ID").setValue(album.getInternalId());
configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
continue;
}
String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
- configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
- configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
+ configuration.getStringValue(imagePrefix + "/ID").setValue(image.getInternalId());
+ configuration.getStringValue(imagePrefix + "/Album").setValue(album.getInternalId());
configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
}
+ /* save compatibility modes. */
+ configuration.getBooleanValue("CompatibilityModes/OldElementIds").setValue(compatibilityModes.contains(CompatibilityMode.oldElementIds));
+
/* save known posts. */
database.save();
}
++soneCounter;
}
+
+ /* load compatibility modes. */
+ if (configuration.getBooleanValue("CompatibilityModes/OldElementIds").getValue(false)) {
+ setCompatibilityMode(CompatibilityMode.oldElementIds);
+ }
}
/**
import net.pterodactylus.util.xml.SimpleXML;
import net.pterodactylus.util.xml.XML;
+import com.google.common.base.Optional;
import org.w3c.dom.Document;
/**
logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
return null;
}
- Album parent = null;
+ Optional<Album> parent = Optional.absent();
if (parentId != null) {
parent = core.getAlbum(parentId);
- if (parent == null) {
+ if (!parent.isPresent()) {
logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
return null;
}
.setTitle(title)
.setDescription(description)
.update();
- if (parent != null) {
- parent.addAlbum(album);
+ if (parent.isPresent()) {
+ parent.get().addAlbum(album);
} else {
topLevelAlbums.add(album);
}
* @return The ID of this album
*/
String getId();
+ String getInternalId();
/**
* Returns the Sone this album belongs to.
--- /dev/null
+package net.pterodactylus.sone.data;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.ThreadSafe;
+
+import com.google.common.base.Charsets;
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hashing;
+
+/**
+ * Builds (practically) unique IDs by combining Sone and element IDs.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+@ThreadSafe
+public class IdBuilder {
+
+ private static final HashFunction HASH_FUNCTION = Hashing.sha256();
+ public static final int ID_STRING_LENGTH = HASH_FUNCTION.bits() / 4;
+
+ private final HashFunction sha256 = HASH_FUNCTION;
+
+ @Nonnull
+ public String buildId(@Nonnull String soneId, @Nonnull String id) {
+ return sha256.newHasher()
+ .putBytes(soneId.getBytes(Charsets.UTF_8))
+ .putBytes(id.getBytes(Charsets.UTF_8))
+ .hash().toString();
+ }
+
+}
* @return The ID of this image
*/
String getId();
+ String getInternalId();
/**
* Returns the Sone this image belongs to.
* @return The ID of the post
*/
public String getId();
+ String getInternalId();
/**
* Returns whether this post has already been loaded.
}
@Override
+ public String getInternalId() {
+ return id;
+ }
+
+ @Override
public boolean isLoaded() {
return false;
}
return this;
}
checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
- this.avatar = avatar.getId();
+ this.avatar = avatar.getInternalId();
return this;
}
* @return The ID of the reply
*/
public String getId();
+ String getInternalId();
/**
* Returns the Sone that posted this reply.
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
import net.pterodactylus.sone.template.SoneAccessor;
+import com.google.common.base.Optional;
import freenet.keys.FreenetURI;
import com.google.common.base.Function;
}
};
+ Function<Sone, Collection<Post>> toAllPosts = new Function<Sone, Collection<Post>>() {
+ @Override
+ public Collection<Post> apply(@Nullable Sone sone) {
+ return (sone != null) ? sone.getPosts() : Collections.<Post>emptyList();
+ }
+ };
+
/**
* Returns the identity of this Sone.
*
* @return The root album of this Sone
*/
Album getRootAlbum();
+ Optional<Image> getImageByInternalId(final String internalId);
/**
* Returns Sone-specific options.
import java.util.UUID;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Sone;
*/
public class AlbumImpl implements Album {
+ private final IdBuilder idBuilder = new IdBuilder();
+
/** The ID of this album. */
private final String id;
@Override
public String getId() {
+ return idBuilder.buildId(sone.getId(), id);
+ }
+
+ @Override
+ public String getInternalId() {
return id;
}
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.SoneOptions;
import net.pterodactylus.sone.freenet.wot.Identity;
+import com.google.common.base.Optional;
import freenet.keys.FreenetURI;
import com.google.common.base.Objects;
}
@Override
+ public Optional<Image> getImageByInternalId(String internalId) {
+ return Optional.absent();
+ }
+
+ @Override
public SoneOptions getOptions() {
return null;
}
import java.util.UUID;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Sone;
*/
public class ImageImpl implements Image {
+ private final IdBuilder idBuilder = new IdBuilder();
+
/** The ID of the image. */
private final String id;
@Override
public String getId() {
+ return idBuilder.buildId(sone.getId(), id);
+ }
+
+ @Override
+ public String getInternalId() {
return id;
}
package net.pterodactylus.sone.data.impl;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.database.SoneProvider;
*/
public class PostImpl implements Post {
+ private final IdBuilder idBuilder = new IdBuilder();
+
/** The Sone provider. */
private final SoneProvider soneProvider;
*/
@Override
public String getId() {
+ return idBuilder.buildId(soneId, id);
+ }
+
+ @Override
+ public String getInternalId() {
return id;
}
package net.pterodactylus.sone.data.impl;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.database.SoneProvider;
*/
public abstract class ReplyImpl<T extends Reply<T>> implements Reply<T> {
+ private final IdBuilder idBuilder = new IdBuilder();
+
/** The Sone provider. */
private final SoneProvider soneProvider;
*/
@Override
public String getId() {
+ return idBuilder.buildId(soneId, id);
+ }
+
+ @Override
+ public String getInternalId() {
return id;
}
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.annotation.Nullable;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.freenet.wot.Identity;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
import freenet.keys.FreenetURI;
import com.google.common.hash.Hasher;
return rootAlbum;
}
+ @Override
+ public Optional<Image> getImageByInternalId(final String internalId) {
+ return FluentIterable.from(toAllImages.apply(this)).filter(new Predicate<Image>() {
+ @Override
+ public boolean apply(@Nullable Image input) {
+ return (input != null) && input.getInternalId().equals(internalId);
+ }
+ }).first();
+ }
+
/**
* Returns Sone-specific options.
*
import java.util.UUID;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.database.SoneProvider;
*/
class MemoryPost implements Post {
+ private final IdBuilder idBuilder = new IdBuilder();
+
/** The post database. */
private final MemoryDatabase postDatabase;
*/
@Override
public String getId() {
+ return idBuilder.buildId(soneId, id.toString());
+ }
+
+ @Override
+ public String getInternalId() {
return id.toString();
}
package net.pterodactylus.sone.database.memory;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
*/
class MemoryPostReply implements PostReply {
+ private final IdBuilder idBuilder = new IdBuilder();
+
/** The database. */
private final MemoryDatabase database;
*/
@Override
public String getId() {
+ return idBuilder.buildId(soneId, id);
+ }
+
+ @Override
+ public String getInternalId() {
return id;
}
--- /dev/null
+package net.pterodactylus.sone.template;
+
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import net.pterodactylus.sone.data.IdBuilder;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+
+/**
+ * Filter that {@link IdBuilder builds IDs} from a piped-in element ID and a Sone or Sone ID given as parameter “sone.”
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BuildIdFilter implements Filter {
+
+ private final IdBuilder idBuilder = new IdBuilder();
+
+ @Override
+ public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+ Optional<String> soneId = getSoneId(parameters);
+ if (!soneId.isPresent()) {
+ return null;
+ }
+ Optional<String> elementId = Optional.fromNullable(data).transform(getStringValue());
+ if (!elementId.isPresent()) {
+ return null;
+ }
+ return idBuilder.buildId(soneId.get(), elementId.get());
+ }
+
+ private Optional<String> getSoneId(Map<String, Object> parameters) {
+ Object soneObject = parameters.get("sone");
+ if (soneObject instanceof String) {
+ return Optional.of((String) soneObject);
+ } else if (soneObject instanceof Sone) {
+ return Optional.of(((Sone) soneObject).getId());
+ }
+ return Optional.absent();
+ }
+
+ private Function<? super Object, String> getStringValue() {
+ return new Function<Object, String>() {
+ @Nullable
+ @Override
+ public String apply(Object input) {
+ return (input != null) ? input.toString() : null;
+ }
+ };
+ }
+
+}
package net.pterodactylus.sone.template;
import static java.lang.String.valueOf;
-import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
import java.io.IOException;
import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
-import java.io.Writer;
-import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Collections;
import java.util.Map;
import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.text.FreenetLinkPart;
-import net.pterodactylus.sone.text.LinkPart;
import net.pterodactylus.sone.text.Part;
-import net.pterodactylus.sone.text.PlainTextPart;
-import net.pterodactylus.sone.text.PostPart;
-import net.pterodactylus.sone.text.SonePart;
import net.pterodactylus.sone.text.SoneTextParser;
import net.pterodactylus.sone.text.SoneTextParserContext;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Filter;
-import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.template.TemplateContextFactory;
-import net.pterodactylus.util.template.TemplateParser;
/**
- * Filter that filters a given text through a {@link SoneTextParser}.
+ * Filter that filters a given text through a {@link SoneTextParser} and returns the parsed {@link Part}s.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class ParserFilter implements Filter {
- /** The core. */
private final Core core;
-
- /** The link parser. */
private final SoneTextParser soneTextParser;
- /** The template context factory. */
- private final TemplateContextFactory templateContextFactory;
-
- /** The template for {@link PlainTextPart}s. */
- private static final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
-
- /** The template for {@link FreenetLinkPart}s. */
- private static final Template linkTemplate = TemplateParser.parse(new StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"));
-
- /**
- * Creates a new filter that runs its input through a {@link SoneTextParser}
- * .
- *
- * @param core
- * The core
- * @param templateContextFactory
- * The context factory for rendering the parts
- * @param soneTextParser
- * The Sone text parser
- */
- public ParserFilter(Core core, TemplateContextFactory templateContextFactory, SoneTextParser soneTextParser) {
+ public ParserFilter(Core core, SoneTextParser soneTextParser) {
this.core = core;
- this.templateContextFactory = templateContextFactory;
this.soneTextParser = soneTextParser;
}
@Override
public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
String text = valueOf(data);
- int length = parseInt(valueOf(parameters.get("length")), -1);
- int cutOffLength = parseInt(valueOf(parameters.get("cut-off-length")), length);
Object sone = parameters.get("sone");
if (sone instanceof String) {
sone = core.getSone((String) sone).orNull();
}
FreenetRequest request = (FreenetRequest) templateContext.get("request");
SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone);
- StringWriter parsedTextWriter = new StringWriter();
try {
- Iterable<Part> parts = soneTextParser.parse(context, new StringReader(text));
- if (length > -1) {
- int allPartsLength = 0;
- List<Part> shortenedParts = new ArrayList<Part>();
- for (Part part : parts) {
- if (part instanceof PlainTextPart) {
- String longText = ((PlainTextPart) part).getText();
- if (allPartsLength < cutOffLength) {
- if ((allPartsLength + longText.length()) > cutOffLength) {
- shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…"));
- } else {
- shortenedParts.add(part);
- }
- }
- allPartsLength += longText.length();
- } else if (part instanceof LinkPart) {
- if (allPartsLength < cutOffLength) {
- shortenedParts.add(part);
- }
- allPartsLength += ((LinkPart) part).getText().length();
- } else {
- if (allPartsLength < cutOffLength) {
- shortenedParts.add(part);
- }
- }
- }
- if (allPartsLength >= length) {
- parts = shortenedParts;
- }
- }
- render(parsedTextWriter, parts);
+ return soneTextParser.parse(context, new StringReader(text));
} catch (IOException ioe1) {
- /* no exceptions in a StringReader or StringWriter, ignore. */
- }
- return parsedTextWriter.toString();
- }
-
- //
- // PRIVATE METHODS
- //
-
- /**
- * Renders the given parts.
- *
- * @param writer
- * The writer to render the parts to
- * @param parts
- * The parts to render
- */
- private void render(Writer writer, Iterable<Part> parts) {
- for (Part part : parts) {
- render(writer, part);
+ /* no exceptions in a StringReader, ignore. */
+ return Collections.<Part>emptyList();
}
}
- /**
- * Renders the given part.
- *
- * @param writer
- * The writer to render the part to
- * @param part
- * The part to render
- */
- @SuppressWarnings("unchecked")
- private void render(Writer writer, Part part) {
- if (part instanceof PlainTextPart) {
- render(writer, (PlainTextPart) part);
- } else if (part instanceof FreenetLinkPart) {
- render(writer, (FreenetLinkPart) part);
- } else if (part instanceof LinkPart) {
- render(writer, (LinkPart) part);
- } else if (part instanceof SonePart) {
- render(writer, (SonePart) part);
- } else if (part instanceof PostPart) {
- render(writer, (PostPart) part);
- } else if (part instanceof Iterable<?>) {
- render(writer, (Iterable<Part>) part);
- }
- }
-
- /**
- * Renders the given plain-text part.
- *
- * @param writer
- * The writer to render the part to
- * @param plainTextPart
- * The part to render
- */
- private void render(Writer writer, PlainTextPart plainTextPart) {
- TemplateContext templateContext = templateContextFactory.createTemplateContext();
- templateContext.set("text", plainTextPart.getText());
- plainTextTemplate.render(templateContext, writer);
- }
-
- /**
- * Renders the given freenet link part.
- *
- * @param writer
- * The writer to render the part to
- * @param freenetLinkPart
- * The part to render
- */
- private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
- renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
- }
-
- /**
- * Renders the given link part.
- *
- * @param writer
- * The writer to render the part to
- * @param linkPart
- * The part to render
- */
- private void render(Writer writer, LinkPart linkPart) {
- try {
- renderLink(writer, "/external-link/?_CHECKED_HTTP_=" + URLEncoder.encode(linkPart.getLink(), "UTF-8"), linkPart.getText(), linkPart.getTitle(), "internet");
- } catch (UnsupportedEncodingException uee1) {
- /* not possible for UTF-8. */
- throw new RuntimeException("The JVM does not support UTF-8 encoding!", uee1);
- }
- }
-
- /**
- * Renders the given Sone part.
- *
- * @param writer
- * The writer to render the part to
- * @param sonePart
- * The part to render
- */
- private void render(Writer writer, SonePart sonePart) {
- if ((sonePart.getSone() != null) && (sonePart.getSone().getName() != null)) {
- renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
- } else {
- renderLink(writer, "/WebOfTrust/ShowIdentity?id=" + sonePart.getSone().getId(), sonePart.getSone().getId(), sonePart.getSone().getId(), "in-sone");
- }
- }
-
- /**
- * Renders the given post part.
- *
- * @param writer
- * The writer to render the part to
- * @param postPart
- * The part to render
- */
- private void render(Writer writer, PostPart postPart) {
- SoneTextParser parser = new SoneTextParser(core, core);
- SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
- try {
- Iterable<Part> parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
- StringBuilder excerpt = new StringBuilder();
- for (Part part : parts) {
- excerpt.append(part.getText());
- if (excerpt.length() > 20) {
- int lastSpace = excerpt.lastIndexOf(" ", 20);
- if (lastSpace > -1) {
- excerpt.setLength(lastSpace);
- } else {
- excerpt.setLength(20);
- }
- excerpt.append("…");
- break;
- }
- }
- renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
- } catch (IOException ioe1) {
- /* StringReader shouldn’t throw. */
- }
- }
-
- /**
- * Renders the given link.
- *
- * @param writer
- * The writer to render the link to
- * @param link
- * The link to render
- * @param text
- * The text of the link
- * @param title
- * The title of the link
- * @param cssClass
- * The CSS class of the link
- */
- private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
- TemplateContext templateContext = templateContextFactory.createTemplateContext();
- templateContext.set("cssClass", cssClass);
- templateContext.set("link", link);
- templateContext.set("text", text);
- templateContext.set("title", title);
- linkTemplate.render(templateContext, writer);
- }
-
}
if (avatarId == null) {
return null;
}
- if (core.getImage(avatarId, false) == null) {
+ if (!profile.getSone().getImageByInternalId(avatarId).isPresent()) {
/* avatar ID but no matching image? show nothing. */
return null;
}
--- /dev/null
+package net.pterodactylus.sone.template;
+
+import static java.lang.String.valueOf;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.AlbumPart;
+import net.pterodactylus.sone.text.FreenetLinkPart;
+import net.pterodactylus.sone.text.LinkPart;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PartContainer;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.PostPart;
+import net.pterodactylus.sone.text.SonePart;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
+import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.template.TemplateParser;
+
+/**
+ * {@link Filter} implementation that renders an {@link Iterable} (such as a {@link PartContainer}) of {@link Part}s to HTML.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RenderFilter implements Filter {
+
+ private static final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
+ private static final Template linkTemplate =
+ TemplateParser.parse(new StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"));
+ private final Core core;
+ private final TemplateContextFactory templateContextFactory;
+
+ public RenderFilter(Core core, TemplateContextFactory templateContextFactory) {
+ this.core = core;
+ this.templateContextFactory = templateContextFactory;
+ }
+
+ @Override
+ public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+ if (!(data instanceof Iterable<?>)) {
+ return null;
+ }
+ Iterable<Part> parts = (Iterable<Part>) data;
+ int length = parseInt(valueOf(parameters.get("length")), -1);
+ int cutOffLength = parseInt(valueOf(parameters.get("cut-off-length")), length);
+ StringWriter parsedTextWriter = new StringWriter();
+ if (length > -1) {
+ int allPartsLength = 0;
+ List<Part> shortenedParts = new ArrayList<Part>();
+ for (Part part : parts) {
+ if (part instanceof PlainTextPart) {
+ String longText = part.getText();
+ if (allPartsLength < cutOffLength) {
+ if ((allPartsLength + longText.length()) > cutOffLength) {
+ shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…"));
+ } else {
+ shortenedParts.add(part);
+ }
+ }
+ allPartsLength += longText.length();
+ } else if (part instanceof LinkPart) {
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
+ allPartsLength += part.getText().length();
+ } else {
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
+ }
+ }
+ if (allPartsLength >= length) {
+ parts = shortenedParts;
+ }
+ }
+ render(parsedTextWriter, parts);
+ return parsedTextWriter.toString();
+ }
+
+ private void render(Writer writer, Iterable<Part> parts) {
+ for (Part part : parts) {
+ render(writer, part);
+ }
+ }
+
+ private void render(Writer writer, Part part) {
+ if (part instanceof PlainTextPart) {
+ render(writer, (PlainTextPart) part);
+ } else if (part instanceof FreenetLinkPart) {
+ render(writer, (FreenetLinkPart) part);
+ } else if (part instanceof LinkPart) {
+ render(writer, (LinkPart) part);
+ } else if (part instanceof SonePart) {
+ render(writer, (SonePart) part);
+ } else if (part instanceof PostPart) {
+ render(writer, (PostPart) part);
+ } else if (part instanceof AlbumPart) {
+ render(writer, (AlbumPart) part);
+ } else if (part instanceof Iterable<?>) {
+ render(writer, (Iterable<Part>) part);
+ }
+ }
+
+ private void render(Writer writer, PlainTextPart plainTextPart) {
+ TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("text", plainTextPart.getText());
+ plainTextTemplate.render(templateContext, writer);
+ }
+
+ private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
+ renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(),
+ freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
+ }
+
+ private void render(Writer writer, LinkPart linkPart) {
+ try {
+ renderLink(writer, "/external-link/?_CHECKED_HTTP_=" + URLEncoder.encode(linkPart.getLink(), "UTF-8"), linkPart.getText(),
+ linkPart.getTitle(), "internet");
+ } catch (UnsupportedEncodingException uee1) {
+ /* not possible for UTF-8. */
+ throw new RuntimeException("The JVM does not support UTF-8 encoding!", uee1);
+ }
+ }
+
+ private void render(Writer writer, SonePart sonePart) {
+ Sone sone = sonePart.getSone();
+ if ((sone != null) && (sone.getName() != null)) {
+ String niceName = SoneAccessor.getNiceName(sone);
+ renderLink(writer, "viewSone.html?sone=" + sone.getId(), niceName, niceName, "in-sone");
+ } else {
+ renderLink(writer, "/WebOfTrust/ShowIdentity?id=" + sone.getId(), sone.getId(), sone.getId(), "in-sone");
+ }
+ }
+
+ private void render(Writer writer, PostPart postPart) {
+ SoneTextParser parser = new SoneTextParser(core, core, core);
+ SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
+ try {
+ Iterable<Part> parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
+ StringBuilder excerpt = new StringBuilder();
+ for (Part part : parts) {
+ excerpt.append(part.getText());
+ if (excerpt.length() > 20) {
+ int lastSpace = excerpt.lastIndexOf(" ", 20);
+ if (lastSpace > -1) {
+ excerpt.setLength(lastSpace);
+ } else {
+ excerpt.setLength(20);
+ }
+ excerpt.append("…");
+ break;
+ }
+ }
+ renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(),
+ SoneAccessor.getNiceName(postPart.getPost().getSone()), postPart.usesDeprecatedLink() ? "internet" : "in-sone");
+ } catch (IOException ioe1) {
+ /* StringReader shouldn’t throw. */
+ }
+ }
+
+ private void render(Writer writer, AlbumPart albumPart) {
+ Album album = albumPart.getAlbum();
+ renderLink(writer, String.format("imageBrowser.html?album=%s", album.getId()), album.getTitle(), album.getDescription(), "in-sone");
+ }
+
+ private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
+ TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("cssClass", cssClass);
+ templateContext.set("link", link);
+ templateContext.set("text", text);
+ templateContext.set("title", title);
+ linkTemplate.render(templateContext, writer);
+ }
+
+}
--- /dev/null
+package net.pterodactylus.sone.text;
+
+import net.pterodactylus.sone.data.Album;
+
+/**
+ * {@link Part} implementation that contains information about the linked {@link Album}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class AlbumPart implements Part {
+
+ private final Album album;
+
+ public AlbumPart(Album album) {
+ this.album = album;
+ }
+
+ public Album getAlbum() {
+ return album;
+ }
+
+ @Override
+ public String getText() {
+ return album.getTitle();
+ }
+
+}
*/
public class PostPart implements Part {
- /** The post this part refers to. */
private final Post post;
+ private final boolean usesDeprecatedLink;
- /**
- * Creates a new post part.
- *
- * @param post
- * The referenced post
- */
public PostPart(Post post) {
- this.post = post;
+ this(post, false);
}
- //
- // ACCESSORS
- //
+ public PostPart(Post post, boolean usesDeprecatedLink) {
+ this.post = post;
+ this.usesDeprecatedLink = usesDeprecatedLink;
+ }
- /**
- * Returns the post referenced by this part.
- *
- * @return The post referenced by this part
- */
public Post getPost() {
return post;
}
- //
- // PART METHODS
- //
+ public boolean usesDeprecatedLink() {
+ return usesDeprecatedLink;
+ }
- /**
- * {@inheritDoc}
- */
@Override
public String getText() {
return post.getText();
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.util.io.Closer;
HTTP("http://", false),
HTTPS("https://", false),
SONE("sone://", false),
- POST("post://", false);
+ POST("post://", false),
+ ALBUM("album://", false);
private final String scheme;
private final boolean freenetLink;
}
- /** The Sone provider. */
private final SoneProvider soneProvider;
-
- /** The post provider. */
private final PostProvider postProvider;
+ private final AlbumProvider albumProvider;
- /**
- * Creates a new freenet link parser.
- *
- * @param soneProvider
- * The Sone provider
- * @param postProvider
- * The post provider
- */
- public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
+ public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider, AlbumProvider albumProvider) {
this.soneProvider = soneProvider;
this.postProvider = postProvider;
+ this.albumProvider = albumProvider;
}
//
continue;
}
if (linkType == LinkType.POST) {
- if (line.length() >= (7 + 36)) {
- String postId = line.substring(7, 43);
- Optional<Post> post = postProvider.getPost(postId);
- if (post.isPresent()) {
- parts.add(new PostPart(post.get()));
- } else {
- parts.add(new PlainTextPart(line.substring(0, 43)));
- }
- line = line.substring(43);
+ Optional<Post> post = postProvider.getPost(link.substring(7));
+ if (post.isPresent()) {
+ parts.add(new PostPart(post.get(), link.substring(7).equals(post.get().getInternalId())));
} else {
- parts.add(new PlainTextPart(line));
- line = "";
+ parts.add(new PlainTextPart(link));
+ }
+ line = line.substring(link.length());
+ continue;
+ }
+ if (linkType == LinkType.ALBUM) {
+ Optional<Album> album = albumProvider.getAlbum(link.substring(linkType.getScheme().length()));
+ if (album.isPresent()) {
+ parts.add(new AlbumPart(album.get()));
+ } else {
+ parts.add(new PlainTextPart(link));
}
+ line = line.substring(link.length());
continue;
}
package net.pterodactylus.sone.web;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
- String id = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+ String id = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
Optional<Post> post = webInterface.getCore().getPost(id);
if (post.isPresent()) {
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
+
/**
* Page that lets the user create a new album.
*
}
String description = request.getHttpRequest().getPartAsStringFailsafe("description", 256).trim();
Sone currentSone = getCurrentSone(request.getToadletContext());
- String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
- Album parent = webInterface.getCore().getAlbum(parentId);
+ String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH);
+ Optional<Album> parent;
if (parentId.equals("")) {
- parent = currentSone.getRootAlbum();
+ parent = Optional.of(currentSone.getRootAlbum());
+ } else {
+ parent = webInterface.getCore().getAlbum(parentId);
+ if (!parent.isPresent()) {
+ throw new RedirectException("noPermission.html");
+ }
}
- Album album = webInterface.getCore().createAlbum(currentSone, parent);
+ Album album = webInterface.getCore().createAlbum(currentSone, parent.get());
try {
album.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
} catch (AlbumTitleMustNotBeEmpty atmnbe) {
import com.google.common.base.Optional;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.text.TextFilter;
@Override
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
- String postId = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+ String postId = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
String text = request.getHttpRequest().getPartAsStringFailsafe("text", 65536).trim();
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
if (request.getMethod() == Method.POST) {
package net.pterodactylus.sone.web;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
+
/**
* Page that lets the user delete an {@link Album}.
*
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
- String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", 36);
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH);
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
throw new RedirectException("invalid.html");
}
- if (!album.getSone().isLocal()) {
+ if (!album.get().getSone().isLocal()) {
throw new RedirectException("noPermission.html");
}
if (request.getHttpRequest().isPartSet("abortDelete")) {
- throw new RedirectException("imageBrowser.html?album=" + album.getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getId());
}
- Album parentAlbum = album.getParent();
- webInterface.getCore().deleteAlbum(album);
- if (parentAlbum.equals(album.getSone().getRootAlbum())) {
- throw new RedirectException("imageBrowser.html?sone=" + album.getSone().getId());
+ Album parentAlbum = album.get().getParent();
+ webInterface.getCore().deleteAlbum(album.get());
+ if (parentAlbum.equals(album.get().getSone().getRootAlbum())) {
+ throw new RedirectException("imageBrowser.html?sone=" + album.get().getSone().getId());
}
throw new RedirectException("imageBrowser.html?album=" + parentAlbum.getId());
}
String albumId = request.getHttpRequest().getParam("album");
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
throw new RedirectException("invalid.html");
}
templateContext.set("album", album);
package net.pterodactylus.sone.web;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
@Override
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
- String imageId = (request.getMethod() == Method.POST) ? request.getHttpRequest().getPartAsStringFailsafe("image", 36) : request.getHttpRequest().getParam("image");
+ String imageId = (request.getMethod() == Method.POST) ? request.getHttpRequest().getPartAsStringFailsafe("image", IdBuilder.ID_STRING_LENGTH)
+ : request.getHttpRequest().getParam("image");
Image image = webInterface.getCore().getImage(imageId, false);
if (image == null) {
throw new RedirectException("invalid.html");
import com.google.common.base.Optional;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
templateContext.set("post", post.get());
templateContext.set("returnPage", returnPage);
} else if (request.getMethod() == Method.POST) {
- String postId = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+ String postId = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
Optional<Post> post = webInterface.getCore().getPost(postId);
if (!post.isPresent() || !post.get().getSone().isLocal()) {
package net.pterodactylus.sone.web;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
@Override
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
- String replyId = request.getHttpRequest().getPartAsStringFailsafe("reply", 36);
+ String replyId = request.getHttpRequest().getPartAsStringFailsafe("reply", IdBuilder.ID_STRING_LENGTH);
Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
if (request.getMethod() == Method.POST) {
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
+
/**
* Page that lets the user edit the name and description of an album.
*
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
- String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", 36);
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH);
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
throw new RedirectException("invalid.html");
}
- if (!album.getSone().isLocal()) {
+ if (!album.get().getSone().isLocal()) {
throw new RedirectException("noPermission.html");
}
if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveLeft", 4))) {
- album.getParent().moveAlbumUp(album);
+ album.get().getParent().moveAlbumUp(album.get());
webInterface.getCore().touchConfiguration();
- throw new RedirectException("imageBrowser.html?album=" + album.getParent().getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId());
} else if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveRight", 4))) {
- album.getParent().moveAlbumDown(album);
+ album.get().getParent().moveAlbumDown(album.get());
webInterface.getCore().touchConfiguration();
- throw new RedirectException("imageBrowser.html?album=" + album.getParent().getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId());
}
- String albumImageId = request.getHttpRequest().getPartAsStringFailsafe("album-image", 36);
+ String albumImageId = request.getHttpRequest().getPartAsStringFailsafe("album-image", IdBuilder.ID_STRING_LENGTH);
if (webInterface.getCore().getImage(albumImageId, false) == null) {
albumImageId = null;
}
- album.modify().setAlbumImage(albumImageId).update();
+ album.get().modify().setAlbumImage(albumImageId).update();
String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim();
String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1000).trim();
try {
- album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
+ album.get().modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
} catch (AlbumTitleMustNotBeEmpty atmnbe) {
throw new RedirectException("emptyAlbumTitle.html");
}
webInterface.getCore().touchConfiguration();
- throw new RedirectException("imageBrowser.html?album=" + album.getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getId());
}
}
package net.pterodactylus.sone.web;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.page.FreenetRequest;
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
- String imageId = request.getHttpRequest().getPartAsStringFailsafe("image", 36);
+ String imageId = request.getHttpRequest().getPartAsStringFailsafe("image", IdBuilder.ID_STRING_LENGTH);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
Image image = webInterface.getCore().getImage(imageId, false);
if (image == null) {
import java.util.List;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.DuplicateField;
import net.pterodactylus.sone.data.Profile.Field;
birthDay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null);
birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null);
birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null);
- avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
+ avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", IdBuilder.ID_STRING_LENGTH);
profile.setFirstName(firstName.length() > 0 ? firstName : null);
profile.setMiddleName(middleName.length() > 0 ? middleName : null);
profile.setLastName(lastName.length() > 0 ? lastName : null);
profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
- profile.setAvatar(webInterface.getCore().getImage(avatarId, false));
+ profile.setAvatar(currentSone.getImageByInternalId(avatarId).orNull());
for (Field field : fields) {
String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
String filteredValue = filter(request.getHttpRequest().getHeader("Host"), value);
super.processTemplate(request, templateContext);
String albumId = request.getHttpRequest().getParam("album", null);
if (albumId != null) {
- Album album = webInterface.getCore().getAlbum(albumId);
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
templateContext.set("albumRequested", true);
- templateContext.set("album", album);
+ templateContext.set("album", album.orNull());
templateContext.set("page", request.getHttpRequest().getParam("page"));
return;
}
package net.pterodactylus.sone.web;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.page.FreenetRequest;
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
String type = request.getHttpRequest().getPartAsStringFailsafe("type", 16);
- String id = request.getHttpRequest().getPartAsStringFailsafe(type, 36);
+ String id = request.getHttpRequest().getPartAsStringFailsafe(type, IdBuilder.ID_STRING_LENGTH);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
Sone currentSone = getCurrentSone(request.getToadletContext());
if ("post".equals(type)) {
import java.util.ArrayList;
import java.util.List;
+import net.pterodactylus.sone.core.CompatibilityMode;
import net.pterodactylus.sone.core.Preferences;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
Integer fcpFullAccessRequiredInteger = parseInt(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
+
+ boolean compatOldElementIds = request.getHttpRequest().isPartSet("compat-old-element-ids");
+ if (compatOldElementIds) {
+ webInterface.getCore().setCompatibilityMode(CompatibilityMode.oldElementIds);
+ } else {
+ webInterface.getCore().clearCompatibilityMod(CompatibilityMode.oldElementIds);
+ }
+
webInterface.getCore().touchConfiguration();
if (fieldErrors.isEmpty()) {
throw new RedirectException(getPath());
templateContext.set("trust-comment", preferences.getTrustComment());
templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
+ templateContext.set("compat-old-element-ids", webInterface.getCore().isCompatibilityMode(CompatibilityMode.oldElementIds));
}
}
import java.util.logging.Level;
import java.util.logging.Logger;
+import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
*/
private String getAlbumId(String phrase) {
String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
- return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null;
+ return webInterface.getCore().getAlbum(albumId).isPresent() ? albumId : null;
}
/**
import java.util.Set;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
- String id = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+ String id = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
Optional<Post> post = webInterface.getCore().getPost(id);
if (post.isPresent()) {
package net.pterodactylus.sone.web;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.page.FreenetRequest;
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
String type = request.getHttpRequest().getPartAsStringFailsafe("type", 16);
- String id = request.getHttpRequest().getPartAsStringFailsafe(type, 36);
+ String id = request.getHttpRequest().getPartAsStringFailsafe(type, IdBuilder.ID_STRING_LENGTH);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
Sone currentSone = getCurrentSone(request.getToadletContext());
if ("post".equals(type)) {
import javax.imageio.stream.ImageInputStream;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Image.Modifier.ImageTitleMustNotBeEmpty;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
import com.google.common.io.ByteStreams;
import freenet.support.api.Bucket;
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
Sone currentSone = getCurrentSone(request.getToadletContext());
- String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
- Album parent = webInterface.getCore().getAlbum(parentId);
- if (parent == null) {
+ String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH);
+ Optional<Album> parent = webInterface.getCore().getAlbum(parentId);
+ if (!parent.isPresent()) {
throw new RedirectException("noPermission.html");
}
- if (!currentSone.equals(parent.getSone())) {
+ if (!currentSone.equals(parent.get().getSone())) {
throw new RedirectException("noPermission.html");
}
String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200);
}
String mimeType = getMimeType(imageData);
TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData);
- net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent, temporaryImage);
+ net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent.get(), temporaryImage);
image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null)).update();
} catch (IOException ioe1) {
logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
Closer.close(imageDataInputStream);
Closer.flush(uploadedImage);
}
- throw new RedirectException("imageBrowser.html?album=" + parent.getId());
+ throw new RedirectException("imageBrowser.html?album=" + parent.get().getId());
}
}
import net.pterodactylus.sone.main.SonePlugin;
import net.pterodactylus.sone.notify.ListNotification;
import net.pterodactylus.sone.template.AlbumAccessor;
+import net.pterodactylus.sone.template.BuildIdFilter;
import net.pterodactylus.sone.template.CollectionAccessor;
import net.pterodactylus.sone.template.CssClassNameFilter;
import net.pterodactylus.sone.template.HttpRequestAccessor;
import net.pterodactylus.sone.template.ParserFilter;
import net.pterodactylus.sone.template.PostAccessor;
import net.pterodactylus.sone.template.ProfileAccessor;
+import net.pterodactylus.sone.template.RenderFilter;
import net.pterodactylus.sone.template.ReplyAccessor;
import net.pterodactylus.sone.template.ReplyGroupFilter;
import net.pterodactylus.sone.template.RequestChangeFilter;
/** The parser filter. */
private final ParserFilter parserFilter;
+ private final RenderFilter renderFilter;
/** The “new Sone” notification. */
private final ListNotification<Sone> newSoneNotification;
this.sonePlugin = sonePlugin;
this.loaders = loaders;
formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
- soneTextParser = new SoneTextParser(getCore(), getCore());
+ soneTextParser = new SoneTextParser(getCore(), getCore(), getCore());
templateContextFactory = new TemplateContextFactory();
templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
templateContextFactory.addFilter("match", new MatchFilter());
templateContextFactory.addFilter("css", new CssClassNameFilter());
templateContextFactory.addFilter("js", new JavascriptFilter());
- templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
+ templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), soneTextParser));
templateContextFactory.addFilter("reparse", new ReparseFilter());
+ templateContextFactory.addFilter("render", renderFilter = new RenderFilter(getCore(), templateContextFactory));
templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
templateContextFactory.addFilter("format", new FormatFilter());
templateContextFactory.addFilter("sort", new CollectionSortFilter());
templateContextFactory.addFilter("unique", new UniqueElementFilter());
templateContextFactory.addFilter("mod", new ModFilter());
templateContextFactory.addFilter("paginate", new PaginationFilter());
+ templateContextFactory.addFilter("build-id", new BuildIdFilter());
templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
templateContextFactory.addProvider(loaders.getTemplateProvider());
templateContextFactory.addTemplateObject("webInterface", this);
pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
- pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
- pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this, parserFilter, renderFilter)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter, renderFilter)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
package net.pterodactylus.sone.web.ajax;
+import java.util.Collections;
+
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Image;
+import net.pterodactylus.sone.template.ParserFilter;
+import net.pterodactylus.sone.template.RenderFilter;
+import net.pterodactylus.sone.text.Part;
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
/**
* Page that stores a user’s album modifications.
*/
public class EditAlbumAjaxPage extends JsonPage {
- /**
- * Creates a new edit album AJAX page.
- *
- * @param webInterface
- * The Sone web interface
- */
- public EditAlbumAjaxPage(WebInterface webInterface) {
+ private final ParserFilter parserFilter;
+ private final RenderFilter renderFilter;
+
+ public EditAlbumAjaxPage(WebInterface webInterface, ParserFilter parserFilter, RenderFilter renderFilter) {
super("editAlbum.ajax", webInterface);
+ this.parserFilter = parserFilter;
+ this.renderFilter = renderFilter;
}
//
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
String albumId = request.getHttpRequest().getParam("album");
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
return createErrorJsonObject("invalid-album-id");
}
- if (!album.getSone().isLocal()) {
+ if (!album.get().getSone().isLocal()) {
return createErrorJsonObject("not-authorized");
}
if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
- Album swappedAlbum = album.getParent().moveAlbumUp(album);
+ Album swappedAlbum = album.get().getParent().moveAlbumUp(album.get());
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId());
+ return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId());
}
if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
- Album swappedAlbum = album.getParent().moveAlbumDown(album);
+ Album swappedAlbum = album.get().getParent().moveAlbumDown(album.get());
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId());
+ return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId());
}
String title = request.getHttpRequest().getParam("title").trim();
String description = request.getHttpRequest().getParam("description").trim();
try {
- album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
+ album.get().modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("albumId", album.getId()).put("title", album.getTitle()).put("description", album.getDescription());
+ return createSuccessJsonObject().put("albumId", album.get().getId()).put("title", album.get().getTitle()).put("description", parseDescription(album.get()));
} catch (IllegalStateException e) {
return createErrorJsonObject("invalid-album-title");
}
}
+ private String parseDescription(Album album) {
+ Iterable<Part> parts = (Iterable<Part>) parserFilter.format(new TemplateContext(), album.getDescription(),
+ ImmutableMap.<String, Object>builder().put("sone", album.getSone()).build());
+ return (String) renderFilter.format(new TemplateContext(), parts, Collections.<String, Object>emptyMap());
+ }
+
}
package net.pterodactylus.sone.web.ajax;
+import java.util.Collections;
+
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.template.ParserFilter;
+import net.pterodactylus.sone.template.RenderFilter;
+import net.pterodactylus.sone.text.Part;
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
*/
public class EditImageAjaxPage extends JsonPage {
- /** Parser for image descriptions. */
private final ParserFilter parserFilter;
+ private final RenderFilter renderFilter;
/**
* Creates a new edit image AJAX page.
* @param parserFilter
* The parser filter for image descriptions
*/
- public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter) {
+ public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter, RenderFilter renderFilter) {
super("editImage.ajax", webInterface);
this.parserFilter = parserFilter;
+ this.renderFilter = renderFilter;
}
//
String description = request.getHttpRequest().getParam("description").trim();
image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", (String) parserFilter.format(new TemplateContext(), image.getDescription(), ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build()));
+ return createSuccessJsonObject().put("imageId", image.getId())
+ .put("title", image.getTitle())
+ .put("description", image.getDescription())
+ .put("parsedDescription", parseDescription(image));
+ }
+
+ private String parseDescription(Image image) {
+ Iterable<Part> parts = (Iterable<Part>) parserFilter.format(new TemplateContext(), image.getDescription(),
+ ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build());
+ return (String) renderFilter.format(new TemplateContext(), parts, Collections.<String, Object>emptyMap());
}
}
Page.Options.Option.FcpFullAccessRequired.Value.No=No
Page.Options.Option.FcpFullAccessRequired.Value.Writing=For Write Access
Page.Options.Option.FcpFullAccessRequired.Value.Always=Always
+Page.Options.Section.CompatibilityOptions.Title=Compatibility Options
+Page.Options.Section.CompatibilityOptions.Description=These options control how much deprecated functionality Sone will support.
+Page.Options.Option.CompatibilityOptions.OldElementIds.Description=Support old post IDs in links. Activating this will try to locate a post linked to by an old ID even though it can not be guaranteed that it is the post that was originally linked to.
Page.Options.Section.Cleaning.Title=Clean Up
Page.Options.Option.ClearOnNextRestart.Description=Resets the configuration of the Sone plugin at the next restart. Warning! {strong}This will destroy all of your Sones{/strong} so make sure you have backed up everyhing you still need! Also, you need to set the next option to true to actually do it.
Page.Options.Option.ReallyClearOnNextRestart.Description=This option needs to be set to “yes” if you really, {strong}really{/strong} want to clear the plugin configuration on the next restart.
View.Post.NotDownloaded=This post has not yet been downloaded, or it has been deleted.
View.Post.ShowMore=show more
View.Post.ShowLess=show less
+View.Post.LinkedAlbum.SizeAndAuthor={1,number} {1,choice,0#images|1#image|1<images} by [link]{0}[/link]
View.UpdateStatus.Text.ChooseSenderIdentity=Choose the sender identity
Notification.SoneIsInserting.Text=Your Sone sone://{0} is now being inserted.
Notification.SoneIsInserted.Text=Your Sone sone://{0} has been inserted in {1,number} {1,choice,0#seconds|1#second|1<seconds}.
Notification.SoneInsertAborted.Text=Your Sone sone://{0} could not be inserted.
+# 72-74
cursor: pointer;
}
+#sone .post .linked-album {
+ display: table;
+ margin-top: 1ex;
+ border-collapse: collapse;
+}
+
+#sone .post .linked-album .album-header {
+ display: table-caption;
+ margin-bottom: 1ex;
+}
+
+#sone .post .linked-album .album-header .album-title {
+ font-size: 150%;
+ font-weight: bold;
+}
+
+#sone .post .linked-image {
+ display: table-row;
+}
+
+#sone .post .linked-image .image-left {
+ display: table-cell;
+ padding-bottom: 1ex;
+}
+
+#sone .post .linked-image .image {
+ width: 160px;
+ height: 90px;
+ overflow: hidden;
+ border: solid 1px black;
+ display: inline-block;
+ padding: 0px;
+}
+
+#sone .post .linked-image .about-image {
+ display: table-cell;
+ padding-left: 1ex;
+}
+
+#sone .post .linked-image .about-image .title {
+ font-size: 110%;
+ font-weight: bold;
+}
+
+#sone .post .linked-image .about-image .description {
+ margin-top: 0.5ex;
+}
+
#sone .post .status-line {
margin-top: 0.5ex;
font-size: 85%;
height: 250px;
overflow: hidden;
padding: -1px;
+}
+
+#sone .image-container {
border: solid 1px #000;
}
width: 95%;
}
+#sone .album-container .image-container {
+ position: absolute;
+}
+
+#sone .album-container .link-to-album {
+ display: none;
+}
+
+#sone .album-container:hover .link-to-album {
+ display: block;
+ float: right;
+ z-index: 1;
+ padding: 0.5ex;
+ background-color: #eee;
+ position: relative;
+ top: 250px;
+ right: 0.5ex;
+ margin-top: -2em;
+}
+
#sone .image .album-sone {
font-size: 80%;
}
</li>
<%foreach currentSone.allImages image>
<li>
- <input type="radio" name="avatarId" value="<%image.id|html>"<%if avatarId|match value=image.id> checked="checked"<%/if>/>
+ <input type="radio" name="avatarId" value="<%image.internalId|html>"<%if avatarId|match value=image.internalId> checked="checked"<%/if>/>
<div class="post-avatar"><% image|image-link max-width==48 max-height==48 mode==enlarge title=image.title></div>
</li>
<%/foreach>
var albumDescriptionField = getAlbum(data.albumId).find(".album-description");
if (data.success) {
albumTitleField.text(data.title);
- albumDescriptionField.text(data.description);
+ albumDescriptionField.html(data.description);
getAlbum(data.albumId).find(":input[name='title']").attr("defaultValue", title);
getAlbum(data.albumId).find(":input[name='description']").attr("defaultValue", description);
} else {
<%/foreach>
</div>
- <p id="description"><% album.description|parse sone=album.sone></p>
+ <p id="description"><% album.description|parse sone=album.sone|render></p>
<%if album.sone.local>
<div class="show-edit-album hidden toggle-link"><a class="small-link">» <%= Page.ImageBrowser.Album.Edit.Title|l10n|html></a></div>
</div>
<div class="show-data">
<div class="image-title"><% image.title|html></div>
- <div class="image-description"><% image.description|parse sone=image.sone></div>
+ <div class="image-description"><% image.description|parse sone=image.sone|render></div>
</div>
<%if album.sone.local>
<form class="edit-image" action="editImage.html" method="post">
<%/if>
</div>
- <p class="parsed"><%image.description|parse sone=image.sone></p>
+ <p class="parsed"><%image.description|parse sone=image.sone|render></p>
<%if image.sone.local>
<div class="show-data">
<div class="album-sone"><a href="imageBrowser.html?sone=<%album.sone.id|html>"><%album.sone.niceName|html></a></div>
<div class="album-title"><% album.title|html> (<%= View.Sone.Stats.Images|l10n 0=album.images.size>)</div>
- <div class="album-description"><% album.description|parse sone=album.sone></div>
+ <div class="album-description"><% album.description|parse sone=album.sone|render></div>
</div>
</div>
<%= false|store key==endRow>
<%foreach albums album>
<%first><h2><%= Page.ImageBrowser.Header.Albums|l10n|html></h2><%/first>
<%if loop.count|mod divisor==3><div class="album-row"><%/if>
- <div id="album-<% album.id|html>" class="album">
- <div class="album-id hidden"><% album.id|html></div>
- <div class="album-container">
- <a href="imageBrowser.html?album=<% album.id|html>" title="<% album.title|html>">
- <%ifnull album.albumImage>
- <img src="images/unknown-image-0.png" width="333" height="250" alt="<% album.title|html>" title="<% album.title|html>" style="position: relative; top: 0px; left: -41px;" />
- <%else><!-- TODO -->
- <% album.albumImage|image-link max-width==250 max-height==250 mode==enlarge title=album.title>
- <%/if>
- </a>
- </div>
- <div class="show-data">
- <div class="album-title"><% album.title|html> (<%= View.Sone.Stats.Images|l10n 0=album.images.size>)</div>
- <div class="album-description"><% album.description|parse sone=album.sone></div>
- </div>
- <%if album.sone.local>
- <form class="edit-album" action="editAlbum.html" method="post">
- <input type="hidden" name="formPassword" value="<%formPassword|html>" />
- <input type="hidden" name="returnPage" value="<%request.uri|html>" />
- <input type="hidden" name="album" value="<%album.id|html>" />
-
- <div class="move-buttons">
- <button <%first>class="hidden" <%/first>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
- <button <%last>class="hidden" <%/last>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
- </div>
-
- <div class="edit-data hidden">
- <div>
- <input type="text" name="title" value="<%album.title|html>" />
- </div>
- <div>
- <textarea name="description"><%album.description|html></textarea>
- </div>
- <div>
- <button <%first>class="hidden" <%/first>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
- <button type="submit" name="submit"><%= Page.ImageBrowser.Album.Button.Save|l10n|html></button>
- <button <%last>class="hidden" <%/last>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
- </div>
- </div>
- </form>
- <%/if>
- </div>
+ <%include include/viewAlbum.html>
<%= false|store key==endRow>
<%if loop.count|mod divisor==3 offset==1><%= true|store key==endRow><%/if>
<%last><%= true|store key==endRow><%/last>
<a class="picture" href="index.html">
<%ifnull !currentSone>
<%ifnull !currentSone.profile.avatar>
- <%currentSone.profile.avatar|image-link max-width==80 max-height==80 mode==enlarge title=="Profile Avatar">
+ <%currentSone.profile.avatar|build-id sone=currentSone|image-link max-width==80 max-height==80 mode==enlarge title=="Profile Avatar">
<%else>
<img src="/WebOfTrust/GetIdenticon?identity=<% currentSone.id|html>&width=80&height=80" width="80" height="80" alt="Profile Avatar" />
<%/if>
<div class="sone-menu-id hidden"><%sone.id|html></div>
<div class="avatar menu-avatar">
<%ifnull !sone.profile.avatar>
- <%sone.profile.avatar|image-link max-width==64 max-height==64 mode==enlarge title=sone.niceName>
+ <%sone.profile.avatar|build-id sone=sone|image-link max-width==64 max-height==64 mode==enlarge title=sone.niceName>
<%else>
<img src="/WebOfTrust/GetIdenticon?identity=<%sone.id|html>&width=128&height=128" width="64" height="64" alt="Avatar Image" />
<%/if>
--- /dev/null
+<div id="album-<% album.id|html>" class="album">
+ <div class="album-id hidden"><% album.id|html></div>
+ <div class="album-container">
+ <a href="imageBrowser.html?album=<% album.id|html>" title="<% album.title|html>">
+ <%ifnull album.albumImage>
+ <img src="images/unknown-image-0.png" width="333" height="250" alt="<% album.title|html>" title="<% album.title|html>" style="position: relative; top: 0px; left: -41px;" />
+ <%else><!-- TODO -->
+ <div class="image-container">
+ <% album.albumImage|image-link class==album-image max-width==250 max-height==250 mode==enlarge title=album.title>
+ </div>
+ <div class="link-to-album"><a href="album://<% album.id|html>">[link album]</a></div>
+ <%/if>
+ </a>
+ </div>
+ <div class="show-data">
+ <div class="album-title"><% album.title|html> (<%= View.Sone.Stats.Images|l10n 0=album.images.size>)</div>
+ <div class="album-description"><% album.description|parse sone=album.sone|render></div>
+ </div>
+ <%if album.sone.local>
+ <form class="edit-album" action="editAlbum.html" method="post">
+ <input type="hidden" name="formPassword" value="<%formPassword|html>" />
+ <input type="hidden" name="returnPage" value="<%request.uri|html>" />
+ <input type="hidden" name="album" value="<%album.id|html>" />
+
+ <div class="move-buttons">
+ <button <%if loop.first>class="hidden" <%/if>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
+ <button <%if loop.last>class="hidden" <%/if>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
+ </div>
+
+ <div class="edit-data hidden">
+ <div>
+ <input type="text" name="title" value="<%album.title|html>" />
+ </div>
+ <div>
+ <textarea name="description"><%album.description|html></textarea>
+ </div>
+ <div>
+ <button <%if loop.first>class="hidden" <%/if>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
+ <button type="submit" name="submit"><%= Page.ImageBrowser.Album.Button.Save|l10n|html></button>
+ <button <%if loop.last>class="hidden" <%/if>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
+ </div>
+ </div>
+ </form>
+ <%/if>
+</div>
<div class="avatar post-avatar" >
<%if post.loaded>
<%ifnull !post.sone.profile.avatar>
- <%post.sone.profile.avatar|image-link max-width==48 max-height==48 mode==enlarge title=="Avatar Image">
+ <%post.sone.profile.avatar|build-id sone=post.sone|image-link max-width==48 max-height==48 mode==enlarge title=="Avatar Image">
<%else>
<img src="/WebOfTrust/GetIdenticon?identity=<% post.sone.id|html>&width=96&height=96" width="48" height="48" alt="Avatar Image" />
<%/if>
<%/if>
<%/if>
<% post.text|html|store key==originalText text==true>
- <% post.text|parse sone=post.sone|store key==parsedText text==true>
- <% post.text|parse sone=post.sone length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
+ <% post.text|parse sone=post.sone|store key==parts>
+ <% parts|render|store key==parsedText text==true>
+ <% parts|render length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
<div class="post-text raw-text<%if !raw> hidden<%/if>"><% originalText></div>
<div class="post-text text<%if raw> hidden<%/if><%if !shortText|match key=parsedText> hidden<%/if>"><% parsedText></div>
<div class="post-text short-text<%if raw> hidden<%/if><%if shortText|match key=parsedText> hidden<%/if>"><% shortText></div>
<%if !shortText|match value=parsedText><%if !raw><a class="expand-post-text" href="viewPost.html?post=<% post.id|html>&raw=true"><%= View.Post.ShowMore|l10n|html></a><%/if><%/if>
<%if !shortText|match value=parsedText><%if !raw><a class="shrink-post-text hidden"><%= View.Post.ShowLess|l10n|html></a><%/if><%/if>
</div>
+ <%foreach parts part>
+ <%if part.class.simpleName|match value==AlbumPart><!-- ← this is so ugly. -->
+ <div class="linked-album">
+ <div class="linked-image">
+ <div class="image-left">
+ <div class="image">
+ <a href="imageBrowser.html?album=<% part.album.id|html>"><% part.album.albumImage|image-link max-width==160 max-height==90 mode==enlarge title=part.album.title></a>
+ </div>
+ </div>
+ <div class="about-image">
+ <div class="title"><% part.album.title|html></div>
+ <div><% =View.Post.LinkedAlbum.SizeAndAuthor|l10n 0=part.album.sone.niceName 1=part.album.images.size|html|replace needle=="[link]" replacement=='<a href="viewSone.html?sone=<sone-id>">'|replace needle=='[/link]' replacement=='</a>'|replace needle=='<sone-id>' replacement=part.album.sone.id></div>
+ <div class="description"><% part.album.description|parse sone=part.album.sone|render></div>
+ </div>
+ </div>
+ </div>
+ <%/if>
+ <%/foreach>
<div class="post-status-line status-line<%if !post.loaded> hidden<%/if>">
<div class="bookmarks">
<form class="unbookmark<%if !post.bookmarked> hidden<%/if>" action="unbookmark.html" method="post">
<%include include/soneMenu.html class=="sone-reply-menu" sone=reply.sone>
<div class="avatar reply-avatar">
<%ifnull !reply.sone.profile.avatar>
- <% reply.sone.profile.avatar|image-link max-width==36 max-height==36 mode==enlarge title=="Avatar Image">
+ <% reply.sone.profile.avatar|build-id sone=reply.sone|image-link max-width==36 max-height==36 mode==enlarge title=="Avatar Image">
<%else>
<img src="/WebOfTrust/GetIdenticon?identity=<% reply.sone.id|html>&width=72&height=72" width="36" height="36" alt="Avatar Image" />
<%/if>
<div>
<div class="author profile-link"><a href="viewSone.html?sone=<% reply.sone.id|html>"><% reply.sone.niceName|html></a></div>
<% reply.text|html|store key==originalText text==true>
- <% reply.text|parse sone=reply.sone|store key==parsedText text==true>
- <% reply.text|parse sone=reply.sone length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
+ <% reply.text|parse sone=reply.sone|store key==parts>
+ <% parts|render|store key==parsedText text==true>
+ <% parts|render length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
<div class="reply-text raw-text<%if !raw> hidden<%/if>"><% originalText></div>
<div class="reply-text text<%if raw> hidden<%/if><%if !shortText|match key=parsedText> hidden<%/if>"><% parsedText></div>
<div class="reply-text short-text<%if raw> hidden<%/if><%if shortText|match key=parsedText> hidden<%/if>"><% shortText></div>
<posts>
<%foreach currentSone.posts post>
<post>
- <id><% post.id|xml></id>
+ <id><% post.internalId|xml></id>
<recipient><%if post.recipientId.present><% post.recipientId.get|xml><%/if></recipient>
<time><% post.time></time>
<text><% post.text|xml></text>
<replies>
<%foreach currentSone.replies reply>
<reply>
- <id><% reply.id></id>
+ <id><% reply.internalId></id>
<post-id><% reply.postId|xml></post-id>
<time><% reply.time></time>
<text><% reply.text|xml></text>
<albums>
<%/first>
<album>
- <id><%album.id|xml></id>
+ <id><%album.internalId|xml></id>
<%if !album.parent.root>
- <parent><%album.parent.id|xml></parent>
+ <parent><%album.parent.internalId|xml></parent>
<%/if>
<title><%album.title|xml></title>
<description><%album.description|xml></description>
- <album-image><%album.albumImage.id|xml></album-image>
+ <album-image><%album.albumImage.internalId|xml></album-image>
<%foreach album.images image>
<%first>
<images>
<%/first>
<image>
- <id><%image.id|xml></id>
+ <id><%image.internalId|xml></id>
<creation-time><%image.creationTime|xml></creation-time>
<key><%image.key|xml></key>
<title><%image.title|xml></title>
<%foreach messages message>
<%if message|substring start==0 length==1|match value=='!'>
- <p class="error"><% message|substring start==1|parse></p>
+ <p class="error"><% message|substring start==1|parse|render></p>
<%else>
- <p><% message|parse></p>
+ <p><% message|parse|render></p>
<%/if>
<%foreachelse>
<p><%= Page.Invalid.Text|l10n|html|replace needle=="{link}" replacement=='<a href="index.html">'|replace needle=="{/link}" replacement=='</a>'></p>
-<div class="text"><%= Notification.NewVersion.Text|l10n|replace needle=="{version}" replacement=latestVersion|replace needle=="{edition}" replacement=latestEdition|parse sone=="nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"></div>
+<div class="text"><%= Notification.NewVersion.Text|l10n|replace needle=="{version}" replacement=latestVersion|replace needle=="{edition}" replacement=latestEdition|parse sone=="nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"|render></div>
<%if soneStatus|match value=="inserting">
- <%= Notification.SoneIsInserting.Text|l10n 0=insertSone.id|parse>
+ <%= Notification.SoneIsInserting.Text|l10n 0=insertSone.id|parse|render>
<%elseif soneStatus|match value=="inserted">
- <%= Notification.SoneIsInserted.Text|l10n 0=insertSone.id 1=insertDuration|parse>
+ <%= Notification.SoneIsInserted.Text|l10n 0=insertSone.id 1=insertDuration|parse|render>
<%elseif soneStatus|match value=="insert-aborted">
- <%= Notification.SoneInsertAborted.Text|l10n 0=insertSone.id|parse>
+ <%= Notification.SoneInsertAborted.Text|l10n 0=insertSone.id|parse|render>
<%/if>
</select>
</p>
+ <h2><%= Page.Options.Section.CompatibilityOptions.Title|l10n|html></h2>
+
+ <p><%= Page.Options.Section.CompatibilityOptions.Description|l10n|html></p>
+
+ <p>
+ <input type="checkbox" name="compat-old-element-ids"<%if compat-old-element-ids> checked="checked"<%/if> />
+ <%= Page.Options.Option.CompatibilityOptions.OldElementIds.Description|l10n|html>
+ </p>
+
<p><button type="submit"><%= Page.Options.Button.Save|l10n|html></button></p>
</form>
<%foreach sone.profile.fields field>
<div class="profile-field">
<div class="name"><% field.name|html></div>
- <div class="value"><% field.value|parse sone=sone></div>
+ <div class="value"><% field.value|parse sone=sone|render></div>
</div>
<%/foreach>
byte[] imageData = new byte[] { 1, 2, 3, 4 };
temporaryImage.setImageData(imageData);
Image image = new ImageImpl("image-id");
+ image.modify().setSone(sone).update();
InsertToken insertToken = freenetInterface.new InsertToken(image);
InsertContext insertContext = mock(InsertContext.class);
when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
byte[] imageData = new byte[] { 1, 2, 3, 4 };
temporaryImage.setImageData(imageData);
Image image = new ImageImpl("image-id");
+ image.modify().setSone(sone).update();
InsertToken insertToken = freenetInterface.new InsertToken(image);
InsertContext insertContext = mock(InsertContext.class);
when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Album.Modifier;
import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.IdBuilder;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
@Before
public void setupAlbums() {
- when(core.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
+ when(core.getAlbum(anyString())).thenAnswer(new Answer<Optional<Album>>() {
@Override
- public Album answer(InvocationOnMock invocation)
+ public Optional<Album> answer(InvocationOnMock invocation)
throws Throwable {
- return albums.get(invocation.getArguments()[0]);
+ return Optional.fromNullable(albums.get(invocation.getArguments()[0]));
}
});
}
when(imageBuilder.withId(anyString())).thenAnswer(new Answer<ImageBuilder>() {
@Override
public ImageBuilder answer(InvocationOnMock invocation) {
- when(image.getId()).thenReturn(
- (String) invocation.getArguments()[0]);
+ when(image.getId()).thenReturn(new IdBuilder().buildId("identity", (String) invocation.getArguments()[0]));
+ when(image.getInternalId()).thenReturn((String) invocation.getArguments()[0]);
return imageBuilder;
}
});
assertThat(sone.getRootAlbum().getAlbums(), hasSize(1));
assertThat(sone.getRootAlbum().getAlbums().get(0).getImages(), hasSize(1));
Image image = sone.getRootAlbum().getAlbums().get(0).getImages().get(0);
- assertThat(image.getId(), is("image-id"));
+ assertThat(image.getId(), is(new IdBuilder().buildId("identity", "image-id")));
+ assertThat(image.getInternalId(), is("image-id"));
assertThat(image.getCreationTime(), is(1407197508000L));
assertThat(image.getKey(), is("KSK@GPLv3.txt"));
assertThat(image.getTitle(), is("image-title"));
--- /dev/null
+package net.pterodactylus.sone.data;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdBuilderTest}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdBuilderTest {
+
+ private static final String SONE_ID = "~Yp72VX0c6FLDvgIzip5wIvaGIIrjKcKvnX~pTaMKXs";
+ private static final String ELEMENT_ID = "88CC70AE-E853-4EEE-B245-E4C55F40DDDF";
+ private static final String EXPECTED_ID = "139a629a13f6a2c4191fb19ecead7e57335ea3deb2a971b88d5e004378c4daad";
+
+ private final IdBuilder idBuilder = new IdBuilder();
+
+ @Test
+ public void idBuilderBuildsCorrectIds() {
+ assertThat(idBuilder.buildId(SONE_ID, ELEMENT_ID), is(EXPECTED_ID));
+ }
+
+}
@Test
public void testBasicAlbumFunctionality() {
- Album newAlbum = new AlbumImpl(mock(Sone.class));
+ Album newAlbum = new AlbumImpl(when(mock(Sone.class).getId()).thenReturn(SONE_ID).<Sone>getMock());
assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
memoryDatabase.storeAlbum(newAlbum);
assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));
--- /dev/null
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.Mockito.when;
+
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Unit test for {@link BuildIdFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BuildIdFilterTest {
+
+ private static final String SONE_ID = "~Yp72VX0c6FLDvgIzip5wIvaGIIrjKcKvnX~pTaMKXs";
+ private static final String ELEMENT_ID = "88CC70AE-E853-4EEE-B245-E4C55F40DDDF";
+ private static final Object EXPECTED_ID = "139a629a13f6a2c4191fb19ecead7e57335ea3deb2a971b88d5e004378c4daad";
+
+ private final BuildIdFilter buildIdFilter = new BuildIdFilter();
+ private final TemplateContext templateContext = null;
+
+ @Test
+ public void filterBuildsCorrectIdsWithSoneAsString() {
+ assertThat(buildIdFilter.format(templateContext, ELEMENT_ID, ImmutableMap.<String, Object>of("sone", SONE_ID)), is(EXPECTED_ID));
+ }
+
+ @Test
+ public void filterBuildsCorrectIdsWithSoneAsSone() {
+ Sone sone = Mockito.mock(Sone.class);
+ when(sone.getId()).thenReturn(SONE_ID);
+ assertThat(buildIdFilter.format(templateContext, ELEMENT_ID, ImmutableMap.<String, Object>of("sone", sone)), is(EXPECTED_ID));
+ }
+
+ @Test
+ public void filterReturnsNullIfSoneNotPresent() {
+ assertThat(buildIdFilter.format(templateContext, ELEMENT_ID, ImmutableMap.<String, Object>of()), nullValue());
+ }
+
+ @Test
+ public void filterReturnsNullIfElementNotPresent() {
+ assertThat(buildIdFilter.format(templateContext, null, ImmutableMap.<String, Object>of("sone", SONE_ID)), nullValue());
+ }
+
+}
--- /dev/null
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+/**
+ * Unit test for {@link ParserFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ParserFilterTest {
+
+ private final Core core = mock(Core.class);
+ private final SoneTextParser soneTextParser = mock(SoneTextParser.class);
+ private final ParserFilter parserFilter = new ParserFilter(core, soneTextParser);
+ private final TemplateContext templateContext = new TemplateContext();
+ private final Sone sone = mock(Sone.class);
+
+ @Test
+ public void filterReturnsPartsReturnedByParser() throws IOException {
+ List<Part> parts = setupSoneTextParser();
+ assertThat(parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), is((Object) parts));
+ }
+
+ private List<Part> setupSoneTextParser() throws IOException {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("Text"));
+ when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenReturn(parts);
+ return parts;
+ }
+
+ @Test
+ public void filterUsesGivenSone() throws IOException {
+ List<Part> parts = setupSoneTextParser();
+ assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", sone)), is((Object) parts));
+ verifyThatContextContainsCorrectSone();
+ }
+
+ @Test
+ public void filterGetsCorrectSoneFromCore() throws IOException {
+ when(core.getSone("sone-id")).thenReturn(Optional.of(sone));
+ List<Part> parts = setupSoneTextParser();
+ assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", "sone-id")), is((Object) parts));
+ verifyThatContextContainsCorrectSone();
+ }
+
+ private void verifyThatContextContainsCorrectSone() throws IOException {
+ ArgumentCaptor<SoneTextParserContext> contextArgumentCaptor = ArgumentCaptor.forClass(SoneTextParserContext.class);
+ verify(soneTextParser).parse(contextArgumentCaptor.capture(), any(StringReader.class));
+ assertThat(contextArgumentCaptor.getValue().getPostingSone(), is(sone));
+ }
+
+ @Test
+ public void filterReturnsEmptyCollectionOnExceptionInParser() throws IOException {
+ when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenThrow(IOException.class);
+ assertThat((Collection<Part>) parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), empty());
+ }
+
+}
--- /dev/null
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.AlbumPart;
+import net.pterodactylus.sone.text.FreenetLinkPart;
+import net.pterodactylus.sone.text.LinkPart;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.PostPart;
+import net.pterodactylus.sone.text.SonePart;
+import net.pterodactylus.util.template.HtmlFilter;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link RenderFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RenderFilterTest {
+
+ private static final Map<String, Object> EMPTY_MAP = Collections.emptyMap();
+ private final Core core = mock(Core.class);
+ private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
+ private final RenderFilter renderFilter = new RenderFilter(core, templateContextFactory);
+ private final TemplateContext templateContext = new TemplateContext();
+ private final Sone sone = mock(Sone.class);
+ private final Post post = mock(Post.class);
+
+ @Before
+ public void setupTemplateContextFactory() {
+ templateContextFactory.addFilter("html", new HtmlFilter());
+ }
+
+ @Before
+ public void setupSone() {
+ when(sone.getId()).thenReturn("sone-id");
+ when(sone.getName()).thenReturn("SoneName");
+ when(sone.getProfile()).thenReturn(new Profile(sone));
+ }
+
+ @Before
+ public void setupPost() {
+ when(post.getId()).thenReturn("post-id");
+ when(post.getSone()).thenReturn(sone);
+ }
+
+ @Test
+ public void filterCanRenderPlainText() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "<Text>"));
+ }
+
+ @Test
+ public void filterCanRenderMultiplePlainTextParts() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"), new PlainTextPart("<Foo>"));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "<Text><Foo>"));
+ }
+
+ @Test
+ public void filterCanRenderUntrustedFreenetLinks() {
+ List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", false));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"freenet\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
+ }
+
+ @Test
+ public void filterCanRenderTrustedFreenetLinks() {
+ List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", true));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"freenet-trusted\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
+ }
+
+ @Test
+ public void filterCanRenderInternetLinks() {
+ List<Part> parts = Arrays.<Part>asList(new LinkPart("http://link.sone", "link.sone"));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"internet\" href=\"/external-link/?_CHECKED_HTTP_=http%3A%2F%2Flink.sone\" title=\"link.sone\">link.sone</a>"));
+ }
+
+ @Test
+ public void filterCanRenderSonePartsForUnknownSones() {
+ Sone unknownSone = mock(Sone.class);
+ when(unknownSone.getId()).thenReturn("sone-id");
+ List<Part> parts = Arrays.<Part>asList(new SonePart(unknownSone));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"/WebOfTrust/ShowIdentity?id=sone-id\" title=\"sone-id\">sone-id</a>"));
+ }
+
+ @Test
+ public void filterCanRenderSonePartsForKnownSones() {
+ List<Part> parts = Arrays.<Part>asList(new SonePart(sone));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewSone.html?sone=sone-id\" title=\"SoneName\">SoneName</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostParts() {
+ when(post.getText()).thenReturn("123456789012345678901234567890");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345678901234567890…</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostPartsWithoutBreakingWords() {
+ when(post.getText()).thenReturn("12345 12345 12345 12345 12345 12345");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345 12345…</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostPartsWithShortText() {
+ when(post.getText()).thenReturn("12345 12345");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostPartsWithOldPostIds() {
+ when(post.getText()).thenReturn("12345 12345");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post, true));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"internet\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
+ }
+
+ @Test
+ public void filterCanRenderAlbumParts() {
+ Album album = mock(Album.class);
+ when(album.getId()).thenReturn("album-id");
+ when(album.getTitle()).thenReturn("Title");
+ when(album.getDescription()).thenReturn("Description");
+ List<Part> parts = Arrays.<Part>asList(new AlbumPart(album));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"imageBrowser.html?album=album-id\" title=\"Description\">Title</a>"));
+ }
+
+ @Test
+ public void filterHonorsLength() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
+ assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10")),
+ is((Object) "1234567890…"));
+ }
+
+ @Test
+ public void filterHonorsCutOffLength() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
+ assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10", "cut-off-length", "5")),
+ is((Object) "12345…"));
+ }
+
+}
package net.pterodactylus.sone.text;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
+import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.SoneProvider;
import com.google.common.base.Function;
import com.google.common.base.Optional;
-import junit.framework.TestCase;
+import org.junit.Test;
/**
* JUnit test case for {@link SoneTextParser}.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class SoneTextParserTest extends TestCase {
+public class SoneTextParserTest {
- //
- // ACTIONS
- //
+ private final SoneProvider soneProvider = new TestSoneProvider();
+ private final TestPostProvider postProvider = new TestPostProvider();
+ private final TestAlbumProvider albumProvider = new TestAlbumProvider();
+ private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider, albumProvider);
/**
* Tests basic plain-text operation of the parser.
* @throws IOException
* if an I/O error occurs
*/
- @SuppressWarnings("static-method")
+ @Test
public void testPlainText() throws IOException {
- SoneTextParser soneTextParser = new SoneTextParser(null, null);
Iterable<Part> parts;
/* check basic operation. */
parts = soneTextParser.parse(null, new StringReader("Test."));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, PlainTextPart.class), is("Test."));
/* check empty lines at start and end. */
parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, PlainTextPart.class), is("Test."));
/* check duplicate empty lines in the text. */
parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
}
/**
* @throws IOException
* if an I/O error occurs
*/
- @SuppressWarnings("static-method")
+ @Test
public void testKSKLinks() throws IOException {
- SoneTextParser soneTextParser = new SoneTextParser(null, null);
Iterable<Part> parts;
/* check basic links. */
parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
/* check embedded links. */
parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
/* check embedded links and line breaks. */
parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class),
+ is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
}
/**
* @throws IOException
* if an I/O error occurs
*/
- @SuppressWarnings({ "synthetic-access", "static-method" })
+ @Test
public void testEmptyLinesAndSoneLinks() throws IOException {
- SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
Iterable<Part> parts;
/* check basic links. */
parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, PlainTextPart.class, SonePart.class),
+ is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
}
/**
* @throws IOException
* if an I/O error occurs
*/
- @SuppressWarnings({ "synthetic-access", "static-method" })
+ @Test
public void testEmpyHttpLinks() throws IOException {
- SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
Iterable<Part> parts;
/* check empty http links. */
parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
- assertNotNull("Parts", parts);
- assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
+ assertThat(parts, notNullValue());
+ assertThat(convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
+ }
+
+ @Test
+ public void linksToPostAreParsedCorrectly() throws IOException {
+ postProvider.addValidPostId("foo", "internal", "Post about foo...");
+ Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This post://foo is awesome."));
+ assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|new|foo|Post about foo...] is awesome."));
+ }
+
+ @Test
+ public void linksToPostsWithOldIdsAreParsedCorrectly() throws IOException {
+ postProvider.addValidPostId("foo", "internal", "Post about foo...");
+ Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This post://internal is awesome."));
+ assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|old|foo|Post about foo...] is awesome."));
+ }
+
+ @Test
+ public void linksToAlbumIsParsedCorrectly() throws IOException {
+ albumProvider.addAlbumTitle("album-id", "Super Album");
+ Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This album://album-id rocks!"));
+ assertThat(convertText(parts, PlainTextPart.class, AlbumPart.class), is("This [album|album-id|Super Album] rocks!"));
}
//
private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
StringBuilder text = new StringBuilder();
for (Part part : parts) {
- assertNotNull("Part", part);
+ assertThat(part, notNullValue());
boolean classValid = validClasses.length == 0;
for (Class<?> validClass : validClasses) {
if (validClass.isAssignableFrom(part.getClass())) {
break;
}
}
- if (!classValid) {
- fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
- }
+ assertThat("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses), classValid, is(true));
if (part instanceof PlainTextPart) {
- text.append(((PlainTextPart) part).getText());
+ text.append(part.getText());
} else if (part instanceof FreenetLinkPart) {
FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
- text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
+ text.append('[')
+ .append(freenetLinkPart.getLink())
+ .append('|')
+ .append(freenetLinkPart.isTrusted() ? "trusted|" : "")
+ .append(freenetLinkPart.getTitle())
+ .append('|')
+ .append(freenetLinkPart.getText())
+ .append(']');
} else if (part instanceof LinkPart) {
LinkPart linkPart = (LinkPart) part;
- text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
+ text.append('[')
+ .append(linkPart.getLink())
+ .append('|')
+ .append(linkPart.getTitle())
+ .append('|')
+ .append(linkPart.getText())
+ .append(']');
} else if (part instanceof SonePart) {
SonePart sonePart = (SonePart) part;
text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
+ } else if (part instanceof PostPart) {
+ PostPart postPart = (PostPart) part;
+ text.append("[post|")
+ .append(postPart.usesDeprecatedLink() ? "old" : "new")
+ .append('|')
+ .append(postPart.getPost().getId())
+ .append('|')
+ .append(postPart.getPost().getText())
+ .append(']');
+ } else if (part instanceof AlbumPart) {
+ Album album = ((AlbumPart) part).getAlbum();
+ text.append(String.format("[album|%s|%s]", album.getId(), album.getTitle()));
}
}
return text.toString();
}
+ private static class TestPostProvider implements PostProvider {
+
+ private final Map<String, String> postTexts = new HashMap<String, String>();
+ private final Map<String, String> postInternalIds = new HashMap<String, String>();
+ private final Map<String, String> internalIdPosts = new HashMap<String, String>();
+
+ private void addValidPostId(String validPostId, String internalId, String text) {
+ postTexts.put(validPostId, text);
+ postInternalIds.put(validPostId, internalId);
+ internalIdPosts.put(internalId, validPostId);
+ }
+
+ @Override
+ public Collection<Post> getDirectedPosts(String recipientId) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public Collection<Post> getPosts(String soneId) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public Optional<Post> getPost(String postId) {
+ if (postTexts.containsKey(postId)) {
+ Post post = mock(Post.class);
+ when(post.getId()).thenReturn(postId);
+ when(post.getInternalId()).thenReturn(postInternalIds.get(postId));
+ when(post.getText()).thenReturn(postTexts.get(postId));
+ return Optional.of(post);
+ } else if (internalIdPosts.containsKey(postId)) {
+ Post post = mock(Post.class);
+ when(post.getId()).thenReturn(internalIdPosts.get(postId));
+ when(post.getInternalId()).thenReturn(postId);
+ when(post.getText()).thenReturn(postTexts.get(internalIdPosts.get(postId)));
+ return Optional.of(post);
+ }
+ return Optional.absent();
+ }
+
+ }
+
+ private static class TestAlbumProvider implements AlbumProvider {
+
+ private final Map<String, String> albumTitles = new HashMap<String, String>();
+
+ public void addAlbumTitle(String albumId, String albumTitle) {
+ albumTitles.put(albumId, albumTitle);
+ }
+
+ @Override
+ public Optional<Album> getAlbum(String albumId) {
+ if (albumTitles.containsKey(albumId)) {
+ Album album = mock(Album.class);
+ when(album.getId()).thenReturn(albumId);
+ when(album.getTitle()).thenReturn(albumTitles.get(albumId));
+ return Optional.of(album);
+ }
+ return Optional.absent();
+ }
+
+ }
+
}