From a59ad073b9a1f24c1520f8a24fdf58553a580000 Mon Sep 17 00:00:00 2001 From: Alex Risch Date: Thu, 22 Feb 2024 20:57:20 -0700 Subject: [PATCH] Change to dedicated event names per event Added Enum for Event names Added new event names Updated current duplicate event names to be unique --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 20 +++++++--- ios/XMTPModule.swift | 24 +++++++++-- src/lib/Conversation.ts | 3 +- src/lib/Conversations.ts | 11 ++--- src/lib/Group.ts | 3 +- src/lib/types/EventTypes.ts | 40 +++++++++++++++++++ 6 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 src/lib/types/EventTypes.ts diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index d3b97ef05..4a73a6f0a 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -151,14 +151,22 @@ class XMTPModule : Module() { override fun definition() = ModuleDefinition { Name("XMTP") Events( + // Auth "sign", "authed", + "preCreateIdentityCallback", + "preEnableIdentityCallback", + // Conversations "conversation", - "conversationContainer", "group", + "conversationContainer", "message", - "preEnableIdentityCallback", - "preCreateIdentityCallback" + "allGroupMessage", + // Conversation + "conversationMessage", + // Group + "groupMessage" + ) Function("address") { clientAddress: String -> @@ -1019,7 +1027,7 @@ class XMTPModule : Module() { try { client.conversations.streamAllGroupDecryptedMessages().collect { message -> sendEvent( - "message", + "allGroupMessage", mapOf( "clientAddress" to clientAddress, "message" to DecodedMessageWrapper.encodeMap(message), @@ -1045,7 +1053,7 @@ class XMTPModule : Module() { try { conversation.streamDecryptedMessages().collect { message -> sendEvent( - "message", + "conversationMessage", mapOf( "clientAddress" to clientAddress, "message" to DecodedMessageWrapper.encodeMap(message), @@ -1071,7 +1079,7 @@ class XMTPModule : Module() { try { group.streamDecryptedMessages().collect { message -> sendEvent( - "message", + "groupMessage", mapOf( "clientAddress" to clientAddress, "message" to DecodedMessageWrapper.encodeMap(message), diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index b89a19ac1..d726bfda4 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -65,7 +65,23 @@ public class XMTPModule: Module { public func definition() -> ModuleDefinition { Name("XMTP") - Events("sign", "authed", "conversation", "message", "group", "conversationContainer", "preEnableIdentityCallback", "preCreateIdentityCallback") + Events( + // Auth + "sign", + "authed", + "preCreateIdentityCallback", + "preEnableIdentityCallback", + // Conversations + "conversation", + "group", + "conversationContainer", + "message", + "allGroupMessage", + // Conversation + "conversationMessage", + // Group + "groupMessage" + ) AsyncFunction("address") { (clientAddress: String) -> String in if let client = await clientsManager.getClient(key: clientAddress) { @@ -960,7 +976,7 @@ public class XMTPModule: Module { do { for try await message in await client.conversations.streamAllGroupDecryptedMessages() { do { - try sendEvent("message", [ + try sendEvent("allGroupMessage", [ "clientAddress": clientAddress, "message": DecodedMessageWrapper.encodeToObj(message, client: client), ]) @@ -989,7 +1005,7 @@ public class XMTPModule: Module { do { for try await message in conversation.streamDecryptedMessages() { do { - try sendEvent("message", [ + try sendEvent("conversationMessage", [ "clientAddress": clientAddress, "message": DecodedMessageWrapper.encodeToObj(message, client: client), ]) @@ -1059,7 +1075,7 @@ public class XMTPModule: Module { do { for try await message in group.streamDecryptedMessages() { do { - try sendEvent("message", [ + try sendEvent("groupMessage", [ "clientAddress": clientAddress, "message": DecodedMessageWrapper.encodeToObj(message, client: client), ]) diff --git a/src/lib/Conversation.ts b/src/lib/Conversation.ts index 05be85158..2fc79c6b9 100644 --- a/src/lib/Conversation.ts +++ b/src/lib/Conversation.ts @@ -4,6 +4,7 @@ import { } from './ConversationContainer' import { ConversationSendPayload } from './types/ConversationCodecs' import { DefaultContentTypes } from './types/DefaultContentType' +import { EventTypes } from './types/EventTypes' import { SendOptions } from './types/SendOptions' import * as XMTP from '../index' import { @@ -274,7 +275,7 @@ export class Conversation XMTP.subscribeToMessages(this.client.address, this.topic) const hasSeen = {} const messageSubscription = XMTP.emitter.addListener( - 'message', + EventTypes.ConversationMessage, async ({ clientAddress, message, diff --git a/src/lib/Conversations.ts b/src/lib/Conversations.ts index 6e371a454..f7a47e152 100644 --- a/src/lib/Conversations.ts +++ b/src/lib/Conversations.ts @@ -6,6 +6,7 @@ import { } from './ConversationContainer' import { DecodedMessage } from './DecodedMessage' import { Group } from './Group' +import { EventTypes } from './types/EventTypes' import { ConversationContext } from '../XMTP.types' import * as XMTPModule from '../index' import { ContentCodec } from '../index' @@ -110,7 +111,7 @@ export default class Conversations< ): Promise<() => void> { XMTPModule.subscribeToGroups(this.client.address) const groupsSubscription = XMTPModule.emitter.addListener( - 'group', + EventTypes.Group, async ({ clientAddress, group, @@ -168,7 +169,7 @@ export default class Conversations< ) { XMTPModule.subscribeToConversations(this.client.address) XMTPModule.emitter.addListener( - 'conversation', + EventTypes.Conversation, async ({ clientAddress, conversation, @@ -205,7 +206,7 @@ export default class Conversations< ) { XMTPModule.subscribeToAll(this.client.address) const subscription = XMTPModule.emitter.addListener( - 'conversationContainer', + EventTypes.ConversationContainer, async ({ clientAddress, conversationContainer, @@ -252,7 +253,7 @@ export default class Conversations< ): Promise { XMTPModule.subscribeToAllMessages(this.client.address, includeGroups) XMTPModule.emitter.addListener( - 'message', + EventTypes.Message, async ({ clientAddress, message, @@ -285,7 +286,7 @@ export default class Conversations< ): Promise { XMTPModule.subscribeToAllGroupMessages(this.client.address) XMTPModule.emitter.addListener( - 'message', + EventTypes.AllGroupMessage, async ({ clientAddress, message, diff --git a/src/lib/Group.ts b/src/lib/Group.ts index 1c1119337..4fc816876 100644 --- a/src/lib/Group.ts +++ b/src/lib/Group.ts @@ -5,6 +5,7 @@ import { import { DecodedMessage } from './DecodedMessage' import { ConversationSendPayload } from './types/ConversationCodecs' import { DefaultContentTypes } from './types/DefaultContentType' +import { EventTypes } from './types/EventTypes' import { SendOptions } from './types/SendOptions' import * as XMTP from '../index' @@ -128,7 +129,7 @@ export class Group< XMTP.subscribeToGroupMessages(this.client.address, this.id) const hasSeen = {} const messageSubscription = XMTP.emitter.addListener( - 'message', + EventTypes.GroupMessage, async ({ clientAddress, message, diff --git a/src/lib/types/EventTypes.ts b/src/lib/types/EventTypes.ts new file mode 100644 index 000000000..5c5589500 --- /dev/null +++ b/src/lib/types/EventTypes.ts @@ -0,0 +1,40 @@ +export enum EventTypes { + // Auth + Sign = 'sign', + Authed = 'authed', + PreCreateIdentityCallback = 'preCreateIdentityCallback', + PreEnableIdentityCallback = 'preEnableIdentityCallback', + // Conversations Events + /** + * Current user is in a newly created conversation + */ + Conversation = 'conversation', + /** + * Current user is in a newly created group + */ + Group = 'group', + /** + * Current user is in a newly created group or conversation + */ + ConversationContainer = 'conversationContainer', + /** + * Current user receives a new message in any conversation + */ + Message = 'message', + /** + * Current user receives a new message in any group + * Current limitation, only groups created before listener is added will be received + */ + AllGroupMessage = 'allGroupMessage', + + // Conversation Events + /** + * A new message is sent to a specific conversation + */ + ConversationMessage = 'conversationMessage', + // Group Events + /** + * A new message is sent to a specific group + */ + GroupMessage = 'groupMessage', +}