diff --git a/src/main/java/net/socialhub/model/service/Service.java b/src/main/java/net/socialhub/model/service/Service.java index fbd5e57..36d956a 100644 --- a/src/main/java/net/socialhub/model/service/Service.java +++ b/src/main/java/net/socialhub/model/service/Service.java @@ -18,7 +18,6 @@ public class Service implements Serializable { /** Use Only Mastodon */ private String apiHost; - private String streamApiHost; /** * Constructor @@ -92,13 +91,5 @@ public String getApiHost() { public void setApiHost(String apiHost) { this.apiHost = apiHost; } - - public String getStreamApiHost() { - return streamApiHost; - } - - public void setStreamApiHost(String streamApiHost) { - this.streamApiHost = streamApiHost; - } //endregion } diff --git a/src/main/java/net/socialhub/model/service/addition/mastodon/MastodonComment.java b/src/main/java/net/socialhub/model/service/addition/mastodon/MastodonComment.java index 161884d..84fe206 100644 --- a/src/main/java/net/socialhub/model/service/addition/mastodon/MastodonComment.java +++ b/src/main/java/net/socialhub/model/service/addition/mastodon/MastodonComment.java @@ -16,6 +16,9 @@ */ public class MastodonComment extends MiniBlogComment { + /** Requester host */ + private String requesterHost; + /** Warning text (Mastodon only) */ private AttributedString spoilerText; @@ -34,10 +37,10 @@ public MastodonComment(Service service) { @Override public String getWebUrl() { - MastodonUser user = (MastodonUser) getUser(); - String host = user.getAccountIdentify().split("@")[2]; - String identify = user.getAccountIdentify().split("@")[1]; - return "https://" + host + "/@" + identify + "/" + getId().toString(); + return "https://" + + requesterHost + + "/web/statuses/" + + getId().toString(); } @Override @@ -62,6 +65,14 @@ public List getReactions() { } // region // Getter&Setter + public String getRequesterHost() { + return requesterHost; + } + + public void setRequesterHost(String requesterHost) { + this.requesterHost = requesterHost; + } + public AttributedString getSpoilerText() { return spoilerText; } diff --git a/src/main/java/net/socialhub/service/mastodon/MastodonAction.java b/src/main/java/net/socialhub/service/mastodon/MastodonAction.java index 6acf4fe..8f21325 100644 --- a/src/main/java/net/socialhub/service/mastodon/MastodonAction.java +++ b/src/main/java/net/socialhub/service/mastodon/MastodonAction.java @@ -146,6 +146,7 @@ public User getUser(Identify id) { /** * {@inheritDoc} + * Parse Mastodon user's url, like: * https://mastodon.social/@uakihir0 * https://mastodon.social/web/accounts/1223371 */ @@ -560,19 +561,33 @@ public Comment getComment(Identify id) { /** * {@inheritDoc} + * Parse Mastodon Toot's url, like: + * https://mastodon.social/@uakihir0/104681506368424218 * https://mastodon.social/web/statuses/104681506368424218 */ @Override public Comment getComment(String url) { return proceed(() -> { Service service = getAccount().getService(); - Pattern regex = Pattern.compile("https://(.+?)/web/statuses/(.+)"); - Matcher matcher = regex.matcher(url); + { + Pattern regex = Pattern.compile("https://(.+?)/@(.+?)/(.+)"); + Matcher matcher = regex.matcher(url); - if (matcher.matches()) { - Long id = Long.parseLong(matcher.group(2)); - Identify identify = new Identify(service, id); - return getComment(identify); + if (matcher.matches()) { + Long id = Long.parseLong(matcher.group(3)); + Identify identify = new Identify(service, id); + return getComment(identify); + } + } + { + Pattern regex = Pattern.compile("https://(.+?)/web/statuses/(.+)"); + Matcher matcher = regex.matcher(url); + + if (matcher.matches()) { + Long id = Long.parseLong(matcher.group(2)); + Identify identify = new Identify(service, id); + return getComment(identify); + } } throw new SocialHubException("this url is not supported format."); }); diff --git a/src/main/java/net/socialhub/service/mastodon/MastodonAuth.java b/src/main/java/net/socialhub/service/mastodon/MastodonAuth.java index 9268d6d..a812ff8 100644 --- a/src/main/java/net/socialhub/service/mastodon/MastodonAuth.java +++ b/src/main/java/net/socialhub/service/mastodon/MastodonAuth.java @@ -49,8 +49,11 @@ public Account getAccountWithAccessToken(String accessToken) { this.accessToken = accessToken; Account account = new Account(); + ServiceType type = ServiceType.Mastodon; Service service = new Service(type, account); + service.setApiHost(host); + account.setAction(new MastodonAction(account, this)); account.setService(service); return account; diff --git a/src/main/java/net/socialhub/service/mastodon/MastodonMapper.java b/src/main/java/net/socialhub/service/mastodon/MastodonMapper.java index d587e65..7862472 100644 --- a/src/main/java/net/socialhub/service/mastodon/MastodonMapper.java +++ b/src/main/java/net/socialhub/service/mastodon/MastodonMapper.java @@ -37,6 +37,8 @@ import net.socialhub.model.service.support.PollOption; import net.socialhub.model.service.support.ReactionCandidate; +import java.net.MalformedURLException; +import java.net.URL; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -196,10 +198,15 @@ public static Comment comment( // 投票の設定 model.setPoll(poll(status.getPoll(), service)); + + // リクエストホストを記録 + URL url = new URL(service.getApiHost()); + model.setRequesterHost(url.getHost()); } return model; - } catch (ParseException e) { + } catch (MalformedURLException | ParseException e) { + logger.error(e); throw new IllegalStateException(e); } diff --git a/src/main/java/net/socialhub/service/twitter/TwitterAction.java b/src/main/java/net/socialhub/service/twitter/TwitterAction.java index 0910bdd..37cb3ed 100644 --- a/src/main/java/net/socialhub/service/twitter/TwitterAction.java +++ b/src/main/java/net/socialhub/service/twitter/TwitterAction.java @@ -190,8 +190,8 @@ public User getUser(Identify id) { /** * {@inheritDoc} - * Parse Twitter user's url, - * like "https://twitter.com/uakihir0". + * Parse Twitter user's url, like: + * https://twitter.com/uakihir0 */ @Override public User getUser(String url) { @@ -650,8 +650,8 @@ public Comment getComment(Identify id) { /** * {@inheritDoc} - * Parse Twitter tweet's url, - * like "https://twitter.com/xxxx/status/[0-9]+". + * Parse Twitter tweet's url, like: + * https://twitter.com/xxxx/status/[0-9]+ */ @Override public Comment getComment(String url) { diff --git a/src/test/java/net/socialhub/apis/GetFromUrlTest.java b/src/test/java/net/socialhub/apis/GetFromUrlTest.java index 4c2856f..6aa6917 100644 --- a/src/test/java/net/socialhub/apis/GetFromUrlTest.java +++ b/src/test/java/net/socialhub/apis/GetFromUrlTest.java @@ -40,8 +40,15 @@ public void getUserFromUrlMastodon() { @Test public void getCommentFromUrlMastodon() { Account account = SocialAuthUtil.getMastodonAccount(); - Comment comment = account.action().getComment( - "https://mastodon.social/web/statuses/104681506368424218"); - System.out.println(comment.getDisplayComment().getText().getDisplayText()); + { + Comment comment = account.action().getComment( + "https://mastodon.social/@uakihir0/104681506368424218"); + System.out.println(comment.getDisplayComment().getText().getDisplayText()); + } + { + Comment comment = account.action().getComment( + "https://mastodon.social/web/statuses/104681506368424218"); + System.out.println(comment.getDisplayComment().getText().getDisplayText()); + } } }