Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions fluxer_api/src/api/channel/services/BaseChannelAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import {UnknownGuildError} from '@fluxer/errors/src/domains/guild/UnknownGuildEr
import {NsfwContentRequiresAgeVerificationError} from '@fluxer/errors/src/domains/moderation/NsfwContentRequiresAgeVerificationError';
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import type {GuildMemberResponse} from '@fluxer/schema/src/domains/guild/GuildMemberSchemas';
import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
import type {ChannelID, GuildID, UserID} from '../../BrandedTypes';
import {SYSTEM_USER_ID} from '../../constants/Core';
import type {IGuildRepositoryAggregate} from '../../guild/repositories/IGuildRepositoryAggregate';
import {createGuildMfaEnforcer} from '../../guild/services/GuildMfaEnforcement';
import type {IGatewayService} from '../../infrastructure/IGatewayService';
import type {GuildChannelAuthContext, IGatewayService} from '../../infrastructure/IGatewayService';
import type {Channel} from '../../models/Channel';
import type {GuildMember} from '../../models/GuildMember';
import type {User} from '../../models/User';
Expand Down Expand Up @@ -173,13 +172,14 @@ export abstract class BaseChannelAuthService {
skipNsfwValidation?: boolean;
}): Promise<AuthenticatedChannel> {
const guildId = channel.guildId!;
const [guildDataResult, guildMemberResult] = await Promise.all([
this.fetchGuildDataOrThrow({guildId, userId}),
const [authContextResult, guildMemberResult] = await Promise.all([
this.fetchGuildAuthContextOrThrow({guildId, userId, channelId: this.parentLookupChannelId(channel)}),
this.gatewayService.getGuildMember({guildId, userId}),
]);
if (!guildDataResult) {
if (!authContextResult) {
this.throwGuildAccessError();
}
const guildDataResult = authContextResult.guild;
if (!guildMemberResult.success || !guildMemberResult.memberData) {
this.throwGuildAccessError();
}
Expand All @@ -190,7 +190,7 @@ export abstract class BaseChannelAuthService {
});
const enforceGuildMfa = await createGuildMfaEnforcer({
userRepository: this.userRepository,
guildData: guildDataResult!,
guildData: guildDataResult,
userId,
});
const channelPermissions = await this.gatewayService.getUserPermissions({
Expand All @@ -210,12 +210,12 @@ export abstract class BaseChannelAuthService {
await checkPermission(Permissions.VIEW_CHANNEL);
const parentCategory = await this.getParentCategoryContentWarningView({
channel,
guild: guildDataResult!,
parentChannel: authContextResult.parentChannel,
});
const requiresAgeVerification = computeEffectiveChannelNsfw(
channelToContentWarningView(channel),
parentCategory,
guildResponseToContentWarningView(guildDataResult!),
guildResponseToContentWarningView(guildDataResult),
);
if (
this.options.validateNsfw &&
Expand All @@ -233,27 +233,32 @@ export abstract class BaseChannelAuthService {
}
return {
channel,
guild: guildDataResult!,
guild: guildDataResult,
member,
hasPermission,
checkPermission,
};
}

private parentLookupChannelId(channel: Channel): ChannelID | undefined {
if (!channel.parentId || channel.type === ChannelTypes.GUILD_CATEGORY) {
return undefined;
}
return channel.parentId;
}

private async getParentCategoryContentWarningView({
channel,
guild,
parentChannel,
}: {
channel: Channel;
guild: GuildResponse;
parentChannel: GuildChannelAuthContext['parentChannel'];
}): Promise<ContentWarningChannelLike | null> {
if (!channel.parentId || channel.type === ChannelTypes.GUILD_CATEGORY) {
return null;
}
const parentId = channel.parentId.toString();
const parentFromGateway = guild.channels?.find((guildChannel) => guildChannel.id === parentId);
if (parentFromGateway) {
return channelResponseToContentWarningView(parentFromGateway);
if (parentChannel) {
return channelResponseToContentWarningView(parentChannel);
}
const parentCategory = await this.channelRepository.channelData.findUnique(channel.parentId);
return parentCategory ? channelToContentWarningView(parentCategory) : null;
Expand All @@ -266,10 +271,14 @@ export abstract class BaseChannelAuthService {
throw new UnknownChannelError();
}

private async fetchGuildDataOrThrow(params: {guildId: GuildID; userId: UserID}): Promise<GuildResponse | null> {
const {guildId, userId} = params;
private async fetchGuildAuthContextOrThrow(params: {
guildId: GuildID;
userId: UserID;
channelId?: ChannelID;
}): Promise<GuildChannelAuthContext | null> {
const {guildId, userId, channelId} = params;
try {
return await this.gatewayService.getGuildData({guildId, userId});
return await this.gatewayService.getGuildAuthContext({guildId, userId, channelId});
} catch (error) {
await this.handleGuildAccessError(error, guildId);
return null;
Expand Down
14 changes: 7 additions & 7 deletions fluxer_api/src/api/channel/tests/MessageSendPermissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('Message send permissions', () => {
});
await ensureSessionStarted(harness, member.token);
const gatewayService = getGatewayService();
const getGuildData = vi.spyOn(gatewayService, 'getGuildData');
const getGuildAuthContext = vi.spyOn(gatewayService, 'getGuildAuthContext');
const getGuildMember = vi.spyOn(gatewayService, 'getGuildMember');
const getUserPermissions = vi.spyOn(gatewayService, 'getUserPermissions');

Expand All @@ -121,27 +121,27 @@ describe('Message send permissions', () => {
.body({content: 'authenticate me once'})
.execute();
const sendCounts = {
guildData: getGuildData.mock.calls.length,
authContext: getGuildAuthContext.mock.calls.length,
guildMember: getGuildMember.mock.calls.length,
userPermissions: getUserPermissions.mock.calls.length,
};
getGuildData.mockClear();
getGuildAuthContext.mockClear();
getGuildMember.mockClear();
getUserPermissions.mockClear();
await createBuilder<MessageResponse>(harness, member.token)
.patch(`/channels/${systemChannel.id}/messages/${sentMessage.id}`)
.body({content: 'authenticate me once again'})
.execute();
const editCounts = {
guildData: getGuildData.mock.calls.length,
authContext: getGuildAuthContext.mock.calls.length,
guildMember: getGuildMember.mock.calls.length,
userPermissions: getUserPermissions.mock.calls.length,
};

expect(sendCounts).toEqual({guildData: 1, guildMember: 1, userPermissions: 1});
expect(editCounts).toEqual({guildData: 1, guildMember: 1, userPermissions: 1});
expect(sendCounts).toEqual({authContext: 1, guildMember: 1, userPermissions: 1});
expect(editCounts).toEqual({authContext: 1, guildMember: 1, userPermissions: 1});
} finally {
getGuildData.mockRestore();
getGuildAuthContext.mockRestore();
getGuildMember.mockRestore();
getUserPermissions.mockRestore();
}
Expand Down
Loading