Skip to content

Commit

Permalink
fix mastodon url handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
uakihir0 committed Aug 15, 2020
1 parent c423b59 commit d321b64
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 27 deletions.
9 changes: 0 additions & 9 deletions src/main/java/net/socialhub/model/service/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class Service implements Serializable {

/** Use Only Mastodon */
private String apiHost;
private String streamApiHost;

/**
* Constructor
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
public class MastodonComment extends MiniBlogComment {

/** Requester host */
private String requesterHost;

/** Warning text (Mastodon only) */
private AttributedString spoilerText;

Expand All @@ -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
Expand All @@ -62,6 +65,14 @@ public List<Reaction> getReactions() {
}

// region // Getter&Setter
public String getRequesterHost() {
return requesterHost;
}

public void setRequesterHost(String requesterHost) {
this.requesterHost = requesterHost;
}

public AttributedString getSpoilerText() {
return spoilerText;
}
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/net/socialhub/service/mastodon/MastodonAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 10 additions & 3 deletions src/test/java/net/socialhub/apis/GetFromUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

0 comments on commit d321b64

Please sign in to comment.