import { Body, Controller, Delete, Get, Post, Query, UseGuards } from '@nestjs/common'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { CurrentUser } from '../auth/current-user.decorator'; import { RequireOrgModule } from '../billing/org-module.decorator'; import { OrgModuleGuard } from '../billing/org-module.guard'; import { CreateConversationDmDto, CreateConversationGroupDto, CreateConversationChannelDto, HideConversationMessagesDto, PinConversationMessageDto, PinConversationRoomDto, UnpinConversationMessageDto, AddConversationRoomMemberDto, RemoveConversationRoomMemberDto, UpdateConversationRoomMemberRoleDto, ConversationTypingDto, MuteConversationRoomDto, ToggleMessageReactionDto, } from './dto'; import { ConversationsService } from './conversations.service'; @Controller('conversations') @UseGuards(JwtAuthGuard, OrgModuleGuard) @RequireOrgModule('conversations') export class ConversationsController { constructor(private readonly conversations: ConversationsService) {} @Get('access') access(@CurrentUser() user: { userId: string; organizationId: string; role: string }) { return this.conversations.getAccess(user.userId, user.organizationId, user.role); } @Get('session') session(@CurrentUser() user: { userId: string; organizationId: string; role: string }) { return this.conversations.getSession(user.userId, user.organizationId, user.role); } @Get('peers') peers(@CurrentUser() user: { userId: string; organizationId: string; role: string }) { return this.conversations.listPeers(user.userId, user.organizationId, user.role); } @Get('rooms') rooms(@CurrentUser() user: { userId: string; organizationId: string; role: string }) { return this.conversations.listRooms(user.userId, user.organizationId, user.role); } @Post('rooms/typing') signalTyping( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: ConversationTypingDto, ) { return this.conversations.signalTyping( user.userId, user.organizationId, user.role, dto.externalRoomId, ); } @Get('rooms/typing') listTyping( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Query('externalRoomId') externalRoomId: string, ) { // بدون roomId → همه اتاق‌های سازمان (لیست گفتگو) if (!externalRoomId?.trim()) { return this.conversations.listTypingAll(user.userId, user.organizationId, user.role); } return this.conversations.listTyping( user.userId, user.organizationId, user.role, externalRoomId || '', ); } @Post('rooms/dm') createDm( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: CreateConversationDmDto, ) { return this.conversations.createDm( user.userId, user.organizationId, user.role, dto.peerUserId, ); } @Post('rooms/group') createGroup( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: CreateConversationGroupDto, ) { return this.conversations.createGroup( user.userId, user.organizationId, user.role, dto.name, dto.memberUserIds, ); } @Post('rooms/channel') createChannel( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: CreateConversationChannelDto, ) { return this.conversations.createChannel( user.userId, user.organizationId, user.role, dto.name, dto.memberUserIds, ); } @Get('rooms/members') listMembers( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Query('externalRoomId') externalRoomId: string, ) { return this.conversations.listRoomMembers( user.userId, user.organizationId, user.role, externalRoomId || '', ); } @Post('rooms/members') addMember( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: AddConversationRoomMemberDto, ) { return this.conversations.addRoomMember( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.userId, dto.role === 'admin' ? 'admin' : 'member', ); } @Post('rooms/members/remove') removeMember( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: RemoveConversationRoomMemberDto, ) { return this.conversations.removeRoomMember( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.userId, ); } @Post('rooms/members/role') updateMemberRole( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: UpdateConversationRoomMemberRoleDto, ) { return this.conversations.updateRoomMemberRole( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.userId, dto.role === 'admin' ? 'admin' : 'member', ); } @Post('rooms/pin') pinRoom( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: PinConversationRoomDto, ) { return this.conversations.setRoomPinned( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.pinned, ); } @Post('rooms/mute') muteRoom( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: MuteConversationRoomDto, ) { return this.conversations.setRoomMuted( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.muted, dto.allowMentions !== false, ); } @Get('messages/reactions') listReactions( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Query('externalRoomId') externalRoomId: string, ) { return this.conversations.listMessageReactions( user.userId, user.organizationId, user.role, externalRoomId || '', ); } @Post('messages/reactions') toggleReaction( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: ToggleMessageReactionDto, ) { return this.conversations.toggleMessageReaction( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.messageId, dto.emoji, ); } @Post('messages/hide') hideMessages( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: HideConversationMessagesDto, ) { return this.conversations.hideMessages( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.messageIds, dto.forEveryone !== false, ); } @Get('messages/hidden') listHidden( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Query('externalRoomId') externalRoomId: string, ) { return this.conversations.listHiddenMessageIds( user.userId, user.organizationId, user.role, externalRoomId || '', ); } @Get('messages/pin') getPinnedMessage( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Query('externalRoomId') externalRoomId: string, ) { return this.conversations.getPinnedMessage( user.userId, user.organizationId, user.role, externalRoomId || '', ); } @Post('messages/pin') pinMessage( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: PinConversationMessageDto, ) { return this.conversations.pinMessage( user.userId, user.organizationId, user.role, dto.externalRoomId, dto.messageId, dto.preview, ); } @Post('messages/unpin') unpinMessage( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Body() dto: UnpinConversationMessageDto, ) { return this.conversations.unpinMessage( user.userId, user.organizationId, user.role, dto.externalRoomId, ); } @Delete('messages/pin') unpinMessageDelete( @CurrentUser() user: { userId: string; organizationId: string; role: string }, @Query('externalRoomId') externalRoomId: string, ) { return this.conversations.unpinMessage( user.userId, user.organizationId, user.role, externalRoomId || '', ); } }