1
0
Fork 1
mirror of https://github.com/Vendicated/Vencord.git synced 2025-01-10 09:56:24 +00:00

Optimize automaticMuteBlockedUsers Function for Improved Performance

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.
This commit is contained in:
Vexify 2024-11-24 13:55:44 +01:00
parent fc0cdc1da5
commit 24f80fb366

View file

@ -20,7 +20,7 @@ const { isLocalMute } = findByPropsLazy("isLocalMute");
const { addRelationship } = findByPropsLazy("addRelationship"); const { addRelationship } = findByPropsLazy("addRelationship");
const RoleButtonClasses = findByPropsLazy("button", "buttonInner", "icon", "banner"); const RoleButtonClasses = findByPropsLazy("button", "buttonInner", "icon", "banner");
const blockedUserIds: Set<string> = new Set(); let blockedUserIds: Set<string> = new Set();
let blockedUserCount = 0; let blockedUserCount = 0;
const userContextPatch: NavContextMenuPatchCallback = (children, { user }: { user?: User, onClose(): void; }) => { const userContextPatch: NavContextMenuPatchCallback = (children, { user }: { user?: User, onClose(): void; }) => {
@ -109,30 +109,31 @@ export default definePlugin({
if (!autoMuteBlocked) return; if (!autoMuteBlocked) return;
// Get all relationships and filter for blocked users // Get all relationships and filter for blocked users
const blockedIds = Object.entries(RelationshipStore.getRelationships()) const blockedIdsSet = new Set(
.filter(([_, v]) => v === 2) Object.entries(RelationshipStore.getRelationships())
.map(([k]) => UserStore.getUser(k).id); .filter(([_, v]) => v === 2)
.map(([k]) => UserStore.getUser(k).id)
);
// Mute blocked users // Mute blocked users in batch
for (const ID of blockedIds) { const toMute = [...blockedIdsSet].filter(ID => !isLocalMute(ID));
if (!isLocalMute(ID)) { if (toMute.length > 0) {
toggleLocalMute(ID); toMute.forEach(ID => toggleLocalMute(ID));
} blockedUserIds = new Set([...blockedUserIds, ...toMute]); // Update blockedUserIds
blockedUserIds.add(ID);
} }
if (blockedUserCount > blockedIds.length) { // Handle unblocking
const unblockedUsers = [...blockedUserIds].filter(id => !blockedIds.includes(id)); if (blockedUserCount > blockedIdsSet.size) {
const unblockedUsers = [...blockedUserIds].filter(id => !blockedIdsSet.has(id));
for (const ID of unblockedUsers) { unblockedUsers.forEach(ID => {
if (isLocalMute(ID)) { if (isLocalMute(ID)) {
toggleLocalMute(ID); toggleLocalMute(ID);
} }
blockedUserIds.delete(ID); blockedUserIds.delete(ID);
} });
} }
blockedUserCount = blockedIds.length; blockedUserCount = blockedIdsSet.size;
}, },
BlockUnblockButton: ErrorBoundary.wrap(({ user }: { user: User; }) => { BlockUnblockButton: ErrorBoundary.wrap(({ user }: { user: User; }) => {
if (!user) return null; // Return null if no user is provided if (!user) return null; // Return null if no user is provided