SHA256
CORE/CONTENT test5593 и адаптация UI
This commit is contained in:
@@ -64,7 +64,7 @@ import {
|
||||
} from '../services/shine-routes.js';
|
||||
import { parseDmTechBlocks } from '../services/dm-tech-blocks.js';
|
||||
import { dropPageData, readPageData, writePageData } from '../services/page-data-cache.js';
|
||||
import { sortedChannelPublications } from '../services/channel-message-ordinals.js';
|
||||
import { sortedChannelPublications, canonicalReadPosts, readPrefixCount } from '../services/channel-message-ordinals.js';
|
||||
import { scrollNodeIntoContainer } from '../services/scroll-utils.js';
|
||||
|
||||
export const pageMeta = { id: 'channel-view', get title() { return t('screens.channel_view.text_001'); }, shellMode: { scrollbar: 'hidden', topFade: false, contentUnderTopbar: false, contentWidth: 'wide' } };
|
||||
@@ -316,27 +316,26 @@ function scrollChannelToUnreadLine(screen, unreadCount = 0, smooth = false) {
|
||||
function createChannelReadTracker({
|
||||
screen,
|
||||
routeKey,
|
||||
ownerBlockchainName,
|
||||
channelName,
|
||||
readSummary,
|
||||
posts,
|
||||
initializeIfMissing = false,
|
||||
unreadCount,
|
||||
messagesCount,
|
||||
initialSeenCount,
|
||||
onPersistError = null,
|
||||
onPersistSuccess = null,
|
||||
}) {
|
||||
const login = String(state.session.login || '').trim();
|
||||
const storagePwd = state.session.storagePwdInMemory;
|
||||
const cleanOwnerBlockchainName = String(ownerBlockchainName || '').trim();
|
||||
const cleanChannelName = String(channelName || '').trim();
|
||||
const orderedPosts = canonicalReadPosts(posts, readSummary);
|
||||
const rootRef = readSummary?.channel?.channelRoot;
|
||||
const missingWriteParts = [];
|
||||
if (!cleanOwnerBlockchainName) missingWriteParts.push('ownerBlockchainName');
|
||||
if (!cleanChannelName) missingWriteParts.push('channelName');
|
||||
if (!orderedPosts) missingWriteParts.push('CORE read prefix');
|
||||
if (!login) missingWriteParts.push('login');
|
||||
if (!storagePwd) missingWriteParts.push('storagePwd');
|
||||
const canWrite = missingWriteParts.length === 0;
|
||||
const safeMessagesCount = Math.max(0, Number(messagesCount || 0));
|
||||
const safeMessagesCount = orderedPosts?.length || 0;
|
||||
const safeInitialSeenCount = Math.max(0, Math.min(Number(initialSeenCount || 0), safeMessagesCount));
|
||||
const seenOrdinals = new Set();
|
||||
const unreadAnchorFraction = getUnreadAnchorViewportFraction(unreadCount);
|
||||
|
||||
let desiredSeenCount = safeInitialSeenCount;
|
||||
@@ -375,16 +374,19 @@ function createChannelReadTracker({
|
||||
|
||||
inFlight = true;
|
||||
try {
|
||||
const anchor = next === 0 ? rootRef : orderedPosts[next - 1].messageRef;
|
||||
const persisted = await authService.setChannelReadState({
|
||||
login,
|
||||
ownerBlockchainName: cleanOwnerBlockchainName,
|
||||
channelName: cleanChannelName,
|
||||
readCount: next,
|
||||
login, owner: readSummary.channel.ownerLogin, root: rootRef.blockNumber, rootHash: rootRef.blockHash,
|
||||
followHash: readSummary.followHash, anchor: anchor.blockNumber, anchorHash: anchor.blockHash,
|
||||
timeMs: Date.now(),
|
||||
storagePwd,
|
||||
});
|
||||
const serverReadCount = Math.max(0, Number(persisted?.read_count ?? next));
|
||||
const serverUnreadCount = Math.max(0, Number(persisted?.unread_count ?? Math.max(0, safeMessagesCount - serverReadCount)));
|
||||
if (disposed) return;
|
||||
if (persisted?.state?.followHash !== readSummary.followHash || !persisted.state.initialized || !Number.isInteger(persisted.state.readCount) || !Number.isInteger(persisted.state.unreadCount)) throw new Error('READ_STATE_RETRY');
|
||||
const serverReadCount = persisted.state.readCount;
|
||||
const serverUnreadCount = persisted.state.unreadCount;
|
||||
// A changed count can mean that the canonical prefix was replaced while the view was open.
|
||||
if (serverReadCount < persistedSeenCount || serverReadCount + serverUnreadCount !== safeMessagesCount) throw new Error('READ_STATE_RETRY');
|
||||
persistedSeenCount = Math.max(persistedSeenCount, Math.min(serverReadCount, safeMessagesCount));
|
||||
desiredSeenCount = Math.max(desiredSeenCount, persistedSeenCount);
|
||||
initialPersistPending = false;
|
||||
@@ -396,8 +398,9 @@ function createChannelReadTracker({
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (disposed) return;
|
||||
const errorCode = String(error?.code || '').trim().toUpperCase();
|
||||
if (errorCode === 'CHANNEL_NOT_FOLLOWED') {
|
||||
if (['CHANNEL_NOT_FOLLOWED','READ_ANCHOR_STALE','READ_STATE_RETRY','BAD_READ_STATE','BAD_TIME','BAD_SIGNATURE','LOGIN_MISMATCH','SESSION_NOT_FOUND','AUTH_REQUIRED','CHANNEL_NOT_FOUND','ACCOUNT_NOT_REGISTERED'].includes(errorCode || String(error?.message || ''))) {
|
||||
// This is a persistent state mismatch, not a transient transport failure.
|
||||
// Stop writes for the current channel view instead of hammering the server
|
||||
// after every scroll/resize. A fresh view after subscribe/refresh will create
|
||||
@@ -426,19 +429,14 @@ function createChannelReadTracker({
|
||||
const viewportHeight = root?.clientHeight || window.innerHeight || document.documentElement.clientHeight || 0;
|
||||
const thresholdTop = Math.max(0, Math.round(viewportHeight * unreadAnchorFraction));
|
||||
const visibleBottom = Math.max(thresholdTop, viewportHeight - 24);
|
||||
let seen = safeInitialSeenCount;
|
||||
if (!orderedPosts) return safeInitialSeenCount;
|
||||
for (const card of cards) {
|
||||
const localNumber = Number(card.dataset.localNumber || 0);
|
||||
if (!Number.isFinite(localNumber) || localNumber <= 0) continue;
|
||||
if (!Number.isInteger(localNumber) || localNumber <= 0) continue;
|
||||
const rect = card.getBoundingClientRect();
|
||||
if (rect.top > visibleBottom) break;
|
||||
if (rect.bottom < 0) {
|
||||
seen = Math.max(seen, localNumber);
|
||||
continue;
|
||||
}
|
||||
seen = Math.max(seen, localNumber);
|
||||
if (rect.top <= visibleBottom && rect.bottom > 0) seenOrdinals.add(localNumber);
|
||||
}
|
||||
return Math.max(safeInitialSeenCount, Math.min(seen, safeMessagesCount));
|
||||
return readPrefixCount(orderedPosts, persistedSeenCount, seenOrdinals);
|
||||
};
|
||||
|
||||
const measure = () => {
|
||||
@@ -1389,8 +1387,7 @@ async function loadFromApi(route, channelId) {
|
||||
const ownerLoginForLookup = routeOwnerLoginFromBch || (!looksLikeBlockchainName(routeOwnerRaw) ? routeOwnerRaw : '');
|
||||
if (ownerLoginForLookup) {
|
||||
try {
|
||||
const ownerFeed = await authService.listSubscriptionsFeed(ownerLoginForLookup, 500);
|
||||
const ownerRows = Array.isArray(ownerFeed?.ownedChannels) ? ownerFeed.ownedChannels : [];
|
||||
const ownerRows = await authService.listOwnedChannelSummaries(ownerLoginForLookup, 500);
|
||||
const ownerLoginNormalized = ownerLoginForLookup.toLowerCase();
|
||||
channel = ownerRows.find((item) => (
|
||||
String(item?.channel?.channelName || '').trim().toLowerCase() === selector.channelName.toLowerCase()
|
||||
@@ -1457,13 +1454,11 @@ async function loadFromApi(route, channelId) {
|
||||
|
||||
const payload = await authService.getChannelMessages(selector, null, 'asc', currentSessionLogin);
|
||||
const messages = Array.isArray(payload.messages) ? payload.messages : [];
|
||||
let reverseChannelMissingWarning = '';
|
||||
let mergedMessages = [...messages];
|
||||
const reverseChannelMissingWarning = '';
|
||||
const mergedMessages = [...messages];
|
||||
if (!messagesCount) messagesCount = mergedMessages.length;
|
||||
|
||||
const currentLogin = currentSessionLogin;
|
||||
const ownerLogin = String(payload.channel?.ownerLogin || '').trim();
|
||||
const channelName = String(payload.channel?.channelName || '').trim();
|
||||
const channelTypeCode = Number(payload.channel?.channelTypeCode ?? 1);
|
||||
// Channel-publication URLs are only unambiguous for public channels, not merged private chats.
|
||||
selector.channelName = String(payload.channel?.channelName || selector.channelName || '').trim();
|
||||
@@ -1472,46 +1467,24 @@ async function loadFromApi(route, channelId) {
|
||||
if (channelTypeCode === CHANNEL_TYPE_STORIES) {
|
||||
throw new Error(t('screens.channel_view.text_120'));
|
||||
}
|
||||
const canResolveReverse = (
|
||||
channelTypeCode === CHANNEL_TYPE_PERSONAL
|
||||
&& !!currentLogin
|
||||
&& !!ownerLogin
|
||||
&& !!channelName
|
||||
&& ownerLogin.toLowerCase() === currentLogin.toLowerCase()
|
||||
);
|
||||
|
||||
if (canResolveReverse) {
|
||||
const allRows = await getAllRows();
|
||||
const reverseSummary = allRows.find((item) => (
|
||||
Number(item?.channel?.channelTypeCode ?? 1) === CHANNEL_TYPE_PERSONAL
|
||||
&& String(item?.channel?.ownerLogin || '').trim().toLowerCase() === channelName.toLowerCase()
|
||||
&& String(item?.channel?.channelName || '').trim().toLowerCase() === currentLogin.toLowerCase()
|
||||
));
|
||||
|
||||
if (reverseSummary?.channel?.ownerBlockchainName && reverseSummary?.channel?.channelRoot?.blockNumber != null) {
|
||||
const reverseSelector = {
|
||||
ownerBlockchainName: String(reverseSummary.channel.ownerBlockchainName),
|
||||
channelRootBlockNumber: Number(reverseSummary.channel.channelRoot.blockNumber),
|
||||
channelRootBlockHash: normalizeRouteHash(reverseSummary.channel.channelRoot.blockHash),
|
||||
};
|
||||
const reversePayload = await authService.getChannelMessages(reverseSelector, null, 'asc', currentSessionLogin);
|
||||
const reverseMessages = Array.isArray(reversePayload?.messages) ? reversePayload.messages : [];
|
||||
mergedMessages = mergedMessages.concat(reverseMessages);
|
||||
} else {
|
||||
reverseChannelMissingWarning = t('screens.channel_view.text_145', { value1: channelName });
|
||||
}
|
||||
}
|
||||
|
||||
// The current server already merges both sides of PERSONAL channels.
|
||||
// Fetching and appending the reverse side again duplicates every post.
|
||||
const posts = sortedChannelPublications(
|
||||
mergedMessages.map((message, index) => mapApiMessageToPost(message, selector, index + 1)),
|
||||
).map((post, index) => ({ ...post, localNumber: index + 1 }));
|
||||
const isOwnChannel = ownerLogin.toLowerCase() === currentSessionLogin.toLowerCase();
|
||||
const followedRows = Array.isArray(state.channelsFeed?.followedChannels) ? state.channelsFeed.followedChannels : [];
|
||||
const isSubscribed = isAuthorized && followedRows.some((row) => (
|
||||
String(row?.channel?.ownerBlockchainName || '') === String(selector.ownerBlockchainName || '')
|
||||
const followedRows = isAuthorized ? (await ensureFeed())?.followedChannels || [] : [];
|
||||
const readSummary = followedRows.find(row =>
|
||||
String(row?.channel?.ownerBlockchainName || '').toLowerCase() === String(selector.ownerBlockchainName || '').toLowerCase()
|
||||
&& Number(row?.channel?.channelRoot?.blockNumber) === Number(selector.channelRootBlockNumber)
|
||||
&& normalizeRouteHash(row?.channel?.channelRoot?.blockHash) === normalizeRouteHash(selector.channelRootBlockHash)
|
||||
));
|
||||
&& normalizeRouteHash(row?.channel?.channelRoot?.blockHash) === normalizeRouteHash(selector.channelRootBlockHash));
|
||||
const isSubscribed = !!readSummary;
|
||||
if (readSummary) {
|
||||
unreadCount = Number(readSummary.unreadCount || 0);
|
||||
messagesCount = Number(readSummary.messagesCount || 0);
|
||||
readCount = Number(readSummary.readCount || 0);
|
||||
readStateInitialized = !!readSummary.readStateInitialized;
|
||||
}
|
||||
|
||||
return {
|
||||
channel: {
|
||||
@@ -1539,6 +1512,7 @@ async function loadFromApi(route, channelId) {
|
||||
isOwnChannel,
|
||||
isSubscribed,
|
||||
readStateInitialized,
|
||||
readSummary,
|
||||
selector,
|
||||
};
|
||||
}
|
||||
@@ -2306,7 +2280,9 @@ function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
||||
const postsByKey = new Map();
|
||||
const metaEvents = (Array.isArray(channelData.metaEvents) ? channelData.metaEvents : [])
|
||||
.map((event) => mapChannelMetaEvent(event, channelData.channel));
|
||||
let unreadLineInserted = unreadCount === 0;
|
||||
const canonicalPosts = canonicalReadPosts(channelData.posts, channelData.readSummary);
|
||||
const unreadOrdinals = canonicalPosts ? new Set(canonicalPosts.slice(readCount).map(post => post.localNumber)) : null;
|
||||
let unreadLineInserted = unreadCount === 0 || !unreadOrdinals;
|
||||
const feedItems = [
|
||||
...metaEvents.map((event) => ({
|
||||
type: 'meta',
|
||||
@@ -2339,7 +2315,7 @@ function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
||||
separator.innerHTML = `<span>${escapeHtml(formatDayLabel(item.timestampMs))}</span>`;
|
||||
feed.append(separator);
|
||||
}
|
||||
if (!unreadLineInserted && item.type === 'post' && Number(item?.post?.localNumber || 0) > readCount) {
|
||||
if (!unreadLineInserted && item.type === 'post' && unreadOrdinals?.has(item.post.localNumber)) {
|
||||
const unreadLine = document.createElement('div');
|
||||
unreadLine.className = 'card channel-unread-line';
|
||||
unreadLine.textContent = t('dialogs.modals.channel_view.label_055');
|
||||
@@ -2426,17 +2402,17 @@ function renderBody(screen, navigate, routeKey, channelData, handlers) {
|
||||
channelData.isSubscribed
|
||||
&& !channelData.isOwnChannel
|
||||
&& !channelData.isDiary
|
||||
&& !!channelData.readSummary?.followHash
|
||||
);
|
||||
|
||||
const tracker = shouldTrackReadState
|
||||
? createChannelReadTracker({
|
||||
screen,
|
||||
routeKey,
|
||||
ownerBlockchainName: channelData.channel?.ownerBlockchainName || channelData.selector?.ownerBlockchainName,
|
||||
channelName: channelData.channel?.name || channelData.channel?.channelName,
|
||||
readSummary: channelData.readSummary,
|
||||
posts: channelData.posts,
|
||||
initializeIfMissing: !!(!channelData.readStateInitialized),
|
||||
unreadCount,
|
||||
messagesCount,
|
||||
initialSeenCount: readCount,
|
||||
onPersistError: (error, options = {}) => {
|
||||
const detail = String(error?.message || '').trim();
|
||||
@@ -2678,19 +2654,8 @@ export function render({ navigate, route, chrome }) {
|
||||
});
|
||||
if (disposed) return;
|
||||
|
||||
try {
|
||||
await authService.setChannelReadState({
|
||||
login,
|
||||
ownerBlockchainName: apiData.channel?.ownerBlockchainName || apiData.selector?.ownerBlockchainName,
|
||||
channelName: apiData.channel?.name || apiData.channel?.channelName,
|
||||
readCount: Math.max(0, Number(apiData.messagesCount || 0)),
|
||||
timeMs: Date.now(),
|
||||
storagePwd,
|
||||
});
|
||||
} catch (readStateError) {
|
||||
showStatus(toUserMessage(readStateError, t('screens.channel_view.text_058')));
|
||||
}
|
||||
|
||||
// Refresh the signed subscription identity; the viewport tracker initializes
|
||||
// the root and advances only over the actually seen canonical prefix.
|
||||
const feed = await authService.listSubscriptionsFeed(login, 200);
|
||||
if (disposed) return;
|
||||
setChannelsFeed(feed, state.channelsIndex);
|
||||
|
||||
Reference in New Issue
Block a user