From ca94f0b8951a3520f219dceb68e67fe8e9886c07 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 8 Dec 2022 15:57:30 +0300 Subject: Redirect thread URL to ActivityPub version if Accept header matches --- src/main/java/com/juick/www/controllers/Site.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/main/java/com/juick/www') 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 replies = messagesService.getReplies(visitor, msg.getMid()); -- cgit v1.2.3