Skip to content

Commit

Permalink
feat(chat): chat improvement, bug fixes (#3997)
Browse files Browse the repository at this point in the history
  • Loading branch information
maljuburi committed Jul 14, 2022
1 parent 601f47c commit 1cc282e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
14 changes: 10 additions & 4 deletions components/views/user/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 38 additions & 9 deletions libraries/Iridium/chat/ChatManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export default class ChatManager extends Emitter<ConversationMessage> {
this.emit(`conversation/${conversationId}`, payload)
}

hasConversation(id: string) {
return this.state.conversations.includes(id)
}

async createConversation(
name: string,
type: 'group' | 'direct',
Expand Down Expand Up @@ -135,11 +139,21 @@ export default class ChatManager extends Emitter<ConversationMessage> {
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
Expand Down Expand Up @@ -198,12 +212,14 @@ export default class ChatManager extends Emitter<ConversationMessage> {
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}`, {
Expand All @@ -219,15 +235,28 @@ export default class ChatManager extends Emitter<ConversationMessage> {
* @param did {string} did
* @param onMessage {EmitterCallback<IridiumMessage>} function to be called
*/
async subscribeToChannel(
did: string,
onMessage: EmitterCallback<ConversationMessage>,
) {
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,
})
}
},
)
}
}
9 changes: 2 additions & 7 deletions pages/chat/direct/_address.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
})
Expand Down

0 comments on commit 1cc282e

Please sign in to comment.