Skip to content

Commit

Permalink
Merge branch 'master' into hindi
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jan 3, 2024
2 parents 4a0b826 + c2fe974 commit 93321db
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
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

0 comments on commit 93321db

Please sign in to comment.