A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(api): resolve visitor access to community channels
Hampus0bf12752d71a
diff --git a/fluxer_api/src/api/channel/services/AuthenticatedChannel.ts b/fluxer_api/src/api/channel/services/AuthenticatedChannel.tsindex 84f21442c..1492accf3 100644--- a/fluxer_api/src/api/channel/services/AuthenticatedChannel.ts+++ b/fluxer_api/src/api/channel/services/AuthenticatedChannel.ts@@ -3,6 +3,7 @@import type {Channel} from '@app/api/models/Channel';import type {ThreadMember} from '@app/api/models/ThreadMember';import type {ThreadState} from '@app/api/models/ThreadState';+import type {AccountTier} from '@app/api/user/AccountTier';import type {ThreadActorContext} from '@fluxer/constants/src/ThreadPermissionUtils';import type {GuildMemberResponse} from '@fluxer/schema/src/domains/guild/GuildMemberSchemas';import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';@@ -17,6 +18,15 @@ export interface AuthenticatedThread {enforceMfa: (permission: bigint) => void;}+interface AuthenticatedVisitor {+ permissions: bigint;+ tier: AccountTier;+ adultContentAllowed: boolean;+ historyCutoff: Date | null;+}++export type ChannelAccess = 'member' | 'visitor_read';+export interface AuthenticatedChannel {channel: Channel;guild: GuildResponse | null;@@ -24,4 +34,5 @@ export interface AuthenticatedChannel {hasPermission: (permission: bigint) => Promise<boolean>;checkPermission: (permission: bigint) => Promise<void>;thread?: AuthenticatedThread;+ visitor?: AuthenticatedVisitor;}diff --git a/fluxer_api/src/api/channel/services/BaseChannelAuthService.ts b/fluxer_api/src/api/channel/services/BaseChannelAuthService.tsindex 56e903206..3c6e3663e 100644--- a/fluxer_api/src/api/channel/services/BaseChannelAuthService.ts+++ b/fluxer_api/src/api/channel/services/BaseChannelAuthService.ts@@ -2,7 +2,11 @@import type {ChannelID, GuildID, UserID} from '@app/api/BrandedTypes';import type {ChannelRepository} from '@app/api/channel/ChannelRepository';-import type {AuthenticatedChannel, AuthenticatedThread} from '@app/api/channel/services/AuthenticatedChannel';+import type {+ AuthenticatedChannel,+ AuthenticatedThread,+ ChannelAccess,+} from '@app/api/channel/services/AuthenticatedChannel';import {DMPermissionValidator} from '@app/api/channel/services/DMPermissionValidator';import {ensurePersonalNotesChannelExists,@@ -25,9 +29,19 @@ import {type ThreadViewer,viewerActive,} from '@app/api/experiment/ChannelThreadsGate';-import {isGuildMemberTimedOut} from '@app/api/guild/GuildModel';+import {isGuildMemberTimedOut, mapGuildToGuildResponse} from '@app/api/guild/GuildModel';import type {GuildRepository} from '@app/api/guild/repositories/GuildRepository';import {createGuildMfaEnforcer} from '@app/api/guild/services/GuildMfaEnforcement';+import {guildVisitHistoryWindowTotal} from '@app/api/guild/visits/GuildVisitMetrics';+import {+ chargeGuildVisitorBuckets,+ computeVisitorChannelPermissionsFor,+ GUILD_VISITOR_READ_BUCKETS,+ recordGuildVisitHistoryRead,+ resolveGuildVisitorAccess,+ toVisitorChannelModel,+ visitorHistoryCutoff,+} from '@app/api/guild/visits/GuildVisitorAccess';import type {GuildChannelAuthContext, IGatewayService} from '@app/api/infrastructure/IGatewayService';import type {Channel} from '@app/api/models/Channel';import type {GuildMember} from '@app/api/models/GuildMember';@@ -35,6 +49,7 @@ import type {User} from '@app/api/models/User';import type {UserRepository} from '@app/api/user/repositories/UserRepository';import {canUserAccessNsfwContent} from '@app/api/utils/AgeUtils';import {ChannelTypes, Permissions} from '@fluxer/constants/src/ChannelConstants';+import {GUILD_VISITOR_PERMISSIONS_MASK} from '@fluxer/constants/src/GuildVisitConstants';import {canViewThread,isThreadModerator,@@ -96,11 +111,13 @@ export abstract class BaseChannelAuthService {channelId,viewer,skipNsfwValidation,+ access = 'member',}: {userId: UserID;channelId: ChannelID;viewer: ThreadViewer;skipNsfwValidation?: boolean;+ access?: ChannelAccess;}): Promise<AuthenticatedChannel> {if (isPersonalNotesChannelId({userId, channelId})) {const channel = await ensurePersonalNotesChannelExists({@@ -121,7 +138,112 @@ export abstract class BaseChannelAuthService {const recipients = await this.userRepository.listUsers(Array.from(channel.recipientIds));return this.getDMChannelAuth({channel, recipients, userId});}- return this.getGuildChannelAuth({channel, userId, skipNsfwValidation});+ if (access !== 'visitor_read') {+ return this.getGuildChannelAuth({channel, userId, skipNsfwValidation});+ }+ try {+ return await this.getGuildChannelAuth({channel, userId, skipNsfwValidation});+ } catch (memberError) {+ const visitorAuth = await this.getVisitorChannelAuth({channel, userId});+ if (visitorAuth) return visitorAuth;+ throw memberError;+ }+ }++ protected async getVisitorChannelAuth({+ channel,+ userId,+ }: {+ channel: Channel;+ userId: UserID;+ }): Promise<AuthenticatedChannel | null> {+ const guildId = channel.guildId;+ if (guildId === null) return null;+ const access = await resolveGuildVisitorAccess({userId, guildId});+ if (!access) return null;+ const channelData = this.channelRepository.channelData;+ let thread: AuthenticatedThread | undefined;+ let permissions: bigint;+ if (THREAD_CHANNEL_TYPES.has(channel.type)) {+ const [state, parent] = await Promise.all([+ this.channelRepository.threads.getState(channel.id),+ channel.parentId ? channelData.findUnique(channel.parentId) : Promise.resolve(null),+ ]);+ if (!state || !parent || parent.guildId !== guildId || !THREAD_PARENT_CHANNEL_TYPES.has(parent.type)) {+ throw new UnknownChannelError();+ }+ const category = await this.findParentCategory(parent);+ const parentPermissions = computeVisitorChannelPermissionsFor({+ access,+ channel: parent,+ parent: category,+ thread: {private: state.type === ChannelTypes.PRIVATE_THREAD},+ });+ permissions = threadViewPermissions(parentPermissions) & GUILD_VISITOR_PERMISSIONS_MASK;+ thread = {+ state,+ parent: toVisitorChannelModel(parent),+ member: null,+ parentPermissions,+ actor: {+ permissions: parentPermissions,+ isOwner: false,+ timedOut: false,+ thread: {+ type: state.type,+ archived: state.archived,+ locked: state.locked,+ invitable: state.invitable ?? true,+ },+ isThreadOwner: false,+ isMember: false,+ },+ isModerator: false,+ enforceMfa: () => {},+ };+ } else {+ permissions = computeVisitorChannelPermissionsFor({+ access,+ channel,+ parent: await this.findParentCategory(channel),+ });+ }+ if ((permissions & Permissions.VIEW_CHANNEL) !== Permissions.VIEW_CHANNEL) throw new UnknownChannelError();+ await chargeGuildVisitorBuckets({buckets: GUILD_VISITOR_READ_BUCKETS, userId, guildId, tier: access.tier});+ await recordGuildVisitHistoryRead({userId, guildId, tier: access.tier});+ const guild = mapGuildToGuildResponse(access.view.guild);+ const canReadHistory = (permissions & Permissions.READ_MESSAGE_HISTORY) === Permissions.READ_MESSAGE_HISTORY;+ const windowed = access.historyCutoff !== null;+ const effectivePermissions = windowed ? permissions & ~Permissions.READ_MESSAGE_HISTORY : permissions;+ const cutoff = visitorHistoryCutoff({+ canReadHistory,+ guildCutoff: guild.message_history_cutoff ?? null,+ windowCutoff: access.historyCutoff,+ });+ if (windowed) guildVisitHistoryWindowTotal.inc();+ const hasPermission = async (permission: bigint): Promise<boolean> =>+ (effectivePermissions & permission) === permission;+ return {+ channel: toVisitorChannelModel(channel),+ guild: {...guild, message_history_cutoff: cutoff},+ member: null,+ hasPermission,+ checkPermission: async (permission: bigint) => {+ if (!(await hasPermission(permission))) throw new MissingPermissionsError();+ },+ thread,+ visitor: {+ permissions: effectivePermissions,+ tier: access.tier,+ adultContentAllowed: access.adultContentAllowed,+ historyCutoff: access.historyCutoff,+ },+ };+ }++ private async findParentCategory(channel: Channel): Promise<Channel | null> {+ if (!channel.parentId || channel.type === ChannelTypes.GUILD_CATEGORY) return null;+ return this.channelRepository.channelData.findUnique(channel.parentId);}protected async getRealPersonalNotesChannelAuth({@@ -276,7 +398,7 @@ export abstract class BaseChannelAuthService {};}- async resolveThreadPermissionContext({+ private async resolveThreadPermissionContext({thread,userId,}: {diff --git a/fluxer_api/src/api/channel/services/ChannelRequestService.ts b/fluxer_api/src/api/channel/services/ChannelRequestService.tsindex 7b69ba62d..5a0981580 100644--- a/fluxer_api/src/api/channel/services/ChannelRequestService.ts+++ b/fluxer_api/src/api/channel/services/ChannelRequestService.ts@@ -14,19 +14,13 @@ import type {UserCacheService} from '@app/api/infrastructure/UserCacheService';import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware';import type {Channel} from '@app/api/models/Channel';import type {User} from '@app/api/models/User';-import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';import {ChannelTypes, Permissions} from '@fluxer/constants/src/ChannelConstants';import {InvalidChannelTypeError} from '@fluxer/errors/src/domains/channel/InvalidChannelTypeError';import {UnknownChannelError} from '@fluxer/errors/src/domains/channel/UnknownChannelError';-import {ThrottledError} from '@fluxer/errors/src/domains/core/ThrottledError';import type {ChannelUpdateGatedRequest} from '@fluxer/schema/src/domains/channel/ChannelRequestSchemas';import type {ChannelResponse, ChannelSlowmodeStateResponse} from '@fluxer/schema/src/domains/channel/ChannelSchemas';import type {ICacheService} from '@pkgs/cache/src/ICacheService';-const FORUM_TAGS_LOCK_TTL_SECONDS = 5;-const FORUM_TAGS_LOCK_ACQUIRE_ATTEMPTS = 6;-const FORUM_TAGS_LOCK_RETRY_DELAY_MS = 50;-export class ChannelRequestService {constructor(private readonly channelService: ChannelService,@@ -44,6 +38,7 @@ export class ChannelRequestService {userId: params.userId,viewer: params.viewer,channelId: params.channelId,+ access: 'visitor_read',});return this.maskedChannelResponse(channel, params);}diff --git a/fluxer_api/src/api/channel/services/MessageInteractionService.ts b/fluxer_api/src/api/channel/services/MessageInteractionService.tsindex 8a6472803..9e7e27adc 100644--- a/fluxer_api/src/api/channel/services/MessageInteractionService.ts+++ b/fluxer_api/src/api/channel/services/MessageInteractionService.ts@@ -104,21 +104,24 @@ export class MessageInteractionService {userId,viewer,channelId,- requestCache,beforeTimestamp,limit,}: {userId: UserID;viewer: ThreadViewer;channelId: ChannelID;- requestCache: RequestCache;beforeTimestamp?: Date;limit?: number;}): Promise<{items: Array<ChannelPinResponse>;has_more: boolean;}> {- const authChannel = await this.pinAuthService.getChannelAuthenticated({userId, channelId, viewer});+ const authChannel = await this.pinAuthService.getChannelAuthenticated({+ userId,+ channelId,+ viewer,+ access: 'visitor_read',+ });const pins = await this.pinService.getChannelPins({authChannel, userId, requestCache, beforeTimestamp, limit});const original = pins.items.map((item) => item.message as MessageResponse);const messages = maskThreadArtifactsFor(viewer, authChannel.channel.guildId, original);@@ -161,14 +164,12 @@ export class MessageInteractionService {viewer,channelId,messageId,- requestCache,auditLogReason,}: {userId: UserID;viewer: ThreadViewer;channelId: ChannelID;messageId: MessageID;- requestCache: RequestCache;auditLogReason?: string | null;}): Promise<void> {const authChannel = await this.authService.getChannelAuthenticated({userId, channelId, viewer});@@ -266,7 +267,6 @@ export class MessageInteractionService {messageId: MessageID;emoji: string;targetId: UserID;- requestCache: RequestCache;}): Promise<void> {const authChannel = await this.authService.getChannelAuthenticated({userId, channelId, viewer});await this.reactionService.removeReaction({authChannel, messageId, emoji, targetId, sessionId, actorId: userId});@@ -279,7 +279,6 @@ export class MessageInteractionService {channelId,messageId,emoji,- requestCache,}: {userId: UserID;viewer: ThreadViewer;@@ -287,7 +286,6 @@ export class MessageInteractionService {channelId: ChannelID;messageId: MessageID;emoji: string;- requestCache: RequestCache;}): Promise<void> {await this.removeReaction({userId, viewer, sessionId, channelId, messageId, emoji, targetId: userId, requestCache});}diff --git a/fluxer_api/src/api/channel/services/message/MessageRetrievalService.ts b/fluxer_api/src/api/channel/services/message/MessageRetrievalService.tsindex b118b9333..af9fe5e8e 100644--- a/fluxer_api/src/api/channel/services/message/MessageRetrievalService.ts+++ b/fluxer_api/src/api/channel/services/message/MessageRetrievalService.ts@@ -4,7 +4,7 @@ import {AttachmentDecayService} from '@app/api/attachment/AttachmentDecayServiceimport type {ChannelID, MessageID, UserID} from '@app/api/BrandedTypes';import {mapChannelToResponse} from '@app/api/channel/ChannelMappers';import type {ChannelRepository} from '@app/api/channel/ChannelRepository';-import type {AuthenticatedChannel} from '@app/api/channel/services/AuthenticatedChannel';+import type {AuthenticatedChannel, ChannelAccess} from '@app/api/channel/services/AuthenticatedChannel';import {getDmChannelIdsForScope} from '@app/api/channel/services/message/DmScopeUtils';import type {MessageChannelAuthService} from '@app/api/channel/services/message/MessageChannelAuthService';import {attachmentDecayPayloadsForMessage} from '@app/api/channel/services/message/MessageHelpers';@@ -17,6 +17,8 @@ import type {MessageSearchService} from '@app/api/channel/services/message/Messaimport {maskThreadArtifactsFor, ThreadMessageResponses} from '@app/api/channel/services/message/ThreadMessageResponses';import type {ThreadViewer} from '@app/api/experiment/ChannelThreadsGate';import {maskChannelResponseThreadBits} from '@app/api/guild/services/ThreadPermissionBits';+import {chargeGuildVisitorBuckets, GUILD_VISITOR_SEARCH_BUCKETS} from '@app/api/guild/visits/GuildVisitorAccess';+import {assertVisitorSearchFilters} from '@app/api/guild/visits/GuildVisitorSearch';import type {UserCacheService} from '@app/api/infrastructure/UserCacheService';import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware';import type {Channel} from '@app/api/models/Channel';@@ -91,6 +93,7 @@ export class MessageRetrievalService {channelId: ChannelID;messageId?: MessageID;authChannel?: AuthenticatedChannel;+ access?: ChannelAccess;}): Promise<{access: MessageResponseAccessContext; authChannel: AuthenticatedChannel}> {const authChannel =params.authChannel ??@@ -98,6 +101,7 @@ export class MessageRetrievalService {userId: params.userId,channelId: params.channelId,viewer: params.viewer,+ access: params.access,}));if (params.messageId && !(await this.canAccessMessage(authChannel, params.messageId))) {throw new UnknownMessageError();@@ -119,13 +123,15 @@ export class MessageRetrievalService {viewer,channelId,messageId,+ access,}: {userId: UserID;viewer: ThreadViewer;channelId: ChannelID;messageId: MessageID;+ access?: ChannelAccess;}): Promise<Message> {- const authChannel = await this.channelAuthService.getChannelAuthenticated({userId, channelId, viewer});+ const authChannel = await this.channelAuthService.getChannelAuthenticated({userId, channelId, viewer, access});if (!(await this.canAccessMessage(authChannel, messageId))) {throw new UnknownMessageError();}@@ -143,13 +149,15 @@ export class MessageRetrievalService {viewer,channelId,messageIds,+ access,}: {userId: UserID;viewer: ThreadViewer;channelId: ChannelID;messageIds: Array<MessageID>;+ access?: ChannelAccess;}): Promise<Map<string, Message>> {- const authChannel = await this.channelAuthService.getChannelAuthenticated({userId, channelId, viewer});+ const authChannel = await this.channelAuthService.getChannelAuthenticated({userId, channelId, viewer, access});const canReadMessageHistory =!authChannel.guild || (await authChannel.hasPermission(Permissions.READ_MESSAGE_HISTORY));const cutoff = authChannel.guild?.message_history_cutoff ?? null;@@ -181,8 +189,23 @@ export class MessageRetrievalService {searchParams: MessageSearchRequest;requestCache: RequestCache;}): Promise<MessageSearchResponse> {- const authChannel = await this.channelAuthService.getChannelAuthenticated({userId, channelId, viewer});+ const authChannel = await this.channelAuthService.getChannelAuthenticated({+ userId,+ channelId,+ viewer,+ access: 'visitor_read',+ });const {channel} = authChannel;+ if (authChannel.visitor && channel.guildId !== null) {+ assertVisitorSearchFilters(searchParams);+ searchParams = {...searchParams, scope: 'current', channel_id: undefined, exclude_channel_id: undefined};+ await chargeGuildVisitorBuckets({+ buckets: GUILD_VISITOR_SEARCH_BUCKETS,+ userId,+ guildId: channel.guildId,+ tier: authChannel.visitor.tier,+ });+ }const hasReadHistory = !authChannel.guild || (await authChannel.hasPermission(Permissions.READ_MESSAGE_HISTORY));if (!hasReadHistory) {const cutoff = authChannel.guild?.message_history_cutoff;@@ -210,12 +233,14 @@ export class MessageRetrievalService {searchParams,});const scope = resolvedSearchParams.scope ?? 'current';- const channelIdStrings =- scope === 'current'- ? [channelId.toString()]- : resolvedSearchParams.channel_id- ? resolvedSearchParams.channel_id.map((id) => id.toString())- : [channelId.toString()];+ let channelIdStrings: Array<string>;+ if (scope === 'current') {+ channelIdStrings = [channelId.toString()];+ } else if (resolvedSearchParams.channel_id) {+ channelIdStrings = resolvedSearchParams.channel_id.map((id) => id.toString());+ } else {+ channelIdStrings = [channelId.toString()];+ }const normalizedSearchParams =scope === 'current' ? {...resolvedSearchParams, channel_id: undefined} : resolvedSearchParams;const filters = buildMessageSearchFilters(normalizedSearchParams, channelIdStrings);@@ -234,9 +259,12 @@ export class MessageRetrievalService {messageHistoryCutoff: !hasReadHistory ? (authChannel.guild?.message_history_cutoff ?? null) : null,canReadMessageHistory: hasReadHistory,};+ const visibleMessages = hasReadHistory+ ? result.messages+ : result.messages.filter((message) => this.isWithinCutoff(message.id, access.messageHistoryCutoff));const builtMessages = await createMessageResponseDataService().buildMessages({userId,- messages: result.messages,+ messages: visibleMessages,access,});const messageResponses = maskThreadArtifactsFor(viewer, channel.guildId, builtMessages).map(@@ -252,6 +280,10 @@ export class MessageRetrievalService {};}+ private isWithinCutoff(messageId: MessageID, cutoffIso: string | null): boolean {+ return cutoffIso !== null && this.isMessageAfterCutoff(messageId, cutoffIso);+ }+private async mapSearchChannelResponse(channel: Channel,userId: UserID,diff --git a/fluxer_api/src/api/channel/services/thread/ThreadForumService.ts b/fluxer_api/src/api/channel/services/thread/ThreadForumService.tsindex f15e380d5..ed6415679 100644--- a/fluxer_api/src/api/channel/services/thread/ThreadForumService.ts+++ b/fluxer_api/src/api/channel/services/thread/ThreadForumService.ts@@ -209,6 +209,7 @@ export class ThreadForumService {userId,channelId,viewer,+ access: 'visitor_read',});const allowed = threadOnly? THREAD_ONLY_CHANNEL_TYPES.has(auth.channel.type)diff --git a/fluxer_api/src/api/channel/services/thread/ThreadServiceContext.ts b/fluxer_api/src/api/channel/services/thread/ThreadServiceContext.tsindex 64d68e1a8..263032586 100644--- a/fluxer_api/src/api/channel/services/thread/ThreadServiceContext.ts+++ b/fluxer_api/src/api/channel/services/thread/ThreadServiceContext.ts@@ -94,6 +94,7 @@ export class ThreadServiceContext {}async parentActor(auth: AuthenticatedChannel, userId: UserID): Promise<ThreadActor> {+ if (auth.visitor) return {permissions: auth.visitor.permissions, isOwner: false, timedOut: false};const permissions = await this.gatewayService.getUserPermissions({guildId: auth.channel.guildId!,userId,diff --git a/fluxer_api/src/api/guild/visits/GuildVisitorAccess.ts b/fluxer_api/src/api/guild/visits/GuildVisitorAccess.tsnew file mode 100644index 000000000..9fa29874a--- /dev/null+++ b/fluxer_api/src/api/guild/visits/GuildVisitorAccess.ts@@ -0,0 +1,438 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {AsyncLocalStorage} from 'node:async_hooks';+import type {GuildID, UserID} from '@app/api/BrandedTypes';+import {guildIdToRoleId} from '@app/api/BrandedTypes';+import type {GuildVisitRow} from '@app/api/database/types/GuildVisitTypes';+import {userGuildVisitsActive} from '@app/api/experiment/GuildVisitsGate';+import {assertNotBannedFromGuildVisit, endGuildVisit} from '@app/api/guild/visits/GuildVisitLifecycle';+import {guildVisitorRateLimitedTotal} from '@app/api/guild/visits/GuildVisitMetrics';+import {readGuildVisitorViewGeneration} from '@app/api/guild/visits/GuildVisitorViewGeneration';+import {GuildVisitRepository} from '@app/api/guild/visits/GuildVisitRepository';+import {emitActivity} from '@app/api/infrastructure/activity/ActivityEvents';+import {Logger} from '@app/api/Logger';+import {resolveLimitSafe} from '@app/api/limits/LimitConfigUtils';+import {createLimitMatchContext} from '@app/api/limits/LimitMatchContextBuilder';+import {getKVClient} from '@app/api/middleware/ServiceRegistry';+import {+ getGuildDiscoveryRepository,+ getGuildRepository,+ getLimitConfigService,+ getUserRepository,+} from '@app/api/middleware/ServiceSingletons';+import {Channel} from '@app/api/models/Channel';+import type {Guild} from '@app/api/models/Guild';+import type {User} from '@app/api/models/User';+import type {HonoEnv} from '@app/api/types/HonoEnv';+import {type AccountTier, getAccountTier} from '@app/api/user/AccountTier';+import {canUserAccessNsfwContent} from '@app/api/utils/AgeUtils';+import {getRequestClientIp} from '@app/api/utils/RequestClientIp';+import {ChannelOverwriteTypes} from '@fluxer/constants/src/ChannelConstants';+import {DiscoveryApplicationStatus} from '@fluxer/constants/src/DiscoveryConstants';+import {GuildFeatures} from '@fluxer/constants/src/GuildConstants';+import {+ computeGuildVisitorChannelPermissions,+ GUILD_VISIT_OPEN_REFRESH_SECONDS,+ GuildVisitEndReasons,+ type GuildVisitorChannelInput,+ GuildVisitSources,+} from '@fluxer/constants/src/GuildVisitConstants';+import {MS_PER_DAY, MS_PER_HOUR} from '@fluxer/date_utils/src/DateConstants';+import {RateLimitError} from '@fluxer/errors/src/domains/core/RateLimitError';+import {BannedFromGuildError} from '@fluxer/errors/src/domains/guild/BannedFromGuildError';+import {IpBannedFromGuildError} from '@fluxer/errors/src/domains/guild/IpBannedFromGuildError';+import {getSameIpDecisionKey} from '@fluxer/ip_utils/src/IpAddress';+import {DEFAULT_STOCK_LIMITS} from '@fluxer/limits/src/LimitDefaults';+import type {IRateLimitService} from '@pkgs/rate_limit/src/IRateLimitService';+import type {Context} from 'hono';+import {createMiddleware} from 'hono/factory';+import {ms} from 'itty-time';++const VISIT_ROW_CACHE_MS = 5_000;+const VISITOR_VIEW_CACHE_MS = 60_000;+const MAX_CACHE_ENTRIES = 20_000;+const HISTORY_READ_KEY_TTL_SECONDS = 3 * 3600;++interface GuildVisitorView {+ guild: Guild;+ everyonePermissions: bigint;+ discoveryApproved: boolean;+}++export interface GuildVisitorAccess {+ visit: GuildVisitRow;+ view: GuildVisitorView;+ user: User;+ tier: AccountTier;+ adultContentAllowed: boolean;+ historyCutoff: Date | null;+}++const visitRepository = new GuildVisitRepository();+const visitRowCache = new Map<string, {row: GuildVisitRow | null; expiresAt: number}>();+const viewCache = new Map<string, {generation: string; view: GuildVisitorView; expiresAt: number}>();++function boundedSet<V>(map: Map<string, V>, key: string, value: V): void {+ map.delete(key);+ map.set(key, value);+ if (map.size > MAX_CACHE_ENTRIES) {+ const oldest = map.keys().next().value;+ if (oldest !== undefined) {+ map.delete(oldest);+ }+ }+}++function visitKey(userId: UserID, guildId: GuildID): string {+ return `${userId}:${guildId}`;+}++export function guildVisitRepository(): GuildVisitRepository {+ return visitRepository;+}++export function forgetGuildVisitRow(userId: UserID, guildId: GuildID): void {+ visitRowCache.delete(visitKey(userId, guildId));+}++export function resetGuildVisitorCachesForTesting(): void {+ visitRowCache.clear();+ viewCache.clear();+}++async function findGuildVisitRowCached(userId: UserID, guildId: GuildID): Promise<GuildVisitRow | null> {+ const key = visitKey(userId, guildId);+ const now = Date.now();+ const cached = visitRowCache.get(key);+ if (cached && cached.expiresAt > now) {+ return cached.row;+ }+ const row = await visitRepository.get(userId, guildId);+ boundedSet(visitRowCache, key, {row, expiresAt: now + VISIT_ROW_CACHE_MS});+ return row;+}++export async function loadGuildVisitorView(guildId: GuildID): Promise<GuildVisitorView | null> {+ const generation = await readGuildVisitorViewGeneration(guildId);+ const key = guildId.toString();+ const now = Date.now();+ const cached = viewCache.get(key);+ if (cached && cached.generation === generation && cached.expiresAt > now) {+ return cached.view;+ }+ const guildRepository = getGuildRepository();+ const [guild, everyoneRole, discovery] = await Promise.all([+ guildRepository.findUnique(guildId),+ guildRepository.getRole(guildIdToRoleId(guildId), guildId),+ getGuildDiscoveryRepository().findByGuildId(guildId),+ ]);+ if (!guild) {+ viewCache.delete(key);+ return null;+ }+ const view: GuildVisitorView = {+ guild,+ everyonePermissions: everyoneRole?.permissions ?? 0n,+ discoveryApproved: discovery?.status === DiscoveryApplicationStatus.APPROVED,+ };+ boundedSet(viewCache, key, {generation, view, expiresAt: now + VISITOR_VIEW_CACHE_MS});+ return view;+}++export function isGuildUnavailable(guild: Pick<Guild, 'features'>): boolean {+ return (+ guild.features.has(GuildFeatures.UNAVAILABLE_FOR_EVERYONE) ||+ guild.features.has(GuildFeatures.UNAVAILABLE_FOR_EVERYONE_BUT_STAFF)+ );+}++export function guildVisitAuthorizedByView(visit: Pick<GuildVisitRow, 'source'>, view: GuildVisitorView): boolean {+ if (isGuildUnavailable(view.guild)) {+ return false;+ }+ if (visit.source === GuildVisitSources.OPERATOR) {+ return true;+ }+ if (!view.guild.features.has(GuildFeatures.VISITS_ENABLED)) {+ return false;+ }+ if (visit.source === GuildVisitSources.DISCOVERY && !view.discoveryApproved) {+ return false;+ }+ return true;+}++function guildVisitHistoryDays(user: User): number {+ return resolveLimitSafe(+ getLimitConfigService().getConfigSnapshot(),+ createLimitMatchContext({user}),+ 'guild_visit_history_days_unverified',+ DEFAULT_STOCK_LIMITS.guild_visit_history_days_unverified,+ );+}++function guildVisitHistoryCutoff(user: User, tier: AccountTier, now: number = Date.now()): Date | null {+ if (tier === 'verified') {+ return null;+ }+ return new Date(now - guildVisitHistoryDays(user) * MS_PER_DAY);+}++export async function resolveGuildVisitorAccess(params: {+ userId: UserID;+ guildId: GuildID;+ user?: User | null;+}): Promise<GuildVisitorAccess | null> {+ const {userId, guildId} = params;+ if (!userGuildVisitsActive(userId)) {+ return null;+ }+ const visit = await findGuildVisitRowCached(userId, guildId);+ if (!visit) {+ return null;+ }+ const now = Date.now();+ if (visit.invite_expires_at !== null && visit.invite_expires_at.getTime() <= now) {+ return null;+ }+ const [member, view, user] = await Promise.all([+ getGuildRepository().getMember(guildId, userId),+ loadGuildVisitorView(guildId),+ params.user ? Promise.resolve(params.user) : getUserRepository().findUnique(userId),+ ]);+ if (member || !view || !user) {+ return null;+ }+ if (!guildVisitAuthorizedByView(visit, view)) {+ return null;+ }+ if (!(await visitorStillAllowedIn(guildId, user))) {+ return null;+ }+ const adultContentAllowed = canUserAccessNsfwContent(user);+ if (view.guild.nsfw && !adultContentAllowed) {+ return null;+ }+ const tier = getAccountTier(user);+ if (now - visit.opened_at.getTime() >= GUILD_VISIT_OPEN_REFRESH_SECONDS * 1000) {+ forgetGuildVisitRow(userId, guildId);+ void refreshGuildVisitOpenedAt(visit, new Date(now)).catch((error) => {+ Logger.warn({error, userId, guildId}, 'Failed to refresh guild visit activity');+ });+ }+ return {visit, view, user, tier, adultContentAllowed, historyCutoff: guildVisitHistoryCutoff(user, tier, now)};+}++const ENDED_VISIT_MARKER_TTL_SECONDS = 120;++function endedVisitMarkerKey(userId: UserID, guildId: GuildID): string {+ return `guild_visit:ended:${userId}:${guildId}`;+}++export async function markGuildVisitEnded(userId: UserID, guildId: GuildID): Promise<void> {+ await getKVClient().setex(endedVisitMarkerKey(userId, guildId), ENDED_VISIT_MARKER_TTL_SECONDS, '1');+}++async function refreshGuildVisitOpenedAt(visit: GuildVisitRow, now: Date): Promise<void> {+ if ((await getKVClient().get(endedVisitMarkerKey(visit.user_id, visit.guild_id))) !== null) {+ return;+ }+ await guildVisitRepository().touch(visit, now);+}++async function visitorStillAllowedIn(guildId: GuildID, user: User): Promise<boolean> {+ try {+ await assertNotBannedFromGuildVisit({guildId, user, ip: visitorRequestContext.getStore()?.ip ?? null});+ return true;+ } catch (error) {+ if (!(error instanceof BannedFromGuildError || error instanceof IpBannedFromGuildError)) {+ throw error;+ }+ await endGuildVisit({userId: user.id, guildId, reason: GuildVisitEndReasons.BANNED});+ return false;+ }+}++export function channelToVisitorInput(channel: Channel): GuildVisitorChannelInput {+ return {+ id: channel.id.toString(),+ type: channel.type,+ nsfw_override: channel.nsfwOverride,+ permission_overwrites: Array.from(channel.permissionOverwrites.entries()).map(([id, overwrite]) => ({+ id: id.toString(),+ type: overwrite.type,+ allow: overwrite.allow.toString(),+ deny: overwrite.deny.toString(),+ })),+ };+}++export function toVisitorChannelModel(channel: Channel): Channel {+ const kept = new Map(+ Array.from(channel.permissionOverwrites.entries())+ .filter(+ ([id, overwrite]) =>+ id.toString() === channel.guildId?.toString() && overwrite.type === ChannelOverwriteTypes.ROLE,+ )+ .map(([id, overwrite]) => [id, overwrite.toPermissionOverwrite()] as const),+ );+ if (kept.size === channel.permissionOverwrites.size) {+ return channel;+ }+ return new Channel({...channel.toRow(), permission_overwrites: kept.size > 0 ? kept : null});+}++export function visitorHistoryCutoff(params: {+ canReadHistory: boolean;+ guildCutoff: string | null;+ windowCutoff: Date | null;+}): string | null {+ const {canReadHistory, guildCutoff, windowCutoff} = params;+ if (windowCutoff === null) {+ return guildCutoff;+ }+ if (canReadHistory) {+ return windowCutoff.toISOString();+ }+ if (guildCutoff === null) {+ return null;+ }+ return new Date(Math.max(new Date(guildCutoff).getTime(), windowCutoff.getTime())).toISOString();+}++export function computeVisitorChannelPermissionsFor(params: {+ access: Pick<GuildVisitorAccess, 'view' | 'adultContentAllowed'>;+ channel: Channel;+ parent: Channel | null;+ thread?: {private: boolean} | null;+}): bigint {+ const {access, channel, parent} = params;+ return computeGuildVisitorChannelPermissions({+ guildId: access.view.guild.id.toString(),+ everyonePermissions: access.view.everyonePermissions,+ guildNsfw: access.view.guild.nsfw,+ channel: channelToVisitorInput(channel),+ parent: parent ? {nsfw_override: parent.nsfwOverride} : null,+ thread: params.thread ?? null,+ adultContentAllowed: access.adultContentAllowed,+ });+}++interface VisitorRequestContext {+ ip: string | null;+ ipKey: string | null;+ rateLimitService: IRateLimitService | null;+ enforce: boolean;+}++const visitorRequestContext = new AsyncLocalStorage<VisitorRequestContext>();++export function createVisitorRequestContextMiddleware(shouldEnforce: (ctx: Context<HonoEnv>) => boolean) {+ return createMiddleware<HonoEnv>(async (ctx, next) => {+ const ip = getRequestClientIp(ctx);+ const ipKey = ip ? (getSameIpDecisionKey(ip) ?? ip) : null;+ await visitorRequestContext.run(+ {ip, ipKey, rateLimitService: ctx.get('rateLimitService') ?? null, enforce: shouldEnforce(ctx)},+ next,+ );+ });+}++interface VisitorBucket {+ bucket: string;+ limit: number;+ windowMs: number;+ key: 'user' | 'ip' | 'guild';+ unverifiedOnly?: boolean;+}++export const GUILD_VISITOR_READ_BUCKETS: ReadonlyArray<VisitorBucket> = [+ {bucket: 'channel:messages:visitor', limit: 10, windowMs: ms('10 seconds'), key: 'user'},+ {bucket: 'channel:messages:visitor:ip', limit: 30, windowMs: ms('10 seconds'), key: 'ip'},+ {+ bucket: 'channel:messages:visitor:guild',+ limit: 600,+ windowMs: ms('60 seconds'),+ key: 'guild',+ unverifiedOnly: true,+ },+];++export const GUILD_VISITOR_SEARCH_BUCKETS: ReadonlyArray<VisitorBucket> = [+ {bucket: 'channel:search:visitor', limit: 5, windowMs: ms('60 seconds'), key: 'user'},+ {bucket: 'channel:search:visitor:ip', limit: 15, windowMs: ms('60 seconds'), key: 'ip'},+];++export const GUILD_VISIT_START_IP_BUCKETS: ReadonlyArray<VisitorBucket> = [+ {bucket: 'guild:visit:start:ip', limit: 30, windowMs: ms('60 seconds'), key: 'ip'},+];++export async function chargeGuildVisitorBuckets(params: {+ buckets: ReadonlyArray<VisitorBucket>;+ userId: UserID;+ guildId: GuildID | null;+ tier: AccountTier;+}): Promise<void> {+ const context = visitorRequestContext.getStore();+ if (!context?.enforce || !context.rateLimitService) {+ return;+ }+ for (const bucket of params.buckets) {+ if (bucket.unverifiedOnly && params.tier === 'verified') {+ continue;+ }+ let subject: string | null;+ if (bucket.key === 'user') {+ subject = `user:${params.userId}`;+ } else if (bucket.key === 'ip') {+ subject = context.ipKey;+ } else {+ subject = params.guildId === null ? null : `guild:${params.guildId}`;+ }+ if (!subject) {+ continue;+ }+ const result = await context.rateLimitService.checkBucketLimit(`${subject}:${bucket.bucket}`, {+ limit: bucket.limit,+ windowMs: bucket.windowMs,+ });+ if (!result.allowed) {+ guildVisitorRateLimitedTotal.inc(`key="${bucket.key}"`);+ throw new RateLimitError({+ retryAfter: result.retryAfter ?? Math.max(1, Math.ceil((result.resetTime.getTime() - Date.now()) / 1000)),+ retryAfterDecimal: result.retryAfterDecimal,+ limit: result.limit,+ resetTime: result.resetTime,+ resetAfterDecimal: result.resetAfterDecimal,+ scope: bucket.key === 'guild' ? 'shared' : 'user',+ });+ }+ }+}++export async function recordGuildVisitHistoryRead(params: {+ userId: UserID;+ guildId: GuildID;+ tier: AccountTier;+ now?: number;+}): Promise<void> {+ const hour = Math.floor((params.now ?? Date.now()) / MS_PER_HOUR);+ const kv = getKVClient();+ const currentKey = `guild_visit:hr:${params.userId}:${params.guildId}:${hour}`;+ const pages = await kv.incr(currentKey);+ if (pages !== 1) {+ return;+ }+ await kv.expire(currentKey, HISTORY_READ_KEY_TTL_SECONDS);+ const previous = await kv.getdel(`guild_visit:hr:${params.userId}:${params.guildId}:${hour - 1}`);+ const previousPages = previous === null ? 0 : Number.parseInt(previous, 10);+ if (!Number.isFinite(previousPages) || previousPages <= 0) {+ return;+ }+ void emitActivity('guild_visit_history_read', params.userId.toString(), {+ user_id: params.userId.toString(),+ guild_id: params.guildId.toString(),+ pages: previousPages,+ tier: params.tier,+ });+}diff --git a/fluxer_api/src/api/guild/visits/GuildVisitorSearch.ts b/fluxer_api/src/api/guild/visits/GuildVisitorSearch.tsnew file mode 100644index 000000000..e0889e462--- /dev/null+++ b/fluxer_api/src/api/guild/visits/GuildVisitorSearch.ts@@ -0,0 +1,21 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';+import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';+import type {MessageSearchRequest} from '@fluxer/schema/src/domains/message/MessageRequestSchemas';++const MEMBER_ONLY_SEARCH_FILTERS = [+ 'author_id',+ 'exclude_author_id',+ 'mentions',+ 'exclude_mentions',+ 'mention_everyone',+] as const satisfies ReadonlyArray<keyof MessageSearchRequest>;++export function assertVisitorSearchFilters(params: MessageSearchRequest): void {+ for (const field of MEMBER_ONLY_SEARCH_FILTERS) {+ if (params[field] !== undefined) {+ throw InputValidationError.fromCode(field, ValidationErrorCodes.SEARCH_FILTER_NEEDS_MEMBERSHIP);+ }+ }+}diff --git a/fluxer_api/src/api/middleware/GuildVisitorRequestContextMiddleware.ts b/fluxer_api/src/api/middleware/GuildVisitorRequestContextMiddleware.tsnew file mode 100644index 000000000..1f4763eda--- /dev/null+++ b/fluxer_api/src/api/middleware/GuildVisitorRequestContextMiddleware.ts@@ -0,0 +1,11 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {createVisitorRequestContextMiddleware} from '@app/api/guild/visits/GuildVisitorAccess';+import {shouldEnforceRateLimits} from '@app/api/middleware/RateLimitMiddleware';+import {UserFlags} from '@fluxer/constants/src/UserConstants';++export const GuildVisitorRequestContextMiddleware = createVisitorRequestContextMiddleware((ctx) => {+ const user = ctx.get('user');+ if (user && (user.flags & UserFlags.RATE_LIMIT_BYPASS) !== 0n) return false;+ return shouldEnforceRateLimits(ctx);+});