diff --git a/components/views/user/User.vue b/components/views/user/User.vue index df31e6d69e..4e433844f1 100644 --- a/components/views/user/User.vue +++ b/components/views/user/User.vue @@ -164,10 +164,16 @@ export default Vue.extend({ return } - await iridium.chat?.createConversation(this.user?.name, 'direct', [ - this.user?.did, - iridium.connector.id, - ]) + if (await iridium.chat?.hasConversation(this.user.did)) { + await iridium.chat?.getConversation(this.user?.did) + } + + if (!(await iridium.chat?.hasConversation(this.user?.did))) { + await iridium.chat?.createConversation(this.user?.name, 'direct', [ + this.user?.did, + iridium.connector.id, + ]) + } // this.$store.dispatch('conversation/setConversation', { // id: this.user.did, diff --git a/libraries/Iridium/chat/ChatManager.ts b/libraries/Iridium/chat/ChatManager.ts index ebbfb7f96a..1c30ca3568 100644 --- a/libraries/Iridium/chat/ChatManager.ts +++ b/libraries/Iridium/chat/ChatManager.ts @@ -88,6 +88,10 @@ export default class ChatManager extends Emitter { this.emit(`conversation/${conversationId}`, payload) } + hasConversation(id: string) { + return this.state.conversations.includes(id) + } + async createConversation( name: string, type: 'group' | 'direct', @@ -135,11 +139,21 @@ export default class ChatManager extends Emitter { if (!conversation) { throw new Error(ChatError.CONVERSATION_NOT_FOUND) } + this.state.conversation[id] = conversation this.emit(`conversation/${id}`, conversation) + + await this.subscribeToChannel(id) + // this.emit(`conversations`, this.state.conversations) return conversation } + async loadMessages(id: string) { + if (id && this.hasConversation(id)) { + return Object.values(this.state.conversation[id]?.message) + } + } + /** * @method subscribeToConversation * @description Adds a watcher to conversation activity @@ -198,12 +212,14 @@ export default class ChatManager extends Emitter { action: 'message', message: messageCID, }) + + const pids = await Promise.all( + conversation.participants.map((p) => Iridium.DIDToPeerId(p)), + ) await this.iridium.connector.send( { type: 'chat/message', conversationId: id, messageCID }, { - to: await Promise.all( - conversation.participants.map((p) => Iridium.DIDToPeerId(p)), - ), + to: pids, }, ) this.emit(`conversation/${id}`, { @@ -219,15 +235,28 @@ export default class ChatManager extends Emitter { * @param did {string} did * @param onMessage {EmitterCallback} function to be called */ - async subscribeToChannel( - did: string, - onMessage: EmitterCallback, - ) { + async subscribeToChannel(did: string) { const pid = await Iridium.DIDToPeerId(did) - if (this.iridium.connector._peers[pid]) + if (this.iridium.connector?._peers[pid]) this.iridium.connector.on( this.iridium.connector._peers[pid].channel, - onMessage, + async (event: any) => { + const { messageCID, type } = event.payload + if (messageCID && type === 'chat/message') { + const message = await this.iridium.connector.load(messageCID, { + decrypt: true, + }) + await this.iridium.connector.set( + `/chat/conversation/${did}/message/${messageCID}`, + message, + ) + this.emit(`conversation/${did}`, { + action: 'message', + message: messageCID, + from: this.iridium.connector.id, + }) + } + }, ) } } diff --git a/pages/chat/direct/_address.vue b/pages/chat/direct/_address.vue index 8186fc9224..b60f1bbb8c 100644 --- a/pages/chat/direct/_address.vue +++ b/pages/chat/direct/_address.vue @@ -86,18 +86,13 @@ export default Vue.extend({ // }, }, async mounted() { + this.messages = [...((await iridium.chat.loadMessages(this.friend)) || [])] if (this.friend) { await iridium.chat.subscribeToConversation(this.friend, async (event) => { const message = await iridium.connector.load(event.message) this.messages.push(message) }) - await iridium.chat.subscribeToChannel(this.friend, async (event) => { - if (event.payload.messageCID && event.payload.type === 'chat/message') { - this.messages.push( - await iridium.connector.load(event.payload.messageCID), - ) - } - }) + await iridium.chat.subscribeToChannel(this.friend) } }, })