Skip to content

Commit 5b014c7

Browse files
committed
wip
1 parent d117227 commit 5b014c7

File tree

5 files changed

+175
-24
lines changed

5 files changed

+175
-24
lines changed

features/consent/use-allow-group.mutation.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import {
2+
addConversationToUnknownConsentConversationsQuery,
3+
removeConversationFromUnknownConsentConversationsQueryData,
4+
} from "@/queries/unknown-consent-conversations-query";
15
import { updateConversationInConversationsQueryData } from "@/queries/use-conversations-query";
2-
import { captureError } from "@/utils/capture-error";
36
import { GroupWithCodecsType } from "@/utils/xmtpRN/client.types";
47
import { queryClient } from "@queries/queryClient";
58
import {
@@ -95,6 +98,10 @@ export const getAllowGroupMutationOptions = (
9598
state: "allowed",
9699
},
97100
});
101+
removeConversationFromUnknownConsentConversationsQueryData({
102+
account,
103+
topic: group.topic,
104+
});
98105
return {
99106
previousConsent,
100107
};
@@ -108,8 +115,6 @@ export const getAllowGroupMutationOptions = (
108115
) => {
109116
const { account, group } = variables;
110117

111-
captureError(error);
112-
113118
if (!context) {
114119
return;
115120
}
@@ -126,6 +131,10 @@ export const getAllowGroupMutationOptions = (
126131
state: context.previousConsent,
127132
},
128133
});
134+
addConversationToUnknownConsentConversationsQuery({
135+
account,
136+
conversation: group,
137+
});
129138
},
130139
};
131140
};

