Skip to content

Commit

Permalink
feat: Dual Sending
Browse files Browse the repository at this point in the history
Bumps SDK to support dual sending
  • Loading branch information
alexrisch committed Nov 12, 2024
1 parent 3f53b99 commit fd49aea
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 83 deletions.
5 changes: 3 additions & 2 deletions components/Chat/ChatPlaceholder/GroupChatPlaceholder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ export function GroupChatPlaceholder({ messagesCount }: Props) {
const { members } = useGroupMembers(conversation?.topic ?? "");

const styles = useStyles();

const groupCreatedByUser = useMemo(() => {
if (!group || !currentAccount) {
if (!group || !currentAccount || !group.addedByInboxId) {
return false;
}
const creatorInfo = members?.byId[group.creatorInboxId];
const creatorInfo = members?.byId[group.addedByInboxId];

return creatorInfo?.addresses.some(
(a) => a.toLowerCase() === currentAccount.toLowerCase()
Expand Down
2 changes: 1 addition & 1 deletion components/Chat/ConsentPopup/GroupConsentPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function GroupConsentPopup() {
const styles = useStyles();
const colorScheme = useColorScheme();
const { consent, blockGroup, allowGroup } = useGroupConsent(topic);
const { groupCreator } = useGroupCreator(topic);
const { data: groupCreator } = useGroupCreator(topic);
const { groupStatus } = useSettingsStore(useSelect(["groupStatus"]));
const { members } = useGroupMembers(topic);
const groupId = getGroupIdFromTopic(topic);
Expand Down
6 changes: 3 additions & 3 deletions hooks/useGroupConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import { useCallback } from "react";

import { useGroupCreator } from "./useGroupCreator";

interface OnConsentOptions {
type OnConsentOptions = {
includeCreator?: boolean;
includeAddedBy?: boolean;
}
};

export const useGroupConsent = (
topic: string,
queryOptions?: Partial<QueryObserverOptions<"allowed" | "denied" | "unknown">>
) => {
const account = currentAccount();
const { data: group } = useGroupQuery(account, topic);
const { groupCreator } = useGroupCreator(topic);
const { data: groupCreator } = useGroupCreator(topic);
const { data, isLoading, isError } = useGroupConsentQuery(
account,
topic,
Expand Down
20 changes: 14 additions & 6 deletions hooks/useGroupCreator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { useGroupQuery } from "@queries/useGroupQuery";
import { useQuery } from "@tanstack/react-query";

import { currentAccount } from "../data/store/accountsStore";

export const useGroupCreator = (topic: string) => {
const account = currentAccount();
const { data, isLoading, isError } = useGroupQuery(account, topic);
const { data } = useGroupQuery(account, topic);

return {
groupCreator: data?.creatorInboxId,
isLoading,
isError,
};
return useQuery({
queryKey: ["groupCreator", account, topic],
queryFn: async () => {
const creatorInboxId = await data?.creatorInboxId();
return creatorInboxId ?? null;
},
enabled: !!account && !!topic && !!data,
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
staleTime: Infinity,
});
};
4 changes: 2 additions & 2 deletions ios/ConverseNotificationExtension/Xmtp/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func getPersistedConversation(xmtpClient: XMTP.Client, contentTopic: String) asy
let data = try Xmtp_KeystoreApi_V1_TopicMap.TopicData(
serializedData: Data(base64Encoded: topicData)!
)
let conversation = await xmtpClient.conversations.importTopicData(data: data)
let conversation = await try xmtpClient.conversations.importTopicData(data: data)
return conversation
} catch {
sentryTrackError(error: error, extras: ["message": "Could not import topic data in XMTP Client"])
Expand All @@ -106,7 +106,7 @@ func getPersistedConversation(xmtpClient: XMTP.Client, contentTopic: String) asy
let data = try Xmtp_KeystoreApi_V1_TopicMap.TopicData(
serializedData: Data(base64Encoded: Data(persistedTopicData!.utf8))!
)
let conversation = await xmtpClient.conversations.importTopicData(data: data)
let conversation = await try xmtpClient.conversations.importTopicData(data: data)
return conversation
} catch {
sentryTrackError(error: error, extras: ["message": "Could not import topic data in XMTP Client"])
Expand Down
11 changes: 6 additions & 5 deletions ios/ConverseNotificationExtension/Xmtp/Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,17 @@ func decodeMessage(xmtpClient: XMTP.Client, envelope: XMTP.Envelope) async throw

if let group = try! xmtpClient.findGroup(groupId: getGroupIdFromTopic(topic: envelope.contentTopic) ) {
do {
sentryTrackMessage(message: "[NotificationExtension] Syncing Group", extras: [:])
sentryAddBreadcrumb(message: "[NotificationExtension] Syncing Group", extras: [:])
try await group.sync()
sentryTrackMessage(message: "[NotificationExtension] Done Syncing Group", extras: [:])
sentryAddBreadcrumb(message: "[NotificationExtension] Decoding group message...")
let envelopeBytes = envelope.message
let decodedMessage = try await group.processMessage(envelopeBytes: envelopeBytes)
print("[NotificationExtension] Group message decoded!")
let message = try await group.processMessage(envelopeBytes: envelopeBytes)
let decodedMessage = try message.decode()
sentryAddBreadcrumb(message:"[NotificationExtension] Group message decoded!")
return decodedMessage
} catch {
sentryTrackMessage(message: "NOTIFICATION_DECODING_ERROR", extras: ["error": error, "envelope": envelope])
print("[NotificationExtension] ERROR WHILE DECODING \(error)")
sentryAddBreadcrumb(message: "[NotificationExtension] ERROR WHILE DECODING \(error)")
return nil
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ install! 'cocoapods',

# Version must match version from XMTP Podspec (matching @xmtp/react-native-sdk from package.json)
# https://github.com/xmtp/xmtp-react-native/blob/v2.6.2/ios/XMTPReactNative.podspec#L29
$xmtpVersion = '0.15.0'
$xmtpVersion = '0.16.2'

# Pinning MMKV to 1.3.3 that has included that fix https://github.com/Tencent/MMKV/pull/1222#issuecomment-1905164314
$mmkvVersion = '1.3.3'
Expand Down
20 changes: 10 additions & 10 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ PODS:
- libwebp/sharpyuv (1.3.2)
- libwebp/webp (1.3.2):
- libwebp/sharpyuv
- LibXMTP (0.5.9-beta0)
- LibXMTP (0.6.0)
- Logging (1.0.0)
- MessagePacker (0.4.7)
- MMKV (1.3.3):
Expand Down Expand Up @@ -1836,16 +1836,16 @@ PODS:
- GenericJSON (~> 2.0)
- Logging (~> 1.0.0)
- secp256k1.swift (~> 0.1)
- XMTP (0.15.0):
- XMTP (0.16.2):
- Connect-Swift (= 0.12.0)
- GzipSwift
- LibXMTP (= 0.5.9-beta0)
- LibXMTP (= 0.6.0)
- web3.swift
- XMTPReactNative (2.6.4):
- XMTPReactNative (2.8.4):
- ExpoModulesCore
- MessagePacker
- secp256k1.swift
- XMTP (= 0.15.0)
- XMTP (= 0.16.2)
- Yoga (0.0.0)

DEPENDENCIES:
Expand Down Expand Up @@ -1984,7 +1984,7 @@ DEPENDENCIES:
- Sentry/HybridSDK (= 8.36.0)
- SQLite.swift
- UMAppLoader (from `../node_modules/unimodules-app-loader/ios`)
- XMTP (= 0.15.0)
- XMTP (= 0.16.2)
- "XMTPReactNative (from `../node_modules/@xmtp/react-native-sdk/ios`)"
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)

Expand Down Expand Up @@ -2340,7 +2340,7 @@ SPEC CHECKSUMS:
libavif: 84bbb62fb232c3018d6f1bab79beea87e35de7b7
libdav1d: 23581a4d8ec811ff171ed5e2e05cd27bad64c39f
libwebp: 1786c9f4ff8a279e4dac1e8f385004d5fc253009
LibXMTP: 5a38722a68a9469be2e711857a5e7d9dd3aa8a61
LibXMTP: 059c6d51b2c59419941ecff600aa586bbe083673
Logging: 9ef4ecb546ad3169398d5a723bc9bea1c46bef26
MessagePacker: ab2fe250e86ea7aedd1a9ee47a37083edd41fd02
MMKV: f902fb6719da13c2ab0965233d8963a59416f911
Expand Down Expand Up @@ -2441,10 +2441,10 @@ SPEC CHECKSUMS:
SwiftProtobuf: 407a385e97fd206c4fbe880cc84123989167e0d1
UMAppLoader: f17a5ee8e85b536ace0fc254b447a37ed198d57e
web3.swift: 2263d1e12e121b2c42ffb63a5a7beb1acaf33959
XMTP: 09faa347569b092005997364f7fe787ccc33f3d5
XMTPReactNative: 0c92d55c576ac6ff0775357fa60e90ea8e73afc2
XMTP: 281c763321f3be82b3e5d91bfd79f107b1169e30
XMTPReactNative: 5e039fac34738e2c4a180f492e066ab28d43ab70
Yoga: 1ab23c1835475da69cf14e211a560e73aab24cb0

PODFILE CHECKSUM: 137cb0cd2dafbfb3e5b9343435f9db8e28690806
PODFILE CHECKSUM: 27696309188920aa8303d8624bb4a75339671e2b

COCOAPODS: 1.15.2
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@xmtp/content-type-transaction-reference": "^1.0.3",
"@xmtp/frames-client": "^0.5.4",
"@xmtp/proto": "^3.60.0",
"@xmtp/react-native-sdk": "^2.6.4",
"@xmtp/react-native-sdk": "^2.8.4",
"@xmtp/xmtp-js": "11.5.0",
"@yornaath/batshit": "^0.10.1",
"alchemy-sdk": "^3.4.4",
Expand Down
4 changes: 2 additions & 2 deletions queries/useGroupMembersQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const useGroupMembersQuery = (
ids: [],
};
}
const updatedMembers = await group.membersList();
const members = await group.members();
return entifyWithAddress(
updatedMembers,
members,
(member) => member.inboxId,
// TODO: Multiple addresses support
(member) => getCleanAddress(member.addresses[0])
Expand Down
21 changes: 0 additions & 21 deletions queries/useGroupPendingMessages.ts

This file was deleted.

3 changes: 2 additions & 1 deletion queries/useGroupQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ export const useGroupQuery = (account: string, topic: string) => {
setGroupPhotoQueryData(account, topic, group.imageUrlSquare, {
updatedAt: groupDataUpdatedAt,
});
const members = await group.members();
setGroupMembersQueryData(
account,
topic,
entifyWithAddress(
group.members,
members,
(member) => member.inboxId,
// TODO: Multiple addresses support
(member) => getCleanAddress(member.addresses[0])
Expand Down
54 changes: 35 additions & 19 deletions utils/xmtpRN/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,22 @@ const protocolConversationToStateConversation = (
isGroup: false,
});

const protocolGroupToStateConversation = (
const protocolGroupToStateConversation = async (
account: string,
group: GroupWithCodecsType
): XmtpConversation => {
): Promise<XmtpConversation> => {
// We'll pre-cache some queries since we know
// this data is up to date - was just queried!
setGroupQueryData(account, group.topic, group as Group);
setGroupNameQueryData(account, group.topic, group.name);
setGroupPhotoQueryData(account, group.topic, group.imageUrlSquare);
const members = await group.members();
const creatorInboxId = await group.creatorInboxId();
setGroupMembersQueryData(
account,
group.topic,
entifyWithAddress(
group.members,
members,
(member) => member.inboxId,
// TODO: Multiple addresses support
(member) => getCleanAddress(member.addresses[0])
Expand All @@ -90,11 +92,11 @@ const protocolGroupToStateConversation = (
let groupCreator: string | undefined;
let groupAddedBy: string | undefined;

group.members.forEach((m) => {
members.forEach((m) => {
const firstAddress = getCleanAddress(m.addresses[0]);
if (firstAddress) {
groupMembersAddresses.push(firstAddress);
if (m.inboxId === group.creatorInboxId) {
if (m.inboxId === creatorInboxId) {
groupCreator = firstAddress;
}
if (m.inboxId === groupAddedByInboxId) {
Expand Down Expand Up @@ -196,12 +198,14 @@ const handleNewConversation = async (
setOpenedConversation(client.address, conversation);
const isGroup = conversation.version === ConversationVersion.GROUP;
const isDMConversation = !!(conversation as any).peerAddress;

const members = isGroup
? await (conversation as GroupWithCodecsType).members()
: [];
// Temporary fix to stop receiving messages for groups
// we are not member of
const shouldSkip =
isGroup &&
!(conversation as GroupWithCodecsType).members.some(
!members.some(
(m) => m.addresses[0].toLowerCase() === client.address.toLowerCase()
);
if (shouldSkip) {
Expand All @@ -219,7 +223,7 @@ const handleNewConversation = async (
? protocolConversationToStateConversation(
conversation as ConversationWithCodecsType
)
: protocolGroupToStateConversation(
: await protocolGroupToStateConversation(
client.address,
conversation as GroupWithCodecsType
),
Expand Down Expand Up @@ -379,7 +383,7 @@ export const loadConversations = async (
const knownGroups: GroupWithCodecsType[] = [];
const updatedGroups: GroupWithCodecsType[] = [];

groups.forEach((g) => {
groups.forEach(async (g) => {
if (!knownTopicsSet.has(g.topic)) {
newGroups.push(g);
} else {
Expand All @@ -392,8 +396,9 @@ export const loadConversations = async (
updatedGroups.push(g);
} else {
const currentMembersSet = new Set(existingGroup.groupMembers);
const newMembers = await g.members();
const newMembersSet = new Set(
g.members.map((m) => getCleanAddress(m.addresses[0]))
newMembers.map((m) => getCleanAddress(m.addresses[0]))
);

if (
Expand All @@ -415,14 +420,24 @@ export const loadConversations = async (
} sec`
);

const conversationsToSave = newConversations.map(
protocolConversationToStateConversation
const conversationsToSave = await Promise.all(
newConversations.map(async (c) => {
const conversation = await protocolConversationToStateConversation(c);
return conversation;
})
);
const groupsToCreate = newGroups.map((g) =>
protocolGroupToStateConversation(account, g)
const groupsToCreate = await Promise.all(
newGroups.map(async (g) => {
const group = await protocolGroupToStateConversation(account, g);
return group;
})
);
const groupsToUpdate = updatedGroups.map((g) =>
protocolGroupToStateConversation(account, g)

const groupsToUpdate = await Promise.all(
updatedGroups.map(async (g) => {
const group = await protocolGroupToStateConversation(account, g);
return group;
})
);

const afterMappedConvos = new Date().getTime();
Expand Down Expand Up @@ -680,7 +695,8 @@ export const createGroup = async (
if (groupDescription) {
setGroupDescriptionQueryData(account, group.topic, groupDescription);
}
saveMemberInboxIds(account, group.members);
const members = await group.members();
saveMemberInboxIds(account, members);
await handleNewConversation(client, group);
return group.topic;
};
Expand All @@ -695,10 +711,10 @@ export const refreshGroup = async (account: string, topic: string) => {
await group.sync();
saveConversations(
client.address,
[protocolGroupToStateConversation(account, group)],
[await protocolGroupToStateConversation(account, group)],
true
);
const updatedMembers = await group.membersList();
const updatedMembers = await group.members();
saveMemberInboxIds(account, updatedMembers);
};

Expand Down
Loading

0 comments on commit fd49aea

Please sign in to comment.