From 3e4807157e3c244820dd4d5149997970530e4fcb Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Wed, 23 Aug 2017 00:47:37 +0300
Subject: www: Twitter Cards
---
.../java/com/juick/www/controllers/UserThread.java | 21 ++++++++-
.../src/test/java/com/juick/www/WebAppTests.java | 52 +++++++++++++++++++++-
2 files changed, 71 insertions(+), 2 deletions(-)
(limited to 'juick-www/src')
diff --git a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java
index f15861f4..400f231a 100644
--- a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java
+++ b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java
@@ -16,11 +16,15 @@
*/
package com.juick.www.controllers;
+import com.juick.formatters.PlainTextFormatter;
import com.juick.server.util.HttpForbiddenException;
import com.juick.server.util.HttpNotFoundException;
import com.juick.server.util.UserUtils;
+import com.juick.service.CrosspostService;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringEscapeUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
@@ -46,6 +50,8 @@ public class UserThread {
private MessagesService messagesService;
@Inject
private UserService userService;
+ @Inject
+ private CrosspostService crosspostService;
@GetMapping("/{uname}/{mid}")
protected String threadAction(ModelMap model,
@@ -94,12 +100,25 @@ public class UserThread {
model.addAttribute("title", title);
model.addAttribute("visitor", visitor);
String headers = "";
+ String pageUrl = "//juick.com/" + msg.getUser().getName() + "/" + msg.getMid();
if (paramView != null) {
- headers += "";
+ headers += "";
}
if (msg.Hidden) {
headers += "";
}
+ String cardType = StringUtils.isNotEmpty(msg.getAttachmentType()) ? "summary_large_image" : "summary";
+ String msgImage = StringUtils.isNotEmpty(msg.getAttachmentType()) ? msg.getAttachmentURL() : "//i.juick.com/a/" + msg.getUser().getUid() + ".png";
+ headers += "\n" +
+ "\n" +
+ "\n" +
+ "\n" +
+ "\n" +
+ "";
+ String twitterName = crosspostService.getTwitterName(msg.getUser().getUid());
+ if (StringUtils.isNotEmpty(twitterName)) {
+ headers += "\n";
+ }
model.addAttribute("headers", headers);
model.addAttribute("contentStyle", "margin-left: 0; width: 100%");
model.addAttribute("isModerator", visitor.getUid() == 3694);
diff --git a/juick-www/src/test/java/com/juick/www/WebAppTests.java b/juick-www/src/test/java/com/juick/www/WebAppTests.java
index d09bded2..464e9416 100644
--- a/juick-www/src/test/java/com/juick/www/WebAppTests.java
+++ b/juick-www/src/test/java/com/juick/www/WebAppTests.java
@@ -20,6 +20,7 @@ package com.juick.www;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.css.StyleElement;
+import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.juick.Message;
import com.juick.Tag;
@@ -29,6 +30,7 @@ import com.juick.server.configuration.BaseWebConfiguration;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.test.util.MockUtils;
+import com.juick.util.MessageUtils;
import com.juick.www.configuration.SapeConfiguration;
import com.juick.www.configuration.WwwAppConfiguration;
import com.juick.www.configuration.WwwServletConfiguration;
@@ -51,13 +53,16 @@ import javax.inject.Inject;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
-import java.util.*;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.startsWith;
import static org.mockito.Mockito.when;
/**
@@ -195,4 +200,49 @@ public class WebAppTests {
String output = writer.toString().trim();
assertThat(output, equalTo(">_<"));
}
+
+ public DomElement fetchMeta(String url, String name) throws IOException {
+ HtmlPage threadPage = webClient.getPage(url);
+ DomElement emptyMeta = new DomElement("", "meta", null, null);
+ return threadPage.getElementsByTagName("meta").stream()
+ .filter(t -> t.getAttribute("name").equals(name)).findFirst().orElse(emptyMeta);
+ }
+ @Test
+ public void testTwitterCards() throws Exception {
+ String ugnichName = "ugnich";
+ String ugnichPassword = "MyPassw0rd!";
+ String msgText = "Привет, я - Угнич";
+ String hash = "12345678";
+
+ User user = MockUtils.mockUser(1, ugnichName, ugnichPassword);
+ Message msg = MockUtils.mockMessage(1, user, msgText);
+
+ when(userService.getUIDbyName(ugnichName))
+ .thenReturn(1);
+ when(userService.getUserByName(ugnichName))
+ .thenReturn(user);
+ when(userService.getUserByUID(1))
+ .thenReturn(Optional.of(user));
+ when(userService.getFullyUserByName(ugnichName))
+ .thenReturn(user);
+ when(messagesService.getMyFeed(1, 0))
+ .thenReturn(Collections.singletonList(1));
+ when(messagesService.getMessages(Collections.singletonList(1)))
+ .thenReturn(Collections.singletonList(msg));
+ when(userService.getUIDbyHash(hash))
+ .thenReturn(1);
+ when(messagesService.getMessageAuthor(1)).thenReturn(user);
+ when(messagesService.canViewThread(1, 0)).thenReturn(true);
+ when(messagesService.getMessage(1)).thenReturn(msg);
+
+ assertThat(fetchMeta("http://localhost:8080/ugnich/1", "twitter:card")
+ .getAttribute("content"), equalTo("summary"));
+ msg.setAttachmentType("png");
+ assertThat(fetchMeta("http://localhost:8080/ugnich/1", "twitter:card")
+ .getAttribute("content"), equalTo("summary_large_image"));
+ assertThat(fetchMeta("http://localhost:8080/ugnich/1", "og:description")
+ .getAttribute("content"),
+ startsWith(StringEscapeUtils.escapeHtml4(MessageUtils.getMessageHashTags(msg))));
+
+ }
}
--
cgit v1.2.3