Move toju-app into own its folder
This commit is contained in:
59
toju-app/src/app/domains/chat/domain/message-sync.rules.ts
Normal file
59
toju-app/src/app/domains/chat/domain/message-sync.rules.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/** Maximum number of recent messages to include in sync inventories. */
|
||||
export const INVENTORY_LIMIT = 1000;
|
||||
|
||||
/** Number of messages per chunk for inventory / batch transfers. */
|
||||
export const CHUNK_SIZE = 200;
|
||||
|
||||
/** Aggressive sync poll interval (10 seconds). */
|
||||
export const SYNC_POLL_FAST_MS = 10_000;
|
||||
|
||||
/** Idle sync poll interval after a clean (no-new-messages) cycle (15 minutes). */
|
||||
export const SYNC_POLL_SLOW_MS = 900_000;
|
||||
|
||||
/** Sync timeout duration before auto-completing a cycle (5 seconds). */
|
||||
export const SYNC_TIMEOUT_MS = 5_000;
|
||||
|
||||
/** Large limit used for legacy full-sync operations. */
|
||||
export const FULL_SYNC_LIMIT = 10_000;
|
||||
|
||||
/** Inventory item representing a message's sync state. */
|
||||
export interface InventoryItem {
|
||||
id: string;
|
||||
ts: number;
|
||||
rc: number;
|
||||
ac?: number;
|
||||
}
|
||||
|
||||
/** Splits an array into chunks of the given size. */
|
||||
export function chunkArray<T>(items: T[], size: number): T[][] {
|
||||
const chunks: T[][] = [];
|
||||
|
||||
for (let index = 0; index < items.length; index += size) {
|
||||
chunks.push(items.slice(index, index + size));
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/** Identifies missing or stale message IDs by comparing remote items against a local map. */
|
||||
export function findMissingIds(
|
||||
remoteItems: readonly { id: string; ts: number; rc?: number; ac?: number }[],
|
||||
localMap: ReadonlyMap<string, { ts: number; rc: number; ac: number }>
|
||||
): string[] {
|
||||
const missing: string[] = [];
|
||||
|
||||
for (const item of remoteItems) {
|
||||
const local = localMap.get(item.id);
|
||||
|
||||
if (
|
||||
!local ||
|
||||
item.ts > local.ts ||
|
||||
(item.rc !== undefined && item.rc !== local.rc) ||
|
||||
(item.ac !== undefined && item.ac !== local.ac)
|
||||
) {
|
||||
missing.push(item.id);
|
||||
}
|
||||
}
|
||||
|
||||
return missing;
|
||||
}
|
||||
31
toju-app/src/app/domains/chat/domain/message.rules.ts
Normal file
31
toju-app/src/app/domains/chat/domain/message.rules.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { DELETED_MESSAGE_CONTENT, type Message } from '../../../shared-kernel';
|
||||
|
||||
/** Extracts the effective timestamp from a message (editedAt takes priority). */
|
||||
export function getMessageTimestamp(msg: Message): number {
|
||||
return msg.editedAt || msg.timestamp || 0;
|
||||
}
|
||||
|
||||
/** Computes the most recent timestamp across a batch of messages. */
|
||||
export function getLatestTimestamp(messages: Message[]): number {
|
||||
return messages.reduce(
|
||||
(max, msg) => Math.max(max, getMessageTimestamp(msg)),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
/** Strips sensitive content from a deleted message. */
|
||||
export function normaliseDeletedMessage(message: Message): Message {
|
||||
if (!message.isDeleted)
|
||||
return message;
|
||||
|
||||
return {
|
||||
...message,
|
||||
content: DELETED_MESSAGE_CONTENT,
|
||||
reactions: []
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether the given user is allowed to edit this message. */
|
||||
export function canEditMessage(message: Message, userId: string): boolean {
|
||||
return message.senderId === userId;
|
||||
}
|
||||
Reference in New Issue
Block a user