70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
import { In, Repository } from "typeorm";
|
|
|
|
import { RCode } from "../chat/type";
|
|
import { GetGroupMessageDto } from "./dtos/group.dto";
|
|
import { GroupEntity, GroupMapEntity } from "./entities/group.entity";
|
|
import { GroupMessageEntity } from "./entities/groupMessage.entity";
|
|
|
|
@Injectable()
|
|
export class GroupService {
|
|
constructor(
|
|
@InjectRepository(GroupEntity)
|
|
private readonly groupRepository: Repository<GroupEntity>,
|
|
@InjectRepository(GroupMapEntity)
|
|
private readonly groupMapRepository: Repository<GroupMapEntity>,
|
|
@InjectRepository(GroupMessageEntity)
|
|
private readonly groupMessageRepository: Repository<GroupMessageEntity>,
|
|
) {}
|
|
async postGroups(groupIds: string[]) {
|
|
if (!groupIds || groupIds.length === 0) {
|
|
return { code: RCode.ERROR, data: [], msg: "群组ID不能为空" };
|
|
}
|
|
try {
|
|
const groups = await this.groupRepository.find({
|
|
where: { groupId: In(groupIds) },
|
|
});
|
|
return { code: RCode.OK, data: groups, msg: "获取群信息成功" };
|
|
} catch (e) {
|
|
return { code: RCode.ERROR, data: [], msg: "获取群信息失败" };
|
|
}
|
|
}
|
|
async getUserGroups(userId: string) {
|
|
try {
|
|
if (!userId) {
|
|
const data = await this.groupMapRepository.find();
|
|
return { code: RCode.ERROR, data, msg: "用户ID不能为空" };
|
|
}
|
|
const data = await this.groupMapRepository.find({
|
|
where: { userId },
|
|
});
|
|
return { code: RCode.OK, data, msg: "获取用户群信息成功" };
|
|
} catch (e) {
|
|
return { code: RCode.ERROR, data: [], msg: "获取用户群信息失败" };
|
|
}
|
|
}
|
|
async getGroupMessages(value: GetGroupMessageDto) {
|
|
const { userId, groupId, current, pageSize } = value;
|
|
const groupUser = await this.groupMapRepository
|
|
.findOne({
|
|
where: { userId, groupId },
|
|
})
|
|
.catch((_) => null);
|
|
if (!groupUser) {
|
|
return { code: RCode.ERROR, data: [], msg: "用户不在该群组中" };
|
|
}
|
|
const { createTime } = groupUser;
|
|
const groupMessage = await this.groupMessageRepository
|
|
.createQueryBuilder("groupMessage")
|
|
.where("groupMessage.groupId = :groupId", { groupId })
|
|
.andWhere("groupMessage.time >= :createTime", {
|
|
createTime: createTime - 86400000,
|
|
})
|
|
.skip(current)
|
|
.take(pageSize)
|
|
.orderBy("groupMessage.time", "ASC")
|
|
.getMany();
|
|
}
|
|
}
|