A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(discovery): keep approved guilds in the status index (#3719)
Hampus71d13e837cee
diff --git a/fluxer_api/src/api/guild/repositories/GuildDiscoveryRepository.ts b/fluxer_api/src/api/guild/repositories/GuildDiscoveryRepository.tsindex 427d44822..c0c6d67c4 100644--- a/fluxer_api/src/api/guild/repositories/GuildDiscoveryRepository.ts+++ b/fluxer_api/src/api/guild/repositories/GuildDiscoveryRepository.ts@@ -1,10 +1,11 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport type {GuildID} from '@app/api/BrandedTypes';-import {BatchBuilder, fetchMany, fetchOne} from '@app/api/database/CassandraQueryExecution';+import {BatchBuilder, fetchMany, fetchOne, fetchPage} from '@app/api/database/CassandraQueryExecution';import type {GuildDiscoveryByStatusRow, GuildDiscoveryRow} from '@app/api/database/types/GuildDiscoveryTypes';import {GuildDiscovery, GuildDiscoveryByStatus} from '@app/api/Tables';import {DISCOVERY_DEFAULT_LANGUAGE, DiscoveryCategories} from '@fluxer/constants/src/DiscoveryConstants';+import {BACKGROUND_READ_TIMEOUT_MS} from '@pkgs/cassandra/src/Client';const FETCH_DISCOVERY_BY_GUILD_ID = GuildDiscovery.selectCql({where: GuildDiscovery.where.eq('guild_id'),@@ -13,6 +14,9 @@ const FETCH_DISCOVERY_BY_GUILD_ID = GuildDiscovery.selectCql({const FETCH_DISCOVERY_BY_STATUS = GuildDiscoveryByStatus.selectCql({where: GuildDiscoveryByStatus.where.eq('status'),});+const SCAN_DISCOVERY = GuildDiscovery.selectCql({+ columns: ['guild_id', 'status', 'applied_at'],+});export class GuildDiscoveryRepository {async findByGuildId(guildId: GuildID): Promise<GuildDiscoveryRow | null> {@@ -33,6 +37,30 @@ export class GuildDiscoveryRepository {});}+ async scanStatuses(+ pageState: string | null,+ pageSize: number,+ ): Promise<{rows: Array<GuildDiscoveryByStatusRow>; pageState: string | null}> {+ const result = await fetchPage<GuildDiscoveryByStatusRow>(+ SCAN_DISCOVERY,+ {},+ {pageSize, pageState, readTimeout: BACKGROUND_READ_TIMEOUT_MS},+ );+ return {rows: result.rows, pageState: result.pageState};+ }++ async addStatusEntry(row: GuildDiscoveryByStatusRow): Promise<void> {+ const batch = new BatchBuilder();+ batch.addPrepared(+ GuildDiscoveryByStatus.insert({+ status: row.status,+ applied_at: row.applied_at,+ guild_id: row.guild_id,+ }),+ );+ await batch.execute();+ }+async listFullByStatus(status: string): Promise<Array<GuildDiscoveryRow>> {const indexRows = await this.listByStatus(status);const fullRows = await Promise.all(indexRows.map((indexRow) => this.findByGuildId(indexRow.guild_id)));@@ -88,13 +116,16 @@ export class GuildDiscoveryRepository {updatedRow: GuildDiscoveryRow,): Promise<void> {const batch = new BatchBuilder();- batch.addPrepared(- GuildDiscoveryByStatus.deleteByPk({- status: oldStatus,- applied_at: oldAppliedAt,- guild_id: guildId,- }),- );+ const sameEntry = oldStatus === updatedRow.status && oldAppliedAt.getTime() === updatedRow.applied_at.getTime();+ if (!sameEntry) {+ batch.addPrepared(+ GuildDiscoveryByStatus.deleteByPk({+ status: oldStatus,+ applied_at: oldAppliedAt,+ guild_id: guildId,+ }),+ );+ }batch.addPrepared(GuildDiscovery.insert({guild_id: updatedRow.guild_id,diff --git a/fluxer_api/src/api/guild/services/GuildDiscoveryService.ts b/fluxer_api/src/api/guild/services/GuildDiscoveryService.tsindex 47df2d92a..50697affa 100644--- a/fluxer_api/src/api/guild/services/GuildDiscoveryService.ts+++ b/fluxer_api/src/api/guild/services/GuildDiscoveryService.ts@@ -452,6 +452,9 @@ export class GuildDiscoveryService {for (const guild of guilds) {const counts = freshCounts.get(BigInt(guild.id) as GuildID);if (counts) {+ if (counts.memberCount > 0) {+ guild.member_count = counts.memberCount;+ }guild.online_count = counts.onlineCount;}}diff --git a/fluxer_api/src/api/worker/tasks/SyncDiscoveryIndex.ts b/fluxer_api/src/api/worker/tasks/SyncDiscoveryIndex.tsindex 1d638ebe9..d899b6953 100644--- a/fluxer_api/src/api/worker/tasks/SyncDiscoveryIndex.ts+++ b/fluxer_api/src/api/worker/tasks/SyncDiscoveryIndex.ts@@ -1,5 +1,6 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import type {GuildID} from '@app/api/BrandedTypes';import {GuildDiscoveryRepository} from '@app/api/guild/repositories/GuildDiscoveryRepository';import {getGuildSearchService} from '@app/api/SearchFactory';import {mapWithConcurrency} from '@app/api/utils/ConcurrencyUtils';@@ -7,8 +8,39 @@ import {getWorkerDependencies} from '@app/api/worker/WorkerContext';import {DiscoveryApplicationStatus} from '@fluxer/constants/src/DiscoveryConstants';import type {WorkerTaskHandler} from '@pkgs/worker/src/contracts/WorkerTask';-const BATCH_SIZE = 200;+const SCAN_PAGE_SIZE = 200;const UPDATE_CONCURRENCY = 25;++async function listApprovedGuildIds(discoveryRepository: GuildDiscoveryRepository): Promise<{+ approved: Array<GuildID>;+ restored: number;+}> {+ const indexed = new Set(+ (await discoveryRepository.listByStatus(DiscoveryApplicationStatus.APPROVED)).map((row) => row.guild_id.toString()),+ );+ const approved: Array<GuildID> = [];+ let restored = 0;+ let pageState: string | null = null;+ do {+ const page = await discoveryRepository.scanStatuses(pageState, SCAN_PAGE_SIZE);+ for (const row of page.rows) {+ if (row.status !== DiscoveryApplicationStatus.APPROVED) continue;+ approved.push(row.guild_id);+ if (indexed.has(row.guild_id.toString())) continue;+ const current = await discoveryRepository.findByGuildId(row.guild_id);+ if (current?.status !== DiscoveryApplicationStatus.APPROVED) continue;+ await discoveryRepository.addStatusEntry({+ status: current.status,+ applied_at: current.applied_at,+ guild_id: current.guild_id,+ });+ restored++;+ }+ pageState = page.pageState;+ } while (pageState);+ return {approved, restored};+}+const syncDiscoveryIndex: WorkerTaskHandler = async (_payload, helpers) => {helpers.logger.info('Starting discovery index sync');const guildSearchService = getGuildSearchService();@@ -18,15 +50,17 @@ const syncDiscoveryIndex: WorkerTaskHandler = async (_payload, helpers) => {}const {guildRepository} = getWorkerDependencies();const discoveryRepository = new GuildDiscoveryRepository();- const approvedRows = await discoveryRepository.listByStatus(DiscoveryApplicationStatus.APPROVED);- if (approvedRows.length === 0) {+ const {approved: guildIds, restored} = await listApprovedGuildIds(discoveryRepository);+ if (restored > 0) {+ helpers.logger.warn({restored}, 'Restored approved guilds missing from the discovery status index');+ }+ if (guildIds.length === 0) {helpers.logger.info('No discoverable guilds to sync');return;}- const guildIds = approvedRows.map((row) => row.guild_id);let synced = 0;- for (let i = 0; i < guildIds.length; i += BATCH_SIZE) {- const batch = guildIds.slice(i, i + BATCH_SIZE);+ for (let i = 0; i < guildIds.length; i += SCAN_PAGE_SIZE) {+ const batch = guildIds.slice(i, i + SCAN_PAGE_SIZE);const [guilds, discoveryRows] = await Promise.all([guildRepository.listGuilds(batch),Promise.all(batch.map((guildId) => discoveryRepository.findByGuildId(guildId))),@@ -51,7 +85,7 @@ const syncDiscoveryIndex: WorkerTaskHandler = async (_payload, helpers) => {);synced += updates.length;}- helpers.logger.info({synced, total: guildIds.length}, 'Discovery index sync completed');+ helpers.logger.info({synced, total: guildIds.length, restored}, 'Discovery index sync completed');};export default syncDiscoveryIndex;