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

fix: Requests list items #1567

Merged
merged 9 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions containers/GroupPendingRequestsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useGroupPendingRequests } from "@hooks/useGroupPendingRequests";
import { usePreferredNames } from "@hooks/usePreferredNames";
import { translate } from "@i18n";
import { useAddToGroupMutation } from "@queries/useAddToGroupMutation";
import { invalidatePendingJoinRequestsQuery } from "@queries/usePendingRequestsQuery";
import { invalidateGroupPendingJoinRequestsQuery } from "@/queries/useGroupPendingRequestsQuery";
import { actionSheetColors, textSecondaryColor } from "@styles/colors";
import { updateGroupJoinRequestStatus } from "@/utils/api/api-groups/api-groups";
import { FC, useMemo } from "react";
Expand Down Expand Up @@ -71,7 +71,7 @@ export const GroupPendingRequestsTable: FC<GroupPendingRequestsTableProps> = ({
request.id,
"ACCEPTED"
);
invalidatePendingJoinRequestsQuery(currentAccount);
invalidateGroupPendingJoinRequestsQuery(currentAccount);
break;
case 1:
// deny
Expand Down
15 changes: 12 additions & 3 deletions features/consent/use-allow-group.mutation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {
addConversationToUnknownConsentConversationsQuery,
removeConversationFromUnknownConsentConversationsQueryData,
} from "@/queries/unknown-consent-conversations-query";
import { updateConversationInConversationsQueryData } from "@/queries/use-conversations-query";
import { captureError } from "@/utils/capture-error";
import { GroupWithCodecsType } from "@/utils/xmtpRN/client.types";
import { queryClient } from "@queries/queryClient";
import {
Expand Down Expand Up @@ -95,6 +98,10 @@ export const getAllowGroupMutationOptions = (
state: "allowed",
},
});
removeConversationFromUnknownConsentConversationsQueryData({
account,
topic: group.topic,
});
return {
previousConsent,
};
Expand All @@ -108,8 +115,6 @@ export const getAllowGroupMutationOptions = (
) => {
const { account, group } = variables;

captureError(error);

if (!context) {
return;
}
Expand All @@ -126,6 +131,10 @@ export const getAllowGroupMutationOptions = (
state: context.previousConsent,
},
});
addConversationToUnknownConsentConversationsQuery({
account,
conversation: group,
thierryskoda marked this conversation as resolved.
Show resolved Hide resolved
});
},
};
};
Expand Down
40 changes: 30 additions & 10 deletions features/consent/use-dm-consent-for-current-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {
getCurrentAccount,
useCurrentAccount,
} from "@/data/store/accountsStore";
import { updateConversationInConversationsQueryData } from "@/queries/use-conversations-query";
import { getConversationQueryData } from "@/queries/useConversationQuery";
import { updateConversationInUnknownConsentConversationsQueryData } from "@/queries/unknown-consent-conversations-query";
import { updateConversationInConversationsQueryData } from "@/queries/use-conversations-query";
import { getDmQueryData, setDmQueryData } from "@/queries/useDmQuery";
import { updateObjectAndMethods } from "@/utils/update-object-and-methods";
import { DmWithCodecsType } from "@/utils/xmtpRN/client.types";
Expand All @@ -26,7 +27,7 @@ export function useDmConsentForCurrentAccount() {
conversationId: ConversationId;
topic: ConversationTopic;
}) => {
const { peerInboxId, conversationId, topic } = args;
const { consent, peerInboxId, conversationId } = args;
if (!peerInboxId) {
throw new Error("Peer inbox id not found");
}
Expand All @@ -35,24 +36,23 @@ export function useDmConsentForCurrentAccount() {
updateConsentForGroupsForAccount({
account: currentAccount,
groupIds: [conversationId],
consent: args.consent,
consent,
}),
updateInboxIdsConsentForAccount({
account: currentAccount,
inboxIds: [peerInboxId],
consent: args.consent,
consent,
}),
]);
},
onMutate: (args) => {
const { peerInboxId, conversationId, topic } = args;
onMutate: ({ consent, topic }) => {
const conversation = getConversationQueryData({
account: currentAccount,
topic,
});
if (conversation) {
const updatedDm = updateObjectAndMethods(conversation, {
state: args.consent === "allow" ? "allowed" : "denied",
state: consent === "allow" ? "allowed" : "denied",
});

setDmQueryData({
Expand All @@ -65,38 +65,58 @@ export function useDmConsentForCurrentAccount() {
account: currentAccount,
topic,
conversationUpdate: {
state: args.consent === "allow" ? "allowed" : "denied",
state: consent === "allow" ? "allowed" : "denied",
},
});

updateConversationInUnknownConsentConversationsQueryData({
account: currentAccount,
topic,
conversationUpdate: {
state: consent === "allow" ? "allowed" : "denied",
},
});

return { previousDmConsent: conversation.state };
}
},
onError: (error, variables, context) => {
const { topic } = variables;
onError: (error, { topic }, context) => {
const { previousDmConsent } = context || {};
if (previousDmConsent) {
const dm = getDmQueryData({
account: currentAccount,
peer: topic,
});

if (!dm) {
return;
}

const updatedDm = updateObjectAndMethods(dm, {
state: previousDmConsent,
});

setDmQueryData({
account: currentAccount,
peer: topic,
dm: updatedDm,
});

updateConversationInConversationsQueryData({
account: currentAccount,
topic,
conversationUpdate: {
state: previousDmConsent,
},
});

updateConversationInUnknownConsentConversationsQueryData({
account: currentAccount,
topic,
conversationUpdate: {
state: previousDmConsent,
},
});
}
},
});
Expand Down
4 changes: 2 additions & 2 deletions hooks/useGroupPendingRequests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { usePendingRequestsQuery } from "@queries/usePendingRequestsQuery";
import { useGroupPendingRequestsQuery } from "@/queries/useGroupPendingRequestsQuery";
import { PendingGroupJoinRequest } from "@/utils/api/api-groups/api-groups";
import type { ConversationTopic } from "@xmtp/react-native-sdk";
import { useMemo } from "react";
import { useExistingGroupInviteLink } from "./useExistingGroupInviteLink";
import { useGroupMembers } from "./useGroupMembers";

export const useGroupPendingRequests = (topic: ConversationTopic) => {
const { data } = usePendingRequestsQuery();
const { data } = useGroupPendingRequestsQuery();
const { members } = useGroupMembers(topic);

const groupInviteLink = useExistingGroupInviteLink(topic);
Expand Down
10 changes: 5 additions & 5 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2791,7 +2791,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
AppCheckCore: cc8fd0a3a230ddd401f326489c99990b013f0c4f
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
boost: 4cb898d0bf20404aab1850c656dcea009429d6c1
CoinbaseWalletSDK: ea1f37512bbc69ebe07416e3b29bf840f5cc3152
CoinbaseWalletSDKExpo: 7488819c860868cd5fe117f6be43dd42c2e65919
ComputableLayout: c50faffac4ed9f8f05b0ce5e6f3a60df1f6042c8
Expand All @@ -2800,7 +2800,7 @@ SPEC CHECKSUMS:
CryptoSwift: 967f37cea5a3294d9cce358f78861652155be483
CSecp256k1: 2a59c03e52637ded98896a33be4b2649392cb843
DGSwiftUtilities: 2f0d35d5ff3d57bd70ccc42f15971460db202c41
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
EASClient: d15d70e334b019ae5649609011cda761defe4b1a
EXApplication: 4c72f6017a14a65e338c5e74fca418f35141e819
EXConstants: 6b8c5653492349b3c3fe6b905c556bc45b360405
Expand Down Expand Up @@ -2849,8 +2849,8 @@ SPEC CHECKSUMS:
FirebaseAppCheckInterop: 347aa09a805219a31249b58fc956888e9fcb314b
FirebaseCore: e0510f1523bc0eb21653cac00792e1e2bd6f1771
FirebaseCoreInternal: d98ab91e2d80a56d7b246856a8885443b302c0c2
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: 69ef571f3de08433d766d614c73a9838a06bf7eb
GoogleUtilities: 26a3abef001b6533cf678d3eb38fd3f614b7872d
hermes-engine: 06a9c6900587420b90accc394199527c64259db4
libavif: 84bbb62fb232c3018d6f1bab79beea87e35de7b7
Expand All @@ -2863,7 +2863,7 @@ SPEC CHECKSUMS:
MMKVCore: d26e4d3edd5cb8588c2569222cbd8be4231374e9
OpenSSL-Universal: b60a3702c9fea8b3145549d421fdb018e53ab7b4
PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47
RCT-Folly: 84578c8756030547307e4572ab1947de1685c599
RCT-Folly: 4464f4d875961fce86008d45f4ecf6cef6de0740
RCTDeprecation: fb7d408617e25d7f537940000d766d60149c5fea
RCTRequired: 9aaf0ffcc1f41f0c671af863970ef25c422a9920
RCTTypeSafety: e9a6e7d48184646eb0610295b74c0dd02768cbb2
Expand Down
133 changes: 127 additions & 6 deletions queries/unknown-consent-conversations-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ import { unknownConsentConversationsQueryKey } from "@/queries/QueryKeys";
import { setConversationQueryData } from "@/queries/useConversationQuery";
import { captureError } from "@/utils/capture-error";
import logger from "@/utils/logger";
import { ConverseXmtpClientType } from "@/utils/xmtpRN/client.types";
import {
ConversationWithCodecsType,
ConverseXmtpClientType,
} from "@/utils/xmtpRN/client.types";
import { getXmtpClient } from "@/utils/xmtpRN/sync";
import { queryOptions } from "@tanstack/react-query";
import { QueryObserver, queryOptions, useQuery } from "@tanstack/react-query";
import { reactQueryPersister } from "@/utils/mmkv";
import { updateObjectAndMethods } from "@/utils/update-object-and-methods";
import { ConversationTopic } from "@xmtp/react-native-sdk";
import { queryClient } from "./queryClient";

export type IUnknownConversationsQuery = Awaited<
ReturnType<typeof getUnknownConversations>
>;

async function getUnknownConversations(args: { account: string }) {
const { account } = args;
Expand All @@ -23,7 +34,7 @@ async function getUnknownConversations(args: { account: string }) {
if (timeDiff > 3000) {
captureError(
new Error(
`[ConversationsQuery] Fetching conversations from network took ${timeDiff}ms for unknown consent for account ${account}`
`[ConversationsQuery] Fetching conversations from network took ${timeDiff}ms`
)
);
}
Expand All @@ -38,8 +49,7 @@ async function getUnknownConversations(args: { account: string }) {
lastMessage: true,
description: true,
},
20, // For now we only fetch 20 until we have the right pagination system. At least people will be able to see their conversations
["unknown"]
20 // For now we only fetch 20 until we have the right pagination system. At least people will be able to see their conversations
);

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

export const prefetchUnknownConsentConversationsQuery = (args: {
account: string;
}) => {
return queryClient.prefetchQuery(
getUnknownConsentConversationsQueryOptions(args)
);
};

export const addConversationToUnknownConsentConversationsQuery = (args: {
account: string;
conversation: ConversationWithCodecsType;
}) => {
const { account, conversation } = args;
logger.debug(
`[UnknownConversationsQuery] addConversationToUnknownConsentConversationsQuery for account ${account}`
);
const previousConversationsData = getUnknownConsentConversationsQueryData({
account,
});

if (!previousConversationsData) {
queryClient.setQueryData<IUnknownConversationsQuery>(
getUnknownConsentConversationsQueryOptions({ account }).queryKey,
[conversation]
);
return;
}

const conversationExists = previousConversationsData.some(
(c) => c.topic === conversation.topic
);

if (conversationExists) {
return;
}

queryClient.setQueryData<IUnknownConversationsQuery>(
getUnknownConsentConversationsQueryOptions({ account }).queryKey,
[conversation, ...previousConversationsData]
);
};

export const getUnknownConsentConversationsQueryData = (args: {
account: string;
}) => {
return queryClient.getQueryData<IUnknownConversationsQuery>(
getUnknownConsentConversationsQueryOptions(args).queryKey
);
};

export const updateConversationInUnknownConsentConversationsQueryData = (args: {
account: string;
topic: ConversationTopic;
conversationUpdate: Partial<ConversationWithCodecsType>;
}) => {
const { account, topic, conversationUpdate } = args;

logger.debug(
`[UnknownConversationsQuery] updateConversationInUnknownConsentConversationsQueryData for account ${account} and topic ${topic}`
);

const previousConversationsData = getUnknownConsentConversationsQueryData({
account,
});
if (!previousConversationsData) {
return;
}
const newConversations = previousConversationsData.map((c) => {
if (c.topic === topic) {
return updateObjectAndMethods(c, conversationUpdate);
}
return c;
});

queryClient.setQueryData<IUnknownConversationsQuery>(
getUnknownConsentConversationsQueryOptions({
account,
}).queryKey,
newConversations
);
};

export const removeConversationFromUnknownConsentConversationsQueryData =
(args: { account: string; topic: ConversationTopic }) => {
const { account, topic } = args;

logger.debug(
`[UnknownConversationsQuery] removeConversationFromUnknownConsentConversationsQueryData for account ${account} and topic ${topic}`
);

const previousConversationsData = getUnknownConsentConversationsQueryData({
account,
});

if (!previousConversationsData) {
return;
}

const newConversations = previousConversationsData.filter(
(conversation) => conversation.topic !== topic
);

queryClient.setQueryData<IUnknownConversationsQuery>(
getUnknownConsentConversationsQueryOptions({ account }).queryKey,
newConversations
);
};

export function getUnknownConsentConversationsQueryOptions(args: {
account: string;
caller?: string;
}) {
const { account } = args;
const { account, caller } = args;
return queryOptions({
queryFn: () => getUnknownConversations({ account }),
queryKey: unknownConsentConversationsQueryKey(account),
enabled: !!account,
refetchOnMount: true,
persister: reactQueryPersister,
});
}
1 change: 1 addition & 0 deletions queries/useConversationQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function getConversation(args: IArgs) {
if (!conversation) {
throw new Error(`Conversation ${topic} not found`);
}
await conversation.sync();
}

const totalEnd = new Date().getTime();
Expand Down
Loading
Loading