Skip to content

Commit

Permalink
fix case-sensitivity issues
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Dec 10, 2024
1 parent 338242a commit b823e82
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
45 changes: 33 additions & 12 deletions docs/2-development/1-known-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,51 @@ export async function updateSimpleModeState() {

### 1. Duplicate Keywords

**Problem**: Keywords like "Paris Agreement" appearing multiple times with different cases.
**Problem**: Keywords like "Paris Agreement" appearing multiple times with different cases, particularly when switching between modes.

**Root Cause**: Case-sensitive keyword storage causing duplicates.
**Root Cause**: Inconsistent case handling across different parts of the codebase, especially during mode transitions and context handling.

**Solution**: Implemented case-insensitive storage with original case preservation:
**Solution**: Implemented comprehensive case-insensitive handling across all keyword operations:
```javascript
// Store lowercase for comparison
const lowerKeyword = keyword.toLowerCase();
state.originalMutedKeywords.add(lowerKeyword);

// Preserve original case for display
const originalCase = ourKeywordsMap.get(lowerKeyword);
if (originalCase) {
state.activeKeywords.add(originalCase);
// Helper function for case-insensitive removal
export function removeKeyword(keyword) {
const lowerKeyword = keyword.toLowerCase();
for (const activeKeyword of state.activeKeywords) {
if (activeKeyword.toLowerCase() === lowerKeyword) {
state.activeKeywords.delete(activeKeyword);
break;
}
}
}

// Helper function for case-sensitive addition with deduplication
function addKeywordWithCase(keyword) {
// First remove any existing case variations
removeKeyword(keyword);
// Then add with original case
state.activeKeywords.add(keyword);
}
```

**Implementation Details**:
- Case-insensitive checks using isKeywordActive()
- Case-insensitive removal using removeKeyword()
- Case-preserving addition using addKeywordWithCase()
- Consistent handling across mode switches and context changes

**Result**:
- No more duplicate keywords with different cases
- Maintains proper keyword counts during mode switches
- Preserves original case for display purposes
- Consistent behavior across all operations

### 2. Payload Size Issues

**Problem**: "413 Payload Too Large" error when sending to Bluesky.

**Root Cause**: Duplicate keywords with different cases inflating payload size.

**Solution**: Case-insensitive deduplication before API calls.
**Solution**: Case-insensitive deduplication is now handled consistently across all operations, preventing duplicates from being added in the first place.

## Authentication Issues

Expand Down
22 changes: 15 additions & 7 deletions js/handlers/context/contextState.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { state, saveState, getStorageKey } from '../../state.js';
import { renderInterface } from '../../renderer.js';
import { cache } from './contextCache.js';
import { isKeywordActive } from '../keywordHandlers.js';
import { isKeywordActive, removeKeyword } from '../keywordHandlers.js';
import {
rebuildActiveKeywords,
createDebouncedUpdate,
activateContextKeywords,
notifyKeywordChanges
} from './contextUtils.js';

// Helper function to add keyword with case handling
function addKeywordWithCase(keyword) {
// First remove any existing case variations
removeKeyword(keyword);
// Then add with original case
state.activeKeywords.add(keyword);
}

export async function updateSimpleModeState() {
if (!state.authenticated) return;

Expand Down Expand Up @@ -70,14 +78,14 @@ export async function updateSimpleModeState() {

// Add only original muted keywords that aren't already active and weren't manually unchecked
for (const keyword of state.originalMutedKeywords) {
if (!state.activeKeywords.has(keyword) && !state.manuallyUnchecked.has(keyword)) {
state.activeKeywords.add(keyword);
if (!isKeywordActive(keyword) && !state.manuallyUnchecked.has(keyword)) {
addKeywordWithCase(keyword);
}
}

// Re-apply unchecked status
for (const keyword of uncheckedKeywords) {
state.activeKeywords.delete(keyword);
removeKeyword(keyword);
state.manuallyUnchecked.add(keyword);
}
}
Expand Down Expand Up @@ -171,14 +179,14 @@ export async function initializeState() {

// Add only original muted keywords that aren't already active and weren't manually unchecked
for (const keyword of state.originalMutedKeywords) {
if (!state.activeKeywords.has(keyword) && !state.manuallyUnchecked.has(keyword)) {
state.activeKeywords.add(keyword);
if (!isKeywordActive(keyword) && !state.manuallyUnchecked.has(keyword)) {
addKeywordWithCase(keyword);
}
}

// Re-apply unchecked status
for (const keyword of Array.from(state.manuallyUnchecked)) {
state.activeKeywords.delete(keyword);
removeKeyword(keyword);
}
}

Expand Down
17 changes: 13 additions & 4 deletions js/handlers/context/contextUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { state } from '../../state.js';
import { isKeywordActive, removeKeyword } from '../keywordHandlers.js';

// Helper function to notify keyword changes
export function notifyKeywordChanges() {
Expand Down Expand Up @@ -45,6 +46,14 @@ export function processBatchKeywords(keywords, operation) {
processChunk();
}

// Helper function to add keyword with case handling
function addKeywordWithCase(keyword) {
// First remove any existing case variations
removeKeyword(keyword);
// Then add with original case
state.activeKeywords.add(keyword);
}

// Helper function to activate context keywords
export function activateContextKeywords(contextId, cache) {
const context = state.contextGroups[contextId];
Expand All @@ -57,7 +66,7 @@ export function activateContextKeywords(contextId, cache) {
processBatchKeywords(keywords, keyword => {
// Only activate if not manually unchecked
if (!state.manuallyUnchecked.has(keyword)) {
state.activeKeywords.add(keyword);
addKeywordWithCase(keyword);
}
});
}
Expand All @@ -78,14 +87,14 @@ export function rebuildActiveKeywords(cache) {

// Add only original muted keywords that aren't already active and weren't manually unchecked
for (const keyword of state.originalMutedKeywords) {
if (!state.activeKeywords.has(keyword) && !state.manuallyUnchecked.has(keyword)) {
state.activeKeywords.add(keyword);
if (!isKeywordActive(keyword) && !state.manuallyUnchecked.has(keyword)) {
addKeywordWithCase(keyword);
}
}

// Re-apply unchecked status
for (const keyword of uncheckedKeywords) {
state.activeKeywords.delete(keyword);
removeKeyword(keyword);
state.manuallyUnchecked.add(keyword);
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/handlers/keywordHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function isKeywordActive(keyword) {
}

// Helper to remove keyword (case-insensitive)
function removeKeyword(keyword) {
export function removeKeyword(keyword) {
const lowerKeyword = keyword.toLowerCase();
for (const activeKeyword of state.activeKeywords) {
if (activeKeyword.toLowerCase() === lowerKeyword) {
Expand Down

0 comments on commit b823e82

Please sign in to comment.