Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(api): sort reports when also filtering by status (#3743)

Hampusa1ca5be803b4
diff --git a/fluxer_admin/openapi-admin.json b/fluxer_admin/openapi-admin.json
index a96010120..d7f5bbbaa 100644
--- a/fluxer_admin/openapi-admin.json
+++ b/fluxer_admin/openapi-admin.json
@@ -7339,7 +7339,7 @@
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
}
},
- "description": "Lists user and content reports from the report search index with pagination. Without a status filter the list covers reports of every status. A request that filters by status alone lists the reports with that status newest first and ignores sort_by and sort_order. A free-text query and the entity, category, reason and resolver filters narrow the results. Every response includes the total, offset and limit of the page. Reporter contact details are redacted unless the caller also holds REPORT_VIEW_REPORTER_PII. Requires REPORT_VIEW permission.",
+ "description": "Lists user and content reports from the report search index with pagination. Without a status filter the list covers reports of every status. A free-text query and the entity, category, reason and resolver filters narrow the results. Every response includes the total, offset and limit of the page. Reporter contact details are redacted unless the caller also holds REPORT_VIEW_REPORTER_PII. Requires REPORT_VIEW permission.",
"security": [{"adminApiKey": []}],
"parameters": [
{
diff --git a/fluxer_api/src/api/admin/controllers/ReportAdminController.ts b/fluxer_api/src/api/admin/controllers/ReportAdminController.ts
index 3a7b84726..002232bc7 100644
--- a/fluxer_api/src/api/admin/controllers/ReportAdminController.ts
+++ b/fluxer_api/src/api/admin/controllers/ReportAdminController.ts
@@ -51,22 +51,6 @@ const REPORT_SORT_FIELD_BY_QUERY = {
resolved_at: 'resolvedAt',
} as const;
-function usesReportSearchIndex(query: ListReportsQuery): boolean {
- return (
- query.q !== undefined ||
- query.report_type !== undefined ||
- query.category !== undefined ||
- query.reason !== undefined ||
- query.reporter_id !== undefined ||
- query.reported_user_id !== undefined ||
- query.reported_webhook_id !== undefined ||
- query.reported_guild_id !== undefined ||
- query.reported_channel_id !== undefined ||
- query.guild_context_id !== undefined ||
- query.resolved_by_admin_id !== undefined
- );
-}
-
function toSearchReportsRequest(query: ListReportsQuery): SearchReportsRequest {
return {
query: query.q,
@@ -98,7 +82,7 @@ export function ReportAdminController(app: HonoApp) {
operationId: 'list_admin_reports',
summary: 'List reports',
description:
- 'Lists user and content reports from the report search index with pagination. Without a status filter the list covers reports of every status. A request that filters by status alone lists the reports with that status newest first and ignores sort_by and sort_order. A free-text query and the entity, category, reason and resolver filters narrow the results. Every response includes the total, offset and limit of the page. Reporter contact details are redacted unless the caller also holds REPORT_VIEW_REPORTER_PII. Requires REPORT_VIEW permission.',
+ 'Lists user and content reports from the report search index with pagination. Without a status filter the list covers reports of every status. A free-text query and the entity, category, reason and resolver filters narrow the results. Every response includes the total, offset and limit of the page. Reporter contact details are redacted unless the caller also holds REPORT_VIEW_REPORTER_PII. Requires REPORT_VIEW permission.',
responseSchema: AdminReportListResponse,
statusCode: 200,
security: 'adminApiKey',
@@ -109,15 +93,10 @@ export function ReportAdminController(app: HonoApp) {
const adminUserAcls = ctx.get('adminUserAcls');
const query = ctx.req.valid('query');
const status = query.status;
- const searched = status === undefined || usesReportSearchIndex(query);
- const response = searched
- ? await adminService.reportServiceAggregate.searchReports(toSearchReportsRequest(query), adminUserAcls)
- : await adminService.reportServiceAggregate.listReports(
- REPORT_STATUS_BY_FILTER[status],
- adminUserAcls,
- query.limit,
- query.offset,
- );
+ const response = await adminService.reportServiceAggregate.searchReports(
+ toSearchReportsRequest(query),
+ adminUserAcls,
+ );
await recordAdminRead(ctx, {
targetType: 'report',
targetId: 0n,
@@ -135,8 +114,8 @@ export function ReportAdminController(app: HonoApp) {
reported_channel_id: query.reported_channel_id,
context_guild_id: query.guild_context_id,
resolved_by_admin_user_id: query.resolved_by_admin_id,
- sort_by: searched ? query.sort_by : undefined,
- sort_order: searched ? query.sort_order : undefined,
+ sort_by: query.sort_by,
+ sort_order: query.sort_order,
limit: query.limit,
offset: query.offset,
result_count: response.reports.length,
diff --git a/fluxer_api/src/api/admin/services/AdminReportService.ts b/fluxer_api/src/api/admin/services/AdminReportService.ts
index c0e5feed5..353a216bc 100644
--- a/fluxer_api/src/api/admin/services/AdminReportService.ts
+++ b/fluxer_api/src/api/admin/services/AdminReportService.ts
@@ -174,26 +174,6 @@ function mapReportReasonFields(report: IARSubmission) {
export class AdminReportService {
constructor(private readonly deps: AdminReportServiceDeps) {}
- async listReports(status: number, acls: ReadonlySet<string>, limit?: number, offset?: number) {
- const {reportService} = this.deps;
- const requestedLimit = limit || 50;
- const currentOffset = offset || 0;
- const {reports, total} = await reportService.listReportsByStatus(status, requestedLimit, currentOffset);
- const requestCache = createRequestCache();
- const reportLookupCache = createReportLookupCache();
- const reportResponses = await Promise.all(
- reports.map((report: IARSubmission) =>
- this.mapReportToResponse(report, false, requestCache, acls, reportLookupCache),
- ),
- );
- return {
- reports: reportResponses,
- total,
- offset: currentOffset,
- limit: requestedLimit,
- };
- }
-
listReportReasons(): AdminReportReasonsResponse {
return {
reasons: listReportReasons().map((reason) => ({
diff --git a/fluxer_api/src/api/admin/tests/audit_coverage/ReportAdminAuditCases.ts b/fluxer_api/src/api/admin/tests/audit_coverage/ReportAdminAuditCases.ts
index 4f40dbb58..00ef5176c 100644
--- a/fluxer_api/src/api/admin/tests/audit_coverage/ReportAdminAuditCases.ts
+++ b/fluxer_api/src/api/admin/tests/audit_coverage/ReportAdminAuditCases.ts
@@ -77,6 +77,8 @@ export const ReportAdminAuditCases: ReadonlyArray<AdminAuditCoverageCase> = [
targetId: '0',
metadata: {
status: 'pending',
+ sort_by: 'reported_at',
+ sort_order: 'desc',
limit: '1',
offset: '1',
result_count: '1',
diff --git a/fluxer_api/src/api/report/ReportService.ts b/fluxer_api/src/api/report/ReportService.ts
index 448d02866..48acfb86a 100644
--- a/fluxer_api/src/api/report/ReportService.ts
+++ b/fluxer_api/src/api/report/ReportService.ts
@@ -91,7 +91,6 @@ import {CannotReportOwnMessageError} from '@fluxer/errors/src/domains/channel/Ca
import {UnknownChannelError} from '@fluxer/errors/src/domains/channel/UnknownChannelError';
import {UnknownMessageError} from '@fluxer/errors/src/domains/channel/UnknownMessageError';
import {ConflictError} from '@fluxer/errors/src/domains/core/ConflictError';
-import {FeatureTemporarilyDisabledError} from '@fluxer/errors/src/domains/core/FeatureTemporarilyDisabledError';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import {RateLimitError} from '@fluxer/errors/src/domains/core/RateLimitError';
import {ServiceUnavailableError} from '@fluxer/errors/src/domains/core/ServiceUnavailableError';
@@ -1306,28 +1305,6 @@ export class ReportService {
return report;
}
- async listReportsByStatus(
- status: number,
- limit?: number,
- offset?: number,
- ): Promise<{reports: Array<IARSubmission>; total: number}> {
- if (!this.reportSearchService) {
- throw new FeatureTemporarilyDisabledError();
- }
- const {hits, total} = await this.reportSearchService.listReportsByStatus(status, limit, offset);
- const reportIds = hits.map((hit) => createReportID(BigInt(hit.id)));
- const loaded = await Promise.all(reportIds.map((id) => this.reportRepository.getReport(id)));
- const reports = loaded.filter((report): report is IARSubmission => report !== null);
- const orphanedReportIds = reportIds.filter((_, index) => loaded[index] === null).map((id) => id.toString());
- if (orphanedReportIds.length > 0) {
- Logger.warn(
- {orphanedReportIds, status},
- 'Report search index lists reports that are no longer stored, run refresh_search_index reports',
- );
- }
- return {reports, total};
- }
-
async resolveReport(
reportId: ReportID,
adminUserId: UserID,
diff --git a/fluxer_api/src/api/search/IReportSearchService.ts b/fluxer_api/src/api/search/IReportSearchService.ts
index bdf802897..8437b6ca9 100644
--- a/fluxer_api/src/api/search/IReportSearchService.ts
+++ b/fluxer_api/src/api/search/IReportSearchService.ts
@@ -21,5 +21,4 @@ export interface IReportSearchService extends SchemaISearchAdapter<ReportSearchF
offset?: number;
},
): Promise<SchemaSearchResult<SearchableReport>>;
- listReportsByStatus(status: number, limit?: number, offset?: number): Promise<SchemaSearchResult<SearchableReport>>;
}
diff --git a/fluxer_api/src/api/search/SearchServices.ts b/fluxer_api/src/api/search/SearchServices.ts
index 4c95f3916..95f039aff 100644
--- a/fluxer_api/src/api/search/SearchServices.ts
+++ b/fluxer_api/src/api/search/SearchServices.ts
@@ -247,10 +247,6 @@ export class ReportSearchService
): Promise<SchemaSearchResult<SearchableReport>> {
return this.search(query, filters, options);
}
-
- listReportsByStatus(status: number, limit?: number, offset?: number): Promise<SchemaSearchResult<SearchableReport>> {
- return this.searchReports('', {status}, {limit, offset});
- }
}
export class AuditLogSearchService