Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update reply contexts with latest versions #5101

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,12 @@ import { offlineStore } from "./stores/network";
import { messageFiltersStore, type MessageFilter } from "./stores/messageFilters";
import { draftMessagesStore } from "./stores/draftMessages";
import { locale } from "svelte-i18n";
import { disableLinksInText, extractDisabledLinks, extractEnabledLinks, stripLinkDisabledMarker } from "./utils/linkPreviews";
import {
disableLinksInText,
extractDisabledLinks,
extractEnabledLinks,
stripLinkDisabledMarker,
} from "./utils/linkPreviews";

const UPGRADE_POLL_INTERVAL = 1000;
const MARK_ONLINE_INTERVAL = 61 * 1000;
Expand Down Expand Up @@ -3072,7 +3077,7 @@ export class OpenChat extends OpenChatAgentWorker {

if (threadRootMessageIndex === undefined) {
chatStateStore.updateProp(chatId, "serverEvents", (events) =>
mergeServerEvents(events, newEvents),
mergeServerEvents(events, newEvents, context),
);
const selectedThreadRootMessageIndex = this._liveState.selectedThreadRootMessageIndex;
if (selectedThreadRootMessageIndex !== undefined) {
Expand All @@ -3091,7 +3096,9 @@ export class OpenChat extends OpenChatAgentWorker {
}
}
} else if (messageContextsEqual(context, this._liveState.selectedMessageContext)) {
threadServerEventsStore.update((events) => mergeServerEvents(events, newEvents));
threadServerEventsStore.update((events) =>
mergeServerEvents(events, newEvents, context),
);
}

if (expiredEventRanges.length > 0) {
Expand Down Expand Up @@ -3588,7 +3595,11 @@ export class OpenChat extends OpenChatAgentWorker {
return Promise.resolve(false);
}

hideLinkPreview(messageContext: MessageContext, event: EventWrapper<Message>, link: string): Promise<boolean> {
hideLinkPreview(
messageContext: MessageContext,
event: EventWrapper<Message>,
link: string,
): Promise<boolean> {
if (event.event.content.kind !== "text_content") {
return Promise.resolve(false);
}
Expand Down
39 changes: 38 additions & 1 deletion frontend/openchat-client/src/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ import {
defaultOptionalChatPermissions,
getContentAsFormattedText,
getContentAsText,
messageContextsEqual,
} from "openchat-shared";
import { distinctBy, groupWhile } from "../utils/list";
import { distinctBy, groupWhile, toRecordFiltered } from "../utils/list";
import { areOnSameDay } from "../utils/date";
import { v1 as uuidv1 } from "uuid";
import DRange from "drange";
Expand Down Expand Up @@ -775,12 +776,48 @@ export function containsReaction(userId: string, reaction: string, reactions: Re
export function mergeServerEvents(
events: EventWrapper<ChatEvent>[],
newEvents: EventWrapper<ChatEvent>[],
messageContext: MessageContext,
): EventWrapper<ChatEvent>[] {
updateReplyContexts(events, newEvents, messageContext);

const merged = distinctBy([...newEvents, ...events], (e) => e.index);
merged.sort(sortByTimestampThenEventIndex);
return merged;
}

function updateReplyContexts(
events: EventWrapper<ChatEvent>[],
newEvents: EventWrapper<ChatEvent>[],
messageContext: MessageContext,
) {
if (events.length == 0) return;

const lookup = toRecordFiltered(
newEvents,
(e) => e.index,
(e) => e,
(e) => e.event.kind === "message",
);

for (const event of events) {
if (
event.event.kind === "message" &&
event.event.repliesTo?.kind === "rehydrated_reply_context" &&
(event.event.repliesTo.sourceContext === undefined ||
messageContextsEqual(event.event.repliesTo.sourceContext, messageContext))
) {
const updated = lookup[event.event.repliesTo.eventIndex];
if (updated?.event.kind === "message") {
event.event.repliesTo = {
...event.event.repliesTo,
content: updated.event.content,
edited: updated.event.edited,
};
}
}
}
}

function sortByTimestampThenEventIndex(
a: EventWrapper<ChatEvent>,
b: EventWrapper<ChatEvent>,
Expand Down
Loading