features/consent/use-dm-consent-for-current-account.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {
22
getCurrentAccount,
33
useCurrentAccount,
44
} from "@/data/store/accountsStore";
5-
import { updateConversationInConversationsQueryData } from "@/queries/use-conversations-query";
65
import { getConversationQueryData } from "@/queries/useConversationQuery";
6+
import { updateConversationInUnknownConsentConversationsQueryData } from "@/queries/unknown-consent-conversations-query";
7+
import { updateConversationInConversationsQueryData } from "@/queries/use-conversations-query";
78
import { getDmQueryData, setDmQueryData } from "@/queries/useDmQuery";
89
import { updateObjectAndMethods } from "@/utils/update-object-and-methods";
910
import { DmWithCodecsType } from "@/utils/xmtpRN/client.types";
@@ -26,7 +27,7 @@ export function useDmConsentForCurrentAccount() {
2627
conversationId: ConversationId;
2728
topic: ConversationTopic;
2829
}) => {
29-
const { peerInboxId, conversationId, topic } = args;
30+
const { consent, peerInboxId, conversationId } = args;
3031
if (!peerInboxId) {
3132
throw new Error("Peer inbox id not found");
3233
}
@@ -35,24 +36,23 @@ export function useDmConsentForCurrentAccount() {
3536
updateConsentForGroupsForAccount({
3637
account: currentAccount,
3738
groupIds: [conversationId],
38-
consent: args.consent,
39+
consent,
3940
}),
4041
updateInboxIdsConsentForAccount({
4142
account: currentAccount,
4243
inboxIds: [peerInboxId],
43-
consent: args.consent,
44+
consent,
4445
}),
4546
]);
4647
},
47-
onMutate: (args) => {
48-
const { peerInboxId, conversationId, topic } = args;
48+
onMutate: ({ consent, topic }) => {
4949
const conversation = getConversationQueryData({
5050
account: currentAccount,
5151
topic,
5252
});
5353
if (conversation) {
5454
const updatedDm = updateObjectAndMethods(conversation, {
55-
state: args.consent === "allow" ? "allowed" : "denied",
55+
state: consent === "allow" ? "allowed" : "denied",
5656
});
5757

5858
setDmQueryData({
@@ -65,38 +65,58 @@ export function useDmConsentForCurrentAccount() {
6565
account: currentAccount,
6666
topic,
6767
conversationUpdate: {
68-
state: args.consent === "allow" ? "allowed" : "denied",
68+
state: consent === "allow" ? "allowed" : "denied",
69+
},
70+
});
71+
72+
updateConversationInUnknownConsentConversationsQueryData({
73+
account: currentAccount,
74+
topic,
75+
conversationUpdate: {
76+
state: consent === "allow" ? "allowed" : "denied",
6977
},
7078
});
79+
7180
return { previousDmConsent: conversation.state };
7281
}
7382
},
74-
onError: (error, variables, context) => {
75-
const { topic } = variables;
83+
onError: (error, { topic }, context) => {
7684
const { previousDmConsent } = context || {};
7785
if (previousDmConsent) {
7886
const dm = getDmQueryData({
7987
account: currentAccount,
8088
peer: topic,
8189
});
90+
8291
if (!dm) {
8392
return;
8493
}
94+
8595
const updatedDm = updateObjectAndMethods(dm, {
8696
state: previousDmConsent,
8797
});
98+
8899
setDmQueryData({
89100
account: currentAccount,
90101
peer: topic,
91102
dm: updatedDm,
92103
});
104+
93105
updateConversationInConversationsQueryData({
94106
account: currentAccount,
95107
topic,
96108
conversationUpdate: {
97109
state: previousDmConsent,
98110
},
99111
});
112+
113+
updateConversationInUnknownConsentConversationsQueryData({
114+
account: currentAccount,
115+
topic,
116+
conversationUpdate: {
117+
state: previousDmConsent,
118+
},
119+
});
100120
}
101121
},
102122
});

ios/Podfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,7 +2781,7 @@ EXTERNAL SOURCES:
27812781

27822782
SPEC CHECKSUMS:
27832783
AppCheckCore: cc8fd0a3a230ddd401f326489c99990b013f0c4f
2784-
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
2784+
boost: 4cb898d0bf20404aab1850c656dcea009429d6c1
27852785
CoinbaseWalletSDK: ea1f37512bbc69ebe07416e3b29bf840f5cc3152
27862786
CoinbaseWalletSDKExpo: 7488819c860868cd5fe117f6be43dd42c2e65919
27872787
ComputableLayout: c50faffac4ed9f8f05b0ce5e6f3a60df1f6042c8
@@ -2790,7 +2790,7 @@ SPEC CHECKSUMS:
27902790
CryptoSwift: 967f37cea5a3294d9cce358f78861652155be483
27912791
CSecp256k1: 2a59c03e52637ded98896a33be4b2649392cb843
27922792
DGSwiftUtilities: 2f0d35d5ff3d57bd70ccc42f15971460db202c41
2793-
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
2793+
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
27942794
EASClient: d15d70e334b019ae5649609011cda761defe4b1a
27952795
EXApplication: 88ebf1a95f85faa5d2df160016d61f2d060e9438
27962796
EXConstants: 277129d9a42ba2cf1fad375e7eaa9939005c60be
@@ -2837,8 +2837,8 @@ SPEC CHECKSUMS:
28372837
FirebaseAppCheckInterop: 347aa09a805219a31249b58fc956888e9fcb314b
28382838
FirebaseCore: e0510f1523bc0eb21653cac00792e1e2bd6f1771
28392839
FirebaseCoreInternal: d98ab91e2d80a56d7b246856a8885443b302c0c2
2840-
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
2841-
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
2840+
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
2841+
glog: 69ef571f3de08433d766d614c73a9838a06bf7eb
28422842
GoogleUtilities: 26a3abef001b6533cf678d3eb38fd3f614b7872d
28432843
hermes-engine: 06a9c6900587420b90accc394199527c64259db4
28442844
libavif: 84bbb62fb232c3018d6f1bab79beea87e35de7b7
@@ -2851,7 +2851,7 @@ SPEC CHECKSUMS:
28512851
MMKVCore: d26e4d3edd5cb8588c2569222cbd8be4231374e9
28522852
OpenSSL-Universal: b60a3702c9fea8b3145549d421fdb018e53ab7b4
28532853
PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47
2854-
RCT-Folly: 84578c8756030547307e4572ab1947de1685c599
2854+
RCT-Folly: 4464f4d875961fce86008d45f4ecf6cef6de0740
28552855
RCTDeprecation: fb7d408617e25d7f537940000d766d60149c5fea
28562856
RCTRequired: 9aaf0ffcc1f41f0c671af863970ef25c422a9920
28572857
RCTTypeSafety: e9a6e7d48184646eb0610295b74c0dd02768cbb2

queries/unknown-consent-conversations-query.ts

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ import { unknownConsentConversationsQueryKey } from "@/queries/QueryKeys";
22
import { setConversationQueryData } from "@/queries/useConversationQuery";
33
import { captureError } from "@/utils/capture-error";
44
import logger from "@/utils/logger";
5-
import { ConverseXmtpClientType } from "@/utils/xmtpRN/client.types";
5+
import {
6+
ConversationWithCodecsType,
7+
ConverseXmtpClientType,
8+
} from "@/utils/xmtpRN/client.types";
69
import { getXmtpClient } from "@/utils/xmtpRN/sync";
7-
import { queryOptions } from "@tanstack/react-query";
10+
import { QueryObserver, queryOptions, useQuery } from "@tanstack/react-query";
11+
import { reactQueryPersister } from "@/utils/mmkv";
12+
import { updateObjectAndMethods } from "@/utils/update-object-and-methods";
13+
import { ConversationTopic } from "@xmtp/react-native-sdk";
14+
import { queryClient } from "./queryClient";
15+
16+
export type IUnknownConversationsQuery = Awaited<
17+
ReturnType<typeof getUnknownConversations>
18+
>;
819

920
async function getUnknownConversations(args: { account: string }) {
1021
const { account } = args;
@@ -23,7 +34,7 @@ async function getUnknownConversations(args: { account: string }) {
2334
if (timeDiff > 3000) {
2435
captureError(
2536
new Error(
26-
`[ConversationsQuery] Fetching conversations from network took ${timeDiff}ms for unknown consent for account ${account}`
37+
`[ConversationsQuery] Fetching conversations from network took ${timeDiff}ms`
2738
)
2839
);
2940
}
@@ -38,8 +49,7 @@ async function getUnknownConversations(args: { account: string }) {
3849
lastMessage: true,
3950
description: true,
4051
},
41-
20, // For now we only fetch 20 until we have the right pagination system. At least people will be able to see their conversations
42-
["unknown"]
52+
20 // For now we only fetch 20 until we have the right pagination system. At least people will be able to see their conversations
4353
);
4454

4555
// For now conversations have all the same properties as one conversation
@@ -54,13 +64,124 @@ async function getUnknownConversations(args: { account: string }) {
5464
return conversations;
5565
}
5666

67+
export const prefetchUnknownConsentConversationsQuery = (args: {
68+
account: string;
69+
}) => {
70+
return queryClient.prefetchQuery(
71+
getUnknownConsentConversationsQueryOptions(args)
72+
);
73+
};
74+
75+
export const addConversationToUnknownConsentConversationsQuery = (args: {
76+
account: string;
77+
conversation: ConversationWithCodecsType;
78+
}) => {
79+
const { account, conversation } = args;
80+
logger.debug(
81+
`[UnknownConversationsQuery] addConversationToUnknownConsentConversationsQuery for account ${account}`
82+
);
83+
const previousConversationsData = getUnknownConsentConversationsQueryData({
84+
account,
85+
});
86+
87+
if (!previousConversationsData) {
88+
queryClient.setQueryData<IUnknownConversationsQuery>(
89+
getUnknownConsentConversationsQueryOptions({ account }).queryKey,
90+
[conversation]
91+
);
92+
return;
93+
}
94+
95+
const conversationExists = previousConversationsData.some(
96+
(c) => c.topic === conversation.topic
97+
);
98+
99+
if (conversationExists) {
100+
return;
101+
}
102+
103+
queryClient.setQueryData<IUnknownConversationsQuery>(
104+
getUnknownConsentConversationsQueryOptions({ account }).queryKey,
105+
[conversation, ...previousConversationsData]
106+
);
107+
};
108+
109+
export const getUnknownConsentConversationsQueryData = (args: {
110+
account: string;
111+
}) => {
112+
return queryClient.getQueryData<IUnknownConversationsQuery>(
113+
getUnknownConsentConversationsQueryOptions(args).queryKey
114+
);
115+
};
116+
117+
export const updateConversationInUnknownConsentConversationsQueryData = (args: {
118+
account: string;
119+
topic: ConversationTopic;
120+
conversationUpdate: Partial<ConversationWithCodecsType>;
121+
}) => {
122+
const { account, topic, conversationUpdate } = args;
123+
124+
logger.debug(
125+
`[UnknownConversationsQuery] updateConversationInUnknownConsentConversationsQueryData for account ${account} and topic ${topic}`
126+
);
127+
128+
const previousConversationsData = getUnknownConsentConversationsQueryData({
129+
account,
130+
});
131+
if (!previousConversationsData) {
132+
return;
133+
}
134+
const newConversations = previousConversationsData.map((c) => {
135+
if (c.topic === topic) {
136+
return updateObjectAndMethods(c, conversationUpdate);
137+
}
138+
return c;
139+
});
140+
141+
queryClient.setQueryData<IUnknownConversationsQuery>(
142+
getUnknownConsentConversationsQueryOptions({
143+
account,
144+
}).queryKey,
145+
newConversations
146+
);
147+
};
148+
149+
export const removeConversationFromUnknownConsentConversationsQueryData =
150+
(args: { account: string; topic: ConversationTopic }) => {
151+
const { account, topic } = args;
152+
153+
logger.debug(
154+
`[UnknownConversationsQuery] removeConversationFromUnknownConsentConversationsQueryData for account ${account} and topic ${topic}`
155+
);
156+
157+
const previousConversationsData = getUnknownConsentConversationsQueryData({
158+
account,
159+
});
160+
161+
if (!previousConversationsData) {
162+
return;
163+
}
164+
165+
const newConversations = previousConversationsData.filter(
166+
(conversation) => conversation.topic !== topic
167+
);
168+
169+
queryClient.setQueryData<IUnknownConversationsQuery>(
170+
getUnknownConsentConversationsQueryOptions({ account }).queryKey,
171+
newConversations
172+
);
173+
};
174+
57175
export function getUnknownConsentConversationsQueryOptions(args: {
58176
account: string;
177+
caller?: string;
59178
}) {
60-
const { account } = args;
179+
const { account, caller } = args;
61180
return queryOptions({
62181
queryFn: () => getUnknownConversations({ account }),
63182
queryKey: unknownConsentConversationsQueryKey(account),
64183
enabled: !!account,
184+
refetchOnMount: true,
185+
persister: reactQueryPersister,
65186
});
66187
}

queries/useConversationQuery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async function getConversation(args: IArgs) {
3939
if (!conversation) {
4040
throw new Error(`Conversation ${topic} not found`);
4141
}
42+
await conversation.sync();
4243
}
4344

4445
const totalEnd = new Date().getTime();

0 commit comments

Comments
 (0)