Skip to content

Commit

Permalink
Fixing sending messages, caching groups when fetching them
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalzieu authored and Alex Risch committed Aug 21, 2024
1 parent 56f0375 commit 255907c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
4 changes: 0 additions & 4 deletions queries/useGroupConsentQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ export const useGroupConsentQuery = (
const statusFromState = useSettingsStore(
useShallow((s) => s.groupStatus[topic])
);
const initialDataUpdatedAt = useSettingsStore(
useShallow((s) => s.lastAsyncUpdate)
);
const { data: group } = useGroupQuery(account, topic);
return useQuery({
queryKey: groupConsentQueryKey(account, topic),
Expand All @@ -28,7 +25,6 @@ export const useGroupConsentQuery = (
},
enabled: !!group,
initialData: statusFromState ?? "unknown",
initialDataUpdatedAt,
...queryOptions,
});
};
Expand Down
10 changes: 2 additions & 8 deletions queries/useGroupMembersQuery.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useSettingsStore } from "@data/store/accountsStore";
import { QueryObserverOptions, useQuery } from "@tanstack/react-query";
import { Member } from "@xmtp/react-native-sdk";
import { InboxId } from "@xmtp/react-native-sdk/build/lib/Client";
import { useShallow } from "zustand/react/shallow";

import { groupMembersQueryKey } from "./QueryKeys";
import { entifyWithAddress, EntityObjectWithAddress } from "./entify";
Expand All @@ -16,10 +14,7 @@ export const useGroupMembersQuery = (
topic: string,
queryOptions?: Partial<QueryObserverOptions<GroupMembersSelectData>>
) => {
const { data: group } = useGroupQuery(account, topic);
const initialDataUpdatedAt = useSettingsStore(
useShallow((s) => s.lastAsyncUpdate)
);
const { data: group, dataUpdatedAt } = useGroupQuery(account, topic);
return useQuery<GroupMembersSelectData>({
queryKey: groupMembersQueryKey(account, topic),
queryFn: async () => {
Expand All @@ -30,7 +25,6 @@ export const useGroupMembersQuery = (
ids: [],
};
}
await group.sync();
const updatedMembers = await group.membersList();
return entifyWithAddress(
updatedMembers,
Expand All @@ -47,7 +41,7 @@ export const useGroupMembersQuery = (
(member) => member.addresses[0]
)
: undefined,
initialDataUpdatedAt,
initialDataUpdatedAt: dataUpdatedAt,
enabled: !!group,
...queryOptions,
});
Expand Down
8 changes: 8 additions & 0 deletions queries/useGroupQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ export const invalidateGroupQuery = (account: string, topic: string) => {
queryKey: groupQueryKey(account, topic),
});
};

export const setGroupQueryData = (
account: string,
topic: string,
group: Group
) => {
queryClient.setQueryData(groupQueryKey(account, topic), group);
};
19 changes: 15 additions & 4 deletions utils/xmtpRN/conversations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { setGroupDescriptionQueryData } from "@queries/useGroupDescriptionQuery";
import { setGroupQueryData } from "@queries/useGroupQuery";
import { converseEventEmitter } from "@utils/events";
import { getGroupIdFromTopic } from "@utils/groupUtils/groupId";
import logger from "@utils/logger";
import {
ConsentListEntry,
ConversationContext,
ConversationVersion,
Group,
InboxId,
} from "@xmtp/react-native-sdk";
import { PermissionPolicySet } from "@xmtp/react-native-sdk/build/lib/types/PermissionPolicySet";
Expand Down Expand Up @@ -57,8 +59,10 @@ const protocolConversationToStateConversation = (
});

const protocolGroupToStateConversation = (
account: string,
group: GroupWithCodecsType
): XmtpConversation => {
setGroupQueryData(account, group.topic, group as Group);
const groupMembersAddresses: string[] = [];
const groupAddedByInboxId = group.addedByInboxId;
let groupCreator: string | undefined;
Expand Down Expand Up @@ -177,7 +181,10 @@ const handleNewConversation = async (
? protocolConversationToStateConversation(
conversation as ConversationWithCodecsType
)
: protocolGroupToStateConversation(conversation as GroupWithCodecsType),
: protocolGroupToStateConversation(
client.address,
conversation as GroupWithCodecsType
),
],
true
);
Expand Down Expand Up @@ -328,8 +335,12 @@ export const loadConversations = async (
const conversationsToSave = newConversations.map(
protocolConversationToStateConversation
);
const groupsToCreate = newGroups.map(protocolGroupToStateConversation);
const groupsToUpdate = knownGroups.map(protocolGroupToStateConversation);
const groupsToCreate = newGroups.map((g) =>
protocolGroupToStateConversation(account, g)
);
const groupsToUpdate = knownGroups.map((g) =>
protocolGroupToStateConversation(account, g)
);
saveConversations(client.address, [
...conversationsToSave,
...groupsToCreate,
Expand Down Expand Up @@ -571,7 +582,7 @@ export const refreshGroup = async (account: string, topic: string) => {
await group.sync();
saveConversations(
client.address,
[protocolGroupToStateConversation(group)],
[protocolGroupToStateConversation(account, group)],
true
);
const updatedMembers = await group.membersList();
Expand Down
3 changes: 0 additions & 3 deletions utils/xmtpRN/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ export const syncGroupsMessages = async (
groups: GroupWithCodecsType[],
queryGroupsFromTimestamp: { [topic: string]: number }
) => {
logger.info(`Syncing ${groups.length} groups...`);
for (const group of groups) {
// No need to group.sync here as syncGroupsMessages is called either
// from handleNewConversation which syncs before, or on groups returned
Expand All @@ -235,9 +234,7 @@ export const syncGroupsMessages = async (
);
groupMembers[group.topic] = group.members;
saveMemberInboxIds(account, group.members);
logger.debug("synced group", group.topic);
}
logger.info(`${groups.length} groups synced!`);
const newMessages = (
await Promise.all(
groups.map((g) =>
Expand Down
8 changes: 6 additions & 2 deletions utils/xmtpRN/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TransactionReference,
} from "@xmtp/content-type-transaction-reference";
import {
ConversationVersion,
PreparedLocalMessage,
sendPreparedMessage,
} from "@xmtp/react-native-sdk";
Expand Down Expand Up @@ -178,7 +179,10 @@ export const sendPendingMessages = async (account: string) => {
message.conversationId
);
if (conversation) {
if ((conversation as any).peerAddress && message.status === "sending") {
if (
conversation.version === ConversationVersion.DIRECT &&
message.status === "sending"
) {
// DM Conversation
const preparedMessage = await (
conversation as ConversationWithCodecsType
Expand All @@ -193,7 +197,7 @@ export const sendPendingMessages = async (account: string) => {
newMessageSent: preparedMessage.preparedAt,
message,
};
} else if ((conversation as any).peerInboxIds) {
} else if (conversation.version === ConversationVersion.GROUP) {
// This is a group message. Preparing it will store
// it in libxmtp db to be published later, it's different
// from 1v1 prepareMessage which needs to be sent using sendPreparedMessage
Expand Down

0 comments on commit 255907c

Please sign in to comment.