aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-12-08 15:57:30 +0300
committerGravatar Vitaly Takmazov2022-12-09 02:03:46 +0300
commitca94f0b8951a3520f219dceb68e67fe8e9886c07 (patch)
treeca3d29daf0c0a4bfd44b0d9be23642e159dcfb92 /src
parent7fc3fd3d82d45d2501c03a683ecd1ec71f676943 (diff)
Redirect thread URL to ActivityPub version if Accept header matches
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/juick/www/controllers/Site.java18
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java35
2 files changed, 39 insertions, 14 deletions
diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java
index e28f8e15..2484c8fa 100644
--- a/src/main/java/com/juick/www/controllers/Site.java
+++ b/src/main/java/com/juick/www/controllers/Site.java
@@ -24,6 +24,7 @@ import com.juick.util.HttpForbiddenException;
import com.juick.util.HttpNotFoundException;
import com.juick.util.WebUtils;
import com.juick.www.WebApp;
+import com.juick.www.api.activity.model.Context;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
@@ -34,6 +35,7 @@ import com.juick.util.MessageUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.text.StringEscapeUtils;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -45,8 +47,8 @@ import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.inject.Inject;
import java.net.URLEncoder;
@@ -481,9 +483,11 @@ public class Site {
return "views/pm_sent";
}
- @GetMapping(value = "/{uname}/{mid}", produces = MediaType.TEXT_HTML_VALUE)
+ @GetMapping(value = "/{uname}/{mid}", produces = { MediaType.TEXT_HTML_VALUE, Context.ACTIVITY_MEDIA_TYPE,
+ Context.LD_JSON_MEDIA_TYPE })
protected String threadAction(@Visitor User visitor, ModelMap model, @PathVariable String uname,
@PathVariable int mid,
+ @RequestHeader(name = HttpHeaders.ACCEPT, required = false) String acceptHeader,
@CookieValue(name = "sape_cookie", required = false, defaultValue = StringUtils.EMPTY) String sapeCookie) {
if (!messagesService.canViewThread(mid, visitor.getUid())) {
throw new HttpForbiddenException();
@@ -501,6 +505,16 @@ public class Site {
if (user.isAnonymous() || !msg.getUser().equals(user)) {
return String.format("redirect:/%s/%d", msg.getUser().getName(), mid);
}
+ String linkedDataLocation = "/n/" + mid + "-0";
+ if (StringUtils.isNotBlank(acceptHeader)) {
+ var mediaTypes = MediaType.parseMediaTypes(acceptHeader);
+ var isLinkedDataRequest = (mediaTypes.stream().anyMatch(mediaType -> !mediaType.isWildcardType()
+ && (mediaType.isCompatibleWith(MediaType.valueOf(Context.ACTIVITY_MEDIA_TYPE))
+ || mediaType.isCompatibleWith(MediaType.valueOf(Context.LD_JSON_MEDIA_TYPE)))));
+ if (isLinkedDataRequest) {
+ return "redirect:" + linkedDataLocation;
+ }
+ }
msg.VisitorCanComment = !visitor.isAnonymous();
msg.getUser().setAvatar(webApp.getAvatarWebPath(msg.getUser()));
List<Message> replies = messagesService.getReplies(visitor, msg.getMid());
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 1e956855..d80c9251 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -2331,18 +2331,19 @@ public class ServerTests {
}
@Test
- public void verifiedUsersTest() {
- assertThat(userService.getUserByName("ugnich").isVerified(), is(false));
- jdbcTemplate.update("INSERT INTO telegram(user_id, tg_id) VALUES(?, ?)", ugnich.getUid(), "100001866137681");
- assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false));
- userService.addFacebookState("12345", "http://localhost");
- userService.createFacebookUser(12345, "12345", "5678", "ugnich");
- userService.setFacebookUser("12345", ugnich.getUid());
- assertThat(userService.getUserByName("ugnich").isVerified(), is(true));
- assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(true));
- jdbcTemplate.update("DELETE FROM facebook");
- assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false));
- }
+ public void verifiedUsersTest() {
+ assertThat(userService.getUserByName("ugnich").isVerified(), is(false));
+ jdbcTemplate.update("INSERT INTO telegram(user_id, tg_id) VALUES(?, ?)", ugnich.getUid(),
+ "100001866137681");
+ assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false));
+ userService.addFacebookState("12345", "http://localhost");
+ userService.createFacebookUser(12345, "12345", "5678", "ugnich");
+ userService.setFacebookUser("12345", ugnich.getUid());
+ assertThat(userService.getUserByName("ugnich").isVerified(), is(true));
+ assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(true));
+ jdbcTemplate.update("DELETE FROM facebook");
+ assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false));
+ }
@Test
@Disabled("FIXME: profile changed as expected, but cache is not refreshed or something")
@@ -2733,4 +2734,14 @@ public class ServerTests {
.andExpect(jsonPath("$.pms", empty()));
}
+
+ @Test
+ public void ldRequestToThreadShouldRedirect() throws Exception {
+ var result = commandsManager.processCommand(ugnich, "test", emptyUri);
+ var mid = result.getNewMessage().get().getMid();
+ var htmlUri = String.format("/ugnich/%d", mid);
+ var ldUri = String.format("/n/%d-0", mid);
+ mockMvc.perform(get(htmlUri).accept(Context.LD_JSON_MEDIA_TYPE)).andExpect(status().is3xxRedirection())
+ .andExpect(redirectedUrl(ldUri));
+ }
}