SHA256
Restore unread baseline and read tracking
This commit is contained in:
+1
-1
@@ -161,7 +161,7 @@ final class ChannelsReadSupport {
|
|||||||
AND setting_key = ?
|
AND setting_key = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
long lastSeen = 0;
|
long lastSeen = messagesCount;
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, viewerLogin);
|
ps.setString(1, viewerLogin);
|
||||||
ps.setInt(2, 1);
|
ps.setInt(2, 1);
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
> На первом этапе мы **не используем курсоры** (`nextCursor`) и загружаем полные списки.
|
> На первом этапе мы **не используем курсоры** (`nextCursor`) и загружаем полные списки.
|
||||||
>
|
>
|
||||||
> `unreadCount` для канала считается по `user_settings`:
|
> `unreadCount` для канала считается по `user_settings`:
|
||||||
> если для пары `ownerBlockchainName/channelName` ещё нет записи, канал считается полностью непросмотренным.
|
> если для пары `ownerBlockchainName/channelName` ещё нет записи, канал считается полностью прочитанным, а UI при первом открытии записывает текущий курсор чтения.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,8 @@
|
|||||||
- `value_num = number of messages already seen in channel`;
|
- `value_num = number of messages already seen in channel`;
|
||||||
- `value_text = ''`.
|
- `value_text = ''`.
|
||||||
|
|
||||||
Если настройки нет, канал считается полностью непросмотренным, то есть unread = `messagesCount`.
|
Если настройки нет, канал считается полностью прочитанным, то есть unread = `0`.
|
||||||
После первого открытия канала UI отправляет актуальный курсор и тем самым переводит unread к `0`.
|
После первого открытия канала UI отправляет текущий курсор, чтобы зафиксировать baseline и дальше считать только новые сообщения.
|
||||||
|
|
||||||
## 2. Структура записи
|
## 2. Структура записи
|
||||||
|
|
||||||
|
|||||||
@@ -241,6 +241,183 @@ function buildChannelSettingsKey(ownerBlockchainName, channelName) {
|
|||||||
return `${ownerBch}/${name}`;
|
return `${ownerBch}/${name}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getChannelScrollRoot() {
|
||||||
|
return document.getElementById('app-screen');
|
||||||
|
}
|
||||||
|
|
||||||
|
function scrollRootBy(delta, smooth = false) {
|
||||||
|
const root = getChannelScrollRoot();
|
||||||
|
const behavior = smooth ? 'smooth' : 'auto';
|
||||||
|
if (root && typeof root.scrollBy === 'function') {
|
||||||
|
root.scrollBy({ top: delta, behavior });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.scrollBy({ top: delta, behavior });
|
||||||
|
}
|
||||||
|
|
||||||
|
function scrollElementToViewportFraction(element, fraction = 1 / 3, smooth = false) {
|
||||||
|
if (!element) return false;
|
||||||
|
const root = getChannelScrollRoot();
|
||||||
|
const viewportHeight = root?.clientHeight || window.innerHeight || document.documentElement.clientHeight || 0;
|
||||||
|
const targetTop = Math.max(0, Math.round(viewportHeight * fraction));
|
||||||
|
const rect = element.getBoundingClientRect();
|
||||||
|
const delta = rect.top - targetTop;
|
||||||
|
if (Math.abs(delta) < 2) return true;
|
||||||
|
scrollRootBy(delta, smooth);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function scrollChannelToUnreadLine(screen, smooth = false) {
|
||||||
|
return scrollElementToViewportFraction(screen.querySelector('.channel-unread-line'), 1 / 3, smooth);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createChannelReadTracker({
|
||||||
|
screen,
|
||||||
|
routeKey,
|
||||||
|
settingKey,
|
||||||
|
unreadCount,
|
||||||
|
messagesCount,
|
||||||
|
initialSeenCount,
|
||||||
|
}) {
|
||||||
|
const login = String(state.session.login || '').trim();
|
||||||
|
const storagePwd = state.session.storagePwdInMemory;
|
||||||
|
const canWrite = !!(settingKey && login && storagePwd);
|
||||||
|
const safeMessagesCount = Math.max(0, Number(messagesCount || 0));
|
||||||
|
const safeInitialSeenCount = Math.max(0, Math.min(Number(initialSeenCount || 0), safeMessagesCount));
|
||||||
|
const unreadLine = unreadCount > 0 ? screen.querySelector('.channel-unread-line') : null;
|
||||||
|
|
||||||
|
let desiredSeenCount = safeInitialSeenCount;
|
||||||
|
let persistedSeenCount = safeInitialSeenCount;
|
||||||
|
let inFlight = false;
|
||||||
|
let disposed = false;
|
||||||
|
let rafId = 0;
|
||||||
|
let timerId = 0;
|
||||||
|
|
||||||
|
const clearTimer = () => {
|
||||||
|
if (timerId) {
|
||||||
|
clearTimeout(timerId);
|
||||||
|
timerId = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const queueFlush = (delayMs = 180) => {
|
||||||
|
if (disposed || !canWrite) return;
|
||||||
|
clearTimer();
|
||||||
|
timerId = setTimeout(() => {
|
||||||
|
timerId = 0;
|
||||||
|
void flush();
|
||||||
|
}, Math.max(0, Number(delayMs) || 0));
|
||||||
|
};
|
||||||
|
|
||||||
|
const flush = async () => {
|
||||||
|
if (disposed || !canWrite) return;
|
||||||
|
const next = Math.max(safeInitialSeenCount, Math.min(desiredSeenCount, safeMessagesCount));
|
||||||
|
if (next <= persistedSeenCount) return;
|
||||||
|
if (inFlight) {
|
||||||
|
queueFlush(120);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
inFlight = true;
|
||||||
|
try {
|
||||||
|
await authService.upsertUserSetting({
|
||||||
|
login,
|
||||||
|
settingType: 1,
|
||||||
|
settingKey,
|
||||||
|
timeMs: Date.now(),
|
||||||
|
valueText: '',
|
||||||
|
valueNum: next,
|
||||||
|
storagePwd,
|
||||||
|
});
|
||||||
|
persistedSeenCount = next;
|
||||||
|
} catch {
|
||||||
|
queueFlush(800);
|
||||||
|
} finally {
|
||||||
|
inFlight = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const collectSeenCount = () => {
|
||||||
|
const cards = Array.from(screen.querySelectorAll('.channel-message-card[data-local-number]'));
|
||||||
|
if (!cards.length) return safeInitialSeenCount;
|
||||||
|
if (!unreadLine) return safeMessagesCount;
|
||||||
|
|
||||||
|
const root = getChannelScrollRoot();
|
||||||
|
const viewportHeight = root?.clientHeight || window.innerHeight || document.documentElement.clientHeight || 0;
|
||||||
|
const thresholdTop = Math.max(0, Math.round(viewportHeight / 3));
|
||||||
|
let seen = safeInitialSeenCount;
|
||||||
|
for (const card of cards) {
|
||||||
|
const localNumber = Number(card.dataset.localNumber || 0);
|
||||||
|
if (!Number.isFinite(localNumber) || localNumber <= 0) continue;
|
||||||
|
const rect = card.getBoundingClientRect();
|
||||||
|
if (rect.top > thresholdTop + 1) break;
|
||||||
|
seen = Math.max(seen, localNumber);
|
||||||
|
}
|
||||||
|
return Math.max(safeInitialSeenCount, Math.min(seen, safeMessagesCount));
|
||||||
|
};
|
||||||
|
|
||||||
|
const measure = () => {
|
||||||
|
if (disposed) return;
|
||||||
|
if (rafId) return;
|
||||||
|
rafId = window.requestAnimationFrame(() => {
|
||||||
|
rafId = 0;
|
||||||
|
const next = collectSeenCount();
|
||||||
|
if (next > desiredSeenCount) {
|
||||||
|
desiredSeenCount = next;
|
||||||
|
queueFlush(180);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const scrollRoot = getChannelScrollRoot();
|
||||||
|
const onScroll = () => measure();
|
||||||
|
const onResize = () => measure();
|
||||||
|
|
||||||
|
if (scrollRoot && typeof scrollRoot.addEventListener === 'function') {
|
||||||
|
scrollRoot.addEventListener('scroll', onScroll, { passive: true });
|
||||||
|
} else {
|
||||||
|
window.addEventListener('scroll', onScroll, { passive: true });
|
||||||
|
}
|
||||||
|
window.addEventListener('resize', onResize);
|
||||||
|
|
||||||
|
const initialSyncRequired = canWrite && unreadCount <= 0 && safeMessagesCount >= 0;
|
||||||
|
if (initialSyncRequired) {
|
||||||
|
void authService.upsertUserSetting({
|
||||||
|
login,
|
||||||
|
settingType: 1,
|
||||||
|
settingKey,
|
||||||
|
timeMs: Date.now(),
|
||||||
|
valueText: '',
|
||||||
|
valueNum: safeMessagesCount,
|
||||||
|
storagePwd,
|
||||||
|
}).catch(() => {});
|
||||||
|
persistedSeenCount = safeMessagesCount;
|
||||||
|
desiredSeenCount = safeMessagesCount;
|
||||||
|
} else {
|
||||||
|
window.setTimeout(() => measure(), 120);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanup = () => {
|
||||||
|
disposed = true;
|
||||||
|
clearTimer();
|
||||||
|
if (rafId) {
|
||||||
|
window.cancelAnimationFrame(rafId);
|
||||||
|
rafId = 0;
|
||||||
|
}
|
||||||
|
if (scrollRoot && typeof scrollRoot.removeEventListener === 'function') {
|
||||||
|
scrollRoot.removeEventListener('scroll', onScroll);
|
||||||
|
} else {
|
||||||
|
window.removeEventListener('scroll', onScroll);
|
||||||
|
}
|
||||||
|
window.removeEventListener('resize', onResize);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
cleanup,
|
||||||
|
measure,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function firstNonEmptyText(...candidates) {
|
function firstNonEmptyText(...candidates) {
|
||||||
for (const candidate of candidates) {
|
for (const candidate of candidates) {
|
||||||
if (typeof candidate !== 'string') continue;
|
if (typeof candidate !== 'string') continue;
|
||||||
@@ -1751,6 +1928,9 @@ function renderPostCard(post, {
|
|||||||
if (refKey) {
|
if (refKey) {
|
||||||
card.dataset.messageKey = refKey;
|
card.dataset.messageKey = refKey;
|
||||||
}
|
}
|
||||||
|
if (Number.isFinite(Number(post.localNumber)) && Number(post.localNumber) > 0) {
|
||||||
|
card.dataset.localNumber = String(Number(post.localNumber));
|
||||||
|
}
|
||||||
card.classList.add('is-counters-visible');
|
card.classList.add('is-counters-visible');
|
||||||
|
|
||||||
if (!post.messageRef || !selector) return card;
|
if (!post.messageRef || !selector) return card;
|
||||||
@@ -1916,6 +2096,10 @@ function renderPostCard(post, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
||||||
|
const unreadCount = Math.max(0, Number(channelData.unreadCount || 0));
|
||||||
|
const messagesCount = Math.max(0, Number(channelData.messagesCount || (Array.isArray(channelData.posts) ? channelData.posts.length : 0) || 0));
|
||||||
|
const readCount = Math.max(0, messagesCount - unreadCount);
|
||||||
|
|
||||||
if (channelData.reverseChannelMissingWarning) {
|
if (channelData.reverseChannelMissingWarning) {
|
||||||
const reverseWarning = document.createElement('p');
|
const reverseWarning = document.createElement('p');
|
||||||
reverseWarning.className = 'channel-head-meta';
|
reverseWarning.className = 'channel-head-meta';
|
||||||
@@ -1923,13 +2107,6 @@ function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
|||||||
screen.append(reverseWarning);
|
screen.append(reverseWarning);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Number(channelData.unreadCount || 0) > 0) {
|
|
||||||
const unreadLine = document.createElement('div');
|
|
||||||
unreadLine.className = 'card channel-unread-line';
|
|
||||||
unreadLine.textContent = `Не прочитано: ${channelData.unreadCount}`;
|
|
||||||
screen.append(unreadLine);
|
|
||||||
}
|
|
||||||
|
|
||||||
const actionButton = document.createElement('button');
|
const actionButton = document.createElement('button');
|
||||||
actionButton.className = 'destructive-btn channel-main-action';
|
actionButton.className = 'destructive-btn channel-main-action';
|
||||||
actionButton.textContent = 'Подписаться на канал';
|
actionButton.textContent = 'Подписаться на канал';
|
||||||
@@ -1948,6 +2125,7 @@ function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
|||||||
const postsByKey = new Map();
|
const postsByKey = new Map();
|
||||||
const metaEvents = (Array.isArray(channelData.metaEvents) ? channelData.metaEvents : [])
|
const metaEvents = (Array.isArray(channelData.metaEvents) ? channelData.metaEvents : [])
|
||||||
.map((event) => mapChannelMetaEvent(event, channelData.channel));
|
.map((event) => mapChannelMetaEvent(event, channelData.channel));
|
||||||
|
let unreadLineInserted = unreadCount === 0;
|
||||||
const feedItems = [
|
const feedItems = [
|
||||||
...metaEvents.map((event) => ({
|
...metaEvents.map((event) => ({
|
||||||
type: 'meta',
|
type: 'meta',
|
||||||
@@ -1969,6 +2147,13 @@ function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
|||||||
|
|
||||||
if (feedItems.length) {
|
if (feedItems.length) {
|
||||||
feedItems.forEach((item) => {
|
feedItems.forEach((item) => {
|
||||||
|
if (!unreadLineInserted && item.type === 'post' && Number(item?.post?.localNumber || 0) > readCount) {
|
||||||
|
const unreadLine = document.createElement('div');
|
||||||
|
unreadLine.className = 'card channel-unread-line';
|
||||||
|
unreadLine.textContent = `Не прочитано: ${unreadCount}`;
|
||||||
|
feed.append(unreadLine);
|
||||||
|
unreadLineInserted = true;
|
||||||
|
}
|
||||||
if (item.type === 'meta') {
|
if (item.type === 'meta') {
|
||||||
feed.append(renderChannelMetaEventCard(item.event));
|
feed.append(renderChannelMetaEventCard(item.event));
|
||||||
return;
|
return;
|
||||||
@@ -2020,10 +2205,25 @@ function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
|||||||
screen.append(feed, backButton);
|
screen.append(feed, backButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
applyPendingScroll(screen, routeKey, channelData.isOwnChannel || channelData.isDiary || Number(channelData.unreadCount || 0) === 0);
|
const hasPendingScrollTarget = pendingScrollByRoute.has(routeKey);
|
||||||
return () => {
|
applyPendingScroll(screen, routeKey, channelData.isOwnChannel || channelData.isDiary || unreadCount === 0);
|
||||||
// noop
|
if (!hasPendingScrollTarget && unreadCount > 0 && !channelData.isOwnChannel && !channelData.isDiary) {
|
||||||
};
|
window.setTimeout(() => scrollChannelToUnreadLine(screen, false), 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tracker = createChannelReadTracker({
|
||||||
|
screen,
|
||||||
|
routeKey,
|
||||||
|
settingKey: buildChannelSettingsKey(
|
||||||
|
channelData.channel?.ownerBlockchainName || channelData.selector?.ownerBlockchainName,
|
||||||
|
channelData.channel?.name || channelData.channel?.channelName,
|
||||||
|
),
|
||||||
|
unreadCount,
|
||||||
|
messagesCount,
|
||||||
|
initialSeenCount: readCount,
|
||||||
|
});
|
||||||
|
|
||||||
|
return tracker.cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderSkeleton(screen) {
|
function renderSkeleton(screen) {
|
||||||
@@ -2319,22 +2519,6 @@ export function render({ navigate, route, chrome }) {
|
|||||||
try {
|
try {
|
||||||
const apiData = await loadFromApi(route, channelId);
|
const apiData = await loadFromApi(route, channelId);
|
||||||
activeSelector = apiData?.selector || null;
|
activeSelector = apiData?.selector || null;
|
||||||
const lastSeenCount = Number(apiData?.messagesCount || (Array.isArray(apiData?.posts) ? apiData.posts.length : 0) || 0);
|
|
||||||
const settingKey = buildChannelSettingsKey(
|
|
||||||
apiData?.channel?.ownerBlockchainName || apiData?.selector?.ownerBlockchainName,
|
|
||||||
apiData?.channel?.name || apiData?.channel?.channelName,
|
|
||||||
);
|
|
||||||
if (settingKey && state.session.login && state.session.storagePwdInMemory) {
|
|
||||||
void authService.upsertUserSetting({
|
|
||||||
login: state.session.login,
|
|
||||||
settingType: 1,
|
|
||||||
settingKey,
|
|
||||||
timeMs: Date.now(),
|
|
||||||
valueText: '',
|
|
||||||
valueNum: lastSeenCount,
|
|
||||||
storagePwd: state.session.storagePwdInMemory,
|
|
||||||
}).catch(() => {});
|
|
||||||
}
|
|
||||||
const titleLabel = apiData?.channel?.displayTitle || apiData?.channel?.name || 'channel';
|
const titleLabel = apiData?.channel?.displayTitle || apiData?.channel?.name || 'channel';
|
||||||
const entrypointPosts = getEntrypointPosts(apiData?.posts);
|
const entrypointPosts = getEntrypointPosts(apiData?.posts);
|
||||||
const openEntrypointHistory = () => {
|
const openEntrypointHistory = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user