feat: implement connection-based visibility filtering for agent search results

This commit is contained in:
pradeepkumar
2026-03-30 15:28:01 +05:30
parent 9cdf0dd897
commit 6056f160ac
2 changed files with 26 additions and 10 deletions

View File

@@ -153,13 +153,14 @@ export class AgentsService {
throw new ForbiddenException('This profile is not available');
}
if (visibility === 'connections' && requestingUserId) {
const connected = await this.connectionRequestsService.isConnected(requestingUserId, profile.user.id);
if (!connected) {
if (visibility === 'connections') {
let isConnected = false;
if (requestingUserId) {
isConnected = await this.connectionRequestsService.isConnected(requestingUserId, profile.user.id);
}
if (!isConnected) {
throw new ForbiddenException('This profile is only visible to connections');
}
} else if (visibility === 'connections' && !requestingUserId) {
throw new ForbiddenException('This profile is only visible to connections');
}
// Strip privacyPreferences from response
@@ -280,7 +281,7 @@ export class AgentsService {
}
}
async searchAgents(dto: SearchAgentsDto) {
async searchAgents(dto: SearchAgentsDto, requestingUserId?: string | null) {
const {
search,
location,
@@ -570,10 +571,23 @@ export class AgentsService {
this.prisma.agentProfile.count({ where }),
]);
// Filter out private profiles (safe for null privacyPreferences)
// Get connected user IDs for filtering "connections only" profiles
let connectedUserIds: string[] = [];
if (requestingUserId) {
connectedUserIds = await this.connectionRequestsService.getConnectedUserIds(requestingUserId);
}
// Filter out private and connections-only (for non-connected users) profiles
const visibleAgents = agents.filter((agent: any) => {
const visibility = this.getProfileVisibility(agent.user?.privacyPreferences);
return visibility !== 'private';
if (visibility === 'private') return false;
if (visibility === 'connections') {
// Own profile always visible
if (requestingUserId && agent.user?.id === requestingUserId) return true;
// Only show if requesting user is connected
return requestingUserId && connectedUserIds.includes(agent.user?.id);
}
return true; // 'public' or no setting
});
// Compute match score for each agent based on search context