Skip to content

Commit

Permalink
add misskey url feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
uakihir0 committed Aug 15, 2020
1 parent d321b64 commit d50b84e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

public class MisskeyComment extends MiniBlogComment {

/** Requester host */
private String requesterHost;

/** ID for Paging */
private String pagingId;

Expand All @@ -38,9 +41,10 @@ public MisskeyComment(Service service) {

@Override
public String getWebUrl() {
MisskeyUser user = (MisskeyUser) getUser();
String host = user.getAccountIdentify().split("@")[2];
return "https://" + host + "/notes/" + getId().toString();
return "https://"
+ requesterHost
+ "/notes/"
+ getId().toString();
}

@Override
Expand Down Expand Up @@ -83,6 +87,14 @@ public String getIdForPaging() {
}

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

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

public void setPagingId(String pagingId) {
this.pagingId = pagingId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ public Pageable<Comment> getSearchTimeLine(String query, Paging paging) {
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public void postComment(CommentForm req) {
proceed(() -> {
Mastodon mastodon = auth.getAccessor();
Expand Down
53 changes: 52 additions & 1 deletion src/main/java/net/socialhub/service/misskey/MisskeyAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -208,6 +210,34 @@ public User getUser(Identify id) {
});
}

/**
* {@inheritDoc}
* https://misskey.io/@syuilo
* https://misskey.io/@[email protected]
*/
@Override
public User getUser(String url) {
return proceed(() -> {
Service service = getAccount().getService();
Pattern regex = Pattern.compile("https://(.+?)/@(.+)");
Matcher matcher = regex.matcher(url);

if (matcher.matches()) {
String host = matcher.group(1);
String identify = matcher.group(2);

if (identify.contains("@")) {
String format = ("@" + identify);
return getUser(new Identify(service, format));
} else {
String format = ("@" + identify + "@" + host);
return getUser(new Identify(service, format));
}
}
throw new SocialHubException("this url is not supported format.");
});
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -574,7 +604,6 @@ public Pageable<Comment> getSearchTimeLine(String query, Paging paging) {
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public void postComment(CommentForm req) {
if (req.isMessage()) {
postMessage(req);
Expand Down Expand Up @@ -661,6 +690,28 @@ public Comment getComment(Identify id) {
});
}

/**
* {@inheritDoc}
* Parse Misskey Post's url, like:
* https://misskey.io/notes/8axwbcxiff
*/
@Override
public Comment getComment(String url) {
return proceed(() -> {
Service service = getAccount().getService();
Pattern regex = Pattern.compile("https://(.+?)/notes/(.+)");
Matcher matcher = regex.matcher(url);

if (matcher.matches()) {
String id = matcher.group(2);
Identify identify = new Identify(service, id);
return getComment(identify);
}

throw new SocialHubException("this url is not supported format.");
});
}

/**
* {@inheritDoc}
*/
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/socialhub/service/misskey/MisskeyAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ public Account getAccountWithAccessToken(String accessToken) {

this.accessToken = accessToken;
Account account = new Account();

ServiceType type = ServiceType.Misskey;
Service service = new Service(type, account);
service.setApiHost(host);

account.setAction(new MisskeyAction(account, this));
account.setService(service);
return account;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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 @@ -208,9 +210,14 @@ public static Comment comment(
note.getEmojis(),
note.getMyReaction()));

// リクエストホストを記録
URL url = new URL(service.getApiHost());
model.setRequesterHost(url.getHost());

return model;

} catch (ParseException e) {
} catch (ParseException | MalformedURLException e) {

logger.error(e);
throw new IllegalStateException(e);
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/net/socialhub/apis/GetFromUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,25 @@ public void getCommentFromUrlMastodon() {
System.out.println(comment.getDisplayComment().getText().getDisplayText());
}
}

@Test
public void getUserFromUrlMisskey() {
Account account = SocialAuthUtil.getMisskeyAccount();
{
User user = account.action().getUser("https://misskey.io/@syuilo");
System.out.println(user.getName());
}
{
User user = account.action().getUser("https://misskey.io/@[email protected]");
System.out.println(user.getName());
}
}

@Test
public void getCommentFromUrlMisskey() {
Account account = SocialAuthUtil.getMisskeyAccount();
Comment comment = account.action().getComment(
"https://misskey.io/notes/8axwbcxiff");
System.out.println(comment.getDisplayComment().getText().getDisplayText());
}
}

0 comments on commit d50b84e

Please sign in to comment.