Skip to content

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

TypeScript fluxerfluxerapp

refactor(api): replace nested ternaries with plain branches

Hampusfd5e8694456b
diff --git a/fluxer_api/src/api/admin/services/AdminBanManagementService.ts b/fluxer_api/src/api/admin/services/AdminBanManagementService.ts
index 4ba9fcb45..1d57ebcae 100644
--- a/fluxer_api/src/api/admin/services/AdminBanManagementService.ts
+++ b/fluxer_api/src/api/admin/services/AdminBanManagementService.ts
@@ -25,15 +25,16 @@ import {phraseBlocklistCache} from '@app/api/middleware/PhraseBlocklistCache';
import {profileSubstringBlocklistCache} from '@app/api/middleware/ProfileSubstringBlocklistCache';
import {urlBlocklistCache} from '@app/api/middleware/UrlBlocklistCache';
import {canonicalizeStoredPhrase} from '@app/api/utils/PhraseBlocklistNormalization';
+import {compareText} from '@app/api/utils/StringComparison';
import {parseUrlDomainEntry} from '@app/api/utils/UrlHostRules';
import {canonicalizeUrl} from '@app/api/utils/UrlNormalizer';
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
+import {toIsoOrNull} from '@fluxer/date_utils/src/DateSerialization';
import {BadRequestError} from '@fluxer/errors/src/domains/core/BadRequestError';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import {NotFoundError} from '@fluxer/errors/src/domains/core/NotFoundError';
import {IpBanLinkedToDecisionError} from '@fluxer/errors/src/domains/moderation/IpBanLinkedToDecisionError';
-import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import type {AdminBlocklistListType} from '@fluxer/schema/src/domains/admin/AdminBlocklistSchemas';
interface AdminBanManagementServiceDeps {
@@ -87,10 +88,6 @@ function createBlocklistEntry(
};
}
-function toIsoString(value: Date | null | undefined): string | null {
- return value ? value.toISOString() : null;
-}
-
function toSnowflakeString(value: bigint | null | undefined): string | null {
return value == null ? null : value.toString();
}
@@ -196,7 +193,7 @@ export class AdminBanManagementService {
const match = ipBanCache.getMatch(data.ip);
return {
banned: match !== null,
- expires_at: toIsoString(match?.expiresAt),
+ expires_at: toIsoOrNull(match?.expiresAt),
decision_id: toSnowflakeString(match?.decision?.decisionId),
};
}
@@ -762,7 +759,7 @@ export class AdminBanManagementService {
scope: BannedProfileSubstringScope | null;
}): Promise<AdminBlocklistEntryPage> {
const entries = await this.loadBlocklistEntries(params.listType, params.scope);
- entries.sort((left, right) => (left.value < right.value ? -1 : left.value > right.value ? 1 : 0));
+ entries.sort((left, right) => compareText(left.value, right.value));
const after = params.after;
const remaining = after == null ? entries : entries.filter((entry) => entry.value > after);
const page = remaining.slice(0, params.limit);
@@ -786,8 +783,8 @@ export class AdminBanManagementService {
return rows.map((row) =>
createBlocklistEntry(listType, row.ip, {
reason: row.reason,
- expires_at: toIsoString(row.expiresAt),
- created_at: toIsoString(row.createdAt),
+ expires_at: toIsoOrNull(row.expiresAt),
+ created_at: toIsoOrNull(row.createdAt),
}),
);
}
@@ -807,7 +804,7 @@ export class AdminBanManagementService {
severity: row.severity,
source_url: row.source_url,
notes: row.notes,
- created_at: toIsoString(row.added_at),
+ created_at: toIsoOrNull(row.added_at),
created_by_user_id: toSnowflakeString(row.added_by),
}),
);
@@ -821,7 +818,7 @@ export class AdminBanManagementService {
source_url: row.source_url,
notes: row.notes,
match_subdomains: row.match_subdomains,
- created_at: toIsoString(row.added_at),
+ created_at: toIsoOrNull(row.added_at),
created_by_user_id: toSnowflakeString(row.added_by),
}),
);
@@ -835,7 +832,7 @@ export class AdminBanManagementService {
source_url: row.source_url,
notes: row.notes,
content_type: row.content_type,
- created_at: toIsoString(row.added_at),
+ created_at: toIsoOrNull(row.added_at),
created_by_user_id: toSnowflakeString(row.added_by),
}),
);
@@ -848,7 +845,7 @@ export class AdminBanManagementService {
severity: row.severity,
source_url: row.source_url,
notes: row.notes,
- created_at: toIsoString(row.added_at),
+ created_at: toIsoOrNull(row.added_at),
created_by_user_id: toSnowflakeString(row.added_by),
}),
);
@@ -861,7 +858,7 @@ export class AdminBanManagementService {
createBlocklistEntry(listType, row.substring, {
scope: row.scope,
notes: row.notes,
- created_at: toIsoString(row.added_at),
+ created_at: toIsoOrNull(row.added_at),
created_by_user_id: toSnowflakeString(row.added_by),
}),
);
diff --git a/fluxer_api/src/api/channel/repositories/ChannelDataRepository.ts b/fluxer_api/src/api/channel/repositories/ChannelDataRepository.ts
index 04506dcee..6dd839a5c 100644
--- a/fluxer_api/src/api/channel/repositories/ChannelDataRepository.ts
+++ b/fluxer_api/src/api/channel/repositories/ChannelDataRepository.ts
@@ -196,10 +196,7 @@ export class ChannelDataRepository {
channelId: channelId.toString(),
kind,
failureCount: failures.length,
- error:
- failures[0].status === 'rejected' && failures[0].reason instanceof Error
- ? failures[0].reason.message
- : String(failures[0].status === 'rejected' ? failures[0].reason : ''),
+ error: failures[0].reason instanceof Error ? failures[0].reason.message : String(failures[0].reason),
},
'Failed to write through private channel snapshot fan-out',
);
diff --git a/fluxer_api/src/api/channel/services/ChannelDataService.ts b/fluxer_api/src/api/channel/services/ChannelDataService.ts
index 02ba4d616..6487d2433 100644
--- a/fluxer_api/src/api/channel/services/ChannelDataService.ts
+++ b/fluxer_api/src/api/channel/services/ChannelDataService.ts
@@ -170,7 +170,6 @@ export class ChannelDataService {
authChannel,
userId,
data: data as Omit<ChannelUpdateThreadRequest, 'type'>,
- requestCache,
auditLogReason,
});
}
diff --git a/fluxer_api/src/api/channel/services/message/MessageHelpers.ts b/fluxer_api/src/api/channel/services/message/MessageHelpers.ts
index 1cbbd218e..6fd022a00 100644
--- a/fluxer_api/src/api/channel/services/message/MessageHelpers.ts
+++ b/fluxer_api/src/api/channel/services/message/MessageHelpers.ts
@@ -55,11 +55,12 @@ function selectForwardAttachments(
if (!hasForwardMediaSelection(selection)) {
return [...attachments];
}
- if (!selection?.attachmentIds?.size) {
+ const attachmentIds = selection?.attachmentIds;
+ if (!attachmentIds?.size) {
return [];
}
- const selected = attachments.filter((attachment) => selection.attachmentIds!.has(attachment.id));
- if (selected.length !== selection.attachmentIds.size) {
+ const selected = attachments.filter((attachment) => attachmentIds.has(attachment.id));
+ if (selected.length !== attachmentIds.size) {
throw InputValidationError.fromCode(
'message_reference.attachment_ids',
ValidationErrorCodes.REFERENCED_ATTACHMENT_NOT_FOUND,
@@ -309,19 +310,12 @@ export async function createMessageSnapshotsForForward(
content: isMediaOnlyForward ? null : referencedMessage.content,
timestamp: snowflakeToDate(referencedMessage.id),
edited_timestamp: isMediaOnlyForward ? null : referencedMessage.editedTimestamp,
- mention_users: isMediaOnlyForward
- ? null
- : referencedMessage.mentionedUserIds.size > 0
- ? referencedMessage.mentionedUserIds
- : null,
- mention_roles: isMediaOnlyForward
- ? null
- : referencedMessage.mentionedRoleIds.size > 0
- ? referencedMessage.mentionedRoleIds
- : null,
- mention_channels: isMediaOnlyForward
- ? null
- : referencedMessage.mentionedChannelIds.size > 0
+ mention_users:
+ !isMediaOnlyForward && referencedMessage.mentionedUserIds.size > 0 ? referencedMessage.mentionedUserIds : null,
+ mention_roles:
+ !isMediaOnlyForward && referencedMessage.mentionedRoleIds.size > 0 ? referencedMessage.mentionedRoleIds : null,
+ mention_channels:
+ !isMediaOnlyForward && referencedMessage.mentionedChannelIds.size > 0
? referencedMessage.mentionedChannelIds
: null,
attachments: clonedAttachments.length > 0 ? clonedAttachments : null,
@@ -478,7 +472,7 @@ export function attachmentDecayPayloadsForMessage(message: Message): Array<Attac
}));
}
-export function countedThreadMessages(channel: Channel, messageIds: Array<MessageID>): number {
+function countedThreadMessages(channel: Channel, messageIds: Array<MessageID>): number {
if (!channel.isThread()) return 0;
const starterId = channel.id.toString();
return messageIds.filter((messageId) => messageId.toString() !== starterId).length;
diff --git a/fluxer_api/src/api/channel/services/message/MessageMentionService.ts b/fluxer_api/src/api/channel/services/message/MessageMentionService.ts
index 3edbf062a..e23441948 100644
--- a/fluxer_api/src/api/channel/services/message/MessageMentionService.ts
+++ b/fluxer_api/src/api/channel/services/message/MessageMentionService.ts
@@ -95,13 +95,14 @@ export class MessageMentionService {
mentionsEveryone = false;
mentionsHere = false;
}
+ const referencedAuthorId = referencedMessage?.authorId;
const shouldAddReferencedUser =
- referencedMessage?.authorId &&
- referencedMessage.authorId !== message.authorId &&
+ referencedAuthorId &&
+ referencedAuthorId !== message.authorId &&
!isDMChannel &&
allowedMentions?.replied_user !== false;
if (shouldAddReferencedUser) {
- userMentions.add(referencedMessage!.authorId!);
+ userMentions.add(referencedAuthorId);
}
const sendableFlags = message.flags & SENDABLE_MESSAGE_FLAGS;
let flags = message.flags & ~SENDABLE_MESSAGE_FLAGS;
@@ -221,9 +222,10 @@ export class MessageMentionService {
allowedMentions.roles === undefined &&
allowedMentions.replied_user === undefined;
const hasExplicitLists = allowedMentions.users !== undefined || allowedMentions.roles !== undefined;
- const parse = isExplicitlyEmptyObject
- ? []
- : (allowedMentions.parse ?? (hasExplicitLists ? [] : ALLOWED_MENTIONS_PARSE));
+ let parse: Readonly<NonNullable<AllowedMentionsRequest['parse']>> = [];
+ if (!isExplicitlyEmptyObject) {
+ parse = allowedMentions.parse ?? (hasExplicitLists ? [] : ALLOWED_MENTIONS_PARSE);
+ }
const users = allowedMentions.users ?? [];
const roles = allowedMentions.roles ?? [];
const hasExplicitParse = allowedMentions.parse !== undefined;
diff --git a/fluxer_api/src/api/channel/services/thread/ThreadListService.ts b/fluxer_api/src/api/channel/services/thread/ThreadListService.ts
index e7a49ff53..d25d12ab7 100644
--- a/fluxer_api/src/api/channel/services/thread/ThreadListService.ts
+++ b/fluxer_api/src/api/channel/services/thread/ThreadListService.ts
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import assert from 'node:assert/strict';
import type {ChannelID, GuildID, UserID} from '@app/api/BrandedTypes';
import type {AuthenticatedChannel} from '@app/api/channel/services/AuthenticatedChannel';
import {assertThreadAllowed} from '@app/api/channel/services/thread/ThreadDenials';
@@ -20,6 +21,7 @@ import type {
ActiveThreadsResponse,
ArchivedThreadsResponse,
} from '@fluxer/schema/src/domains/channel/ThreadRequestSchemas';
+import {compareSnowflakes} from '@fluxer/snowflake/src/SnowflakeComparison';
export class ThreadListService {
constructor(private readonly ctx: ThreadServiceContext) {}
@@ -59,7 +61,7 @@ export class ThreadListService {
);
const visible = candidates
.filter((state) => !state.isPrivate || memberships.has(state.threadId) || moderatedParents.has(state.parentId))
- .sort((a, b) => (a.threadId > b.threadId ? -1 : a.threadId < b.threadId ? 1 : 0));
+ .sort((a, b) => compareSnowflakes(b.threadId, a.threadId));
return this.respond(visible, memberships, userId);
}
@@ -73,7 +75,7 @@ export class ThreadListService {
const states = await this.ctx.channelRepository.threads.getStates(threadIds);
const views = await loadThreadViews(
this.ctx.channelRepository,
- states.sort((a, b) => (a.threadId > b.threadId ? -1 : a.threadId < b.threadId ? 1 : 0)),
+ states.sort((a, b) => compareSnowflakes(b.threadId, a.threadId)),
);
return views.map((view) => mapThreadToResponse(view));
}
@@ -85,7 +87,7 @@ export class ThreadListService {
before?: Date;
limit: number;
}): Promise<ArchivedThreadsResponse> {
- const auth = await this.authorizeParent(params.viewer, params.userId, params.channelId, false);
+ const auth = await this.authorizeParent(params.viewer, params.userId, params.channelId, {privateThreads: false});
const page = await this.ctx.channelRepository.threads.listArchivedThreads(auth.channel.id, false, {
before: params.before,
limit: params.limit,
@@ -100,7 +102,7 @@ export class ThreadListService {
before?: Date;
limit: number;
}): Promise<ArchivedThreadsResponse> {
- const auth = await this.authorizeParent(params.viewer, params.userId, params.channelId, true);
+ const auth = await this.authorizeParent(params.viewer, params.userId, params.channelId, {privateThreads: true});
const page = await this.ctx.channelRepository.threads.listArchivedThreads(auth.channel.id, true, {
before: params.before,
limit: params.limit,
@@ -115,10 +117,15 @@ export class ThreadListService {
before?: ChannelID;
limit: number;
}): Promise<ArchivedThreadsResponse> {
- const auth = await this.authorizeParent(params.viewer, params.userId, params.channelId, false, true);
+ const auth = await this.authorizeParent(params.viewer, params.userId, params.channelId, {
+ privateThreads: false,
+ textOnly: true,
+ });
+ const guildId = auth.channel.guildId;
+ assert(guildId !== null);
const page = await this.ctx.channelRepository.threads.listJoinedPrivateArchivedThreads(
params.userId,
- auth.channel.guildId!,
+ guildId,
auth.channel.id,
{before: params.before, limit: params.limit},
);
@@ -129,18 +136,20 @@ export class ThreadListService {
viewer: ThreadViewer,
userId: UserID,
channelId: ChannelID,
- privateThreads: boolean,
- textOnly = privateThreads,
+ {privateThreads, textOnly = privateThreads}: {privateThreads: boolean; textOnly?: boolean},
): Promise<AuthenticatedChannel> {
const auth = await this.ctx.channelAuth.getChannelAuthenticated({
userId,
channelId,
viewer,
skipNsfwValidation: true,
+ access: privateThreads || textOnly ? 'member' : 'visitor_read',
});
const {channel} = auth;
const allowed = textOnly ? channel.type === ChannelTypes.GUILD_TEXT : THREAD_PARENT_CHANNEL_TYPES.has(channel.type);
- if (!auth.guild || !allowed) throw new InvalidChannelTypeError();
+ if (!auth.guild || !allowed) {
+ throw new InvalidChannelTypeError();
+ }
assertThreadAllowed(canListArchivedThreads(await this.ctx.parentActor(auth, userId), {privateThreads}));
return auth;
}
diff --git a/fluxer_api/src/api/channel/services/thread/ThreadModifyService.ts b/fluxer_api/src/api/channel/services/thread/ThreadModifyService.ts
index edf0fcd43..9676fd043 100644
--- a/fluxer_api/src/api/channel/services/thread/ThreadModifyService.ts
+++ b/fluxer_api/src/api/channel/services/thread/ThreadModifyService.ts
@@ -106,7 +106,6 @@ export class ThreadModifyService {
authChannel: AuthenticatedChannel;
userId: UserID;
data: ThreadUpdateData;
- requestCache: RequestCache;
auditLogReason: string | null;
}): Promise<Channel> {
const data = {
@@ -162,8 +161,10 @@ export class ThreadModifyService {
});
}
const before = await loadThreadView(this.deps.channelRepository, channel, state, parent);
- const renamed = data.name !== undefined && data.name !== channel.name;
- const rateChanged = data.rate_limit_per_user !== undefined && data.rate_limit_per_user !== channel.rateLimitPerUser;
+ const requestedName = data.name;
+ const renamed = requestedName !== undefined && requestedName !== channel.name;
+ const requestedRateLimit = data.rate_limit_per_user;
+ const rateChanged = requestedRateLimit !== undefined && requestedRateLimit !== channel.rateLimitPerUser;
const statePatch: ThreadStatePatch = {
archived: data.archived,
locked: data.locked,
@@ -181,17 +182,17 @@ export class ThreadModifyService {
if (pinning) await this.deps.channelRepository.threads.releaseForumPin(parent.id, channel.id);
throw new UnknownChannelError();
}
- const updatedChannel =
- renamed || rateChanged
- ? await this.deps.channelRepository.channelData.upsert(
- {
- ...channel.toRow(),
- name: renamed ? data.name! : channel.name,
- rate_limit_per_user: rateChanged ? data.rate_limit_per_user! : channel.rateLimitPerUser,
- },
- channel.toRow(),
- )
- : channel;
+ let updatedChannel = channel;
+ if (renamed || rateChanged) {
+ updatedChannel = await this.deps.channelRepository.channelData.upsert(
+ {
+ ...channel.toRow(),
+ name: renamed ? requestedName : channel.name,
+ rate_limit_per_user: rateChanged ? requestedRateLimit : channel.rateLimitPerUser,
+ },
+ channel.toRow(),
+ );
+ }
const next = transition.state;
if (rateChanged) await this.clearThreadSlowmode(channel);
enqueueThreadSearchSync(channel.id);
diff --git a/fluxer_api/src/api/database/CassandraDevLogger.ts b/fluxer_api/src/api/database/CassandraDevLogger.ts
index e93e28fab..6c42240e8 100644
--- a/fluxer_api/src/api/database/CassandraDevLogger.ts
+++ b/fluxer_api/src/api/database/CassandraDevLogger.ts
@@ -13,10 +13,22 @@ const colors = {
green: '\x1b[32m',
magenta: '\x1b[35m',
blue: '\x1b[34m',
- gray: '\x1b[90m',
white: '\x1b[37m',
} as const;
+const MODERATE_QUERY_THRESHOLD_MS = 50;
+const SLOW_QUERY_THRESHOLD_MS = 100;
+
+function getDurationColor(durationMs: number): string {
+ if (durationMs > SLOW_QUERY_THRESHOLD_MS) {
+ return colors.yellow;
+ }
+ if (durationMs > MODERATE_QUERY_THRESHOLD_MS) {
+ return colors.dim;
+ }
+ return colors.green;
+}
+
function formatValue(value: unknown): string {
if (value === null) return `${colors.dim}null${colors.reset}`;
if (value === undefined) return `${colors.dim}undefined${colors.reset}`;
@@ -92,7 +104,7 @@ export function logQuery(
QUERY: colors.white,
};
const typeColor = typeColors[queryType] || colors.white;
- const durationColor = durationMs > 100 ? colors.yellow : durationMs > 50 ? colors.dim : colors.green;
+ const durationColor = getDurationColor(durationMs);
const lines = [
`${colors.dim}\u250c\u2500\u2500${colors.reset} ${typeColor}${colors.bold}${queryType}${colors.reset} ${colors.dim}\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500${colors.reset}`,
`${colors.dim}\u2502${colors.reset} ${formatCql(cql)}`,
@@ -138,7 +150,7 @@ export function logBatch(
lines.push(`${colors.dim}\u2502${colors.reset} ${colors.dim}(${paramEntries.length} params)${colors.reset}`);
}
}
- const durationColor = durationMs > 100 ? colors.yellow : durationMs > 50 ? colors.dim : colors.green;
+ const durationColor = getDurationColor(durationMs);
lines.push(
`${colors.dim}\u2514\u2500\u2500${colors.reset} ${durationColor}${durationMs.toFixed(2)}ms${colors.reset}`,
);
diff --git a/fluxer_api/src/api/database/CassandraTableDsl.ts b/fluxer_api/src/api/database/CassandraTableDsl.ts
index 087aa9493..d2081c830 100644
--- a/fluxer_api/src/api/database/CassandraTableDsl.ts
+++ b/fluxer_api/src/api/database/CassandraTableDsl.ts
@@ -168,7 +168,10 @@ WHERE ${pk.map((k) => `${k} = :${k}`).join(' AND ')};
}
}
const orderBy = opts.orderBy != null ? ` ORDER BY ${opts.orderBy.col} ${opts.orderBy.direction ?? 'ASC'}` : '';
- const limit = typeof opts.limit === 'number' ? ` LIMIT ${limitParamName ? `:${limitParamName}` : opts.limit}` : '';
+ let limit = '';
+ if (typeof opts.limit === 'number') {
+ limit = ` LIMIT ${limitParamName ? `:${limitParamName}` : opts.limit}`;
+ }
return `SELECT ${selectCols} FROM ${def.name}${where}${orderBy}${limit};`;
}
function selectCql(
diff --git a/fluxer_api/src/api/database/PostgresKvQueryExecutor.ts b/fluxer_api/src/api/database/PostgresKvQueryExecutor.ts
index 271de7516..5139f1d56 100644
--- a/fluxer_api/src/api/database/PostgresKvQueryExecutor.ts
+++ b/fluxer_api/src/api/database/PostgresKvQueryExecutor.ts
@@ -12,7 +12,6 @@ import type {
import {isConditionalQuery, validateTtlSeconds} from '@app/api/database/CassandraTypes';
import {Logger} from '@app/api/Logger';
import {type IPostgresClient, type PostgresQueryable, quoteIdentifier} from '@pkgs/postgres/src/Client';
-import cassandra from 'cassandra-driver';
type Row = Record<string, unknown>;
type EqWhereExpr = Extract<WhereExpr<Row>, {kind: 'eq'}>;
@@ -59,7 +58,6 @@ export type CandidatePlan =
| {kind: 'range'; lowerBound: string; upperBound: string}
| {kind: 'ranges'; lowerBounds: Array<string>; upperBounds: Array<string>}
| {kind: 'rangeGroups'; groups: Array<RangeGroup>}
- | {kind: 'partitionKeys'; partitionKeys: Array<string>}
| {kind: 'scan'};
interface QueryPlan {
@@ -69,7 +67,6 @@ interface QueryPlan {
interface QueryShape {
leadingClauses: ReadonlyArray<PinnedWhereExpr>;
- partitionClauses: ReadonlyArray<PinnedWhereExpr> | null;
inClauses: ReadonlyArray<InWhereExpr>;
whereColumns: ReadonlyArray<string>;
requiredColumns: ReadonlyArray<string> | null;
@@ -110,11 +107,6 @@ const NON_NUMERIC_ROW_KEY_RANGES = [
`kv.row_key COLLATE "C" >= '${BIGINT_ROW_KEY_PREFIX}:'`,
];
-export async function postgresKvPassIsFresh(
- client: IPostgresClient,
- marker: string,
- maxAgeMs: number,
-): Promise<boolean> {
const result = await client.query(
`SELECT 1 FROM ${quoteIdentifier(client.kvTable())} WHERE table_name = $1 AND row_key = $2 AND (row_data ->> 'applied_at')::timestamptz > now() - make_interval(secs => $3::double precision)`,
[POSTGRES_KV_MIGRATION_TABLE, marker, maxAgeMs / 1000],
@@ -287,6 +279,13 @@ function rowKeyFromParams(meta: KvQueryMeta, params: CassandraParams): string {
return rowKey(meta, paramsRow(params, (meta.pkColumns ?? meta.table.primaryKey) as ReadonlyArray<string>));
}
+function comparisonValue(value: unknown): unknown {
+ if (value instanceof Date) {
+ return value.getTime();
+ }
+ return value?.constructor?.name === 'LocalDate' ? value.toString() : value;
+}
+
function compareValues(left: unknown, right: unknown): number {
if (typeof left === 'bigint' || typeof right === 'bigint') {
const l = typeof left === 'bigint' ? left : BigInt(left as number | string);
@@ -294,9 +293,8 @@ function compareValues(left: unknown, right: unknown): number {
if (l === r) return 0;
return l < r ? -1 : 1;
}
- const l = left instanceof Date ? left.getTime() : left?.constructor?.name === 'LocalDate' ? left.toString() : left;
- const r =
- right instanceof Date ? right.getTime() : right?.constructor?.name === 'LocalDate' ? right.toString() : right;
+ const l = comparisonValue(left);
+ const r = comparisonValue(right);
if (Buffer.isBuffer(l) && Buffer.isBuffer(r)) return Buffer.compare(l, r);
if (l === r) return 0;
return (l as number | string) < (r as number | string) ? -1 : 1;
@@ -452,21 +450,9 @@ function queryShape(meta: KvQueryMeta): QueryShape {
if (!clause) break;
leadingClauses.push(clause);
}
- const partitionColumns = meta.table.partitionKey as ReadonlyArray<string>;
- const partitionClauses: Array<PinnedWhereExpr> = [];
- for (const column of partitionColumns) {
- const clause = pinnedClause(where, column);
- if (!clause) {
- partitionClauses.length = 0;
- break;
- }
- partitionClauses.push(clause);
- }
const whereColumns = [...new Set(where.flatMap(clauseColumns))];
const shape: QueryShape = {
leadingClauses,
- partitionClauses:
- partitionColumns.length > 0 && partitionClauses.length === partitionColumns.length ? partitionClauses : null,
inClauses: where.filter((clause): clause is InWhereExpr => clause.kind === 'in'),
whereColumns,
requiredColumns: requiredColumnsFor(meta, whereColumns),
@@ -632,18 +618,6 @@ export function buildCandidatePlan(meta: KvQueryMeta, params: CassandraParams):
exact,
};
}
- if (shape.partitionClauses) {
- const partition = pinnedColumns(shape.partitionClauses, params);
- if (
- partition.length === shape.partitionClauses.length &&
- combinationCount(partition.map((column) => column.segments)) <= MAX_ROW_KEY_COMBINATIONS
- ) {
- return {
- candidates: {kind: 'partitionKeys', partitionKeys: keyPrefixes(partition.map((column) => column.segments))},
- exact: planIsExact(meta, shape, partition),
- };
- }
- }
return {candidates: {kind: 'scan'}, exact: planIsExact(meta, shape, [])};
}
@@ -667,10 +641,6 @@ function planFragments(plan: Exclude<CandidatePlan, {kind: 'rangeGroups'}>): Pla
};
case 'ranges':
return rangeFragments(plan);
- case 'partitionKeys':
- return plan.partitionKeys.length === 1
- ? {predicate: ' AND kv.partition_key = $2', params: [plan.partitionKeys[0]]}
- : {predicate: ' AND kv.partition_key = ANY($2::text[])', params: [plan.partitionKeys]};
default:
return {predicate: '', params: []};
}
@@ -920,12 +890,6 @@ CREATE TABLE IF NOT EXISTS ${table} (
await db.query(
`CREATE INDEX IF NOT EXISTS ${quoteIdentifier(`${kvTable}_expires_idx`)} ON ${table} (expires_at) WHERE expires_at IS NOT NULL`,
);
- await db.query(
- `CREATE INDEX IF NOT EXISTS ${quoteIdentifier(`${kvTable}_messages_message_idx`)} ON ${table} (partition_key, ((CASE WHEN row_data -> 'message_id' ->> 'value' ~ '^-?[0-9]+$' THEN (row_data -> 'message_id' ->> 'value')::bigint END))) WHERE table_name = 'messages'`,
- );
- await db.query(
- `CREATE INDEX IF NOT EXISTS ${quoteIdentifier(`${kvTable}_message_reactions_message_idx`)} ON ${table} (partition_key, ((CASE WHEN row_data -> 'message_id' ->> 'value' ~ '^-?[0-9]+$' THEN (row_data -> 'message_id' ->> 'value')::bigint END))) WHERE table_name = 'message_reactions'`,
- );
await repairInvalidPostgresKvIndexes(db, kvTable);
const migrated = await db.query(`SELECT 1 FROM ${table} WHERE table_name = $1 AND row_key = $2 LIMIT 1`, [
POSTGRES_KV_MIGRATION_TABLE,
@@ -1100,14 +1064,14 @@ export class PostgresKvQueryExecutor {
const page = await this.walkScan(meta, params, walk, pageSize, this.client);
if (!page.more || page.after === null) return {rows: this.projected(meta, page.entries), pageState: null};
const position = {offset: offset + page.entries.length, after: page.after};
- return {
- rows: this.projected(meta, page.entries),
- pageState: encodePageState(
- walk.numeric
- ? {...position, keyed: true}
- : {...position, byRowKey: true, ...(walk.since === null ? {} : {since: walk.since})},
- ),
- };
+ const rows = this.projected(meta, page.entries);
+ let state: PageState;
+ if (walk.numeric) {
+ state = {...position, keyed: true};
+ } else {
+ state = {...position, byRowKey: true, ...(walk.since === null ? {} : {since: walk.since})};
+ }
+ return {rows, pageState: encodePageState(state)};
}
private async walkScan(
@@ -1342,17 +1306,6 @@ export class PostgresKvQueryExecutor {
private async upsert(meta: KvQueryMeta, params: CassandraParams, db: PostgresQueryable): Promise<Array<Row>> {
const incoming = rowFromParams(meta, params);
- const key = rowKey(meta, incoming);
- if (meta.ifNotExists) {
- if (await this.getRow(meta, key, db)) {
- return [{'[applied]': false}];
- }
- await db.query(
- `DELETE FROM ${this.table} WHERE table_name = $1 AND row_key = $2 AND expires_at IS NOT NULL AND expires_at <= now()`,
- [meta.table.name, key],
- 'kv_del_expired',
- );
- }
const explicit = ttlExpiresAt(meta, params);
const expiresAt = explicit === undefined ? (defaultExpiresAt(meta) ?? null) : explicit;
const result = await db.query(
@@ -1515,12 +1468,15 @@ WHERE NOT $6`,
}
const explicit = ttlExpiresAt(meta, params);
const fallback = explicit === undefined ? defaultExpiresAt(meta) : undefined;
- const [expiresAtExpr, statementName] =
- explicit !== undefined
- ? ['EXCLUDED.expires_at', 'kv_patch_set_ttl']
- : fallback !== undefined
- ? ['GREATEST(kv.expires_at, EXCLUDED.expires_at)', 'kv_patch_default_ttl']
- : [KEPT_EXPIRES_AT, 'kv_patch_keep_ttl'];
+ let patchExpiration: [string, string];
+ if (explicit !== undefined) {
+ patchExpiration = ['EXCLUDED.expires_at', 'kv_patch_set_ttl'];
+ } else if (fallback !== undefined) {
+ patchExpiration = ['GREATEST(kv.expires_at, EXCLUDED.expires_at)', 'kv_patch_default_ttl'];
+ } else {
+ patchExpiration = [KEPT_EXPIRES_AT, 'kv_patch_keep_ttl'];
+ }
+ const [expiresAtExpr, statementName] = patchExpiration;
await db.query(
`INSERT INTO ${this.table} AS kv (table_name, partition_key, row_key, row_data, expires_at, updated_at)
VALUES ($1, $2, $3, $4::jsonb, $5, now())
@@ -1569,14 +1525,4 @@ DO UPDATE SET partition_key = EXCLUDED.partition_key, row_data = ${MERGED_ROW_DA
'kv_del_keys',
);
}
-
- private async getRow(meta: KvQueryMeta, key: string, db: PostgresQueryable): Promise<Row | null> {
- const result = await db.query<{row_data: unknown}>(
- `SELECT row_data FROM ${this.table} WHERE table_name = $1 AND row_key = $2 AND (expires_at IS NULL OR expires_at > now()) LIMIT 1`,
- [meta.table.name, key],
- 'kv_get_row',
- );
- const row = result.rows[0];
- return row ? decodeRow(row.row_data) : null;
- }
}
diff --git a/fluxer_api/src/api/guild/services/GuildSearchService.ts b/fluxer_api/src/api/guild/services/GuildSearchService.ts
index f2f0c3769..81d061b3d 100644
--- a/fluxer_api/src/api/guild/services/GuildSearchService.ts
+++ b/fluxer_api/src/api/guild/services/GuildSearchService.ts
@@ -21,7 +21,6 @@ import {searchExistingMessages} from '@app/api/search/MessageSearchResultReconci
import {channelRequiresAgeVerification} from '@app/api/search/SearchNsfwUtils';
import {accessibleRequestedThreadIds, accessibleThreadIds} from '@app/api/search/ThreadSearchScope';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
-import {canUserAccessNsfwContent} from '@app/api/utils/AgeUtils';
import {mapWithConcurrency} from '@app/api/utils/ConcurrencyUtils';
import type {WorkerTaskName} from '@app/api/worker/WorkerLaneConfig';
import {ChannelTypes, Permissions} from '@fluxer/constants/src/ChannelConstants';
@@ -32,14 +31,12 @@ import {FeatureTemporarilyDisabledError} from '@fluxer/errors/src/domains/core/F
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import {MissingPermissionsError} from '@fluxer/errors/src/domains/core/MissingPermissionsError';
import {NsfwContentRequiresAgeVerificationError} from '@fluxer/errors/src/domains/moderation/NsfwContentRequiresAgeVerificationError';
-import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import type {MessageSearchRequest} from '@fluxer/schema/src/domains/message/MessageRequestSchemas';
import type {MessageSearchResponse} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';
import type {IWorkerService} from '@pkgs/worker/src/contracts/IWorkerService';
const GUILD_FANOUT_CONCURRENCY = 16;
const PERMISSION_CHECK_CONCURRENCY = 64;
-const CHANNEL_INDEX_JOB_ENQUEUE_CONCURRENCY = 16;
export class GuildSearchService {
private readonly responseMapper: MessageSearchResponseMapper;
@@ -174,15 +171,18 @@ export class GuildSearchService {
);
return {indexing: true};
}
- const searchChannelIds = threadsVisible
- ? await this.threadSearchChannelIds({
- userId,
- guildId,
- validChannelIds,
- channelMap,
- requestedIds: explicitChannelIds ? new Set(params.channelIds.map((id) => id.toString())) : null,
- })
- : validChannelIds.map((id) => id.toString());
+ let searchChannelIds: Array<string>;
+ if (threadsVisible) {
+ searchChannelIds = await this.threadSearchChannelIds({
+ userId,
+ guildId,
+ validChannelIds,
+ channelMap,
+ requestedIds: explicitChannelIds ? new Set(params.channelIds.map((id) => id.toString())) : null,
+ });
+ } else {
+ searchChannelIds = validChannelIds.map((id) => id.toString());
+ }
const filters = buildMessageSearchFilters(searchParams, searchChannelIds);
const hitsPerPage = searchParams.hits_per_page ?? 25;
const page = searchParams.page ?? 1;
@@ -364,27 +364,6 @@ export class GuildSearchService {
return lookup;
}
- private async getCanUserAccessNsfw(userId: UserID): Promise<boolean> {
- const user = await this.userRepository.findUnique(userId);
- if (!user) {
- throw new UnknownUserError();
- }
- return canUserAccessNsfwContent(user);
- }
-
- private async queueIndexingChannels(channelIds: Iterable<string>): Promise<void> {
- await mapWithConcurrency(Array.from(channelIds), CHANNEL_INDEX_JOB_ENQUEUE_CONCURRENCY, (channelId) =>
- this.workerService.addJob(
- 'indexChannelMessages',
- {channelId},
- {
- jobKey: `indexChannelMessages-${channelId}`,
- maxAttempts: 3,
- },
- ),
- );
- }
-
private async addRequestedThreads(
userId: UserID,
threadIds: ReadonlyArray<ChannelID>,
diff --git a/fluxer_api/src/api/guild/services/channel/ChannelOperationsService.ts b/fluxer_api/src/api/guild/services/channel/ChannelOperationsService.ts
index 6e6eeb630..191ec5347 100644
--- a/fluxer_api/src/api/guild/services/channel/ChannelOperationsService.ts
+++ b/fluxer_api/src/api/guild/services/channel/ChannelOperationsService.ts
@@ -198,8 +198,12 @@ export class ChannelOperationsService {
channelName = ChannelNameType.parse(channelName);
}
}
- const requestedNsfwOverride =
- params.data.nsfw_override !== undefined ? params.data.nsfw_override : params.data.nsfw === true ? true : null;
+ let requestedNsfwOverride: boolean | null;
+ if (params.data.nsfw_override !== undefined) {
+ requestedNsfwOverride = params.data.nsfw_override;
+ } else {
+ requestedNsfwOverride = params.data.nsfw === true ? true : null;
+ }
const requestedContentWarningLevel =
params.data.content_warning_level === ContentWarningLevel.CONTENT_WARNING
? ContentWarningLevel.CONTENT_WARNING
diff --git a/fluxer_api/src/api/guild/services/data/GuildDataHelpers.ts b/fluxer_api/src/api/guild/services/data/GuildDataHelpers.ts
index 78191019c..a832aa381 100644
--- a/fluxer_api/src/api/guild/services/data/GuildDataHelpers.ts
+++ b/fluxer_api/src/api/guild/services/data/GuildDataHelpers.ts
@@ -41,19 +41,12 @@ export class GuildDataHelpers {
}
computeGuildChanges(
- previousSnapshot: Record<string, unknown> | null,
- guildOrSnapshot: Guild | Record<string, unknown> | null,
- keys?: ReadonlySet<string>,
+ previousSnapshot: Record<string, unknown>,
+ guild: Guild,
+ keys: ReadonlySet<string>,
): GuildAuditLogChange {
- const currentSnapshot = guildOrSnapshot
- ? 'id' in guildOrSnapshot
- ? serializeGuildForAudit(guildOrSnapshot as Guild)
- : guildOrSnapshot
- : null;
+ const currentSnapshot = serializeGuildForAudit(guild);
const changes = this.guildAuditLogService.computeChanges(previousSnapshot, currentSnapshot);
- if (!keys) {
- return changes;
- }
return changes.filter((change) => keys.has(change.key));
}
diff --git a/fluxer_api/src/api/test/mocks/FakeNatsConnectionManager.ts b/fluxer_api/src/api/test/mocks/FakeNatsConnectionManager.ts
index 7aa75d503..a3b08b7f4 100644
--- a/fluxer_api/src/api/test/mocks/FakeNatsConnectionManager.ts
+++ b/fluxer_api/src/api/test/mocks/FakeNatsConnectionManager.ts
@@ -39,12 +39,13 @@ export class FakeNatsConnectionManager implements INatsConnectionManager {
this.requests.push({subject, body, timeout: options?.timeout});
const reply = this.reply(subject, body, options?.timeout);
if (reply instanceof Error) throw reply;
- return {
- data:
- reply instanceof Uint8Array
- ? reply
- : new TextEncoder().encode(typeof reply === 'string' ? reply : JSON.stringify(reply)),
- };
+ let encoded: Uint8Array;
+ if (reply instanceof Uint8Array) {
+ encoded = reply;
+ } else {
+ encoded = new TextEncoder().encode(typeof reply === 'string' ? reply : JSON.stringify(reply));
+ }
+ return {data: encoded};
},
} as unknown as NatsConnection;
}
diff --git a/fluxer_api/src/api/test/mocks/RepositoryBackedMessageResponseDataService.ts b/fluxer_api/src/api/test/mocks/RepositoryBackedMessageResponseDataService.ts
index 875519036..a56bc4f9f 100644
--- a/fluxer_api/src/api/test/mocks/RepositoryBackedMessageResponseDataService.ts
+++ b/fluxer_api/src/api/test/mocks/RepositoryBackedMessageResponseDataService.ts
@@ -226,7 +226,7 @@ export class RepositoryBackedMessageResponseDataService extends MessageResponseD
options.includeReactions ? this.mapReactions(message, options.currentUserId) : Promise.resolve(null),
this.resolveReferencedMessage(message, options),
]);
- return {
+ const responseFields = {
id: message.id.toString(),
channel_id: message.channelId.toString(),
author,
@@ -246,14 +246,19 @@ export class RepositoryBackedMessageResponseDataService extends MessageResponseD
attachments: await this.mapAttachments(message.attachments, message),
stickers: this.mapStickers(message.stickers),
reactions,
- message_reference: message.reference
- ? {
- channel_id: message.reference.channelId.toString(),
- ...(message.reference.messageId ? {message_id: message.reference.messageId.toString()} : {}),
- guild_id: message.reference.guildId?.toString() ?? null,
- type: message.reference.type,
- }
- : null,
+ };
+ let messageReference: MessageResponse['message_reference'] = null;
+ if (message.reference) {
+ messageReference = {
+ channel_id: message.reference.channelId.toString(),
+ ...(message.reference.messageId ? {message_id: message.reference.messageId.toString()} : {}),
+ guild_id: message.reference.guildId?.toString() ?? null,
+ type: message.reference.type,
+ };
+ }
+ return {
+ ...responseFields,
+ message_reference: messageReference,
message_snapshots: await this.mapSnapshots(message.messageSnapshots, message),
nonce: options.nonce ?? null,
call: this.mapCall(message.call),
diff --git a/fluxer_api/src/api/user/services/UserAccountSettingsService.ts b/fluxer_api/src/api/user/services/UserAccountSettingsService.ts
index c77bc06bf..cd4b47536 100644
--- a/fluxer_api/src/api/user/services/UserAccountSettingsService.ts
+++ b/fluxer_api/src/api/user/services/UserAccountSettingsService.ts
@@ -348,11 +348,10 @@ export class UserAccountSettingsService {
const channelOverrides = new Map<ChannelID, ChannelOverride>();
for (const [channelIdStr, override] of Object.entries(data.channel_overrides ?? {})) {
const channelId = createChannelID(BigInt(channelIdStr));
- const flags = flagsWritable
- ? override.flags !== undefined
+ const flags =
+ flagsWritable && override.flags !== undefined
? override.flags & CHANNEL_OVERRIDE_FLAG_MASK
- : previousOverrides.get(channelId)?.flags
- : previousOverrides.get(channelId)?.flags;
+ : previousOverrides.get(channelId)?.flags;
channelOverrides.set(channelId, {
collapsed: override.collapsed,
message_notifications: override.message_notifications,
diff --git a/fluxer_api/src/api/webhook/WebhookRepository.ts b/fluxer_api/src/api/webhook/WebhookRepository.ts
index bc4b1f56c..2b81043f4 100644
--- a/fluxer_api/src/api/webhook/WebhookRepository.ts
+++ b/fluxer_api/src/api/webhook/WebhookRepository.ts
@@ -146,9 +146,8 @@ export class WebhookRepository {
name: string;
avatarHash: string | null;
}>,
- oldData?: WebhookRow | null,
): Promise<Webhook | null> {
- const existing = oldData !== undefined ? (oldData ? new Webhook(oldData) : null) : await this.findUnique(webhookId);
+ const existing = await this.findUnique(webhookId);
if (!existing) return null;
const updatedData: WebhookRow = {
webhook_id: webhookId,
@@ -170,7 +169,6 @@ export class WebhookRepository {
patch: buildPatchFromData(updatedData, current, WEBHOOK_COLUMNS, ['webhook_id', 'webhook_token']),
}),
Webhooks,
- {initialData: oldData},
);
const batch = new BatchBuilder();
if (existing.guildId !== updatedData.guild_id) {
diff --git a/fluxer_api/src/api/webhook/WebhookService.ts b/fluxer_api/src/api/webhook/WebhookService.ts
index 2c3b1adb9..3658bb6d3 100644
--- a/fluxer_api/src/api/webhook/WebhookService.ts
+++ b/fluxer_api/src/api/webhook/WebhookService.ts
@@ -154,6 +154,12 @@ interface WebhookExecuteLibredeskParams extends WebhookTokenParams, WebhookThrea
requestCache: RequestCache;
}
+const WEBHOOK_AUDIT_ACTIONS = {
+ create: {name: 'guild_webhook_create', type: AuditLogActionType.WEBHOOK_CREATE},
+ update: {name: 'guild_webhook_update', type: AuditLogActionType.WEBHOOK_UPDATE},
+ delete: {name: 'guild_webhook_delete', type: AuditLogActionType.WEBHOOK_DELETE},
+} as const;
+
const WEBHOOK_AVATAR_MISSING_CACHE_VALUE = '__fluxer_webhook_avatar_missing__';
const HOSTED_WEBHOOK_AVATAR_URLS = {
github: 'https://fluxer.app/static/img/app-webhook-github.2d0319169a3aee33.webp',
@@ -978,22 +984,12 @@ export class WebhookService {
previousSnapshot?: Record<string, unknown> | null;
auditLogReason?: string | null;
}): Promise<void> {
- const actionName =
- params.action === 'create'
- ? 'guild_webhook_create'
- : params.action === 'update'
- ? 'guild_webhook_update'
- : 'guild_webhook_delete';
+ const actionName = WEBHOOK_AUDIT_ACTIONS[params.action].name;
const previousSnapshot =
params.action === 'create' ? null : (params.previousSnapshot ?? this.serializeWebhookForAudit(params.webhook));
const nextSnapshot = params.action === 'delete' ? null : this.serializeWebhookForAudit(params.webhook);
const changes = this.guildAuditLogService.computeChanges(previousSnapshot, nextSnapshot);
- const actionType =
- params.action === 'create'
- ? AuditLogActionType.WEBHOOK_CREATE
- : params.action === 'update'
- ? AuditLogActionType.WEBHOOK_UPDATE
- : AuditLogActionType.WEBHOOK_DELETE;
+ const actionType = WEBHOOK_AUDIT_ACTIONS[params.action].type;
try {
await this.guildAuditLogService
.createBuilder(params.guildId, params.userId)