Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update favicon logic to empty favicon on removal of URL value (#1473)
Leendert de Borste893602080e2
diff --git a/apps/browser-extension/src/utils/FaviconService.ts b/apps/browser-extension/src/utils/FaviconService.tsindex 08c59f875..eca31ae27 100644--- a/apps/browser-extension/src/utils/FaviconService.ts+++ b/apps/browser-extension/src/utils/FaviconService.ts@@ -173,11 +173,12 @@ export class FaviconService {/*** Fetch and attach favicon to an item if needed.* This is a convenience method that combines URL extraction, deduplication, and fetching.+ * If the URL is empty or invalid, clears any existing logo from the item.* @param item The item to potentially update with a logo* @param urlFieldValue The value of the URL field (can be string or string[])* @param sqliteClient The SQLite client for deduplication check* @param webApi The WebAPI service for making the request- * @returns The updated item with Logo attached (if favicon was fetched), or the original item+ * @returns The updated item with Logo attached (if favicon was fetched), cleared (if URL is empty), or the original item*/public static async fetchAndAttachFavicon(item: Item,@@ -186,8 +187,13 @@ export class FaviconService {webApi: WebApiService): Promise<Item> {const urlString = FaviconService.extractFirstValidUrl(urlFieldValue);++ // If URL is empty or invalid, explicitly clear logo to signal that any existing logo should be removedif (!urlString) {- return item;+ return {+ ...item,+ Logo: undefined+ };}const result = await FaviconService.fetchFavicon(urlString, sqliteClient, webApi);diff --git a/apps/browser-extension/src/utils/db/queries/ItemQueries.ts b/apps/browser-extension/src/utils/db/queries/ItemQueries.tsindex f26dffbd3..bd6db0593 100644--- a/apps/browser-extension/src/utils/db/queries/ItemQueries.ts+++ b/apps/browser-extension/src/utils/db/queries/ItemQueries.ts@@ -156,7 +156,7 @@ export class ItemQueries {VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;/**- * Update an existing item.+ * Update an existing item (preserves LogoId if null is passed).*/public static readonly UPDATE_ITEM = `UPDATE Items@@ -167,6 +167,18 @@ export class ItemQueries {UpdatedAt = ?WHERE Id = ?`;+ /**+ * Update an existing item with explicit LogoId setting (can clear LogoId to null).+ */+ public static readonly UPDATE_ITEM_WITH_LOGO = `+ UPDATE Items+ SET Name = ?,+ ItemType = ?,+ FolderId = ?,+ LogoId = ?,+ UpdatedAt = ?+ WHERE Id = ?`;+/*** Move item to trash (set DeletedAt).*/diff --git a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts b/apps/browser-extension/src/utils/db/repositories/ItemRepository.tsindex 5bc7db2a1..c8b8fd0ff 100644--- a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts+++ b/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts@@ -189,10 +189,12 @@ export class ItemRepository extends BaseRepository {const nameChanged = (item.Name ?? null) !== existing.Name;const itemTypeChanged = String(item.ItemType) !== String(existing.ItemType);const folderIdChanged = (item.FolderId ?? null) !== existing.FolderId;- const logoIdChanged = logoId !== null && logoId !== existing.LogoId;+ // Logo is considered changed if: new logo differs from existing, OR logo was cleared (undefined/null in item.Logo while existing has one)+ const logoIdChanged = logoId !== existing.LogoId;if (nameChanged || itemTypeChanged || folderIdChanged || logoIdChanged) {- this.client.executeUpdate(ItemQueries.UPDATE_ITEM, [+ // Use UPDATE_ITEM_WITH_LOGO to allow explicit clearing of LogoId+ this.client.executeUpdate(ItemQueries.UPDATE_ITEM_WITH_LOGO, [item.Name ?? null,item.ItemType,item.FolderId ?? null,diff --git a/apps/mobile-app/app/(tabs)/items/add-edit.tsx b/apps/mobile-app/app/(tabs)/items/add-edit.tsxindex e5f22f221..016d11a1f 100644--- a/apps/mobile-app/app/(tabs)/items/add-edit.tsx+++ b/apps/mobile-app/app/(tabs)/items/add-edit.tsx@@ -795,6 +795,9 @@ export default function AddEditItemScreen(): React.ReactNode {// Favicon extraction failed or timed out - not critical, continue with save}}+ } else if (!shouldFetchFavicon) {+ // URL is empty or just a placeholder - clear any existing logo+ itemToSave.Logo = undefined;}/*diff --git a/apps/mobile-app/utils/SqliteClient.ts b/apps/mobile-app/utils/SqliteClient.tsindex ad33ec886..36aeca056 100644--- a/apps/mobile-app/utils/SqliteClient.ts+++ b/apps/mobile-app/utils/SqliteClient.ts@@ -33,6 +33,8 @@ class SqliteClient implements IDatabaseClient {public get items(): ItemRepository {if (!this._items) {this._items = new ItemRepository(this);+ // Set the logo repository for logo handling in item updates+ this._items.setLogoRepository(this.logos);}return this._items;}diff --git a/apps/mobile-app/utils/db/queries/ItemQueries.ts b/apps/mobile-app/utils/db/queries/ItemQueries.tsindex f3a81363f..0b1093fec 100644--- a/apps/mobile-app/utils/db/queries/ItemQueries.ts+++ b/apps/mobile-app/utils/db/queries/ItemQueries.ts@@ -178,7 +178,7 @@ export class ItemQueries {VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;/**- * Update an existing item.+ * Update an existing item (preserves LogoId if null is passed).*/public static readonly UPDATE_ITEM = `UPDATE Items@@ -189,6 +189,18 @@ export class ItemQueries {UpdatedAt = ?WHERE Id = ?`;+ /**+ * Update an existing item with explicit LogoId setting (can clear LogoId to null).+ */+ public static readonly UPDATE_ITEM_WITH_LOGO = `+ UPDATE Items+ SET Name = ?,+ ItemType = ?,+ FolderId = ?,+ LogoId = ?,+ UpdatedAt = ?+ WHERE Id = ?`;+/*** Move item to trash (set DeletedAt).*/diff --git a/apps/mobile-app/utils/db/repositories/ItemRepository.ts b/apps/mobile-app/utils/db/repositories/ItemRepository.tsindex 70eb8509d..4251d804c 100644--- a/apps/mobile-app/utils/db/repositories/ItemRepository.ts+++ b/apps/mobile-app/utils/db/repositories/ItemRepository.ts@@ -5,6 +5,7 @@ import { BaseRepository } from '../BaseRepository';import { ItemQueries, FieldValueQueries, FieldHistoryQueries } from '../queries/ItemQueries';import { FieldMapper, type FieldRow } from '../mappers/FieldMapper';import { ItemMapper, type ItemRow, type TagRow, type ItemWithDeletedAt } from '../mappers/ItemMapper';+import type { LogoRepository } from './LogoRepository';/*** SQL query constants for Item-related tag operations.@@ -43,6 +44,15 @@ const TagQueries = {* Handles fetching, creating, updating, and deleting items with their related data.*/export class ItemRepository extends BaseRepository {+ private logoRepository: LogoRepository | null = null;++ /**+ * Set the logo repository for logo handling operations.+ * @param logoRepository - The logo repository instance+ */+ public setLogoRepository(logoRepository: LogoRepository): void {+ this.logoRepository = logoRepository;+ }/*** Fetch all active items (not deleted, not in trash) with their fields and tags.* @returns Array of Item objects@@ -337,25 +347,32 @@ export class ItemRepository extends BaseRepository {return this.withTransaction(async () => {const now = this.now();- // 1. Update Item only if item-level fields changed+ // 1. Handle Logo - resolve new logoId based on item.Logo+ const logoId = await this.resolveLogoId(item, now);++ // 2. Update Item only if item-level fields changedconst existing = await this.client.executeQuery<{Name: string | null;ItemType: string | null;FolderId: string | null;- }>(`SELECT Name, ItemType, FolderId FROM Items WHERE Id = ?`, [item.Id]);+ LogoId: string | null;+ }>(`SELECT Name, ItemType, FolderId, LogoId FROM Items WHERE Id = ?`, [item.Id]);if (existing.length > 0) {const existingItem = existing[0];const nameChanged = item.Name !== existingItem.Name;const itemTypeChanged = String(item.ItemType) !== String(existingItem.ItemType);const folderIdChanged = (item.FolderId || null) !== existingItem.FolderId;+ // Logo changed if: we have a new logo ID, OR we're clearing the logo (item.Logo is undefined and existing has logo)+ const logoIdChanged = logoId !== existingItem.LogoId;- if (nameChanged || itemTypeChanged || folderIdChanged) {- await this.client.executeUpdate(ItemQueries.UPDATE_ITEM, [+ if (nameChanged || itemTypeChanged || folderIdChanged || logoIdChanged) {+ // Use UPDATE_ITEM_WITH_LOGO to allow explicit clearing of LogoId+ await this.client.executeUpdate(ItemQueries.UPDATE_ITEM_WITH_LOGO, [item.Name,item.ItemType,item.FolderId || null,- null, // LogoId update handled separately if needed+ logoId,now,item.Id]);@@ -618,4 +635,38 @@ export class ItemRepository extends BaseRepository {}}}++ /**+ * Resolve the logo ID for an item (create new or reuse existing).+ * If item.Logo is undefined, returns null to clear any existing logo.+ * @param item - The item to resolve logo for+ * @param currentDateTime - The current timestamp+ * @returns The logo ID, or null if no logo+ */+ private async resolveLogoId(item: Item, currentDateTime: string): Promise<string | null> {+ // If no logo repository is set, we can't handle logos+ if (!this.logoRepository) {+ return null;+ }++ // Get URL field for source extraction+ const urlField = item.Fields?.find(f => f.FieldKey === 'login.url');+ const urlValue = urlField?.Value;+ const urlString = Array.isArray(urlValue) ? urlValue[0] : urlValue;+ const source = this.logoRepository.extractSourceFromUrl(urlString);++ // If item has Logo data, create or reuse a logo entry+ if (item.Logo) {+ const logoData = this.logoRepository.convertLogoToUint8Array(item.Logo);+ if (logoData) {+ return this.logoRepository.getOrCreate(source, logoData, currentDateTime);+ }+ } else if (source !== 'unknown') {+ // No logo data provided, but we have a valid URL - try to find existing logo for this source+ return this.logoRepository.getIdForSource(source);+ }++ // No logo data and no valid URL - return null to clear any existing logo+ return null;+ }}diff --git a/apps/server/AliasVault.Client/Services/ItemService.cs b/apps/server/AliasVault.Client/Services/ItemService.csindex b10e2fdfb..c5ac31d2b 100644--- a/apps/server/AliasVault.Client/Services/ItemService.cs+++ b/apps/server/AliasVault.Client/Services/ItemService.cs@@ -1164,6 +1164,7 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf/// <summary>/// Extract favicon from service URL if available. If successful, links the item to the logo./// Checks for existing logo first to avoid unnecessary API calls (deduplication).+ /// If URL is empty or just the placeholder, clears any existing logo from the item./// </summary>/// <param name="item">The Item to extract the favicon for.</param>/// <returns>Task.</returns>@@ -1218,6 +1219,11 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf// Ignore favicon extraction errors}}+ else+ {+ // URL is empty or just the placeholder - clear any existing logo+ item.LogoId = null;+ }}/// <summary>