| 1 |
6a3fcb6d
|
みぞ@CrazyBeatCoder
|
package com.mizo0203.timeline.talker;
|
| 2 |
|
|
|
| 3 |
|
|
import com.mizo0203.timeline.talker.util.DisplayNameUtil;
|
| 4 |
|
|
import com.mizo0203.timeline.talker.util.HTMLParser;
|
| 5 |
|
|
import com.mizo0203.timeline.talker.util.UrlUtil;
|
| 6 |
|
|
import com.sys1yagi.mastodon4j.MastodonClient;
|
| 7 |
|
|
import com.sys1yagi.mastodon4j.api.Handler;
|
| 8 |
|
|
import com.sys1yagi.mastodon4j.api.entity.Account;
|
| 9 |
|
|
import com.sys1yagi.mastodon4j.api.entity.Notification;
|
| 10 |
|
|
import com.sys1yagi.mastodon4j.api.entity.Status;
|
| 11 |
|
|
import com.sys1yagi.mastodon4j.api.method.Streaming;
|
| 12 |
|
|
import org.jetbrains.annotations.NotNull;
|
| 13 |
|
|
|
| 14 |
|
|
import java.io.IOException;
|
| 15 |
|
|
import java.nio.charset.StandardCharsets;
|
| 16 |
|
|
|
| 17 |
|
|
public class MastodonTimelineTalker implements TimelineTalker {
|
| 18 |
|
|
|
| 19 |
|
|
@NotNull private final Streaming mStreaming;
|
| 20 |
|
|
@NotNull private final OnStatusEvent mOnStatusEvent;
|
| 21 |
|
|
|
| 22 |
|
|
/* package */ MastodonTimelineTalker(MastodonClient client, Talker talker) {
|
| 23 |
|
|
mStreaming = new Streaming(client);
|
| 24 |
|
|
mOnStatusEvent = new OnStatusEvent(talker);
|
| 25 |
|
|
}
|
| 26 |
|
|
|
| 27 |
|
|
@Override
|
| 28 |
|
|
public void start() {
|
| 29 |
|
|
|
| 30 |
|
|
try {
|
| 31 |
|
|
mStreaming.user(mOnStatusEvent);
|
| 32 |
|
|
} catch (Exception e) {
|
| 33 |
|
|
e.printStackTrace();
|
| 34 |
|
|
}
|
| 35 |
|
|
}
|
| 36 |
|
|
|
| 37 |
|
|
private static class OnStatusEvent implements Handler {
|
| 38 |
|
|
|
| 39 |
|
|
private final Talker mTalker;
|
| 40 |
|
|
|
| 41 |
|
|
private OnStatusEvent(Talker talker) {
|
| 42 |
|
|
mTalker = talker;
|
| 43 |
|
|
}
|
| 44 |
|
|
|
| 45 |
|
|
@Override
|
| 46 |
|
|
public void onStatus(@NotNull Status status) {
|
| 47 |
|
|
|
| 48 |
|
|
try {
|
| 49 |
|
|
final StringBuffer buffer = new StringBuffer();
|
| 50 |
|
|
final Status reblogStatus = status.getReblog();
|
| 51 |
|
|
|
| 52 |
|
|
String displayName = "誰か";
|
| 53 |
|
|
if (status.getAccount() != null) {
|
| 54 |
|
|
displayName = status.getAccount().getDisplayName();
|
| 55 |
|
|
}
|
| 56 |
|
|
if (reblogStatus != null) {
|
| 57 |
|
|
String reblogDisplayName = "誰か";
|
| 58 |
|
|
if (status.getAccount() != null) {
|
| 59 |
|
|
displayName = status.getAccount().getDisplayName();
|
| 60 |
|
|
}
|
| 61 |
|
|
buffer.append(DisplayNameUtil.removeContext(displayName)).append("さんがブースト。");
|
| 62 |
|
|
buffer.append(DisplayNameUtil.removeContext(reblogDisplayName)).append("さんから、");
|
| 63 |
|
|
buffer.append(
|
| 64 |
|
|
new HTMLParser().parse(reblogStatus.getContent(), StandardCharsets.UTF_8, true));
|
| 65 |
|
|
} else {
|
| 66 |
|
|
buffer.append(DisplayNameUtil.removeContext(displayName)).append("さんから、");
|
| 67 |
|
|
buffer.append(new HTMLParser().parse(status.getContent(), StandardCharsets.UTF_8, true));
|
| 68 |
|
|
}
|
| 69 |
|
|
|
| 70 |
|
|
final String talkText = UrlUtil.convURLEmpty(buffer).replaceAll("\n", "。");
|
| 71 |
|
|
mTalker.talkAlternatelyAsync(talkText);
|
| 72 |
|
|
} catch (IOException e) {
|
| 73 |
|
|
e.printStackTrace();
|
| 74 |
|
|
}
|
| 75 |
|
|
}
|
| 76 |
|
|
|
| 77 |
|
|
@Override
|
| 78 |
|
|
public void onNotification(@NotNull Notification notification) {
|
| 79 |
|
|
final StringBuilder buffer = new StringBuilder();
|
| 80 |
|
|
final Account account = notification.getAccount();
|
| 81 |
|
|
|
| 82 |
|
|
if (account == null) {
|
| 83 |
|
|
return;
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
switch (notification.getType()) {
|
| 87 |
|
|
case "mention":
|
| 88 |
|
|
buffer
|
| 89 |
|
|
.append(DisplayNameUtil.removeContext(account.getDisplayName()))
|
| 90 |
|
|
.append("さんがあなたをメンションしました。");
|
| 91 |
|
|
break;
|
| 92 |
|
|
case "reblog":
|
| 93 |
|
|
buffer
|
| 94 |
|
|
.append(DisplayNameUtil.removeContext(account.getDisplayName()))
|
| 95 |
|
|
.append("さんがあなたのトゥートをブーストしました。");
|
| 96 |
|
|
break;
|
| 97 |
|
|
case "favourite":
|
| 98 |
|
|
buffer
|
| 99 |
|
|
.append(DisplayNameUtil.removeContext(account.getDisplayName()))
|
| 100 |
|
|
.append("さんがあなたのトゥートをお気に入りに登録しました。");
|
| 101 |
|
|
break;
|
| 102 |
|
|
case "follow":
|
| 103 |
|
|
buffer
|
| 104 |
|
|
.append(DisplayNameUtil.removeContext(account.getDisplayName()))
|
| 105 |
|
|
.append("さんにフォローされました。");
|
| 106 |
|
|
break;
|
| 107 |
|
|
default:
|
| 108 |
|
|
return;
|
| 109 |
|
|
}
|
| 110 |
|
|
|
| 111 |
|
|
final String talkText = buffer.toString();
|
| 112 |
|
|
mTalker.talkAlternatelyAsync(talkText);
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
@Override
|
| 116 |
|
|
public void onDelete(long id) {
|
| 117 |
|
|
/* no op */
|
| 118 |
|
|
}
|
| 119 |
|
|
}
|
| 120 |
|
|
}
|