fix: proper boundary on the project in the data
This commit is contained in:
@@ -318,36 +318,54 @@ export class DatabaseConnection {
|
||||
}
|
||||
|
||||
// Create FTS5 virtual table for full-text search
|
||||
// Only stores: id (unindexed, for lookups) and content (stemmed text for matching)
|
||||
// Stores: id (unindexed, for lookups), project_id (unindexed, for filtering), content (stemmed text for matching)
|
||||
// Post data for display comes from the posts table or filesystem files
|
||||
await this.localClient.execute(`
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS posts_fts USING fts5(
|
||||
id UNINDEXED,
|
||||
project_id UNINDEXED,
|
||||
content,
|
||||
content_rowid=rowid
|
||||
);
|
||||
`);
|
||||
|
||||
// Migration: Check if old FTS schema (with multiple columns) exists and recreate
|
||||
// Old schema had: id, title, content, excerpt, tags, categories, content_stemmed
|
||||
// New schema has: id, content (stemmed only)
|
||||
// Migration: Check if old FTS schema exists and recreate with project_id
|
||||
// Old schema had: id, content (or even older: id, title, content, excerpt, tags, categories)
|
||||
// New schema has: id, project_id, content (for project-scoped search)
|
||||
try {
|
||||
// Try to query old columns - if they exist, we need to migrate
|
||||
await this.localClient.execute("SELECT title FROM posts_fts LIMIT 0");
|
||||
|
||||
// Old schema exists - recreate with new simple schema
|
||||
console.log('Migrating posts_fts table to simplified schema...');
|
||||
// Try to query project_id - if it doesn't exist, we need to migrate
|
||||
await this.localClient.execute("SELECT project_id FROM posts_fts LIMIT 0");
|
||||
// project_id exists, check for old multi-column schema
|
||||
try {
|
||||
await this.localClient.execute("SELECT title FROM posts_fts LIMIT 0");
|
||||
// Old multi-column schema exists - recreate
|
||||
console.log('Migrating posts_fts table to new schema with project_id...');
|
||||
await this.localClient.execute('DROP TABLE IF EXISTS posts_fts');
|
||||
await this.localClient.execute(`
|
||||
CREATE VIRTUAL TABLE posts_fts USING fts5(
|
||||
id UNINDEXED,
|
||||
project_id UNINDEXED,
|
||||
content,
|
||||
content_rowid=rowid
|
||||
);
|
||||
`);
|
||||
console.log('FTS table migrated - rebuild index required');
|
||||
} catch {
|
||||
// No title column - we have the correct new schema
|
||||
}
|
||||
} catch {
|
||||
// project_id doesn't exist - migrate from old schema
|
||||
console.log('Migrating posts_fts table to add project_id...');
|
||||
await this.localClient.execute('DROP TABLE IF EXISTS posts_fts');
|
||||
await this.localClient.execute(`
|
||||
CREATE VIRTUAL TABLE posts_fts USING fts5(
|
||||
id UNINDEXED,
|
||||
project_id UNINDEXED,
|
||||
content,
|
||||
content_rowid=rowid
|
||||
);
|
||||
`);
|
||||
console.log('FTS table migrated - rebuild index required');
|
||||
} catch {
|
||||
// Old columns don't exist - we have the new schema or no data, all good
|
||||
}
|
||||
|
||||
// Create default project if none exists
|
||||
|
||||
@@ -96,11 +96,13 @@ export class PostEngine extends EventEmitter {
|
||||
/**
|
||||
* Update the FTS index for a post.
|
||||
* Updates the FTS index for a post.
|
||||
* Stores only the stemmed content (combining title, excerpt, content, tags, categories).
|
||||
* Stores the stemmed content (combining title, excerpt, content, tags, categories).
|
||||
* Includes project_id for project-scoped search.
|
||||
* Only the post ID is returned from searches - actual post data comes from DB/files.
|
||||
*/
|
||||
private async updateFTSIndex(post: {
|
||||
id: string;
|
||||
projectId: string;
|
||||
title: string;
|
||||
content: string;
|
||||
excerpt?: string;
|
||||
@@ -124,10 +126,10 @@ export class PostEngine extends EventEmitter {
|
||||
|
||||
const stemmedContent = stemText(allText, this.searchLanguage);
|
||||
|
||||
// Insert with only id and stemmed content
|
||||
// Insert with id, project_id, and stemmed content
|
||||
await client.execute({
|
||||
sql: 'INSERT INTO posts_fts (id, content) VALUES (?, ?)',
|
||||
args: [post.id, stemmedContent],
|
||||
sql: 'INSERT INTO posts_fts (id, project_id, content) VALUES (?, ?, ?)',
|
||||
args: [post.id, post.projectId, stemmedContent],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -670,26 +672,25 @@ export class PostEngine extends EventEmitter {
|
||||
// Stem the query for multilingual matching
|
||||
const stemmedQuery = stemQuery(query, this.searchLanguage);
|
||||
|
||||
// Search the stemmed content, only return post IDs
|
||||
// Search the stemmed content, filtered by project_id for project isolation
|
||||
const result = await client.execute({
|
||||
sql: `SELECT id FROM posts_fts WHERE posts_fts MATCH ? ORDER BY rank LIMIT 50`,
|
||||
args: [stemmedQuery],
|
||||
sql: `SELECT id FROM posts_fts WHERE project_id = ? AND posts_fts MATCH ? ORDER BY rank LIMIT 50`,
|
||||
args: [this.currentProjectId, stemmedQuery],
|
||||
});
|
||||
|
||||
// Filter to current project and fetch actual post data
|
||||
const projectPosts = await this.getAllPostsUnpaginated();
|
||||
const projectPostMap = new Map(projectPosts.map(p => [p.id, p]));
|
||||
|
||||
// Fetch actual post data for results
|
||||
const db = getDatabase().getLocal();
|
||||
const searchResults: SearchResult[] = [];
|
||||
|
||||
for (const row of result.rows) {
|
||||
const postId = row.id as string;
|
||||
const post = projectPostMap.get(postId);
|
||||
const post = await db.select().from(posts).where(eq(posts.id, postId)).get();
|
||||
if (post) {
|
||||
searchResults.push({
|
||||
id: post.id,
|
||||
title: post.title,
|
||||
slug: post.slug,
|
||||
excerpt: post.excerpt,
|
||||
excerpt: post.excerpt ?? undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,19 +128,19 @@ const PostEditor: React.FC<PostEditorProps> = ({ post }) => {
|
||||
window.electronAPI?.posts.hasPublishedVersion(post.id).then(setHasPublishedVersion);
|
||||
}, [post.id]);
|
||||
|
||||
// Load available categories from localStorage
|
||||
// Load available categories from backend (project-scoped)
|
||||
useEffect(() => {
|
||||
const savedCategories = localStorage.getItem('bds-categories');
|
||||
if (savedCategories) {
|
||||
const loadCategories = async () => {
|
||||
try {
|
||||
const parsed = JSON.parse(savedCategories);
|
||||
if (Array.isArray(parsed) && parsed.length > 0) {
|
||||
setAvailableCategories(parsed);
|
||||
const categories = await window.electronAPI?.meta.getCategories();
|
||||
if (categories && categories.length > 0) {
|
||||
setAvailableCategories(categories);
|
||||
}
|
||||
} catch {
|
||||
// Keep defaults
|
||||
}
|
||||
}
|
||||
};
|
||||
loadCategories();
|
||||
}, []);
|
||||
|
||||
// Resolve media URLs in content for display
|
||||
|
||||
@@ -138,13 +138,13 @@ export const SettingsView: React.FC = () => {
|
||||
setCredentials({ ...defaultCredentials, ...JSON.parse(savedCreds) });
|
||||
}
|
||||
|
||||
// Load saved post categories
|
||||
const savedCategories = localStorage.getItem('bds-categories');
|
||||
if (savedCategories) {
|
||||
const categories = JSON.parse(savedCategories);
|
||||
if (Array.isArray(categories) && categories.length > 0) {
|
||||
setPostCategories(categories);
|
||||
}
|
||||
// Load categories from backend (project-scoped)
|
||||
const categories = await window.electronAPI?.meta.getCategories();
|
||||
if (categories && categories.length > 0) {
|
||||
setPostCategories(categories);
|
||||
} else {
|
||||
// Initialize with defaults if no categories exist
|
||||
setPostCategories(DEFAULT_POST_CATEGORIES);
|
||||
}
|
||||
|
||||
// Check Dropbox status
|
||||
@@ -160,7 +160,7 @@ export const SettingsView: React.FC = () => {
|
||||
}
|
||||
};
|
||||
loadSettings();
|
||||
}, []);
|
||||
}, [activeProject?.id]); // Reload when project changes
|
||||
|
||||
const handleSaveDropbox = async () => {
|
||||
try {
|
||||
@@ -343,20 +343,26 @@ export const SettingsView: React.FC = () => {
|
||||
);
|
||||
|
||||
// Handlers for post categories management
|
||||
const handleAddCategory = () => {
|
||||
const handleAddCategory = async () => {
|
||||
const trimmed = newCategoryInput.trim().toLowerCase();
|
||||
if (trimmed && !postCategories.includes(trimmed)) {
|
||||
const updated = [...postCategories, trimmed];
|
||||
setPostCategories(updated);
|
||||
localStorage.setItem('bds-categories', JSON.stringify(updated));
|
||||
setNewCategoryInput('');
|
||||
showToast.success(`Category "${trimmed}" added`);
|
||||
try {
|
||||
const updatedCategories = await window.electronAPI?.meta.addCategory(trimmed);
|
||||
if (updatedCategories) {
|
||||
setPostCategories(updatedCategories);
|
||||
}
|
||||
setNewCategoryInput('');
|
||||
showToast.success(`Category "${trimmed}" added`);
|
||||
} catch (error) {
|
||||
console.error('Failed to add category:', error);
|
||||
showToast.error('Failed to add category');
|
||||
}
|
||||
} else if (postCategories.includes(trimmed)) {
|
||||
showToast.error('Category already exists');
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemoveCategory = (categoryToRemove: string) => {
|
||||
const handleRemoveCategory = async (categoryToRemove: string) => {
|
||||
if (PROTECTED_CATEGORIES.includes(categoryToRemove)) {
|
||||
showToast.error(`Cannot delete standard category "${categoryToRemove}"`);
|
||||
return;
|
||||
@@ -365,16 +371,39 @@ export const SettingsView: React.FC = () => {
|
||||
showToast.error('Must have at least one category');
|
||||
return;
|
||||
}
|
||||
const updated = postCategories.filter(c => c !== categoryToRemove);
|
||||
setPostCategories(updated);
|
||||
localStorage.setItem('bds-categories', JSON.stringify(updated));
|
||||
showToast.success(`Category "${categoryToRemove}" removed`);
|
||||
try {
|
||||
const updatedCategories = await window.electronAPI?.meta.removeCategory(categoryToRemove);
|
||||
if (updatedCategories) {
|
||||
setPostCategories(updatedCategories);
|
||||
}
|
||||
showToast.success(`Category "${categoryToRemove}" removed`);
|
||||
} catch (error) {
|
||||
console.error('Failed to remove category:', error);
|
||||
showToast.error('Failed to remove category');
|
||||
}
|
||||
};
|
||||
|
||||
const handleResetCategories = () => {
|
||||
setPostCategories(DEFAULT_POST_CATEGORIES);
|
||||
localStorage.setItem('bds-categories', JSON.stringify(DEFAULT_POST_CATEGORIES));
|
||||
showToast.success('Categories reset to defaults');
|
||||
const handleResetCategories = async () => {
|
||||
try {
|
||||
// Remove non-protected categories
|
||||
const currentCategories = await window.electronAPI?.meta.getCategories() || [];
|
||||
for (const cat of currentCategories) {
|
||||
if (!PROTECTED_CATEGORIES.includes(cat)) {
|
||||
await window.electronAPI?.meta.removeCategory(cat);
|
||||
}
|
||||
}
|
||||
// Add any missing default categories
|
||||
for (const cat of DEFAULT_POST_CATEGORIES) {
|
||||
await window.electronAPI?.meta.addCategory(cat);
|
||||
}
|
||||
// Refresh the list
|
||||
const updatedCategories = await window.electronAPI?.meta.getCategories();
|
||||
setPostCategories(updatedCategories || DEFAULT_POST_CATEGORIES);
|
||||
showToast.success('Categories reset to defaults');
|
||||
} catch (error) {
|
||||
console.error('Failed to reset categories:', error);
|
||||
showToast.error('Failed to reset categories');
|
||||
}
|
||||
};
|
||||
|
||||
const renderContentSettings = () => (
|
||||
|
||||
17
src/renderer/types/electron.d.ts
vendored
17
src/renderer/types/electron.d.ts
vendored
@@ -1,5 +1,10 @@
|
||||
// Type definitions for the Electron API exposed via preload
|
||||
|
||||
export interface ProjectMetadata {
|
||||
name: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface ProjectData {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -206,6 +211,18 @@ export interface ElectronAPI {
|
||||
openFolder: (folderPath: string) => Promise<string>;
|
||||
showItemInFolder: (itemPath: string) => Promise<void>;
|
||||
};
|
||||
meta: {
|
||||
getTags: () => Promise<string[]>;
|
||||
getCategories: () => Promise<string[]>;
|
||||
addTag: (tag: string) => Promise<string[]>;
|
||||
removeTag: (tag: string) => Promise<string[]>;
|
||||
addCategory: (category: string) => Promise<string[]>;
|
||||
removeCategory: (category: string) => Promise<string[]>;
|
||||
syncOnStartup: () => Promise<{ tags: string[]; categories: string[]; projectMetadata: ProjectMetadata | null }>;
|
||||
getProjectMetadata: () => Promise<ProjectMetadata | null>;
|
||||
setProjectMetadata: (metadata: { name: string; description?: string }) => Promise<ProjectMetadata | null>;
|
||||
updateProjectMetadata: (updates: { name?: string; description?: string }) => Promise<ProjectMetadata | null>;
|
||||
};
|
||||
on: (channel: string, callback: (...args: unknown[]) => void) => () => void;
|
||||
once: (channel: string, callback: (...args: unknown[]) => void) => void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user