diff --git a/lib/types/store-ops-types.js b/lib/types/store-ops-types.js --- a/lib/types/store-ops-types.js +++ b/lib/types/store-ops-types.js @@ -13,7 +13,10 @@ import type { ClientReportCreationRequest } from './report-types.js'; import type { ClientDBThreadInfo, ThreadStore } from './thread-types.js'; import type { UserInfos } from './user-types.js'; -import type { ClientDBCommunityInfo } from '../ops/community-store-ops.js'; +import type { + ClientDBCommunityInfo, + ClientDBCommunityStoreOperation, +} from '../ops/community-store-ops.js'; import type { ClientDBKeyserverInfo, ClientDBKeyserverStoreOperation, @@ -52,6 +55,7 @@ +messageStoreOperations?: $ReadOnlyArray, +reportStoreOperations?: $ReadOnlyArray, +keyserverStoreOperations?: $ReadOnlyArray, + +communityStoreOperations?: $ReadOnlyArray, }; export type ClientDBStore = { diff --git a/web/database/worker/process-operations.js b/web/database/worker/process-operations.js --- a/web/database/worker/process-operations.js +++ b/web/database/worker/process-operations.js @@ -1,5 +1,6 @@ // @flow +import type { ClientDBCommunityStoreOperation } from 'lib/ops/community-store-ops.js'; import type { ClientDBKeyserverStoreOperation } from 'lib/ops/keyserver-store-ops.js'; import type { ClientDBReportStoreOperation } from 'lib/ops/report-store-ops.js'; import type { ClientDBThreadStoreOperation } from 'lib/ops/thread-store-ops.js'; @@ -154,6 +155,37 @@ } } +function processCommunityStoreOperations( + sqliteQueryExecutor: SQLiteQueryExecutor, + operations: $ReadOnlyArray, + module: EmscriptenModule, +) { + for (const operation: ClientDBCommunityStoreOperation of operations) { + try { + if (operation.type === 'remove_all_communities') { + sqliteQueryExecutor.removeAllCommunities(); + } else if (operation.type === 'remove_communities') { + const { ids } = operation.payload; + sqliteQueryExecutor.removeCommunities(ids); + } else if (operation.type === 'replace_community') { + const { id, communityInfo } = operation.payload; + sqliteQueryExecutor.replaceCommunity({ id, communityInfo }); + } else { + throw new Error('Unsupported community operation'); + } + } catch (e) { + throw new Error( + `Error while processing ${ + operation.type + } community operation: ${getProcessingStoreOpsExceptionMessage( + e, + module, + )}`, + ); + } + } +} + function processDBStoreOperations( sqliteQueryExecutor: SQLiteQueryExecutor, storeOperations: ClientDBStoreOperations, @@ -164,6 +196,7 @@ reportStoreOperations, threadStoreOperations, keyserverStoreOperations, + communityStoreOperations, } = storeOperations; try { @@ -196,6 +229,13 @@ module, ); } + if (communityStoreOperations && communityStoreOperations.length > 0) { + processCommunityStoreOperations( + sqliteQueryExecutor, + communityStoreOperations, + module, + ); + } sqliteQueryExecutor.commitTransaction(); } catch (e) { sqliteQueryExecutor.rollbackTransaction(); @@ -217,7 +257,7 @@ reports: sqliteQueryExecutor.getAllReports(), users: [], keyservers: sqliteQueryExecutor.getAllKeyservers(), - communities: [], + communities: sqliteQueryExecutor.getAllCommunities(), }; }