From f8406131fdbe835c52fd46ad68ddc7b0df54cffe Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 22 Dec 2022 05:43:40 +0300 Subject: `@username@example.com` urls are now redirects to ActivityPub profile --- src/main/java/com/juick/SignatureManager.java | 3 +-- .../java/com/juick/util/ActivityPubRequestInterceptor.java | 7 ++++--- src/main/java/com/juick/util/MessageUtils.java | 6 +++--- src/main/java/com/juick/www/controllers/Compat.java | 14 ++++++++++++++ src/test/java/com/juick/server/tests/ServerTests.java | 6 ++++++ 5 files changed, 28 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main/java/com/juick/SignatureManager.java b/src/main/java/com/juick/SignatureManager.java index fad4e0da..295909cc 100644 --- a/src/main/java/com/juick/SignatureManager.java +++ b/src/main/java/com/juick/SignatureManager.java @@ -177,9 +177,8 @@ public class SignatureManager { public Optional discoverPerson(String acct) { Jid acctId = Jid.of(acct); URI resourceUri = UriComponentsBuilder.fromPath("/.well-known/webfinger").host(acctId.getDomain()) - .scheme("https").queryParam("resource", String.format("%s", acctId.toEscapedString())).build().toUri(); + .scheme("https").queryParam("resource", "acct:" + acct).build().toUri(); HttpHeaders headers = new HttpHeaders(); - headers.add("Accept", "application/jrd+json"); HttpEntity webfingerRequest = new HttpEntity<>(headers); try { ResponseEntity response = apClient.exchange(resourceUri, HttpMethod.GET, webfingerRequest, diff --git a/src/main/java/com/juick/util/ActivityPubRequestInterceptor.java b/src/main/java/com/juick/util/ActivityPubRequestInterceptor.java index 8b578aa0..79beaa09 100644 --- a/src/main/java/com/juick/util/ActivityPubRequestInterceptor.java +++ b/src/main/java/com/juick/util/ActivityPubRequestInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2021, Juick + * Copyright (C) 2008-2022, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,6 +17,7 @@ package com.juick.util; +import org.jetbrains.annotations.NotNull; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; @@ -30,8 +31,8 @@ import com.juick.www.api.activity.model.Activity; public class ActivityPubRequestInterceptor implements ClientHttpRequestInterceptor { @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { - request.getHeaders().set(HttpHeaders.ACCEPT, Activity.ACTIVITY_MEDIA_TYPE); + public @NotNull ClientHttpResponse intercept(HttpRequest request, byte @NotNull [] body, + ClientHttpRequestExecution execution) throws IOException { request.getHeaders().set(HttpHeaders.USER_AGENT, "Juick/2.x"); return execution.execute(request, body); } diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 7a95acb5..0ece7249 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -192,9 +192,9 @@ public class MessageUtils { // /12 msg = msg.replaceAll(replyNumberRegex, "$1/$2$3"); - // @username@jabber.org - // @username@jabber.org - msg = msg.replaceAll(jidRegex, "$1@$2$3"); + // @username@mastodon.social + // @username@mastodon.social + msg = msg.replaceAll(jidRegex, "$1@$2$3"); // @username // @username diff --git a/src/main/java/com/juick/www/controllers/Compat.java b/src/main/java/com/juick/www/controllers/Compat.java index ebf584dc..936a8e5c 100644 --- a/src/main/java/com/juick/www/controllers/Compat.java +++ b/src/main/java/com/juick/www/controllers/Compat.java @@ -17,17 +17,31 @@ package com.juick.www.controllers; +import com.juick.SignatureManager; +import com.juick.util.HttpNotFoundException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.view.RedirectView; +import javax.inject.Inject; + @Controller public class Compat { + @Inject + private SignatureManager signatureManager; @GetMapping("/share") public RedirectView share(@RequestParam String text, RedirectAttributes attributes) { attributes.addAttribute("body", text); return new RedirectView("/post"); } + @GetMapping("/mention") + public RedirectView mention(@RequestParam String username) { + var profile = signatureManager.discoverPerson(username); + if (profile.isPresent()) { + return new RedirectView(profile.get().getUrl()); + } + throw new HttpNotFoundException(); + } } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index a892bcf4..38eace7c 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -2768,4 +2768,10 @@ public class ServerTests { public void shareUrlShouldRedirectToPost() throws Exception { mockMvc.perform(get("/share?text=Hello\nWorld")).andExpect(redirectedUrl("/post?body=Hello%0AWorld")); } + @Test + public void mentionUrlsShouldRedirectToExternalUser() throws Exception { + mockMvc.perform(get("/mention?username=vt@juick.com")).andExpect( + redirectedUrl("https://juick.com/vt/") + ); + } } -- cgit v1.2.3