Display new notification count

This commit is contained in:
AidarKC
2026-09-02 19:20:04 +04:00
parent 0c5089fa79
commit aff601f61a
22 changed files with 475 additions and 260 deletions
@@ -36,6 +36,7 @@ public final class DatabaseInitializer {
public static final int SCHEMA_VERSION_17 = 17;
public static final int SCHEMA_VERSION_18 = 18;
public static final int SCHEMA_VERSION_19 = 19;
public static final int SCHEMA_VERSION_20 = 20;
public static final String POSTGRES_SCHEMA_RESOURCE = "postgres/schema_v1.sql";
public static final String POSTGRES_MIGRATION_V2_RESOURCE = "postgres/migration_v2.sql";
public static final String POSTGRES_MIGRATION_V3_RESOURCE = "postgres/migration_v3.sql";
@@ -55,6 +56,7 @@ public final class DatabaseInitializer {
public static final String POSTGRES_MIGRATION_V17_RESOURCE = "postgres/migration_v17.sql";
public static final String POSTGRES_MIGRATION_V18_RESOURCE = "postgres/migration_v18.sql";
public static final String POSTGRES_MIGRATION_V19_RESOURCE = "postgres/migration_v19.sql";
public static final String POSTGRES_MIGRATION_V20_RESOURCE = "postgres/migration_v20.sql";
private DatabaseInitializer() {}
@@ -200,6 +202,10 @@ public final class DatabaseInitializer {
runSqlScript(conn, POSTGRES_MIGRATION_V19_RESOURCE);
currentVersion = SCHEMA_VERSION_19;
}
if (currentVersion < SCHEMA_VERSION_20) {
runSqlScript(conn, POSTGRES_MIGRATION_V20_RESOURCE);
currentVersion = SCHEMA_VERSION_20;
}
}
}
@@ -0,0 +1,35 @@
package shine.db.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public final class UserNotificationSeenStateDAO {
private static final UserNotificationSeenStateDAO INSTANCE = new UserNotificationSeenStateDAO();
private UserNotificationSeenStateDAO() {}
public static UserNotificationSeenStateDAO getInstance() { return INSTANCE; }
public long getSeenAt(Connection c, String login, String category) throws Exception {
try (PreparedStatement ps = c.prepareStatement("SELECT seen_at_ms FROM user_notification_seen_state WHERE owner_login=? AND category=?")) {
ps.setString(1, login); ps.setString(2, category);
try (ResultSet rs = ps.executeQuery()) { return rs.next() ? rs.getLong(1) : 0L; }
}
}
public long advance(Connection c, String login, String category, long seenAtMs, long signedAtMs, byte[] signedBlob) throws Exception {
String sql = """
INSERT INTO user_notification_seen_state(owner_login, category, seen_at_ms, signed_blob, signed_at_ms, updated_at_ms)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(owner_login, category) DO UPDATE SET
seen_at_ms=EXCLUDED.seen_at_ms, signed_blob=EXCLUDED.signed_blob,
signed_at_ms=EXCLUDED.signed_at_ms, updated_at_ms=EXCLUDED.updated_at_ms
WHERE user_notification_seen_state.seen_at_ms < EXCLUDED.seen_at_ms
""";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, login); ps.setString(2, category); ps.setLong(3, seenAtMs);
ps.setBytes(4, signedBlob); ps.setLong(5, signedAtMs); ps.setLong(6, System.currentTimeMillis());
ps.executeUpdate();
}
return getSeenAt(c, login, category);
}
}
@@ -84,6 +84,33 @@ public final class UserNotificationsStateDAO {
return out;
}
public List<UserNotificationEntry> listVisible(Connection c, String ownerLogin, String kind, long seenAtMs, long cutoffMs) throws SQLException {
String sql = """
SELECT owner_login, notification_kind, created_at_ms, source_login, source_bch_name,
source_block_number, source_block_hash, target_login, target_bch_name,
target_block_number, target_block_hash, source_msg_sub_type, source_text
FROM user_notifications_state
WHERE owner_login = ? AND notification_kind = ?
AND (created_at_ms > ? OR created_at_ms >= ?)
ORDER BY created_at_ms DESC, source_block_number DESC
""";
List<UserNotificationEntry> out = new ArrayList<>();
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, ownerLogin); ps.setString(2, kind); ps.setLong(3, Math.max(0, seenAtMs));
ps.setLong(4, Math.max(0, cutoffMs));
try (ResultSet rs = ps.executeQuery()) { while (rs.next()) out.add(mapRow(rs)); }
}
return out;
}
public long countUnseen(Connection c, String ownerLogin, String kind, long seenAtMs) throws SQLException {
String sql = "SELECT COUNT(*) FROM user_notifications_state WHERE owner_login = ? AND notification_kind = ? AND created_at_ms > ?";
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, ownerLogin); ps.setString(2, kind); ps.setLong(3, Math.max(0, seenAtMs));
try (ResultSet rs = ps.executeQuery()) { return rs.next() ? rs.getLong(1) : 0L; }
}
}
private static UserNotificationEntry mapRow(ResultSet rs) throws SQLException {
UserNotificationEntry e = new UserNotificationEntry();
e.setOwnerLogin(rs.getString("owner_login"));
@@ -0,0 +1,19 @@
-- Notifications v2: three categories + signed seen watermarks.
ALTER TABLE user_notifications_state
DROP CONSTRAINT IF EXISTS user_notifications_state_notification_kind_check;
ALTER TABLE user_notifications_state
ADD CONSTRAINT user_notifications_state_notification_kind_check
CHECK (notification_kind IN ('reply', 'connection', 'event'));
CREATE TABLE IF NOT EXISTS user_notification_seen_state (
owner_login VARCHAR(60) NOT NULL,
category TEXT NOT NULL CHECK (category IN ('replies', 'connections', 'events')),
seen_at_ms BIGINT NOT NULL DEFAULT 0,
signed_blob BYTEA NOT NULL,
signed_at_ms BIGINT NOT NULL,
updated_at_ms BIGINT NOT NULL,
PRIMARY KEY (owner_login, category)
);
UPDATE db_schema_version SET schema_version = 20 WHERE id = 1;
@@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS db_schema_version (
);
INSERT INTO db_schema_version (id, schema_version, updated_at_ms)
VALUES (1, 18, CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT))
VALUES (1, 20, CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT))
ON CONFLICT (id) DO UPDATE SET
schema_version = EXCLUDED.schema_version,
updated_at_ms = EXCLUDED.updated_at_ms;
@@ -765,7 +765,7 @@ CREATE INDEX IF NOT EXISTS idx_signed_message_delivery_session
CREATE TABLE IF NOT EXISTS user_notifications_state (
owner_login TEXT NOT NULL REFERENCES solana_user_pda_current(normalized_login),
notification_kind TEXT NOT NULL CHECK (notification_kind IN ('reply', 'connection')),
notification_kind TEXT NOT NULL CHECK (notification_kind IN ('reply', 'connection', 'event')),
created_at_ms BIGINT NOT NULL,
source_login TEXT NOT NULL,
source_bch_name TEXT NOT NULL,
@@ -1997,8 +1997,20 @@ UPDATE message_stats ms SET
AND rs.to_login=ms.to_login AND rs.to_bch_name=ms.to_bch_name AND rs.to_block_number=ms.to_block_number AND rs.to_block_hash=ms.to_block_hash
AND shine_is_primary(rs.from_login) AND shine_is_shining(rs.from_login));
CREATE TABLE IF NOT EXISTS user_notification_seen_state (
owner_login VARCHAR(60) NOT NULL,
category TEXT NOT NULL CHECK (category IN ('replies', 'connections', 'events')),
seen_at_ms BIGINT NOT NULL DEFAULT 0,
signed_blob BYTEA NOT NULL,
signed_at_ms BIGINT NOT NULL,
updated_at_ms BIGINT NOT NULL,
PRIMARY KEY (owner_login, category)
);
INSERT INTO db_schema_version(id,schema_version,updated_at_ms)
VALUES(1,18,CAST(EXTRACT(EPOCH FROM clock_timestamp())*1000 AS BIGINT))
VALUES(1,20,CAST(EXTRACT(EPOCH FROM clock_timestamp())*1000 AS BIGINT))
ON CONFLICT(id) DO UPDATE SET schema_version=EXCLUDED.schema_version, updated_at_ms=EXCLUDED.updated_at_ms;
COMMIT;