A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(constants): add community visit rules and visitor cases
Hampusa07a7269a031
diff --git a/packages/constants/src/GatewayConstants.ts b/packages/constants/src/GatewayConstants.tsindex 20d8f14be..868fd16e8 100644--- a/packages/constants/src/GatewayConstants.ts+++ b/packages/constants/src/GatewayConstants.ts@@ -30,6 +30,7 @@ export const MEMBER_LIST_RANGE_WINDOW_OVERSCAN_PAGES = 1;export const GatewayIdentifyFlags = {DEBOUNCE_MESSAGE_REACTIONS: 1 << 1,CHANNEL_THREADS: 1 << 2,+ GUILD_VISITS: 1 << 3,} as const;export const GatewayCloseCodes = {UNKNOWN_ERROR: 4000,@@ -68,6 +69,7 @@ export const GatewayErrorCodes = {VOICE_PERMISSION_DENIED: 'VOICE_PERMISSION_DENIED',VOICE_TOKEN_FAILED: 'VOICE_TOKEN_FAILED',VOICE_UNCLAIMED_ACCOUNT: 'VOICE_UNCLAIMED_ACCOUNT',+ VOICE_ACCOUNT_VERIFICATION_REQUIRED: 'VOICE_ACCOUNT_VERIFICATION_REQUIRED',VOICE_USER_MISMATCH: 'VOICE_USER_MISMATCH',VOICE_USER_NOT_IN_VOICE: 'VOICE_USER_NOT_IN_VOICE',} as const;diff --git a/packages/constants/src/GuildConstants.ts b/packages/constants/src/GuildConstants.tsindex fe638a624..6a9a8d6cf 100644--- a/packages/constants/src/GuildConstants.ts+++ b/packages/constants/src/GuildConstants.ts@@ -137,6 +137,7 @@ export const GuildFeatures = {LARGE_GUILD_OVERRIDE: 'LARGE_GUILD_OVERRIDE',VERY_LARGE_GUILD: 'VERY_LARGE_GUILD',ANNOUNCEMENT_CHANNELS_DISABLED: 'ANNOUNCEMENT_CHANNELS_DISABLED',+ VISITS_ENABLED: 'VISITS_ENABLED',} as const;export type GuildFeature = ValueOf<typeof GuildFeatures>;diff --git a/packages/constants/src/GuildVisitConstants.test.ts b/packages/constants/src/GuildVisitConstants.test.tsnew file mode 100644index 000000000..e44689ca7--- /dev/null+++ b/packages/constants/src/GuildVisitConstants.test.ts@@ -0,0 +1,38 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {+ computeGuildVisitorChannelPermissions,+ type GuildVisitorChannelInput,+} from '@fluxer/constants/src/GuildVisitConstants';+import cases from '@fluxer/constants/src/GuildVisitorPermissionCases.json' with {type: 'json'};+import {describe, expect, it} from 'vitest';++interface PermissionCase {+ name: string;+ kind: 'permissions';+ input: {+ guildId: string;+ everyonePermissions: string;+ guildNsfw: boolean;+ channel: GuildVisitorChannelInput;+ parent?: {nsfw_override: boolean | null};+ thread?: {private: boolean};+ adultContentAllowed: boolean;+ };+ expected: string;+}++const rows = (cases as Array<{kind: string}>).filter((row): row is PermissionCase => row.kind === 'permissions');++describe('guild visitor permissions', () => {+ for (const row of rows) {+ it(row.name, () => {+ const input = {...row.input, everyonePermissions: BigInt(row.input.everyonePermissions)};+ expect(computeGuildVisitorChannelPermissions(input).toString()).toBe(row.expected);+ });+ }++ it('has permission cases', () => {+ expect(rows.length).toBeGreaterThan(0);+ });+});diff --git a/packages/constants/src/GuildVisitConstants.ts b/packages/constants/src/GuildVisitConstants.tsnew file mode 100644index 000000000..c9908f400--- /dev/null+++ b/packages/constants/src/GuildVisitConstants.ts@@ -0,0 +1,90 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {ChannelOverwriteTypes, ChannelTypes, Permissions} from '@fluxer/constants/src/ChannelConstants';+import type {ValueOf} from '@fluxer/constants/src/ValueOf';++export const GUILD_VISITOR_PERMISSIONS_MASK = Permissions.VIEW_CHANNEL | Permissions.READ_MESSAGE_HISTORY;++export const GuildVisitSources = {+ INVITE: 1,+ VANITY_URL: 2,+ DISCOVERY: 3,+ OPERATOR: 4,+} as const;++export type GuildVisitSource = ValueOf<typeof GuildVisitSources>;++export const GuildVisitEndReasons = {+ LEFT: 'left',+ JOINED: 'joined',+ REPLACED: 'replaced',+ BANNED: 'banned',+ DISABLED: 'disabled',+ AGE_RESTRICTED: 'age_restricted',+ EXPIRED: 'expired',+ INVITE_REVOKED: 'invite_revoked',+ BUSY: 'busy',+} as const;++export type GuildVisitEndReason = ValueOf<typeof GuildVisitEndReasons>;++export const GUILD_VISIT_TTL_SECONDS = 1_209_600;+export const GUILD_VISIT_OPEN_REFRESH_SECONDS = 86_400;++interface GuildVisitorOverwrite {+ readonly id: string;+ readonly type: number;+ readonly allow: string;+ readonly deny: string;+}++export interface GuildVisitorChannelInput {+ readonly id: string;+ readonly type: number;+ readonly nsfw_override?: boolean | null;+ readonly permission_overwrites?: ReadonlyArray<GuildVisitorOverwrite> | null;+}++export interface GuildVisitorPermissionInput {+ guildId: string;+ everyonePermissions: bigint;+ guildNsfw: boolean;+ channel: GuildVisitorChannelInput;+ parent?: {readonly nsfw_override?: boolean | null} | null;+ thread?: {private: boolean} | null;+ adultContentAllowed: boolean;+}++function parseBits(value: string): bigint {+ try {+ return BigInt(value);+ } catch {+ return 0n;+ }+}++function isGuildVisitorChannelNsfw(input: {+ guildNsfw: boolean;+ channel: GuildVisitorChannelInput;+ parent?: {readonly nsfw_override?: boolean | null} | null;+}): boolean {+ const own = input.channel.nsfw_override;+ if (own !== null && own !== undefined) return own;+ if (input.channel.type !== ChannelTypes.GUILD_CATEGORY) {+ const inherited = input.parent?.nsfw_override;+ if (inherited !== null && inherited !== undefined) return inherited;+ }+ return input.guildNsfw;+}++export function computeGuildVisitorChannelPermissions(input: GuildVisitorPermissionInput): bigint {+ if (input.thread?.private) return 0n;+ if (!input.adultContentAllowed && isGuildVisitorChannelNsfw(input)) return 0n;+ let permissions = input.everyonePermissions & ~Permissions.ADMINISTRATOR;+ for (const overwrite of input.channel.permission_overwrites ?? []) {+ if (overwrite.type !== ChannelOverwriteTypes.ROLE || overwrite.id !== input.guildId) continue;+ permissions &= ~parseBits(overwrite.deny);+ permissions |= parseBits(overwrite.allow);+ }+ return permissions & GUILD_VISITOR_PERMISSIONS_MASK;+}diff --git a/packages/constants/src/GuildVisitorPermissionCases.json b/packages/constants/src/GuildVisitorPermissionCases.jsonnew file mode 100644index 000000000..448d6942b--- /dev/null+++ b/packages/constants/src/GuildVisitorPermissionCases.json@@ -0,0 +1,328 @@+[+ {+ "name": "everyone defaults are masked to view and history",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "1117248",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 0, "nsfw_override": null, "permission_overwrites": []},+ "adultContentAllowed": false+ },+ "visible": true,+ "expected": "66560"+ },+ {+ "name": "administrator on everyone is cleared, not expanded",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "8",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 0, "nsfw_override": null, "permission_overwrites": []},+ "adultContentAllowed": true+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "administrator does not bypass an everyone deny",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66568",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "100", "type": 0, "allow": "0", "deny": "1024"}]+ },+ "adultContentAllowed": true+ },+ "visible": false,+ "expected": "65536"+ },+ {+ "name": "everyone overwrite allowing send stays masked",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "1024",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "100", "type": 0, "allow": "67648", "deny": "0"}]+ },+ "adultContentAllowed": false+ },+ "visible": true,+ "expected": "66560"+ },+ {+ "name": "everyone overwrite denying view hides the channel",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "100", "type": 0, "allow": "0", "deny": "1024"}]+ },+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "65536"+ },+ {+ "name": "everyone overwrite allowing history only grants no view",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "0",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "100", "type": 0, "allow": "65536", "deny": "0"}]+ },+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "65536"+ },+ {+ "name": "role overwrite is ignored",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "0",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "200", "type": 0, "allow": "66560", "deny": "0"}]+ },+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "member overwrite allow is ignored",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "0",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "300", "type": 1, "allow": "66560", "deny": "0"}]+ },+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "member overwrite deny is ignored",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "300", "type": 1, "allow": "0", "deny": "1024"}]+ },+ "adultContentAllowed": false+ },+ "visible": true,+ "expected": "66560"+ },+ {+ "name": "member overwrite with the guild id is not the everyone overwrite",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "0",+ "guildNsfw": false,+ "channel": {+ "id": "10",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [{"id": "100", "type": 1, "allow": "66560", "deny": "0"}]+ },+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "nsfw channel is hidden from a visitor without adult access",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 0, "nsfw_override": true, "permission_overwrites": []},+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "nsfw channel is visible to an adult visitor",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 0, "nsfw_override": true, "permission_overwrites": []},+ "adultContentAllowed": true+ },+ "visible": true,+ "expected": "66560"+ },+ {+ "name": "nsfw is inherited from the category",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 0, "nsfw_override": null, "permission_overwrites": []},+ "parent": {"nsfw_override": true},+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "a channel marked safe overrides an adult guild",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": true,+ "channel": {"id": "10", "type": 0, "nsfw_override": false, "permission_overwrites": []},+ "adultContentAllowed": false+ },+ "visible": true,+ "expected": "66560"+ },+ {+ "name": "an adult guild hides inheriting channels from non-adults",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": true,+ "channel": {"id": "10", "type": 0, "nsfw_override": null, "permission_overwrites": []},+ "adultContentAllowed": false+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "a category does not inherit from a parent",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 4, "nsfw_override": null, "permission_overwrites": []},+ "parent": {"nsfw_override": true},+ "adultContentAllowed": false+ },+ "visible": true,+ "expected": "66560"+ },+ {+ "name": "category-synced channel uses its own copy of the everyone overwrite",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {+ "id": "11",+ "type": 0,+ "nsfw_override": null,+ "permission_overwrites": [+ {"id": "100", "type": 0, "allow": "0", "deny": "65536"},+ {"id": "200", "type": 0, "allow": "65536", "deny": "0"}+ ]+ },+ "parent": {"nsfw_override": null},+ "adultContentAllowed": false+ },+ "visible": true,+ "expected": "1024"+ },+ {+ "name": "public thread follows its parent",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 0, "nsfw_override": null, "permission_overwrites": []},+ "thread": {"private": false},+ "adultContentAllowed": false+ },+ "visible": true,+ "expected": "66560"+ },+ {+ "name": "private thread is never visible",+ "kind": "permissions",+ "input": {+ "guildId": "100",+ "everyonePermissions": "66560",+ "guildNsfw": false,+ "channel": {"id": "10", "type": 0, "nsfw_override": null, "permission_overwrites": []},+ "thread": {"private": true},+ "adultContentAllowed": true+ },+ "visible": false,+ "expected": "0"+ },+ {+ "name": "stripping keeps only the everyone overwrite",+ "kind": "strip",+ "input": {+ "guildId": "100",+ "channel": {+ "id": "10",+ "type": 0,+ "permission_overwrites": [+ {"id": "200", "type": 0, "allow": "1024", "deny": "0"},+ {"id": "100", "type": 0, "allow": "0", "deny": "2048"},+ {"id": "300", "type": 1, "allow": "0", "deny": "1024"}+ ]+ }+ },+ "expected": ["100"]+ },+ {+ "name": "stripping a channel without overwrites leaves it empty",+ "kind": "strip",+ "input": {+ "guildId": "100",+ "channel": {"id": "10", "type": 0, "permission_overwrites": []}+ },+ "expected": []+ }+]diff --git a/packages/constants/src/LimitConfigMetadata.ts b/packages/constants/src/LimitConfigMetadata.tsindex 1f028aabe..b33db1a86 100644--- a/packages/constants/src/LimitConfigMetadata.ts+++ b/packages/constants/src/LimitConfigMetadata.ts@@ -20,6 +20,7 @@ export const LIMIT_KEYS = ['feature_higher_video_quality','feature_per_guild_profiles','feature_voice_entrance_sounds',+ 'guild_visit_history_days_unverified','max_attachments_per_message','max_bio_length','max_bookmarks',@@ -41,6 +42,7 @@ export const LIMIT_KEYS = ['max_guild_roles','max_guild_stickers_more','max_guild_stickers',+ 'max_guild_visits','max_guilds','max_attachment_file_size','max_message_length',@@ -71,6 +73,7 @@ export const LIMIT_KEY_SCOPES: Record<LimitKey, LimitScope> = {feature_higher_video_quality: 'user',feature_per_guild_profiles: 'user',feature_voice_entrance_sounds: 'user',+ guild_visit_history_days_unverified: 'user',max_attachments_per_message: 'both',max_bio_length: 'user',max_bookmarks: 'user',@@ -92,6 +95,7 @@ export const LIMIT_KEY_SCOPES: Record<LimitKey, LimitScope> = {max_guild_roles: 'guild',max_guild_stickers_more: 'guild',max_guild_stickers: 'guild',+ max_guild_visits: 'user',max_guilds: 'user',max_attachment_file_size: 'both',max_message_length: 'both',@@ -194,6 +198,25 @@ export const LIMIT_KEY_METADATA: Record<LimitKey, LimitKeyMetadata> = {isToggle: false,unit: 'count',},+ max_guild_visits: {+ key: 'max_guild_visits',+ label: 'Max Community Visits',+ description:+ 'Maximum communities a user can visit at once without joining. Accounts without a confirmed email get half',+ category: 'guilds',+ scope: 'user',+ isToggle: false,+ unit: 'count',+ },+ guild_visit_history_days_unverified: {+ key: 'guild_visit_history_days_unverified',+ label: 'Visit History Days Without Email',+ description: 'How many days of message history accounts without a confirmed email can read while visiting',+ category: 'guilds',+ scope: 'user',+ isToggle: false,+ unit: 'count',+ },max_guild_members: {key: 'max_guild_members',label: 'Max Guild Members',@@ -345,7 +368,8 @@ export const LIMIT_KEY_METADATA: Record<LimitKey, LimitKeyMetadata> = {max_attachment_file_size: {key: 'max_attachment_file_size',label: 'Max Attachment File Size',- description: 'Maximum size of each attachment file',+ description:+ 'Maximum size of each attachment file. Above 500 MiB, also raise FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES on the media proxy',category: 'files',scope: 'both',isToggle: false,diff --git a/packages/limits/src/LimitDefaults.ts b/packages/limits/src/LimitDefaults.tsindex 1de67171c..127f656bc 100644--- a/packages/limits/src/LimitDefaults.ts+++ b/packages/limits/src/LimitDefaults.ts@@ -54,6 +54,7 @@ export const DEFAULT_RESTRICTED_LIMITS: Record<LimitKey, number> = {feature_higher_video_quality: 0,feature_per_guild_profiles: 0,feature_voice_entrance_sounds: 0,+ guild_visit_history_days_unverified: 30,max_attachment_file_size: ATTACHMENT_MAX_SIZE_NON_PREMIUM,max_attachments_per_message: MAX_ATTACHMENTS_PER_MESSAGE,max_bio_length: MAX_BIO_LENGTH,@@ -76,6 +77,7 @@ export const DEFAULT_RESTRICTED_LIMITS: Record<LimitKey, number> = {max_guild_roles: MAX_GUILD_ROLES,max_guild_stickers_more: MAX_GUILD_STICKERS_MORE_STICKERS,max_guild_stickers: MAX_GUILD_STICKERS,+ max_guild_visits: 10,max_guilds: 100,max_message_length: MAX_MESSAGE_LENGTH_NON_PREMIUM,max_private_channels_per_user: MAX_PRIVATE_CHANNELS_PER_USER,