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

trying to fix threads #4917

Merged
merged 3 commits into from
Dec 4, 2023
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
24 changes: 12 additions & 12 deletions frontend/app/src/components/home/RightPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
}

function onChangeGroupRole(
ev: CustomEvent<{ userId: string; newRole: MemberRole; oldRole: MemberRole }>
ev: CustomEvent<{ userId: string; newRole: MemberRole; oldRole: MemberRole }>,
): void {
if (
$selectedChatId !== undefined &&
Expand All @@ -79,7 +79,7 @@
}

function onChangeCommunityRole(
ev: CustomEvent<{ userId: string; newRole: MemberRole; oldRole: MemberRole }>
ev: CustomEvent<{ userId: string; newRole: MemberRole; oldRole: MemberRole }>,
): void {
if ($selectedCommunity !== undefined) {
const { userId, newRole, oldRole } = ev.detail;
Expand Down Expand Up @@ -150,16 +150,16 @@
interpolateLevel(
"group.inviteUsersFailed",
$multiUserChat.level,
true
)
true,
),
);
break;
}
})
.catch((err) => {
client.logError("InviteUsersFailed", err);
toastStore.showFailureToast(
interpolateLevel("group.inviteUsersFailed", $multiUserChat.level, true)
interpolateLevel("group.inviteUsersFailed", $multiUserChat.level, true),
);
});

Expand Down Expand Up @@ -192,7 +192,7 @@

function findMessage(
events: EventWrapper<ChatEvent>[],
messageId: bigint
messageId: bigint,
): EventWrapper<Message> | undefined {
return events.find((e) => {
return e.event.kind === "message" && e.event.messageId === messageId;
Expand All @@ -203,7 +203,7 @@
chatId: MultiUserChatIdentifier,
userId: string,
newRole: MemberRole,
oldRole: MemberRole
oldRole: MemberRole,
): Promise<void> {
// Call backend to changeRole
return client.changeRole(chatId, userId, newRole, oldRole).then((success) => {
Expand All @@ -222,7 +222,7 @@
id: CommunityIdentifier,
userId: string,
newRole: MemberRole,
oldRole: MemberRole
oldRole: MemberRole,
) {
return client.changeCommunityRole(id, userId, newRole, oldRole).then((success) => {
if (!success) {
Expand Down Expand Up @@ -282,7 +282,7 @@
if ($selectedCommunity !== undefined) {
const success = await client.blockCommunityUser(
$selectedCommunity.id,
ev.detail.userId
ev.detail.userId,
);
if (success) {
toastStore.showSuccessToast("blockUserSucceeded");
Expand Down Expand Up @@ -310,7 +310,7 @@
if ($selectedCommunity !== undefined) {
const success = await client.unblockCommunityUser(
$selectedCommunity.id,
ev.detail.userId
ev.detail.userId,
);
if (success) {
toastStore.showSuccessToast("unblockUserSucceeded");
Expand Down Expand Up @@ -339,8 +339,8 @@
lastState.kind === "invite_community_users"
? "community"
: $selectedChat?.kind === "channel"
? "channel"
: "group"
? "channel"
: "group"
) as Level;
</script>

Expand Down
4 changes: 0 additions & 4 deletions frontend/openchat-client/src/liveState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
DirectChatSummary,
EnhancedReplyContext,
EventWrapper,
Message,
MessageContext,
ThreadSyncDetails,
UserLookup,
Expand Down Expand Up @@ -43,7 +42,6 @@ import {
chatsLoading,
uninitializedDirectChats,
confirmedThreadEventIndexesLoadedStore,
selectedThreadRootEvent,
selectedMessageContext,
allChats,
currentChatMembers,
Expand Down Expand Up @@ -94,7 +92,6 @@ export class LiveState {
focusThreadMessageIndex: number | undefined;
threadEvents!: EventWrapper<ChatEvent>[];
selectedMessageContext: MessageContext | undefined;
selectedThreadRootEvent: EventWrapper<Message> | undefined;
threadsFollowedByMe!: ChatMap<Set<number>>;
currentChatMembers!: Member[];
currentChatRules!: VersionedRules | undefined;
Expand Down Expand Up @@ -152,7 +149,6 @@ export class LiveState {
focusThreadMessageIndex.subscribe((data) => (this.focusThreadMessageIndex = data));
threadEvents.subscribe((data) => (this.threadEvents = data));
selectedMessageContext.subscribe((data) => (this.selectedMessageContext = data));
selectedThreadRootEvent.subscribe((data) => (this.selectedThreadRootEvent = data));
threadsFollowedByMeStore.subscribe((data) => (this.threadsFollowedByMe = data));
currentChatMembers.subscribe((data) => (this.currentChatMembers = data));
currentChatRules.subscribe((data) => (this.currentChatRules = data));
Expand Down
51 changes: 33 additions & 18 deletions frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ import {
removeGroupPreview,
groupPreviewsStore,
isContiguous,
selectedThreadRootEvent,
confirmedThreadEventIndexesLoadedStore,
isContiguousInThread,
focusThreadMessageIndex,
Expand Down Expand Up @@ -2243,24 +2242,40 @@ export class OpenChat extends OpenChatAgentWorker {

openThread(threadRootEvent: EventWrapper<Message>, initiating: boolean): void {
this.clearThreadEvents();
selectedThreadRootEvent.set(threadRootEvent);
if (!initiating && this._liveState.selectedChatId !== undefined) {
if (this._liveState.focusThreadMessageIndex !== undefined) {
this.loadEventWindow(
this._liveState.selectedChatId,
this._liveState.focusThreadMessageIndex,
threadRootEvent,
true,
);
} else {
this.loadPreviousMessages(this._liveState.selectedChatId, threadRootEvent, true);
selectedMessageContext.update((context) => {
if (context) {
return {
...context,
threadRootMessageIndex: threadRootEvent.event.messageIndex,
};
}
return context;
});

const context = this._liveState.selectedMessageContext;
if (context) {
if (!initiating) {
if (this._liveState.focusThreadMessageIndex !== undefined) {
this.loadEventWindow(
context.chatId,
this._liveState.focusThreadMessageIndex,
threadRootEvent,
true,
);
} else {
this.loadPreviousMessages(context.chatId, threadRootEvent, true);
}
}
this.dispatchEvent(new ThreadSelected(threadRootEvent, initiating));
}
this.dispatchEvent(new ThreadSelected(threadRootEvent, initiating));
}

closeThread(): void {
selectedThreadRootEvent.set(undefined);
selectedMessageContext.update((context) => {
if (context) {
return { chatId: context.chatId };
}
});
this.dispatchEvent(new ThreadClosed());
}

Expand Down Expand Up @@ -2690,15 +2705,17 @@ export class OpenChat extends OpenChatAgentWorker {
): Promise<void> {
const confirmedLoaded = confirmedEventIndexesLoaded(serverChat.id);
const confirmedThreadLoaded = this._liveState.confirmedThreadEventIndexesLoaded;
const selectedThreadRootEvent = this._liveState.selectedThreadRootEvent;
const selectedThreadRootMessageIndex = selectedThreadRootEvent?.event?.messageIndex;
const selectedThreadRootMessageIndex =
hpeebles marked this conversation as resolved.
Show resolved Hide resolved
this._liveState.selectedMessageContext?.threadRootMessageIndex;
const selectedChatId = this._liveState.selectedChatId;

// Partition the updated events into those that belong to the currently selected thread and those that don't
const [currentChatEvents, currentThreadEvents] = updatedEvents.reduce(
([chat, thread], e) => {
if (e.threadRootMessageIndex !== undefined) {
if (
e.threadRootMessageIndex === selectedThreadRootMessageIndex &&
chatIdentifiersEqual(serverChat.id, selectedChatId) &&
indexIsInRanges(e.eventIndex, confirmedThreadLoaded)
) {
thread.push(e.eventIndex);
Expand Down Expand Up @@ -2957,7 +2974,6 @@ export class OpenChat extends OpenChatAgentWorker {
e.event.messageIndex === selectedThreadRootMessageIndex,
);
if (threadRootEvent !== undefined) {
selectedThreadRootEvent.set(threadRootEvent as EventWrapper<Message>);
this.dispatchEvent(
new ChatUpdated({
chatId,
Expand Down Expand Up @@ -5812,7 +5828,6 @@ export class OpenChat extends OpenChatAgentWorker {
isLifetimeDiamond = isLifetimeDiamond;
canExtendDiamond = canExtendDiamond;
diamondStatus = diamondStatus;
selectedThreadRootEvent = selectedThreadRootEvent;
selectedThreadRootMessageIndex = selectedThreadRootMessageIndex;
selectedMessageContext = selectedMessageContext;
userGroupSummaries = userGroupSummaries;
Expand Down
38 changes: 16 additions & 22 deletions frontend/openchat-client/src/stores/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
ChatListScope,
AttachmentContent,
ExpiredEventsRange,
MessageContext,
} from "openchat-shared";
import {
compareChats,
Expand All @@ -23,6 +24,7 @@ import {
nullMembership,
chatIdentifiersEqual,
isAttachmentContent,
messageContextsEqual,
} from "openchat-shared";
import { unconfirmed } from "./unconfirmed";
import { derived, get, type Readable, writable, type Writable } from "svelte/store";
Expand Down Expand Up @@ -63,11 +65,19 @@ import { translationStore } from "./translation";
let currentScope: ChatListScope = { kind: "direct_chat" };
chatListScopeStore.subscribe((s) => (currentScope = s));

export const selectedChatId = safeWritable<ChatIdentifier | undefined>(
export const selectedMessageContext = safeWritable<MessageContext | undefined>(
undefined,
chatIdentifiersEqual,
messageContextsEqual,
);

export const selectedThreadRootMessageIndex = derived(selectedMessageContext, ($messageContext) => {
return $messageContext?.threadRootMessageIndex;
});

export const selectedChatId = derived(selectedMessageContext, ($messageContext) => {
return $messageContext?.chatId;
});

export const chatStateStore = createChatSpecificObjectStore<ChatSpecificState>(
selectedChatId,
() => ({
Expand Down Expand Up @@ -313,22 +323,6 @@ export const userMetrics = derived([allServerChats], ([$chats]) => {
.reduce(mergeChatMetrics, emptyChatMetrics());
});

export const selectedThreadRootEvent = writable<EventWrapper<Message> | undefined>(undefined);
export const selectedThreadRootMessageIndex = derived(selectedThreadRootEvent, ($rootEvent) => {
return $rootEvent !== undefined ? $rootEvent.event.messageIndex : undefined;
});
export const selectedMessageContext = derived(
[selectedChatId, selectedThreadRootMessageIndex],
([$selectedChatId, $selectedThreadRootMessageIndex]) => {
if ($selectedChatId !== undefined) {
return {
chatId: $selectedChatId,
threadRootMessageIndex: $selectedThreadRootMessageIndex,
};
}
return undefined;
},
);
export const chatsLoading = writable(true);
export const chatsInitialised = writable(false);

Expand Down Expand Up @@ -585,11 +579,11 @@ export function setSelectedChat(

export function clearSelectedChat(newSelectedChatId?: ChatIdentifier): void {
filteredProposalsStore.set(undefined);
selectedChatId.update((chatId) => {
if (chatId !== undefined) {
chatStateStore.clear(chatId);
selectedMessageContext.update((context) => {
if (context !== undefined) {
chatStateStore.clear(context.chatId);
}
return newSelectedChatId;
return newSelectedChatId ? { chatId: newSelectedChatId } : undefined;
});
}

Expand Down
Loading