SHA256
добаил автозаполнение тестовых пользователей
This commit is contained in:
+18
-10
@@ -3,6 +3,7 @@ package server.logic.ws_protocol.JSON;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
@@ -27,7 +28,7 @@ public final class ActiveConnectionsRegistry {
|
||||
// sessionId (String) -> ConnectionContext
|
||||
private final ConcurrentHashMap<String, ConnectionContext> bySessionId = new ConcurrentHashMap<>();
|
||||
|
||||
// login (String) -> множество ConnectionContext для этого пользователя
|
||||
// lowercase(login) -> множество ConnectionContext для этого пользователя
|
||||
private final ConcurrentHashMap<String, Set<ConnectionContext>> byLogin = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
@@ -50,11 +51,12 @@ public final class ActiveConnectionsRegistry {
|
||||
if (prev != null && prev != ctx) {
|
||||
String prevLogin = prev.getLogin();
|
||||
if (prevLogin != null && !prevLogin.isBlank()) {
|
||||
Set<ConnectionContext> prevSet = byLogin.get(prevLogin);
|
||||
String prevKey = toLoginKey(prevLogin);
|
||||
Set<ConnectionContext> prevSet = byLogin.get(prevKey);
|
||||
if (prevSet != null) {
|
||||
prevSet.remove(prev);
|
||||
if (prevSet.isEmpty()) {
|
||||
byLogin.remove(prevLogin);
|
||||
byLogin.remove(prevKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,7 +65,7 @@ public final class ActiveConnectionsRegistry {
|
||||
}
|
||||
|
||||
byLogin
|
||||
.computeIfAbsent(login, id -> new CopyOnWriteArraySet<>())
|
||||
.computeIfAbsent(toLoginKey(login), id -> new CopyOnWriteArraySet<>())
|
||||
.add(ctx);
|
||||
|
||||
log.debug("registered ctx (login={}, sessionId={})", login, sessionId);
|
||||
@@ -89,11 +91,12 @@ public final class ActiveConnectionsRegistry {
|
||||
}
|
||||
|
||||
if (login != null && !login.isBlank()) {
|
||||
Set<ConnectionContext> set = byLogin.get(login);
|
||||
String key = toLoginKey(login);
|
||||
Set<ConnectionContext> set = byLogin.get(key);
|
||||
if (set != null) {
|
||||
set.remove(ctx);
|
||||
if (set.isEmpty()) {
|
||||
byLogin.remove(login);
|
||||
byLogin.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,11 +115,12 @@ public final class ActiveConnectionsRegistry {
|
||||
|
||||
String login = ctx.getLogin();
|
||||
if (login != null && !login.isBlank()) {
|
||||
Set<ConnectionContext> set = byLogin.get(login);
|
||||
String key = toLoginKey(login);
|
||||
Set<ConnectionContext> set = byLogin.get(key);
|
||||
if (set != null) {
|
||||
set.remove(ctx);
|
||||
if (set.isEmpty()) {
|
||||
byLogin.remove(login);
|
||||
byLogin.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,7 +141,11 @@ public final class ActiveConnectionsRegistry {
|
||||
*/
|
||||
public Set<ConnectionContext> getByLogin(String login) {
|
||||
if (login == null || login.isBlank()) return Set.of();
|
||||
Set<ConnectionContext> set = byLogin.get(login);
|
||||
Set<ConnectionContext> set = byLogin.get(toLoginKey(login));
|
||||
return (set == null) ? Set.of() : set; // CopyOnWriteArraySet можно отдавать как есть
|
||||
}
|
||||
}
|
||||
|
||||
private static String toLoginKey(String login) {
|
||||
return login.trim().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-8
@@ -75,8 +75,8 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
|
||||
SolanaUserEntry userFromContext = ctx.getSolanaUser();
|
||||
String loginFromContext = userFromContext.getLogin();
|
||||
String login = req.getLogin();
|
||||
if (login == null || login.isBlank()) {
|
||||
String loginFromReq = req.getLogin();
|
||||
if (loginFromReq == null || loginFromReq.isBlank()) {
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.BAD_REQUEST,
|
||||
@@ -86,7 +86,8 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
closeConnectionAfterErrorResponse(ctx, 4001, "Auth failed: empty login");
|
||||
return err;
|
||||
}
|
||||
if (!login.equals(loginFromContext)) {
|
||||
loginFromReq = loginFromReq.trim();
|
||||
if (!loginFromReq.equalsIgnoreCase(loginFromContext)) {
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.BAD_REQUEST,
|
||||
@@ -99,7 +100,7 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
|
||||
SolanaUserEntry user;
|
||||
try {
|
||||
user = SolanaUsersDAO.getInstance().getByLogin(login);
|
||||
user = SolanaUsersDAO.getInstance().getByLogin(loginFromContext);
|
||||
} catch (SQLException e) {
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
req,
|
||||
@@ -121,7 +122,8 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (login == null || login.isBlank()) {
|
||||
String canonicalLogin = user.getLogin();
|
||||
if (canonicalLogin == null || canonicalLogin.isBlank()) {
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.SERVER_DATA_ERROR,
|
||||
@@ -273,7 +275,7 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
boolean sigOk;
|
||||
try {
|
||||
sigOk = verifyCreateSessionSignature(
|
||||
login,
|
||||
loginFromReq,
|
||||
sessionKey,
|
||||
storagePwd,
|
||||
authNonce,
|
||||
@@ -342,7 +344,7 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
try {
|
||||
activeSessionEntry = new ActiveSessionEntry(
|
||||
sessionId,
|
||||
login,
|
||||
canonicalLogin,
|
||||
sessionKey, // session_key (pubkey string as-is)
|
||||
storagePwd,
|
||||
now,
|
||||
@@ -358,7 +360,7 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
|
||||
dao.insert(activeSessionEntry);
|
||||
} catch (SQLException e) {
|
||||
log.error("Ошибка БД при создании новой сессии для login={}", login, e);
|
||||
log.error("Ошибка БД при создании новой сессии для login={}", canonicalLogin, e);
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.SERVER_DATA_ERROR,
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ public class Net_AddCloseFriend_Handler implements JsonMessageHandler {
|
||||
}
|
||||
|
||||
private String findPrimaryBlockchain(Connection c, String login) throws Exception {
|
||||
String sql = "SELECT blockchain_name FROM blockchain_state WHERE login=? ORDER BY blockchain_name LIMIT 1";
|
||||
String sql = "SELECT blockchain_name FROM blockchain_state WHERE login = ? COLLATE NOCASE ORDER BY blockchain_name LIMIT 1";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, login);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
|
||||
+21
-4
@@ -12,6 +12,8 @@ import shine.db.MsgSubType;
|
||||
import shine.db.dao.ConnectionsStateDAO;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.List;
|
||||
|
||||
public class Net_GetUserConnectionsGraph_Handler implements JsonMessageHandler {
|
||||
@@ -21,20 +23,35 @@ public class Net_GetUserConnectionsGraph_Handler implements JsonMessageHandler {
|
||||
if (ctx == null || !ctx.isAuthenticatedUser()) {
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.UNVERIFIED, "NOT_AUTHENTICATED", "Требуется авторизация");
|
||||
}
|
||||
String login = (req.getLogin() == null || req.getLogin().isBlank()) ? ctx.getLogin() : req.getLogin().trim();
|
||||
String requestedLogin = (req.getLogin() == null || req.getLogin().isBlank()) ? ctx.getLogin() : req.getLogin().trim();
|
||||
|
||||
try (Connection c = shine.db.SqliteDbController.getInstance().getConnection()) {
|
||||
List<String> out = ConnectionsStateDAO.getInstance().listOutgoingByRelTypeCanonical(c, login, MsgSubType.CONNECTION_FRIEND);
|
||||
List<String> in = ConnectionsStateDAO.getInstance().listIncomingByRelTypeCanonical(c, login, MsgSubType.CONNECTION_FRIEND);
|
||||
String canonicalLogin = findCanonicalLogin(c, requestedLogin);
|
||||
if (canonicalLogin == null) {
|
||||
return NetExceptionResponseFactory.error(req, 404, "USER_NOT_FOUND", "Пользователь не найден");
|
||||
}
|
||||
|
||||
List<String> out = ConnectionsStateDAO.getInstance().listOutgoingByRelTypeCanonical(c, canonicalLogin, MsgSubType.CONNECTION_FRIEND);
|
||||
List<String> in = ConnectionsStateDAO.getInstance().listIncomingByRelTypeCanonical(c, canonicalLogin, MsgSubType.CONNECTION_FRIEND);
|
||||
|
||||
Net_GetUserConnectionsGraph_Response resp = new Net_GetUserConnectionsGraph_Response();
|
||||
resp.setOp(req.getOp());
|
||||
resp.setRequestId(req.getRequestId());
|
||||
resp.setStatus(WireCodes.Status.OK);
|
||||
resp.setLogin(login);
|
||||
resp.setLogin(canonicalLogin);
|
||||
resp.setOutFriends(out);
|
||||
resp.setInFriends(in);
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
private String findCanonicalLogin(Connection c, String loginAnyCase) throws Exception {
|
||||
String sql = "SELECT login FROM solana_users WHERE login = ? COLLATE NOCASE LIMIT 1";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, loginAnyCase);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
return rs.next() ? rs.getString("login") : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -11,7 +11,9 @@ import server.logic.ws_protocol.JSON.handlers.userParams.entyties.Net_ListUserPa
|
||||
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
|
||||
import server.logic.ws_protocol.WireCodes;
|
||||
import shine.db.SqliteDbController;
|
||||
import shine.db.dao.SolanaUsersDAO;
|
||||
import shine.db.dao.UserParamsDAO;
|
||||
import shine.db.entities.SolanaUserEntry;
|
||||
import shine.db.entities.UserParamEntry;
|
||||
|
||||
import java.sql.Connection;
|
||||
@@ -61,7 +63,8 @@ public class Net_ListUserParams_Handler implements JsonMessageHandler {
|
||||
resp.setRequestId(req.getRequestId());
|
||||
resp.setStatus(WireCodes.Status.OK);
|
||||
|
||||
resp.setLogin(login);
|
||||
SolanaUserEntry user = SolanaUsersDAO.getInstance().getByLogin(login);
|
||||
resp.setLogin(user != null && user.getLogin() != null ? user.getLogin() : login);
|
||||
|
||||
List<Net_ListUserParams_Response.Item> items = new ArrayList<>();
|
||||
for (UserParamEntry e : entries) {
|
||||
|
||||
+9
-2
@@ -16,8 +16,10 @@ import server.logic.ws_protocol.JSON.utils.NetIdGenerator;
|
||||
import server.logic.ws_protocol.WireCodes;
|
||||
import shine.db.dao.DirectMessagesDAO;
|
||||
import shine.db.dao.PushTokensDAO;
|
||||
import shine.db.dao.SolanaUsersDAO;
|
||||
import shine.db.entities.DirectMessageEntry;
|
||||
import shine.db.entities.PushTokenEntry;
|
||||
import shine.db.entities.SolanaUserEntry;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -39,9 +41,15 @@ public class Net_SendDirectMessage_Handler implements JsonMessageHandler {
|
||||
}
|
||||
|
||||
String from = ctx.getLogin();
|
||||
String to = req.getToLogin().trim();
|
||||
String toRequest = req.getToLogin().trim();
|
||||
String text = req.getText().trim();
|
||||
|
||||
SolanaUserEntry targetUser = SolanaUsersDAO.getInstance().getByLogin(toRequest);
|
||||
if (targetUser == null) {
|
||||
return NetExceptionResponseFactory.error(req, 404, "USER_NOT_FOUND", "Пользователь не найден");
|
||||
}
|
||||
String to = targetUser.getLogin();
|
||||
|
||||
if (!canSend(from, to)) {
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.UNVERIFIED, "NO_PERMISSION", "Можно писать только контактам или тем, кто уже писал вам");
|
||||
}
|
||||
@@ -120,4 +128,3 @@ public class Net_SendDirectMessage_Handler implements JsonMessageHandler {
|
||||
return from != null && !from.isBlank() && to != null && !to.isBlank();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user