Render freemail links as links
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 29 Oct 2016 08:21:32 +0000 (10:21 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 29 Oct 2016 08:21:32 +0000 (10:21 +0200)
src/main/java/net/pterodactylus/sone/template/ParserFilter.java
src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java [new file with mode: 0644]

index 7cf8c70..c12b36d 100644 (file)
@@ -29,8 +29,11 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import javax.annotation.Nonnull;
+
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.FreemailPart;
 import net.pterodactylus.sone.text.FreenetLinkPart;
 import net.pterodactylus.sone.text.LinkPart;
 import net.pterodactylus.sone.text.Part;
@@ -45,6 +48,9 @@ import net.pterodactylus.util.template.TemplateContext;
 import net.pterodactylus.util.template.TemplateContextFactory;
 import net.pterodactylus.util.template.TemplateParser;
 
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+
 /**
  * Filter that filters a given text through a {@link SoneTextParser}.
  *
@@ -170,6 +176,8 @@ public class ParserFilter implements Filter {
                        render(writer, (SonePart) part);
                } else if (part instanceof PostPart) {
                        render(writer, (PostPart) part);
+               } else if (part instanceof FreemailPart) {
+                       render(writer, (FreemailPart) part);
                } else if (part instanceof Iterable<?>) {
                        render(writer, (Iterable<Part>) part);
                }
@@ -263,6 +271,22 @@ public class ParserFilter implements Filter {
                renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
        }
 
+       private void render(@Nonnull Writer writer, @Nonnull FreemailPart freemailPart) {
+               Optional<Sone> sone = core.getSone(freemailPart.getIdentityId());
+               String soneName = sone.transform(new Function<Sone, String>() {
+                       @Nonnull
+                       @Override
+                       public String apply(Sone input) {
+                               return SoneAccessor.getNiceName(input);
+                       }
+               }).or(freemailPart.getIdentityId());
+               renderLink(writer,
+                               "/Freemail/NewMessage?to=" + freemailPart.getIdentityId(),
+                               String.format("%s@%s.freemail", freemailPart.getEmailLocalPart(), soneName),
+                               String.format("%s", soneName),
+                               "in-sone");
+       }
+
        /**
         * Renders the given link.
         *
diff --git a/src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java b/src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java
new file mode 100644 (file)
index 0000000..a689ab8
--- /dev/null
@@ -0,0 +1,73 @@
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.FreemailPart;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
+import net.pterodactylus.util.template.HtmlFilter;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+
+import com.google.common.base.Optional;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Attribute;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link ParserFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ParserFilterTest {
+
+       private static final String FREEMAIL_ID = "t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra";
+       private static final String SONE_FREEMAIL = "sone@" + FREEMAIL_ID + ".freemail";
+       private static final String SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI";
+
+       private final Core core = mock(Core.class);
+       private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
+       private final SoneTextParser soneTextParser = mock(SoneTextParser.class);
+       private final ParserFilter filter = new ParserFilter(core, templateContextFactory, soneTextParser);
+
+       @Before
+       public void setupTemplateContextFactory() {
+               templateContextFactory.addFilter("html", new HtmlFilter());
+       }
+
+       @Test
+       public void freemailAddressIsDisplayedCorrectly() {
+               Sone sone = mock(Sone.class);
+               when(sone.getProfile()).thenReturn(new Profile(sone));
+               sone.getProfile().setFirstName("Sone");
+               when(core.getSone(SONE_IDENTITY)).thenReturn(Optional.of(sone));
+               when(soneTextParser.parse(anyString(), any(SoneTextParserContext.class))).thenReturn(Collections.<Part>singletonList(new FreemailPart("sone", FREEMAIL_ID, SONE_IDENTITY)));
+               TemplateContext templateContext = templateContextFactory.createTemplateContext();
+               String output = String.valueOf(filter.format(templateContext, SONE_FREEMAIL, Collections.<String, Object>emptyMap()));
+               Document document = Jsoup.parseBodyFragment(output);
+               Element linkNode = document.body().child(0);
+               assertThat(linkNode.nodeName(), is("a"));
+               assertThat(linkNode.attributes().asList(), containsInAnyOrder(
+                               new Attribute("href", "/Freemail/NewMessage?to=" + SONE_IDENTITY),
+                               new Attribute("class", "in-sone"),
+                               new Attribute("title", "Sone")
+               ));
+               assertThat(linkNode.text(), is("sone@Sone.freemail"));
+       }
+
+}