|
|
@@ -1,20 +1,26 @@
|
|
|
import { FastifyInstance } from 'fastify'
|
|
|
import { Repository } from 'typeorm'
|
|
|
import { TgUser } from '../entities/tg-user.entity'
|
|
|
+import { TgGroup } from '../entities/tg-group.entity'
|
|
|
import { TgClientService } from './tgClient.service'
|
|
|
import { buildStringSessionByDcIdAndAuthKey } from '../utils/tg.util'
|
|
|
import { TelegramClient } from 'telegram'
|
|
|
-import { ChatGroupService } from './chat-group.service'
|
|
|
+import { PaginationResponse } from '../dto/common.dto'
|
|
|
+import {
|
|
|
+ CreateTgGroupRecordBody,
|
|
|
+ ListTgGroupQuery,
|
|
|
+ UpdateTgGroupRecordBody
|
|
|
+} from '../dto/tg-group.dto'
|
|
|
|
|
|
export class TgGroupService {
|
|
|
private readonly app: FastifyInstance
|
|
|
private readonly tgClientService: TgClientService
|
|
|
- private readonly chatGroupService: ChatGroupService
|
|
|
+ private readonly tgGroupRepository: Repository<TgGroup>
|
|
|
private readonly senderRepository: Repository<TgUser>
|
|
|
constructor(app: FastifyInstance) {
|
|
|
this.app = app
|
|
|
this.tgClientService = new TgClientService()
|
|
|
- this.chatGroupService = new ChatGroupService(app)
|
|
|
+ this.tgGroupRepository = app.dataSource.getRepository(TgGroup)
|
|
|
this.senderRepository = app.dataSource.getRepository(TgUser)
|
|
|
}
|
|
|
|
|
|
@@ -90,7 +96,7 @@ export class TgGroupService {
|
|
|
await this.tgClientService.sendMessageToChannelGroup(inputChannel, initMsg.trim())
|
|
|
}
|
|
|
|
|
|
- await this.chatGroupService.upsertGroup({
|
|
|
+ await this.upsertGroup({
|
|
|
chatId: groupInfo.chatId,
|
|
|
accessHash: groupInfo.accessHash,
|
|
|
name: groupName,
|
|
|
@@ -132,4 +138,63 @@ export class TgGroupService {
|
|
|
}
|
|
|
|
|
|
async inviteMembers(senderId?: string, chatId?: string, accessHash?: string, members?: string[]): Promise<any> {}
|
|
|
+
|
|
|
+ async upsertGroup(payload: CreateTgGroupRecordBody): Promise<TgGroup> {
|
|
|
+ const existing = await this.tgGroupRepository.findOne({ where: { chatId: payload.chatId } })
|
|
|
+
|
|
|
+ if (existing) {
|
|
|
+ // 更新现有记录
|
|
|
+ Object.assign(existing, {
|
|
|
+ ...payload,
|
|
|
+ chatId: String(payload.chatId),
|
|
|
+ accessHash: String(payload.accessHash)
|
|
|
+ })
|
|
|
+ return await this.tgGroupRepository.save(existing)
|
|
|
+ } else {
|
|
|
+ // 创建新记录
|
|
|
+ const entity = this.tgGroupRepository.create({
|
|
|
+ ...payload,
|
|
|
+ chatId: String(payload.chatId),
|
|
|
+ accessHash: String(payload.accessHash)
|
|
|
+ })
|
|
|
+ return await this.tgGroupRepository.save(entity)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async updateGroup(payload: UpdateTgGroupRecordBody): Promise<TgGroup | null> {
|
|
|
+ const existing = await this.tgGroupRepository.findOne({ where: { chatId: payload.chatId } })
|
|
|
+ if (!existing) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ const { chatId, ...updateData } = payload
|
|
|
+ await this.tgGroupRepository.update({ chatId }, updateData)
|
|
|
+ return await this.tgGroupRepository.findOne({ where: { chatId: payload.chatId } })
|
|
|
+ }
|
|
|
+
|
|
|
+ async findByChatId(chatId: string): Promise<TgGroup | null> {
|
|
|
+ return await this.tgGroupRepository.findOne({ where: { chatId, delFlag: false } })
|
|
|
+ }
|
|
|
+
|
|
|
+ async list(query: ListTgGroupQuery): Promise<PaginationResponse<TgGroup>> {
|
|
|
+ const { page = 0, size = 20, senderId, delFlag } = query
|
|
|
+ const where: any = {}
|
|
|
+ if (senderId !== undefined) where.senderId = senderId
|
|
|
+ if (delFlag !== undefined) where.delFlag = delFlag
|
|
|
+
|
|
|
+ const [content, total] = await this.tgGroupRepository.findAndCount({
|
|
|
+ where,
|
|
|
+ skip: (Number(page) || 0) * (Number(size) || 20),
|
|
|
+ take: Number(size) || 20,
|
|
|
+ order: { createdAt: 'DESC' }
|
|
|
+ })
|
|
|
+
|
|
|
+ return {
|
|
|
+ content,
|
|
|
+ metadata: {
|
|
|
+ total: Number(total),
|
|
|
+ page: Number(page) || 0,
|
|
|
+ size: Number(size) || 20
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|