Skip to content

Commit

Permalink
Optimize automaticMuteBlockedUsers Function for Improved Performance
Browse files Browse the repository at this point in the history
Description:
Refactored the automaticMuteBlockedUsers function to enhance performance and reduce resource usage.
Implemented batch processing for muting blocked users, allowing multiple users to be muted in a single operation.
Utilized a Set for storing blocked user IDs, improving lookup efficiency when checking if a user is blocked.
  • Loading branch information
Vexify4103 committed Nov 24, 2024
1 parent fc0cdc1 commit 24f80fb
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/plugins/voiceMuteBlockedUsers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const { isLocalMute } = findByPropsLazy("isLocalMute");
const { addRelationship } = findByPropsLazy("addRelationship");
const RoleButtonClasses = findByPropsLazy("button", "buttonInner", "icon", "banner");

const blockedUserIds: Set<string> = new Set();
let blockedUserIds: Set<string> = new Set();
let blockedUserCount = 0;

const userContextPatch: NavContextMenuPatchCallback = (children, { user }: { user?: User, onClose(): void; }) => {
Expand Down Expand Up @@ -109,30 +109,31 @@ export default definePlugin({
if (!autoMuteBlocked) return;

// Get all relationships and filter for blocked users
const blockedIds = Object.entries(RelationshipStore.getRelationships())
.filter(([_, v]) => v === 2)
.map(([k]) => UserStore.getUser(k).id);

// Mute blocked users
for (const ID of blockedIds) {
if (!isLocalMute(ID)) {
toggleLocalMute(ID);
}
blockedUserIds.add(ID);
}
const blockedIdsSet = new Set(
Object.entries(RelationshipStore.getRelationships())
.filter(([_, v]) => v === 2)
.map(([k]) => UserStore.getUser(k).id)
);

if (blockedUserCount > blockedIds.length) {
const unblockedUsers = [...blockedUserIds].filter(id => !blockedIds.includes(id));
// Mute blocked users in batch
const toMute = [...blockedIdsSet].filter(ID => !isLocalMute(ID));
if (toMute.length > 0) {
toMute.forEach(ID => toggleLocalMute(ID));
blockedUserIds = new Set([...blockedUserIds, ...toMute]); // Update blockedUserIds
}

for (const ID of unblockedUsers) {
// Handle unblocking
if (blockedUserCount > blockedIdsSet.size) {
const unblockedUsers = [...blockedUserIds].filter(id => !blockedIdsSet.has(id));
unblockedUsers.forEach(ID => {
if (isLocalMute(ID)) {
toggleLocalMute(ID);
}
blockedUserIds.delete(ID);
}
});
}

blockedUserCount = blockedIds.length;
blockedUserCount = blockedIdsSet.size;
},
BlockUnblockButton: ErrorBoundary.wrap(({ user }: { user: User; }) => {
if (!user) return null; // Return null if no user is provided
Expand Down

0 comments on commit 24f80fb

Please sign in to comment.