一堆bug
This commit is contained in:
parent
e4c91adac0
commit
425210963f
2 changed files with 41 additions and 20 deletions
|
@ -7,8 +7,8 @@ import java.util.*;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
public class MarkdownString {
|
public class MarkdownString {
|
||||||
|
|
||||||
private MarkdownString(){
|
private MarkdownString() {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,6 +82,15 @@ public class MarkdownString {
|
||||||
|
|
||||||
|
|
||||||
private static class StringBlock {
|
private static class StringBlock {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "StringBlock{" +
|
||||||
|
"text='" + text + '\'' +
|
||||||
|
", entities=" + entities +
|
||||||
|
", offset=" + offset +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
private final String text;
|
private final String text;
|
||||||
private Map<MessageEntity.Type, MessageEntity> entities;
|
private Map<MessageEntity.Type, MessageEntity> entities;
|
||||||
private final int offset;
|
private final int offset;
|
||||||
|
@ -94,24 +103,29 @@ public class MarkdownString {
|
||||||
|
|
||||||
private String getMarkdownTextWithoutLink() { // todo: 压缩 *text1**_text2_* 为 *text1_text2_*
|
private String getMarkdownTextWithoutLink() { // todo: 压缩 *text1**_text2_* 为 *text1_text2_*
|
||||||
// 链接类不能和链接类套娃
|
// 链接类不能和链接类套娃
|
||||||
assert !((entities.containsKey(MessageEntity.Type.text_link)) && (entities.containsKey(MessageEntity.Type.text_mention)));
|
assert (!entities.containsKey(MessageEntity.Type.text_link)) || (!entities.containsKey(MessageEntity.Type.text_mention));
|
||||||
// 等宽(pre 和 code)完全禁止套娃
|
// 等宽(pre 和 code)完全禁止套娃
|
||||||
assert !((entities.containsKey(MessageEntity.Type.pre) || entities.containsKey(MessageEntity.Type.code)) && (entities.size() > 1));
|
assert (!entities.containsKey(MessageEntity.Type.pre) && !entities.containsKey(MessageEntity.Type.code)) || (entities.size() <= 1);
|
||||||
|
|
||||||
StringBuilder s = new StringBuilder(escapeStr(text));
|
StringBuilder s = new StringBuilder(escapeStr(text));
|
||||||
for (var entry : entities.entrySet()) {
|
for (var entry : entities.entrySet()) {
|
||||||
s = formats.get(entry.getKey()).apply(s, entry.getValue()); // 无用赋值...理论上。
|
BiFunction<StringBuilder, MessageEntity, StringBuilder> repeater = (sb, e) -> sb;
|
||||||
|
s = formats.getOrDefault(entry.getKey(), repeater).apply(s, entry.getValue()); // 无用赋值...理论上。
|
||||||
}
|
}
|
||||||
return s.toString();
|
return s.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String markdownString(Message message) {
|
public static String markdownString(Message message) {
|
||||||
|
if (message.entities() == null || message.entities().length == 0) return message.text();
|
||||||
|
|
||||||
List<StringBlock> stringBlocks = createStringBlocks(message);
|
List<StringBlock> stringBlocks = createStringBlocks(message);
|
||||||
|
|
||||||
|
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
for (int i = 0; i < stringBlocks.size(); i++) {
|
for (int i = 0; i < stringBlocks.size(); i++) {
|
||||||
|
|
||||||
|
|
||||||
var stringBlock = stringBlocks.get(i);
|
var stringBlock = stringBlocks.get(i);
|
||||||
var entities = stringBlock.entities;
|
var entities = stringBlock.entities;
|
||||||
String withoutLink = stringBlock.getMarkdownTextWithoutLink();
|
String withoutLink = stringBlock.getMarkdownTextWithoutLink();
|
||||||
|
@ -131,7 +145,9 @@ public class MarkdownString {
|
||||||
if (stringBlock.text.length() + stringBlock.offset == end) {
|
if (stringBlock.text.length() + stringBlock.offset == end) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
assert (stringBlock.text.length() + stringBlock.offset < end);
|
if (stringBlock.text.length() + stringBlock.offset > end) {
|
||||||
|
throw new IllegalStateException("奶冰可爱捏");
|
||||||
|
}
|
||||||
i++;
|
i++;
|
||||||
stringBlock = stringBlocks.get(i);
|
stringBlock = stringBlocks.get(i);
|
||||||
}
|
}
|
||||||
|
@ -179,6 +195,9 @@ public class MarkdownString {
|
||||||
|
|
||||||
// 给文本重新赋格式(MessageEntity)
|
// 给文本重新赋格式(MessageEntity)
|
||||||
for (MessageEntity entity : message.entities()) {
|
for (MessageEntity entity : message.entities()) {
|
||||||
|
if (!formats.containsKey(entity.type())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
for (StringBlock stringBlock : stringBlocks) {
|
for (StringBlock stringBlock : stringBlocks) {
|
||||||
int blockStart = stringBlock.offset;
|
int blockStart = stringBlock.offset;
|
||||||
int blockEnd = blockStart + stringBlock.text.length();
|
int blockEnd = blockStart + stringBlock.text.length();
|
||||||
|
@ -187,10 +206,10 @@ public class MarkdownString {
|
||||||
int entityEnd = entityStart + entity.length();
|
int entityEnd = entityStart + entity.length();
|
||||||
|
|
||||||
assert (blockStart < blockEnd) && (entityStart < entityEnd);
|
assert (blockStart < blockEnd) && (entityStart < entityEnd);
|
||||||
if (blockStart > entityEnd || blockEnd < entityStart) {
|
if (blockStart >= entityEnd || blockEnd <= entityStart) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
assert (blockStart >= entityStart) && (blockEnd <= entityEnd);
|
assert (blockStart >= entityStart) && (blockEnd <= entityEnd) : String.format("%s,%s,%s,%s", blockStart, blockEnd, entityStart, entityEnd);
|
||||||
MessageEntity old = stringBlock.entities.put(entity.type(), entity);
|
MessageEntity old = stringBlock.entities.put(entity.type(), entity);
|
||||||
assert (old == null);
|
assert (old == null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ public class TGBridge {
|
||||||
|
|
||||||
private long backoffSec = 1L;
|
private long backoffSec = 1L;
|
||||||
|
|
||||||
private static final String DIVIDER = "----------\n";
|
private static final String DIVIDER = "————————\n";
|
||||||
/**
|
/**
|
||||||
* markdown escaped
|
* markdown escaped
|
||||||
*/
|
*/
|
||||||
|
@ -114,7 +114,7 @@ public class TGBridge {
|
||||||
String command = s[0];
|
String command = s[0];
|
||||||
@Nullable String arg = s.length == 2 ? s[1] : null;
|
@Nullable String arg = s.length == 2 ? s[1] : null;
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "/list" -> outbound(genOnlineStatus(), ParseMode.Markdown);
|
case "/list" -> outbound(genOnlineStatus(), ParseMode.MarkdownV2);
|
||||||
case "/setpin" -> {
|
case "/setpin" -> {
|
||||||
Message replyTo = message.replyToMessage();
|
Message replyTo = message.replyToMessage();
|
||||||
if (arg == null || arg.length() == 0) {
|
if (arg == null || arg.length() == 0) {
|
||||||
|
@ -143,7 +143,7 @@ public class TGBridge {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
outbound("old pinned note: \n" + pinNote, ParseMode.Markdown);
|
outbound("old pinned note: \n" + pinNote, ParseMode.MarkdownV2);
|
||||||
|
|
||||||
pinNote = markdownString.substring("/setpin ".length());
|
pinNote = markdownString.substring("/setpin ".length());
|
||||||
if (replyTo != null) {
|
if (replyTo != null) {
|
||||||
|
@ -159,7 +159,7 @@ public class TGBridge {
|
||||||
int messageId = sendResponse.message().messageId();
|
int messageId = sendResponse.message().messageId();
|
||||||
ONLINE_STATUS_MESSAGE_ID = messageId > 0 ? messageId : ONLINE_STATUS_MESSAGE_ID;
|
ONLINE_STATUS_MESSAGE_ID = messageId > 0 ? messageId : ONLINE_STATUS_MESSAGE_ID;
|
||||||
}
|
}
|
||||||
}, ParseMode.Markdown
|
}, ParseMode.MarkdownV2
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -230,7 +230,7 @@ public class TGBridge {
|
||||||
*/
|
*/
|
||||||
private String genOnlineStatus() {
|
private String genOnlineStatus() {
|
||||||
ArrayList<String> out = new ArrayList<>();
|
ArrayList<String> out = new ArrayList<>();
|
||||||
String fmt = server.getAllPlayers().size() > 1 ? "%d players are currently connected to the proxy." : "%d player is currently connected to the proxy.";
|
String fmt = server.getAllPlayers().size() > 1 ? "%d players are currently connected to the proxy\\." : "%d player is currently connected to the proxy\\.";
|
||||||
out.add(String.format(fmt, server.getAllPlayers().size()));
|
out.add(String.format(fmt, server.getAllPlayers().size()));
|
||||||
List<RegisteredServer> registeredServers = new ArrayList<>(server.getAllServers());
|
List<RegisteredServer> registeredServers = new ArrayList<>(server.getAllServers());
|
||||||
for (RegisteredServer registeredServer : registeredServers) {
|
for (RegisteredServer registeredServer : registeredServers) {
|
||||||
|
@ -241,14 +241,15 @@ public class TGBridge {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!onServer.isEmpty()) {
|
if (!onServer.isEmpty()) {
|
||||||
out.add(String.format("[%s] (%d): %s",
|
out.add(
|
||||||
registeredServer.getServerInfo().getName(),
|
String.format("\\[%s\\] \\(%d\\): %s",
|
||||||
onServer.size(),
|
"`" + MarkdownString.escapeStr(registeredServer.getServerInfo().getName()) + "`",
|
||||||
onServer.stream().map(Player::getUsername).collect(Collectors.joining(", ")))
|
onServer.size(),
|
||||||
|
onServer.stream().map(player -> "`" + MarkdownString.escapeStr(player.getUsername()) + "`").collect(Collectors.joining(", ")))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return MarkdownString.escapeStr(String.join("\n", out));
|
return String.join("\n", out);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void tgInbound(User user, String content) {
|
protected void tgInbound(User user, String content) {
|
||||||
|
@ -285,7 +286,8 @@ public class TGBridge {
|
||||||
|
|
||||||
SendMessage sendMessage = new SendMessage(CHAT_ID, content);
|
SendMessage sendMessage = new SendMessage(CHAT_ID, content);
|
||||||
if (parseMode != null) {
|
if (parseMode != null) {
|
||||||
sendMessage.parseMode(parseMode);
|
boolean a = sendMessage == sendMessage.parseMode(parseMode);
|
||||||
|
assert a;
|
||||||
}
|
}
|
||||||
bot.execute(sendMessage, new Callback<SendMessage, SendResponse>() {
|
bot.execute(sendMessage, new Callback<SendMessage, SendResponse>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -415,7 +417,7 @@ public class TGBridge {
|
||||||
}
|
}
|
||||||
BaseResponse response;
|
BaseResponse response;
|
||||||
try {
|
try {
|
||||||
response = bot.execute(new EditMessageText(CHAT_ID, ONLINE_STATUS_MESSAGE_ID, markdownText).parseMode(ParseMode.Markdown));
|
response = bot.execute(new EditMessageText(CHAT_ID, ONLINE_STATUS_MESSAGE_ID, markdownText).parseMode(ParseMode.MarkdownV2));
|
||||||
} catch (RuntimeException e) {return false;}
|
} catch (RuntimeException e) {return false;}
|
||||||
return response != null && response.isOk();
|
return response != null && response.isOk();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue