Skip to content

Commit 60e7dbe

Browse files
committed
checkpoint
1 parent 8ac1bb4 commit 60e7dbe

File tree

6 files changed

+75
-50
lines changed

6 files changed

+75
-50
lines changed
Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { generateGroupHashFromMemberIds } from "@/features/create-conversation/generate-group-hash-from-member-ids";
22
import { getConversationsQueryOptions } from "@/queries/use-conversations-query";
3+
import { getGroupMembersQueryData } from "@/queries/useGroupMembersQuery";
4+
import { getCleanAddress } from "@/utils/evm/getCleanAddress";
35
import logger from "@/utils/logger";
46
import {
57
getCurrentAccount,
@@ -22,58 +24,77 @@ export const useConversationsCount = () => {
2224
return { count, isLoading };
2325
};
2426

25-
export const useFindConversationByMembers = (members: InboxId[]) => {
27+
export const useFindConversationByMembers = (membersToSearch: InboxId[]) => {
2628
const account = useCurrentAccount();
2729

2830
logger.debug(
2931
`[useFindConversationByMembers] Finding conversation for members:`,
30-
members
32+
membersToSearch
3133
);
3234

33-
const membersHash = generateGroupHashFromMemberIds([
34-
...members,
35-
getCurrentAccount()!,
36-
]);
37-
38-
logger.debug(
39-
`[useFindConversationByMembers] Generated members hash: ${membersHash}`
40-
);
41-
42-
const { data: conversation, isLoading } = useQuery({
35+
const { data: conversations, isLoading } = useQuery({
4336
...getConversationsQueryOptions({
4437
account: account!,
4538
caller: "useConversationByMembers",
4639
}),
47-
select: (data) => {
40+
select: (conversations) => {
4841
logger.debug(
49-
`[useFindConversationByMembers] Searching through ${data?.length} conversations`
42+
`[useFindConversationByMembers] Searching through ${conversations?.length} conversations`
5043
);
5144

52-
const found = data?.find((c) => {
53-
const matches = c.membersHash === membersHash;
45+
const groupsThatIncludeMembers = conversations?.filter((c) => {
46+
logger.debug(
47+
`[useFindConversationByMembers] Checking conversation ${c.topic}`
48+
);
49+
50+
const groupMembers = getGroupMembersQueryData(account!, c.topic);
5451
logger.debug(
55-
`[useFindConversationByMembers] Checking conversation ${c.topic}:`,
56-
`membersHash=${c.membersHash}`,
57-
`matches=${matches}`
52+
`[useFindConversationByMembers] Group members for ${c.topic}:`,
53+
groupMembers?.ids
54+
);
55+
56+
const membersIdsSet = new Set(
57+
groupMembers?.addresses.map((address) => address.toLowerCase()) ?? []
58+
);
59+
const membersToSearchSet = new Set(
60+
membersToSearch.map((member) => member.toLowerCase())
5861
);
59-
return matches;
62+
63+
logger.debug(
64+
`[useFindConversationByMembers] Checking if\n${Array.from(
65+
membersToSearchSet
66+
)}\n is subset of\n${Array.from(membersIdsSet).join("\n")}`
67+
);
68+
69+
// const areMembersToSearchAllInGroup =
70+
// membersToSearchSet.isSubsetOf(membersIdsSet);
71+
// write brute force subset check
72+
const areMembersToSearchAllInGroup = membersToSearch.every((member) =>
73+
membersIdsSet.has(member.toLowerCase())
74+
);
75+
76+
logger.debug(
77+
`[useFindConversationByMembers] Members are ${
78+
areMembersToSearchAllInGroup ? "" : "not "
79+
}all in group ${c.topic}`
80+
);
81+
82+
return areMembersToSearchAllInGroup;
6083
});
6184

6285
logger.debug(
63-
`[useFindConversationByMembers] Found conversation:`,
64-
found ? found.topic : "none"
86+
`[useFindConversationByMembers] Found ${groupsThatIncludeMembers?.length} conversations that include members`
6587
);
6688

67-
return found;
89+
return groupsThatIncludeMembers;
6890
},
69-
enabled: !!membersHash,
91+
enabled: !!membersToSearch && membersToSearch.length > 0,
7092
});
7193

7294
logger.debug(
7395
`[useFindConversationByMembers] Returning:`,
74-
`conversation=${conversation?.topic ?? "nope!"}`,
7596
`isLoading=${isLoading}`
7697
);
7798

78-
return { conversation, isLoading };
99+
return { conversations, isLoading };
79100
};

features/create-conversation/create-conversation-screen.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ export function CreateConversationScreen({
4646
}>
4747
>([]);
4848
const {
49-
conversation: existingConversation,
49+
conversations: existingConversations,
5050
isLoading: isLoadingExistingConversation,
5151
} = useFindConversationByMembers(selectedUsers.map((u) => u.address));
52-
logJson({ json: existingConversation, msg: "existingConversation" });
52+
logJson({ json: existingConversations, msg: "existingConversations" });
5353

5454
const selectedUsersCount = selectedUsers.length;
5555
const composerDisabled = selectedUsersCount === 0;
@@ -205,14 +205,18 @@ export function CreateConversationScreen({
205205
</View>
206206
)}
207207

208-
{existingConversation && (
208+
{existingConversations && (
209209
<MessageSection
210-
message={`Conversation already exists with ${existingConversation.topic}`}
210+
message={`${
211+
existingConversations.length
212+
} conversations already exist with ${selectedUsers
213+
.map((u) => u.name)
214+
.join(", ")}`}
211215
isError={false}
212216
/>
213217
)}
214218

215-
{!existingConversation && !isLoadingExistingConversation && (
219+
{!existingConversations && !isLoadingExistingConversation && (
216220
<MessageSection
217221
message={"Conversation does not exist with those members"}
218222
isError={false}

queries/entify.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type EntityObjectWithAddress<T, KeyType extends string = string> = {
5656
byId: Record<KeyType, T>;
5757
byAddress: Record<string, KeyType>;
5858
ids: KeyType[];
59+
addresses: string[];
5960
};
6061

6162
/**
@@ -99,6 +100,7 @@ export function entifyWithAddress<T, KeyType extends string = string>(
99100
const id = idFunc(item);
100101
const address = addressFunc(item);
101102
acc.ids.push(id);
103+
acc.addresses.push(address);
102104
acc.byId[id] = item;
103105
acc.byAddress[address] = id;
104106
return acc;
@@ -107,6 +109,7 @@ export function entifyWithAddress<T, KeyType extends string = string>(
107109
byId: {} as Record<KeyType, T>,
108110
byAddress: {},
109111
ids: [],
112+
addresses: [],
110113
}
111114
);
112115
}

queries/useGroupMembersQuery.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { entifyWithAddress, EntityObjectWithAddress } from "./entify";
88
import { queryClient } from "./queryClient";
99
import { groupMembersQueryKey } from "./QueryKeys";
1010
import { getOrFetchGroupQuery } from "./useGroupQuery";
11+
import logger from "@utils/logger";
1112

1213
export type GroupMembersSelectData = EntityObjectWithAddress<Member, InboxId>;
1314

@@ -16,6 +17,11 @@ const fetchGroupMembers = async (args: {
1617
topic: ConversationTopic;
1718
}): Promise<EntityObjectWithAddress<Member, InboxId>> => {
1819
const { account, topic } = args;
20+
logger.debug("[fetchGroupMembers] Fetching group members", {
21+
account,
22+
topic,
23+
});
24+
1925
const group = await getOrFetchGroupQuery({
2026
account,
2127
topic,
@@ -28,6 +34,11 @@ const fetchGroupMembers = async (args: {
2834

2935
const members = await group.members();
3036

37+
logger.debug("[fetchGroupMembers] Successfully fetched members", {
38+
topic,
39+
memberCount: members.length,
40+
});
41+
3142
return entifyWithAddress(
3243
members,
3344
(member) => member.inboxId,
@@ -116,10 +127,10 @@ export async function ensureGroupMembersQueryData(args: {
116127
topic: ConversationTopic;
117128
}) {
118129
const { account, topic } = args;
119-
return queryClient.ensureQueryData({
120-
queryKey: getGroupMemberQueryOptions({
130+
return queryClient.ensureQueryData(
131+
getGroupMemberQueryOptions({
121132
account,
122133
topic,
123-
}).queryKey,
124-
});
134+
})
135+
);
125136
}

queries/useGroupQuery.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,6 @@ export function getGroupQueryOptions(args: {
7070
if (!data) {
7171
return null;
7272
}
73-
/**
74-
* Type error: Group<SupportedCodecsType> | Dm<SupportedCodecsType> is not
75-
* assignable to ConversationWithCodecsType. Group<SupportedCodecsType> is
76-
* missing resolvedMembers and membersHash properties.
77-
*
78-
* note(lustig) I really think we should get away from leaking xmtp types into our codebase.
79-
* these generics are starting to be very annoying and counter productive.
80-
*/
81-
// @ts-expect-error TS2345
8273
if (!isConversationGroup(data)) {
8374
throw new Error(
8475
"Expected group conversation but received different type"

utils/xmtpRN/client.types.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ export type SupportedCodecsType = [
3030

3131
export type ConverseXmtpClientType = Client<SupportedCodecsType>;
3232

33-
// export type ConversationWithCodecsType = Conversation<SupportedCodecsType>;
34-
35-
export type ConversationWithCodecsType = Conversation<SupportedCodecsType> & {
36-
resolvedMembers: Member[];
37-
membersHash: string;
38-
};
33+
export type ConversationWithCodecsType = Conversation<SupportedCodecsType>;
3934

4035
export type DmWithCodecsType = Dm<SupportedCodecsType>;
4136

0 commit comments

Comments
 (0)