Skip to content

Commit

Permalink
fix bug with membership sync logic
Browse files Browse the repository at this point in the history
- logic for collecting users which are to leave the room was incorrect
- additionally, now we just work with matrix user ids for both leaving/joining of new users, as opposed to working with SlackGhost object
  • Loading branch information
ashfame committed Oct 10, 2023
1 parent ec1ab0b commit 190c92a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/TeamSyncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,28 +419,36 @@ export class TeamSyncer {
// Ghosts will exist already: We joined them in the user sync.
const ghosts = await Promise.all(members.members.map(async(slackUserId) => this.main.ghostStore.get(slackUserId, teamInfo.domain, teamId)));

const joinedUsers = ghosts.filter((g) => !existingGhosts.includes(g.matrixUserId)); // Skip users that are joined.
const leftUsers = existingGhosts.map((userId) => ghosts.find((g) => g.matrixUserId === userId )).filter(g => !!g) as SlackGhost[];
log.info(`Joining ${joinedUsers.length} ghosts to ${roomId}`);
log.info(`Leaving ${leftUsers.length} ghosts to ${roomId}`);
const joinedUsers = ghosts.filter((g) => !existingGhosts.includes(g.matrixUserId)).map(g => g.matrixUserId);
const leftUsers = existingGhosts.map((userId, index) => {
const ghost = ghosts.find((g) => g.matrixUserId === userId);
return ghost === undefined ? index : null;
}).filter(index => index !== null).map(index =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
existingGhosts[index]
);

log.info(`Joining ${joinedUsers.length} ghosts to ${roomId}`,joinedUsers);
log.info(`Leaving ${leftUsers.length} ghosts to ${roomId}`,leftUsers);

const queue = new PQueue({concurrency: JOIN_CONCURRENCY});

// Join users who aren't joined
queue.addAll(joinedUsers.map((ghost) => async () => {
queue.addAll(joinedUsers.map((userId) => async () => {
try {
await this.main.membershipQueue.join(roomId, ghost.matrixUserId, { getId: () => ghost.matrixUserId });
await this.main.membershipQueue.join(roomId, userId, { getId: () => userId });
} catch (ex) {
log.warn(`Failed to join ${ghost.matrixUserId} to ${roomId}`);
log.warn(`Failed to join ${userId} to ${roomId}`);
}
})).catch((ex) => log.error(`queue.addAll(joinedUsers) rejected with an error:`, ex));

// Leave users who are joined
queue.addAll(leftUsers.map((ghost) => async () => {
queue.addAll(leftUsers.map((userId) => async () => {
try {
await this.main.membershipQueue.leave(roomId, ghost.matrixUserId, { getId: () => ghost.matrixUserId });
await this.main.membershipQueue.leave(roomId, userId, { getId: () => userId });
} catch (ex) {
log.warn(`Failed to leave ${ghost.matrixUserId} from ${roomId}`);
log.warn(`Failed to leave ${userId} from ${roomId}`);
}
})).catch((ex) => log.error(`queue.addAll(leftUsers) rejected with an error:`, ex));

Expand Down

0 comments on commit 190c92a

Please sign in to comment.