Add channel to conversation
This commit is contained in:
@@ -4,6 +4,7 @@ import { sanitizePermissions } from '../org-roles/role-defaults';
|
||||
export type ConversationAccess = {
|
||||
canUse: boolean;
|
||||
canCreateGroup: boolean;
|
||||
canCreateChannel: boolean;
|
||||
};
|
||||
|
||||
export function resolveConversationAccess(
|
||||
@@ -11,11 +12,14 @@ export function resolveConversationAccess(
|
||||
orgRolePermissions: unknown,
|
||||
): ConversationAccess {
|
||||
if (membershipRole === 'owner' || membershipRole === 'admin') {
|
||||
return { canUse: true, canCreateGroup: true };
|
||||
return { canUse: true, canCreateGroup: true, canCreateChannel: true };
|
||||
}
|
||||
const permissions = sanitizePermissions(orgRolePermissions) as RolePermissionKey[];
|
||||
const canUse = permissions.includes('conversations');
|
||||
const canCreateGroup = permissions.includes('conversations_groups');
|
||||
return {
|
||||
canUse: permissions.includes('conversations'),
|
||||
canCreateGroup: permissions.includes('conversations_groups'),
|
||||
canUse,
|
||||
canCreateGroup,
|
||||
canCreateChannel: canCreateGroup,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { OrgModuleGuard } from '../billing/org-module.guard';
|
||||
import {
|
||||
CreateConversationDmDto,
|
||||
CreateConversationGroupDto,
|
||||
CreateConversationChannelDto,
|
||||
HideConversationMessagesDto,
|
||||
PinConversationMessageDto,
|
||||
PinConversationRoomDto,
|
||||
@@ -66,6 +67,20 @@ export class ConversationsController {
|
||||
);
|
||||
}
|
||||
|
||||
@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,
|
||||
);
|
||||
}
|
||||
|
||||
@Post('rooms/pin')
|
||||
pinRoom(
|
||||
@CurrentUser() user: { userId: string; organizationId: string; role: string },
|
||||
|
||||
@@ -190,34 +190,45 @@ export class ConversationsService {
|
||||
});
|
||||
const trackedByExternal = new Map(tracked.map((t) => [t.externalRoomId, t]));
|
||||
const pinnedIds = await this.userPinnedRoomIds(organizationId, userId);
|
||||
const membership = await this.prisma.membership.findFirst({
|
||||
where: { userId, organizationId, isActive: true },
|
||||
select: { role: true },
|
||||
});
|
||||
const effectiveRole = String(membership?.role ?? role);
|
||||
|
||||
try {
|
||||
const external = await this.chat.listUserRooms(creds.host, creds.apiKey, chatUserId);
|
||||
return external.map((row) => {
|
||||
const dto = this.toRoomDto(row, trackedByExternal, organizationId, userId);
|
||||
const dto = this.toRoomDto(row, trackedByExternal, organizationId, userId, effectiveRole);
|
||||
return { ...dto, pinned: pinnedIds.has(dto.externalRoomId) };
|
||||
});
|
||||
} catch (e) {
|
||||
// اگر سرویس چت در دسترس نبود، حداقل roomهای ثبتشده در CRM را نشان بده
|
||||
if (tracked.length) {
|
||||
return tracked.map((t) => ({
|
||||
id: t.id,
|
||||
externalRoomId: t.externalRoomId,
|
||||
kind: (t.kind === 'group' ? 'group' : 'dm') as 'dm' | 'group',
|
||||
name: t.name || (t.kind === 'group' ? 'گروه' : 'گفتگو'),
|
||||
unreadCount: 0,
|
||||
lastMessagePreview: null,
|
||||
lastMessageAt: null,
|
||||
peerUserId:
|
||||
t.kind === 'dm'
|
||||
? t.dmUserMinId === userId
|
||||
? t.dmUserMaxId
|
||||
: t.dmUserMaxId === userId
|
||||
? t.dmUserMinId
|
||||
: null
|
||||
: null,
|
||||
pinned: pinnedIds.has(t.externalRoomId),
|
||||
}));
|
||||
return tracked.map((t) => {
|
||||
const kind = this.normalizeKind(t.kind);
|
||||
return {
|
||||
id: t.id,
|
||||
externalRoomId: t.externalRoomId,
|
||||
kind,
|
||||
name:
|
||||
t.name ||
|
||||
(kind === 'channel' ? 'کانال' : kind === 'group' ? 'گروه' : 'گفتگو'),
|
||||
unreadCount: 0,
|
||||
lastMessagePreview: null,
|
||||
lastMessageAt: null,
|
||||
peerUserId:
|
||||
kind === 'dm'
|
||||
? t.dmUserMinId === userId
|
||||
? t.dmUserMaxId
|
||||
: t.dmUserMaxId === userId
|
||||
? t.dmUserMinId
|
||||
: null
|
||||
: null,
|
||||
pinned: pinnedIds.has(t.externalRoomId),
|
||||
canPost: this.canPostInRoom(kind, t.createdByUserId, userId, effectiveRole),
|
||||
};
|
||||
});
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
@@ -353,6 +364,69 @@ export class ConversationsService {
|
||||
lastMessagePreview: null,
|
||||
lastMessageAt: null,
|
||||
peerUserId: null,
|
||||
canPost: true,
|
||||
};
|
||||
}
|
||||
|
||||
async createChannel(
|
||||
userId: string,
|
||||
organizationId: string,
|
||||
role: string,
|
||||
name: string,
|
||||
memberUserIds: string[],
|
||||
): Promise<ConversationRoomDto> {
|
||||
const access = await this.loadAccess(userId, organizationId, role);
|
||||
if (!access.canCreateChannel) {
|
||||
throw new ForbiddenException('اجازه ساخت کانال را ندارید');
|
||||
}
|
||||
|
||||
const uniqueIds = [...new Set(memberUserIds.filter((id) => id && id !== userId))];
|
||||
const memberships = await this.prisma.membership.findMany({
|
||||
where: {
|
||||
organizationId,
|
||||
userId: { in: uniqueIds },
|
||||
isActive: true,
|
||||
user: { isActive: true },
|
||||
},
|
||||
select: { userId: true },
|
||||
});
|
||||
if (memberships.length !== uniqueIds.length) {
|
||||
throw new BadRequestException('برخی اعضای کانال در این سازمان فعال نیستند');
|
||||
}
|
||||
|
||||
const creds = await this.getCredentials(organizationId);
|
||||
const members = [
|
||||
buildChatUserId(organizationId, userId),
|
||||
...uniqueIds.map((id) => buildChatUserId(organizationId, id)),
|
||||
];
|
||||
const trimmedName = name.trim();
|
||||
// سرویس خارجی معمولاً CHANNEL ندارد؛ بهصورت GROUP ساخته و در CRM بهعنوان کانال یکطرفه نگه داشته میشود
|
||||
const created = await this.chat.createRoom(creds.host, creds.apiKey, {
|
||||
name: trimmedName,
|
||||
type: 'GROUP',
|
||||
members,
|
||||
});
|
||||
|
||||
const row = await this.prisma.conversationRoom.create({
|
||||
data: {
|
||||
organizationId,
|
||||
externalRoomId: created.room_id,
|
||||
kind: 'channel',
|
||||
name: trimmedName,
|
||||
createdByUserId: userId,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
externalRoomId: row.externalRoomId,
|
||||
kind: 'channel',
|
||||
name: trimmedName,
|
||||
unreadCount: 0,
|
||||
lastMessagePreview: null,
|
||||
lastMessageAt: null,
|
||||
peerUserId: null,
|
||||
canPost: true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -422,6 +496,23 @@ export class ConversationsService {
|
||||
}
|
||||
}
|
||||
|
||||
private normalizeKind(raw: string | null | undefined): 'dm' | 'group' | 'channel' {
|
||||
if (raw === 'channel') return 'channel';
|
||||
if (raw === 'dm') return 'dm';
|
||||
return 'group';
|
||||
}
|
||||
|
||||
private canPostInRoom(
|
||||
kind: 'dm' | 'group' | 'channel',
|
||||
createdByUserId: string | null | undefined,
|
||||
currentUserId: string,
|
||||
role: string,
|
||||
): boolean {
|
||||
if (kind !== 'channel') return true;
|
||||
if (createdByUserId && createdByUserId === currentUserId) return true;
|
||||
return role === 'owner' || role === 'admin';
|
||||
}
|
||||
|
||||
private toRoomDto(
|
||||
row: ExternalRoomRow,
|
||||
trackedByExternal: Map<
|
||||
@@ -431,14 +522,16 @@ export class ConversationsService {
|
||||
name: string | null;
|
||||
dmUserMinId: string | null;
|
||||
dmUserMaxId: string | null;
|
||||
createdByUserId: string | null;
|
||||
}
|
||||
>,
|
||||
organizationId: string,
|
||||
currentUserId: string,
|
||||
role = '',
|
||||
): ConversationRoomDto {
|
||||
const externalRoomId = String(row.id ?? row.room_id ?? '').trim();
|
||||
const tracked = trackedByExternal.get(externalRoomId);
|
||||
const kind = tracked?.kind === 'group' ? 'group' : tracked?.kind === 'dm' ? 'dm' : 'group';
|
||||
const kind = this.normalizeKind(tracked?.kind);
|
||||
|
||||
const lastMsg = (row.last_message ?? row.lastMessage) as Record<string, unknown> | undefined;
|
||||
const lastType = String(lastMsg?.type_content ?? lastMsg?.type ?? 'text')
|
||||
@@ -478,7 +571,7 @@ export class ConversationsService {
|
||||
|
||||
let name =
|
||||
String(tracked?.name ?? row.name ?? row.title ?? '').trim() ||
|
||||
(kind === 'group' ? 'گروه' : 'گفتگو');
|
||||
(kind === 'channel' ? 'کانال' : kind === 'group' ? 'گروه' : 'گفتگو');
|
||||
|
||||
let peerUserId: string | null = null;
|
||||
if (tracked?.kind === 'dm' && tracked.dmUserMinId && tracked.dmUserMaxId) {
|
||||
@@ -489,13 +582,14 @@ export class ConversationsService {
|
||||
return {
|
||||
id: externalRoomId,
|
||||
externalRoomId,
|
||||
kind: kind as 'dm' | 'group',
|
||||
kind,
|
||||
name,
|
||||
unreadCount: Number.isFinite(unread) ? Math.max(0, unread) : 0,
|
||||
lastMessagePreview: preview || null,
|
||||
lastMessageAt: lastAtRaw ? String(lastAtRaw) : null,
|
||||
peerUserId,
|
||||
pinned: false,
|
||||
canPost: this.canPostInRoom(kind, tracked?.createdByUserId, currentUserId, role),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,19 @@ export class CreateConversationGroupDto {
|
||||
memberUserIds!: string[];
|
||||
}
|
||||
|
||||
export class CreateConversationChannelDto {
|
||||
@IsString()
|
||||
@MinLength(2)
|
||||
@MaxLength(120)
|
||||
name!: string;
|
||||
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
@ArrayMaxSize(100)
|
||||
@IsString({ each: true })
|
||||
memberUserIds!: string[];
|
||||
}
|
||||
|
||||
export class HideConversationMessagesDto {
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
|
||||
Reference in New Issue
Block a user