Fix the mechanical style rules. [skip build]
This commit is contained in:
+119
-108
@@ -44,6 +44,34 @@ function isForumRootContent(content: Record<string, unknown>): boolean {
|
||||
return typeof formatted === 'string' && /<h1[\s>]/i.test(formatted);
|
||||
}
|
||||
|
||||
function postTitleFromContent(content: Record<string, unknown>): { title: string; body: string } {
|
||||
const rawBody = typeof content.body === 'string' ? content.body : '';
|
||||
const customTitle =
|
||||
typeof content['com.matrixsso.title'] === 'string'
|
||||
? String(content['com.matrixsso.title']).trim()
|
||||
: '';
|
||||
const htmlTitle =
|
||||
typeof content.formatted_body === 'string'
|
||||
? titleFromFormattedBody(content.formatted_body)
|
||||
: undefined;
|
||||
const firstLine = rawBody.split(/\r?\n/, 1)[0]?.trim() ?? '';
|
||||
const title = (customTitle || htmlTitle || firstLine || '(untitled post)').slice(0, 160);
|
||||
|
||||
let body = rawBody;
|
||||
if (customTitle && rawBody.startsWith(customTitle)) {
|
||||
body = rawBody.slice(customTitle.length).replace(/^\s+/, '');
|
||||
} else {
|
||||
const [leadingBlock, ...restBlocks] = rawBody.split(/\r?\n\r?\n/);
|
||||
if (restBlocks.length > 0 && leadingBlock.trim() === firstLine) {
|
||||
body = restBlocks.join('\n\n').trim();
|
||||
} else if (firstLine && rawBody.startsWith(firstLine)) {
|
||||
body = rawBody.slice(firstLine.length).replace(/^\s+/, '');
|
||||
}
|
||||
}
|
||||
|
||||
return { title, body: body || rawBody };
|
||||
}
|
||||
|
||||
function syncForumRootContentAfterEdit(
|
||||
merged: Record<string, unknown>,
|
||||
baseContent: Record<string, unknown>,
|
||||
@@ -109,34 +137,6 @@ function parseBundledThreadReplyCount(event: IRoomEvent): number | undefined {
|
||||
return Math.floor(count);
|
||||
}
|
||||
|
||||
function postTitleFromContent(content: Record<string, unknown>): { title: string; body: string } {
|
||||
const rawBody = typeof content.body === 'string' ? content.body : '';
|
||||
const customTitle =
|
||||
typeof content['com.matrixsso.title'] === 'string'
|
||||
? String(content['com.matrixsso.title']).trim()
|
||||
: '';
|
||||
const htmlTitle =
|
||||
typeof content.formatted_body === 'string'
|
||||
? titleFromFormattedBody(content.formatted_body)
|
||||
: undefined;
|
||||
const firstLine = rawBody.split(/\r?\n/, 1)[0]?.trim() ?? '';
|
||||
const title = (customTitle || htmlTitle || firstLine || '(untitled post)').slice(0, 160);
|
||||
|
||||
let body = rawBody;
|
||||
if (customTitle && rawBody.startsWith(customTitle)) {
|
||||
body = rawBody.slice(customTitle.length).replace(/^\s+/, '');
|
||||
} else {
|
||||
const [leadingBlock, ...restBlocks] = rawBody.split(/\r?\n\r?\n/);
|
||||
if (restBlocks.length > 0 && leadingBlock.trim() === firstLine) {
|
||||
body = restBlocks.join('\n\n').trim();
|
||||
} else if (firstLine && rawBody.startsWith(firstLine)) {
|
||||
body = rawBody.slice(firstLine.length).replace(/^\s+/, '');
|
||||
}
|
||||
}
|
||||
|
||||
return { title, body: body || rawBody };
|
||||
}
|
||||
|
||||
/** Title + body for a forum matrix message payload (including edits). */
|
||||
export function forumPostTitleFromContent(content: Record<string, unknown>): string {
|
||||
return postTitleFromContent(content).title;
|
||||
@@ -144,9 +144,27 @@ export function forumPostTitleFromContent(content: Record<string, unknown>): str
|
||||
|
||||
function sortRepliesRecursively(posts: ForumPost[]) {
|
||||
posts.sort((a, b) => a.timestamp - b.timestamp);
|
||||
for (const post of posts) {
|
||||
posts.forEach((post) => {
|
||||
if (post.replies.length > 0) sortRepliesRecursively(post.replies);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function mergeRootPosts(existing: ForumPost[], incoming: ForumPost[]): ForumPost[] {
|
||||
const byId = new Map(existing.map((post) => [post.eventId, post]));
|
||||
incoming.forEach((post) => {
|
||||
const previous = byId.get(post.eventId);
|
||||
if (!previous) {
|
||||
byId.set(post.eventId, post);
|
||||
return;
|
||||
}
|
||||
const replyIds = new Set(previous.replies.map((reply) => reply.eventId));
|
||||
const mergedReplies = [
|
||||
...previous.replies,
|
||||
...post.replies.filter((reply) => !replyIds.has(reply.eventId)),
|
||||
];
|
||||
byId.set(post.eventId, { ...post, replies: mergedReplies });
|
||||
});
|
||||
return [...byId.values()].sort((a, b) => b.timestamp - a.timestamp);
|
||||
}
|
||||
|
||||
function buildPosts(mx: MatrixClient, roomId: string, timelineEvents: IRoomEvent[]): ForumPost[] {
|
||||
@@ -202,7 +220,7 @@ function buildPosts(mx: MatrixClient, roomId: string, timelineEvents: IRoomEvent
|
||||
.sort((a, b) => a.timestamp - b.timestamp);
|
||||
|
||||
const postsById = new Map<string, ForumPost>();
|
||||
for (const event of normalized) {
|
||||
normalized.forEach((event) => {
|
||||
postsById.set(event.eventId, {
|
||||
eventId: event.eventId,
|
||||
title: event.title,
|
||||
@@ -214,14 +232,14 @@ function buildPosts(mx: MatrixClient, roomId: string, timelineEvents: IRoomEvent
|
||||
bundledReplyCount: event.bundledReplyCount,
|
||||
replies: [],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const roots: ForumPost[] = [];
|
||||
const placedReplyIds = new Set<string>();
|
||||
|
||||
for (const event of normalized) {
|
||||
normalized.forEach((event) => {
|
||||
const current = postsById.get(event.eventId);
|
||||
if (!current) continue;
|
||||
if (!current) return;
|
||||
|
||||
const { relation } = event;
|
||||
let threadRootId: string | null = null;
|
||||
@@ -236,10 +254,10 @@ function buildPosts(mx: MatrixClient, roomId: string, timelineEvents: IRoomEvent
|
||||
|
||||
if (!threadRootId) {
|
||||
roots.push(current);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
if (placedReplyIds.has(event.eventId)) continue;
|
||||
if (placedReplyIds.has(event.eventId)) return;
|
||||
|
||||
if (parentEventId && parentEventId !== threadRootId) {
|
||||
const parent = postsById.get(parentEventId);
|
||||
@@ -247,7 +265,7 @@ function buildPosts(mx: MatrixClient, roomId: string, timelineEvents: IRoomEvent
|
||||
current.replyToEventId = parentEventId;
|
||||
parent.replies.push(current);
|
||||
placedReplyIds.add(event.eventId);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
current.replyToEventId = parentEventId;
|
||||
@@ -257,7 +275,7 @@ function buildPosts(mx: MatrixClient, roomId: string, timelineEvents: IRoomEvent
|
||||
threadRoot.replies.push(current);
|
||||
placedReplyIds.add(event.eventId);
|
||||
}
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
const threadRoot = postsById.get(threadRootId);
|
||||
@@ -265,7 +283,7 @@ function buildPosts(mx: MatrixClient, roomId: string, timelineEvents: IRoomEvent
|
||||
threadRoot.replies.push(current);
|
||||
placedReplyIds.add(event.eventId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
sortRepliesRecursively(roots);
|
||||
return roots.sort((a, b) => b.timestamp - a.timestamp);
|
||||
@@ -285,14 +303,13 @@ export async function listForumTopics(
|
||||
}
|
||||
|
||||
const parentByRoomId = new Map<string, string>();
|
||||
for (const room of rooms) {
|
||||
if (!room?.room_id || !Array.isArray(room.children_state)) continue;
|
||||
for (const childState of room.children_state) {
|
||||
if (childState?.type !== 'm.space.child' || typeof childState.state_key !== 'string')
|
||||
continue;
|
||||
rooms.forEach((room) => {
|
||||
if (!room?.room_id || !Array.isArray(room.children_state)) return;
|
||||
room.children_state.forEach((childState) => {
|
||||
if (childState?.type !== 'm.space.child' || typeof childState.state_key !== 'string') return;
|
||||
parentByRoomId.set(childState.state_key, room.room_id);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const fromHierarchy = rooms
|
||||
.filter((room) => room?.room_id && room.room_id !== spaceRoomId)
|
||||
@@ -321,8 +338,8 @@ export async function listForumTopics(
|
||||
const parentRoom = mx.getRoom(parentId);
|
||||
if (!parentRoom) return;
|
||||
|
||||
for (const childId of getSpaceChildren(parentRoom)) {
|
||||
if (childId === spaceRoomId || visited.has(childId)) continue;
|
||||
getSpaceChildren(parentRoom).forEach((childId) => {
|
||||
if (childId === spaceRoomId || visited.has(childId)) return;
|
||||
const child = mx.getRoom(childId);
|
||||
const name = child?.name || childId;
|
||||
const childIsSpace = isSpace(child ?? null);
|
||||
@@ -333,7 +350,7 @@ export async function listForumTopics(
|
||||
parentRoomId: parentId,
|
||||
});
|
||||
if (childIsSpace) collectFromSpace(childId, depth + 1);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
collectFromSpace(spaceRoomId, 0);
|
||||
@@ -371,35 +388,17 @@ export async function listTopicFeedPosts(
|
||||
return { posts: merged, nextBatch: nextBatch ?? null };
|
||||
}
|
||||
|
||||
function mergeRootPosts(existing: ForumPost[], incoming: ForumPost[]): ForumPost[] {
|
||||
const byId = new Map(existing.map((post) => [post.eventId, post]));
|
||||
for (const post of incoming) {
|
||||
const previous = byId.get(post.eventId);
|
||||
if (!previous) {
|
||||
byId.set(post.eventId, post);
|
||||
continue;
|
||||
}
|
||||
const replyIds = new Set(previous.replies.map((reply) => reply.eventId));
|
||||
const mergedReplies = [
|
||||
...previous.replies,
|
||||
...post.replies.filter((reply) => !replyIds.has(reply.eventId)),
|
||||
];
|
||||
byId.set(post.eventId, { ...post, replies: mergedReplies });
|
||||
}
|
||||
return [...byId.values()].sort((a, b) => b.timestamp - a.timestamp);
|
||||
}
|
||||
|
||||
function mergeUniqueRoomEvents(...lists: IRoomEvent[][]): IRoomEvent[] {
|
||||
const seen = new Set<string>();
|
||||
const unique: IRoomEvent[] = [];
|
||||
for (const list of lists) {
|
||||
for (const event of list) {
|
||||
lists.forEach((list) => {
|
||||
list.forEach((event) => {
|
||||
const id = event.event_id;
|
||||
if (!id || seen.has(id)) continue;
|
||||
if (!id || seen.has(id)) return;
|
||||
seen.add(id);
|
||||
unique.push(event);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
return unique;
|
||||
}
|
||||
|
||||
@@ -437,6 +436,29 @@ function matrixEventToRoomEvent(mEvent: MatrixEvent): IRoomEvent {
|
||||
return mEvent.event as IRoomEvent;
|
||||
}
|
||||
|
||||
function collectLocalReplaceEdits(room: Room, targetIds: Set<string>): IRoomEvent[] {
|
||||
const events: IRoomEvent[] = [];
|
||||
const seen = new Set<string>();
|
||||
|
||||
room
|
||||
.getUnfilteredTimelineSet()
|
||||
.getLiveTimeline()
|
||||
.getEvents()
|
||||
.forEach((mEvent) => {
|
||||
if (mEvent.isRedacted() || mEvent.getType() !== EventType.RoomMessage) return;
|
||||
const relation = mEvent.getRelation();
|
||||
if (relation?.rel_type !== RelationType.Replace) return;
|
||||
const targetId = relation.event_id;
|
||||
if (!targetId || !targetIds.has(targetId)) return;
|
||||
const id = mEvent.getId();
|
||||
if (!id || seen.has(id)) return;
|
||||
seen.add(id);
|
||||
events.push(matrixEventToRoomEvent(mEvent));
|
||||
});
|
||||
|
||||
return events;
|
||||
}
|
||||
|
||||
/** Thread events already in the local room timeline (includes unsent/local echo). */
|
||||
export function collectLocalThreadEvents(
|
||||
mx: MatrixClient,
|
||||
@@ -466,13 +488,17 @@ export function collectLocalThreadEvents(
|
||||
const thread = room.getThread(rootId);
|
||||
if (thread) {
|
||||
add(thread.rootEvent);
|
||||
for (const reply of thread.events) add(reply);
|
||||
for (const reply of thread.liveTimeline?.getEvents() ?? []) add(reply);
|
||||
thread.events.forEach((reply) => add(reply));
|
||||
(thread.liveTimeline?.getEvents() ?? []).forEach((reply) => add(reply));
|
||||
}
|
||||
|
||||
for (const mEvent of room.getUnfilteredTimelineSet().getLiveTimeline().getEvents()) {
|
||||
add(mEvent);
|
||||
}
|
||||
room
|
||||
.getUnfilteredTimelineSet()
|
||||
.getLiveTimeline()
|
||||
.getEvents()
|
||||
.forEach((mEvent) => {
|
||||
add(mEvent);
|
||||
});
|
||||
|
||||
const targetIds = new Set(seen);
|
||||
events.push(...collectLocalReplaceEdits(room, targetIds));
|
||||
@@ -480,25 +506,6 @@ export function collectLocalThreadEvents(
|
||||
return events;
|
||||
}
|
||||
|
||||
function collectLocalReplaceEdits(room: Room, targetIds: Set<string>): IRoomEvent[] {
|
||||
const events: IRoomEvent[] = [];
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (const mEvent of room.getUnfilteredTimelineSet().getLiveTimeline().getEvents()) {
|
||||
if (mEvent.isRedacted() || mEvent.getType() !== EventType.RoomMessage) continue;
|
||||
const relation = mEvent.getRelation();
|
||||
if (relation?.rel_type !== RelationType.Replace) continue;
|
||||
const targetId = relation.event_id;
|
||||
if (!targetId || !targetIds.has(targetId)) continue;
|
||||
const id = mEvent.getId();
|
||||
if (!id || seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
events.push(matrixEventToRoomEvent(mEvent));
|
||||
}
|
||||
|
||||
return events;
|
||||
}
|
||||
|
||||
function collectLocalRootPosts(mx: MatrixClient, roomId: string): IRoomEvent[] {
|
||||
const room = mx.getRoom(roomId);
|
||||
if (!room) return [];
|
||||
@@ -506,18 +513,22 @@ function collectLocalRootPosts(mx: MatrixClient, roomId: string): IRoomEvent[] {
|
||||
const seen = new Set<string>();
|
||||
const events: IRoomEvent[] = [];
|
||||
|
||||
for (const mEvent of room.getUnfilteredTimelineSet().getLiveTimeline().getEvents()) {
|
||||
if (mEvent.isRedacted() || mEvent.getType() !== EventType.RoomMessage) continue;
|
||||
if (isSecondaryMatrixEvent(mEvent)) continue;
|
||||
const relation = threadRelationFromContent(mEvent.getContent());
|
||||
const isThreadReply =
|
||||
relation?.rel_type === RelationType.Thread || relation?.rel_type === 'm.thread';
|
||||
if (isThreadReply) continue;
|
||||
const id = mEvent.getId();
|
||||
if (!id || seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
events.push(matrixEventToRoomEvent(mEvent));
|
||||
}
|
||||
room
|
||||
.getUnfilteredTimelineSet()
|
||||
.getLiveTimeline()
|
||||
.getEvents()
|
||||
.forEach((mEvent) => {
|
||||
if (mEvent.isRedacted() || mEvent.getType() !== EventType.RoomMessage) return;
|
||||
if (isSecondaryMatrixEvent(mEvent)) return;
|
||||
const relation = threadRelationFromContent(mEvent.getContent());
|
||||
const isThreadReply =
|
||||
relation?.rel_type === RelationType.Thread || relation?.rel_type === 'm.thread';
|
||||
if (isThreadReply) return;
|
||||
const id = mEvent.getId();
|
||||
if (!id || seen.has(id)) return;
|
||||
seen.add(id);
|
||||
events.push(matrixEventToRoomEvent(mEvent));
|
||||
});
|
||||
|
||||
const rootIds = new Set(events.map((event) => event.event_id).filter(Boolean) as string[]);
|
||||
events.push(...collectLocalReplaceEdits(room, rootIds));
|
||||
|
||||
Reference in New Issue
Block a user