Сервер: уточнить правила имён каналов и поиск треда

This commit is contained in:
AidarKC
2026-07-25 21:16:15 +04:00
parent 8ddf592ffc
commit 1dddb5fb3c
7 changed files with 34 additions and 35 deletions
@@ -8,6 +8,10 @@ public final class ChannelNameRules {
private static final int MAX_DISPLAY_NAME_LENGTH = 32;
private static final Pattern DISPLAY_ALLOWED_PATTERN =
Pattern.compile("^[A-Za-z0-9_-]+$");
private static final Pattern PUBLIC_CHANNEL_ALLOWED_PATTERN =
Pattern.compile("^[A-Za-z0-9_-]+$");
private static final Pattern DIGITS_ONLY_PATTERN =
Pattern.compile("^[0-9]+$");
private ChannelNameRules() {}
@@ -34,6 +38,17 @@ public final class ChannelNameRules {
return normalized;
}
public static String requireValidPublicDisplayNameForCreate(String rawName) {
String normalized = requireValidDisplayNameForCreate(rawName);
if (!PUBLIC_CHANNEL_ALLOWED_PATTERN.matcher(normalized).matches()) {
throw new IllegalArgumentException("channelName contains unsupported characters");
}
if (DIGITS_ONLY_PATTERN.matcher(normalized).matches()) {
throw new IllegalArgumentException("channelName must not contain only digits");
}
return normalized;
}
public static String toCanonicalSlug(String rawName) {
String normalized = normalizeDisplayName(rawName);
if (normalized.isEmpty()) {
@@ -279,18 +279,19 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
ChannelNameStateEntry channelNameStateEntry = null;
Chat200CreateSeed chat200CreateSeed = null;
if (block.body instanceof CreateChannelBody createChannelBody) {
int channelTypeCode = Short.toUnsignedInt(createChannelBody.channelTypeCode);
int channelTypeVersion = Short.toUnsignedInt(createChannelBody.channelTypeVersion);
final String normalizedName;
final String slug;
try {
normalizedName = ChannelNameRules.requireValidDisplayNameForCreate(createChannelBody.channelName);
normalizedName = channelTypeCode == (CreateChannelBody.CHANNEL_TYPE_PERSONAL & 0xFFFF)
? ChannelNameRules.requireValidDisplayNameForCreate(createChannelBody.channelName)
: ChannelNameRules.requireValidPublicDisplayNameForCreate(createChannelBody.channelName);
slug = ChannelNameRules.toCanonicalSlug(normalizedName);
} catch (IllegalArgumentException badName) {
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_channel_name", serverLastNum, serverLastHashHex);
}
int channelTypeCode = Short.toUnsignedInt(createChannelBody.channelTypeCode);
int channelTypeVersion = Short.toUnsignedInt(createChannelBody.channelTypeVersion);
try {
if (channelNameStateDAO.existsByOwnerTypeAndSlug(blockchainName, channelTypeCode, slug)) {
return new AddBlockResult(409, "channel_name_already_exists", serverLastNum, serverLastHashHex);
@@ -84,11 +84,6 @@ public class Net_GetMessageThread_Handler implements JsonMessageHandler {
PostRow byNumber = findByNumber(c, blockchainName, blockNumber);
if (byNumber != null) return byNumber;
if (blockHash != null) {
PostRow byHash = findByHash(c, blockHash);
if (byHash != null) return byHash;
}
return null;
}
@@ -161,21 +156,6 @@ public class Net_GetMessageThread_Handler implements JsonMessageHandler {
}
}
private PostRow findByHash(Connection c, byte[] blockHash) throws Exception {
String sql = """
SELECT login,bch_name,block_number,block_hash,block_bytes,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
FROM blocks
WHERE block_hash=?
LIMIT 1
""";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setBytes(1, blockHash);
try (ResultSet rs = ps.executeQuery()) {
return rs.next() ? mapRow(rs) : null;
}
}
}
private byte[] parseOptionalHash(String hex) {
String value = String.valueOf(hex == null ? "" : hex).trim();
if (value.isEmpty()) return null;