Skip to content

Commit

Permalink
feat(lib): move emitting events
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-suwala committed Oct 26, 2023
1 parent 7ab016c commit c7e243b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
32 changes: 30 additions & 2 deletions lib/src/entities/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,23 @@ export class Channel {
filter: `channel.id == '${this.id}'`,
})

return await Membership.fromMembershipDTO(
const membershipData = await Membership.fromMembershipDTO(
this.chat,
response.data[0],
user
).setLastReadMessageTimetoken(String((await this.chat.sdk.time()).timetoken))

await this.chat.emitEvent({
channel: user.id,
type: "invite",
method: "publish",
payload: {
channelType: "direct",
channelId: this.id,
},
})

return membershipData
} catch (error) {
throw error
}
Expand All @@ -520,13 +532,29 @@ export class Channel {
})
const { timetoken } = await this.chat.sdk.time()

return await Promise.all(
const inviteesMemberships = await Promise.all(
response.data.map((dataPoint) =>
Membership.fromChannelMemberDTO(this.chat, dataPoint, this).setLastReadMessageTimetoken(
String(timetoken)
)
)
)

await Promise.all(
users.map(async (u) => {
await this.chat.emitEvent({
channel: u.id,
method: "publish",
type: "invite",
payload: {
channelType: "group",
channelId: this.id,
},
})
})
)

return inviteesMemberships
} catch (error) {
throw error
}
Expand Down
24 changes: 0 additions & 24 deletions lib/src/entities/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,16 +766,6 @@ export class Chat {
channel.invite(user),
])

await this.emitEvent({
channel: user.id,
type: "invite",
method: "publish",
payload: {
channelType: "direct",
channelId: channel.id,
},
})

return {
channel,
hostMembership: Membership.fromMembershipDTO(
Expand Down Expand Up @@ -834,20 +824,6 @@ export class Chat {
channel.inviteMultiple(users),
])

await Promise.all(
users.map(async (u) => {
await this.emitEvent({
channel: u.id,
method: "publish",
type: "invite",
payload: {
channelType: "group",
channelId: channel.id,
},
})
})
)

return {
channel,
hostMembership: Membership.fromMembershipDTO(
Expand Down
19 changes: 12 additions & 7 deletions lib/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,11 @@ describe("Channel test", () => {
})

test("should create group conversation", async () => {
const user1 = await createRandomUser()
const user2 = await createRandomUser()
const user3 = await createRandomUser()
const [user1, user2, user3] = await Promise.all([
createRandomUser(),
createRandomUser(),
createRandomUser(),
])
const inviteCallback = jest.fn()

const removeInvitationListener = chat.listenForEvents({
Expand Down Expand Up @@ -285,6 +287,7 @@ describe("Channel test", () => {
expect(channel.description).toEqual("This is a test group channel.")
expect(channel.custom.groupInfo).toEqual("Additional group information")
expect(inviteesMemberships.length).toEqual(3)
await sleep(150) // history calls have around 130ms of cache time
expect(inviteCallback).toHaveBeenCalledTimes(1)
expect(inviteCallback).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -295,10 +298,12 @@ describe("Channel test", () => {
})
)

await user1.delete()
await user2.delete()
await user3.delete()
await channel.delete()
await Promise.all([
user1.delete(),
user2.delete(),
user3.delete(),
channel.delete({ soft: false }),
])
removeInvitationListener()
})

Expand Down

0 comments on commit c7e243b

Please sign in to comment.