Skip to content

Commit

Permalink
fix: group chat users (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
agazso authored Sep 8, 2023
1 parent dcf4d82 commit 8fcc0bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
8 changes: 6 additions & 2 deletions src/lib/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { DraftChat } from '$lib/stores/chat'
import type { Token } from '$lib/stores/balances'
import WakuAdapter from '$lib/adapters/waku'
import type { BaseWallet } from 'ethers'
Expand All @@ -12,7 +11,12 @@ export interface Adapter {
getUserProfile(address: string): Promise<User | undefined>

startChat(address: string, peerAddress: string): Promise<string>
startGroupChat(wallet: BaseWallet, chat: DraftChat): Promise<string>
startGroupChat(
wallet: BaseWallet,
memberAddresses: string[],
name: string,
avatar?: string,
): Promise<string>

addMemberToGroupChat(chatId: string, users: string[]): Promise<void>
removeFromGroupChat(chatId: string, address: string): Promise<void>
Expand Down
51 changes: 32 additions & 19 deletions src/lib/adapters/waku/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { profile } from '$lib/stores/profile'
import type { Adapter } from '..'
import {
chats,
type DraftChat,
type Chat,
type Message,
isGroupChatId,
Expand Down Expand Up @@ -287,26 +286,34 @@ export default class WakuAdapter implements Adapter {
return chatId
}

async startGroupChat(wallet: BaseWallet, chat: DraftChat): Promise<string> {
async startGroupChat(
wallet: BaseWallet,
memberAddresses: string[],
name: string,
avatar?: string,
): Promise<string> {
if (!this.waku) {
this.waku = await connectWaku()
}
if (chat.users.length === 0) {
if (memberAddresses.length === 0) {
throw 'invalid chat'
}

const chatId = genRandomHex(64)

const userPromises = chat.users.map((address) => this.storageProfileToUser(address))
const allUsers = await Promise.all(userPromises)
const users = allUsers.filter((user) => user) as User[]

const userAddresses = [...memberAddresses, wallet.address]
const storageChat = {
users: userAddresses,
name,
avatar,
}
const chat = await this.storageChatToChat(chatId, storageChat)
const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet)

createGroupChat(chatId, users, chat.name, chat.avatar, true)
createGroupChat(chatId, chat.users, name, avatar, true)

const ws = makeWakustore(this.waku)
await ws.setDoc<StorageChat>('group-chats', chatId, chat)
await ws.setDoc<StorageChat>('group-chats', chatId, storageChat)
await this.subscribeToGroupChat(chatId, wallet.address, wakuObjectAdapter)

return chatId
Expand Down Expand Up @@ -699,27 +706,33 @@ export default class WakuAdapter implements Adapter {

private async updateContactProfiles() {
// look for changes in users profile name and picture
const contacts = Array.from(get(chats).chats).flatMap(([, chat]) => chat.users)
const allContacts = Array.from(get(chats).chats).flatMap(([, chat]) => chat.users)
const uniqueContacts = new Map<string, User>(allContacts.map((user) => [user.address, user]))
const changes = new Map<string, User>()
for (const contact of contacts) {
const storageProfile = await this.fetchStorageProfile(contact.address)
for (const contact of uniqueContacts) {
const user = contact[1]
const storageProfile = await this.fetchStorageProfile(user.address)

if (!storageProfile) {
continue
}

if (storageProfile.name != contact.name || storageProfile.avatar != contact.avatar) {
changes.set(contact.address, { ...storageProfile, address: contact.address })
if (storageProfile.name != user.name || storageProfile.avatar != user.avatar) {
changes.set(user.address, { ...storageProfile, address: user.address })
}
}
if (changes.size > 0) {
chats.update((state) => {
const newChats = new Map<string, Chat>(state.chats)
changes.forEach((user) => {
const chat = newChats.get(user.address)
if (chat) {
chat.users[0] = user
}
newChats.forEach((chat) => {
chat.users.forEach((user) => {
const changedUser = changes.get(user.address)
if (!changedUser) {
return
}
user.name = changedUser.name
user.avatar = changedUser.avatar
})
})
return {
...state,
Expand Down
6 changes: 0 additions & 6 deletions src/lib/stores/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ export interface InviteMessage {

export type Message = UserMessage | DataMessage | InviteMessage

export interface DraftChat {
users: string[]
name: string
avatar?: string
}

export interface Chat {
chatId: string
messages: Message[]
Expand Down
8 changes: 1 addition & 7 deletions src/routes/group/new/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@
async function createGroup(wallet: HDNodeWallet) {
buttonDisabled = true
const groupChat = {
messages: [],
users: [...groupMembers, wallet.address],
name,
avatar: picture,
}
const chatId = await adapters.startGroupChat(wallet, groupChat)
const chatId = await adapters.startGroupChat(wallet, groupMembers, name, picture)
await adapters.sendInvite(wallet, chatId, groupMembers)
buttonDisabled = false
Expand Down

1 comment on commit 8fcc0bf

@vercel
Copy link

@vercel vercel bot commented on 8fcc0bf Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.