SHA256
Поправил пуши для личных сообщений
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package shine.db.dao;
|
||||
|
||||
import shine.db.DbController;
|
||||
import shine.db.MsgSubType;
|
||||
import shine.db.entities.SignedMessageEntry;
|
||||
|
||||
@@ -17,6 +18,7 @@ import java.util.Map;
|
||||
|
||||
public final class DmDialogStateDAO {
|
||||
private static volatile DmDialogStateDAO instance;
|
||||
private final DbController db = DbController.getInstance();
|
||||
|
||||
private DmDialogStateDAO() {}
|
||||
|
||||
@@ -129,6 +131,26 @@ public final class DmDialogStateDAO {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Снимок счётчика после записи DM. Используется только сервером для push/WS, не отдаёт текст сообщения. */
|
||||
public NotificationState getNotificationState(String ownerLogin, String peerLogin) throws SQLException {
|
||||
try (Connection c = db.getConnection();
|
||||
PreparedStatement ps = c.prepareStatement("""
|
||||
SELECT unread_count, updated_at_ms
|
||||
FROM dm_dialog_state
|
||||
WHERE LOWER(owner_login) = LOWER(?) AND LOWER(peer_login) = LOWER(?)
|
||||
LIMIT 1
|
||||
""")) {
|
||||
ps.setString(1, normalize(ownerLogin));
|
||||
ps.setString(2, normalize(peerLogin));
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) return null;
|
||||
return new NotificationState(Math.max(0, rs.getInt("unread_count")), rs.getLong("updated_at_ms"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public record NotificationState(int unreadCount, long versionMs) {}
|
||||
|
||||
public void refreshFromEntry(Connection c, SignedMessageEntry entry) throws SQLException {
|
||||
if (entry == null) return;
|
||||
refreshConversationPair(c, entry.getFromLogin(), entry.getToLogin());
|
||||
@@ -274,7 +296,7 @@ public final class DmDialogStateDAO {
|
||||
last_message_time_ms = EXCLUDED.last_message_time_ms,
|
||||
unread_count = EXCLUDED.unread_count,
|
||||
last_read_receipt_time_ms = EXCLUDED.last_read_receipt_time_ms,
|
||||
updated_at_ms = EXCLUDED.updated_at_ms
|
||||
updated_at_ms = GREATEST(dm_dialog_state.updated_at_ms + 1, EXCLUDED.updated_at_ms)
|
||||
""")) {
|
||||
ps.setString(1, summary.ownerLogin());
|
||||
ps.setString(2, summary.peerLogin());
|
||||
|
||||
+38
-24
@@ -9,6 +9,7 @@ import server.logic.ws_protocol.JSON.ConnectionContext;
|
||||
import server.logic.ws_protocol.JSON.push.WebPushSender;
|
||||
import server.logic.ws_protocol.JSON.push.WsEventSender;
|
||||
import shine.db.dao.ActiveSessionsDAO;
|
||||
import shine.db.dao.DmDialogStateDAO;
|
||||
import shine.db.dao.SignedMessagesDAO;
|
||||
import shine.db.entities.ActiveSessionEntry;
|
||||
import shine.db.entities.SignedMessageEntry;
|
||||
@@ -49,6 +50,9 @@ public final class SignedMessagesRealtime {
|
||||
DeliveryCounters counters = new DeliveryCounters();
|
||||
long now = System.currentTimeMillis();
|
||||
for (String targetLogin : targetLoginsForMessage(message)) {
|
||||
boolean incomingNotification = shouldPushNewIncomingMessage(targetLogin, message, block);
|
||||
DmDialogStateDAO.NotificationState notificationState = incomingNotification
|
||||
? loadNotificationState(targetLogin, message.getFromLogin()) : null;
|
||||
List<ActiveSessionEntry> sessions = ActiveSessionsDAO.getInstance().getByLogin(targetLogin);
|
||||
List<String> sessionIdsToTrack = new ArrayList<>();
|
||||
for (ActiveSessionEntry s : sessions) {
|
||||
@@ -64,13 +68,13 @@ public final class SignedMessagesRealtime {
|
||||
if (excludeSessionId != null && excludeSessionId.equals(sessionId)) {
|
||||
continue;
|
||||
}
|
||||
boolean deliveredOnline = sendEventToSessionIfOnline(sessionId, targetLogin, message, false);
|
||||
boolean deliveredOnline = sendEventToSessionIfOnline(sessionId, targetLogin, message, false, notificationState);
|
||||
if (deliveredOnline) {
|
||||
counters.wsDelivered++;
|
||||
continue;
|
||||
}
|
||||
if (shouldPushNewIncomingMessage(targetLogin, message, block)) {
|
||||
boolean pushed = pushNewMessageNotification(s, message);
|
||||
if (incomingNotification) {
|
||||
boolean pushed = pushNewMessageNotification(s, message, notificationState);
|
||||
if (pushed) counters.pushDelivered++;
|
||||
}
|
||||
}
|
||||
@@ -100,7 +104,7 @@ public final class SignedMessagesRealtime {
|
||||
List<SignedMessageEntry> pending = SignedMessagesDAO.getInstance()
|
||||
.listPendingForSession(login, sessionId);
|
||||
for (SignedMessageEntry e : pending) {
|
||||
sendEventToSessionIfOnline(sessionId, login, e, true);
|
||||
sendEventToSessionIfOnline(sessionId, login, e, true, null);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to dispatch pending messages for sessionId={}", sessionId, e);
|
||||
@@ -111,7 +115,8 @@ public final class SignedMessagesRealtime {
|
||||
String sessionId,
|
||||
String actualTargetLogin,
|
||||
SignedMessageEntry message,
|
||||
boolean backlog
|
||||
boolean backlog,
|
||||
DmDialogStateDAO.NotificationState notificationState
|
||||
) {
|
||||
ConnectionContext targetCtx = ActiveConnectionsRegistry.getInstance().getBySessionId(sessionId);
|
||||
if (targetCtx == null) return false;
|
||||
@@ -128,6 +133,10 @@ public final class SignedMessagesRealtime {
|
||||
payload.put("nonce", message.getNonce());
|
||||
payload.put("blobB64", blobB64);
|
||||
payload.put("backlog", backlog);
|
||||
if (message.getMessageType() == SignedMessageBlock.TYPE_INCOMING_TEXT && notificationState != null) {
|
||||
payload.put("unreadCount", notificationState.unreadCount());
|
||||
payload.put("notificationVersionMs", notificationState.versionMs());
|
||||
}
|
||||
if (message.getReceiptRefBaseKey() != null) {
|
||||
payload.put("receiptRefBaseKey", message.getReceiptRefBaseKey());
|
||||
}
|
||||
@@ -141,21 +150,41 @@ public final class SignedMessagesRealtime {
|
||||
return block.revisionTimeMs == 0;
|
||||
}
|
||||
|
||||
private static boolean pushNewMessageNotification(ActiveSessionEntry session, SignedMessageEntry message) {
|
||||
private static DmDialogStateDAO.NotificationState loadNotificationState(String ownerLogin, String peerLogin) {
|
||||
try {
|
||||
return DmDialogStateDAO.getInstance().getNotificationState(ownerLogin, peerLogin);
|
||||
} catch (Exception e) {
|
||||
log.warn("Не удалось получить счётчик непрочитанных DM для {} / {}", ownerLogin, peerLogin, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean pushNewMessageNotification(
|
||||
ActiveSessionEntry session, SignedMessageEntry message, DmDialogStateDAO.NotificationState notificationState
|
||||
) {
|
||||
try {
|
||||
if (session == null) return false;
|
||||
if (isBlank(session.getPushEndpoint()) || isBlank(session.getPushP256dhKey()) || isBlank(session.getPushAuthKey())) {
|
||||
return false;
|
||||
}
|
||||
String text = "Новое сообщение от " + message.getFromLogin();
|
||||
String payload = "{\"kind\":\"new_message\",\"fromLogin\":\"" + jsonEscape(message.getFromLogin()) + "\",\"text\":\"" + jsonEscape(text) + "\"}";
|
||||
ObjectNode payload = MAPPER.createObjectNode();
|
||||
payload.put("kind", "new_message");
|
||||
payload.put("toLogin", session.getLogin());
|
||||
payload.put("fromLogin", message.getFromLogin());
|
||||
payload.put("messageKey", message.getMessageKey());
|
||||
payload.put("text", "Новое сообщение от " + message.getFromLogin());
|
||||
if (notificationState != null) {
|
||||
payload.put("unreadCount", notificationState.unreadCount());
|
||||
payload.put("notificationVersionMs", notificationState.versionMs());
|
||||
}
|
||||
return WebPushSender.sendBase64Payload(
|
||||
session.getPushEndpoint(),
|
||||
session.getPushP256dhKey(),
|
||||
session.getPushAuthKey(),
|
||||
payload
|
||||
payload.toString()
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.warn("Не удалось подготовить Web Push для DM {}", message.getMessageKey(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -182,21 +211,6 @@ public final class SignedMessagesRealtime {
|
||||
return s == null ? "" : s.trim().toLowerCase();
|
||||
}
|
||||
|
||||
private static String jsonEscape(String s) {
|
||||
if (s == null) return "";
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
if (c == '\\') out.append("\\\\");
|
||||
else if (c == '"') out.append("\\\"");
|
||||
else if (c == '\n') out.append("\\n");
|
||||
else if (c == '\r') out.append("\\r");
|
||||
else if (c == '\t') out.append("\\t");
|
||||
else out.append(c);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private static boolean isBlank(String s) {
|
||||
return s == null || s.isBlank();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user