SHA256
72 lines
3.4 KiB
JavaScript
72 lines
3.4 KiB
JavaScript
// Public channel URLs use one-based publication positions, not blockchain block numbers.
|
|
// Order must match the channel feed: message time, then original block number.
|
|
function messageTimestamp(message) {
|
|
const value = Number(message?.timestampMs ?? message?.createdAtMs ?? 0);
|
|
return Number.isFinite(value) ? value : 0;
|
|
}
|
|
|
|
function blockNumber(message) {
|
|
const value = Number(message?.messageRef?.blockNumber);
|
|
return Number.isSafeInteger(value) ? value : 0;
|
|
}
|
|
|
|
export function sortedChannelPublications(messages = []) {
|
|
return [...messages].sort((a, b) => {
|
|
const byTime = messageTimestamp(a) - messageTimestamp(b);
|
|
return byTime !== 0 ? byTime : blockNumber(a) - blockNumber(b);
|
|
});
|
|
}
|
|
|
|
export function parsePublicationOrdinal(value) {
|
|
const text = String(value ?? '').trim();
|
|
if (!/^[1-9][0-9]*$/.test(text)) return null;
|
|
const number = Number(text);
|
|
return Number.isSafeInteger(number) ? number : null;
|
|
}
|
|
|
|
export function publicationAtOrdinal(messages, ordinal) {
|
|
const value = parsePublicationOrdinal(ordinal);
|
|
if (!value) return null;
|
|
return sortedChannelPublications(messages)[value - 1] || null;
|
|
}
|
|
|
|
export function ordinalForPublication(messages, reference) {
|
|
if (!reference) return null;
|
|
const number = Number(reference.blockNumber);
|
|
const hash = String(reference.blockHash || '').trim().toLowerCase();
|
|
const owner = String(reference.blockchainName || '').trim().toLowerCase();
|
|
const i = sortedChannelPublications(messages).findIndex((item) => {
|
|
const ref = item?.messageRef;
|
|
if (Number(ref?.blockNumber) !== number) return false;
|
|
if (hash && String(ref?.blockHash || '').trim().toLowerCase() !== hash) return false;
|
|
if (owner && String(item?.authorBlockchainName || '').trim().toLowerCase() !== owner) return false;
|
|
return true;
|
|
});
|
|
return i < 0 ? null : i + 1;
|
|
}
|
|
|
|
// Read watermarks use canonical block order, not the time-sorted display ordinal.
|
|
// Require the complete list and its exact final anchor from the subscription feed.
|
|
export function canonicalReadPosts(posts, summary) {
|
|
const channel = summary?.channel, root = channel?.channelRoot;
|
|
const hash = v => typeof v === 'string' && /^[0-9a-f]{64}$/.test(v);
|
|
if (!channel?.ownerLogin || !Number.isInteger(root?.blockNumber) || !hash(root.blockHash)
|
|
|| !hash(summary.followHash) || !Number.isInteger(summary.messagesCount) || summary.messagesCount !== posts.length) return null;
|
|
const ordered = [...posts].sort((a,b) => a.messageRef?.blockNumber - b.messageRef?.blockNumber);
|
|
let previous = root.blockNumber;
|
|
const ordinals = new Set();
|
|
for (const post of ordered) {
|
|
const ref = post.messageRef;
|
|
if (String(post.authorLogin || '').toLowerCase() !== channel.ownerLogin.toLowerCase() || !Number.isInteger(ref?.blockNumber) || ref.blockNumber <= previous || !hash(ref.blockHash) || !Number.isInteger(post.localNumber) || post.localNumber <= 0 || ordinals.has(post.localNumber)) return null;
|
|
previous = ref.blockNumber; ordinals.add(post.localNumber);
|
|
}
|
|
const last = ordered.at(-1)?.messageRef || root;
|
|
if (last.blockNumber !== summary.readAnchor?.blockNumber || last.blockHash !== summary.readAnchor?.blockHash) return null;
|
|
return ordered;
|
|
}
|
|
export function readPrefixCount(ordered, alreadyRead, seenOrdinals) {
|
|
let count = Math.min(ordered.length, Math.max(0, alreadyRead));
|
|
while (count < ordered.length && seenOrdinals.has(ordered[count].localNumber)) count++;
|
|
return count;
|
|
}
|