Files
doneit-web/src/app/core/chat/entity/group.ts
T

122 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-08-29 20:43:57 +01:00
import { SessionStore } from "src/app/store/session.service";
2024-08-30 12:41:50 +01:00
import { BaseEntity } from "src/app/utils/entity";
2024-08-29 20:43:57 +01:00
import { z } from "zod"
2024-08-30 15:31:29 +01:00
import { MessageEntitySchema } from "./message";
2024-09-17 16:02:12 +01:00
import { IDBoolean } from "src/app/infra/database/dexie/type";
2024-09-18 11:47:23 +01:00
import { Logger } from "src/app/services/logger/main/service";
2024-08-29 20:43:57 +01:00
2024-08-19 16:01:58 +01:00
export enum RoomType {
Group = 1,
Direct = 2
2024-08-29 20:43:57 +01:00
}
const UserSchema = z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
userPhoto: z.string().nullable(),
});
const MemberSchema = z.object({
id: z.string(),
user: UserSchema,
joinAt: z.string(),
isAdmin: z.boolean()
});
export const RoomEntitySchema = z.object({
2024-09-17 16:02:12 +01:00
$id: z.string(),
id: z.string().uuid().optional(),
2024-08-29 20:43:57 +01:00
roomName: z.string(),
createdBy: z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string().email(),
userPhoto: z.string().nullable().optional()// api check
}),
createdAt: z.any(),
2024-09-17 16:02:12 +01:00
expirationDate: z.any().nullable().optional(),
2024-08-29 20:43:57 +01:00
roomType: z.nativeEnum(RoomType),
2024-08-30 15:31:29 +01:00
members: z.array(MemberSchema).optional(),
2024-09-17 16:02:12 +01:00
messages: MessageEntitySchema.array().optional(),
local: z.nativeEnum(IDBoolean).optional(),
receiverId: z.number().optional()
2024-08-29 20:43:57 +01:00
})
2024-08-30 15:31:29 +01:00
export type IRoom = z.infer<typeof RoomEntitySchema>
2024-08-29 20:43:57 +01:00
2024-08-30 15:31:29 +01:00
export class RoomEntity extends BaseEntity<RoomEntity>(RoomEntitySchema) implements IRoom{
2024-08-29 20:43:57 +01:00
2024-09-17 16:02:12 +01:00
$id: typeof RoomEntitySchema._input.$id
2024-08-29 20:43:57 +01:00
id: typeof RoomEntitySchema._input.id
roomName: typeof RoomEntitySchema._input.roomName
createdBy: typeof RoomEntitySchema._input.createdBy
createdAt: typeof RoomEntitySchema._input.createdAt
expirationDate: typeof RoomEntitySchema._input.expirationDate
roomType: typeof RoomEntitySchema._input.roomType
2024-09-18 11:47:23 +01:00
members: typeof RoomEntitySchema._input.members = []
2024-08-30 15:31:29 +01:00
messages: typeof RoomEntitySchema._input.messages
2024-09-17 16:02:12 +01:00
receiverId: typeof RoomEntitySchema._input.receiverId
2024-08-29 20:43:57 +01:00
2024-08-30 15:31:29 +01:00
constructor(data: IRoom) {
2024-08-30 12:41:50 +01:00
super();
2024-08-29 20:43:57 +01:00
Object.assign(this, data)
2024-09-17 16:02:12 +01:00
2024-08-29 20:43:57 +01:00
if(data.roomType == RoomType.Direct) {
this.setName()
2024-09-17 16:02:12 +01:00
2024-09-18 11:47:23 +01:00
if(this.members.length == 2) {
if(!this.$id ) {
this.$id = this.getReceiverId()
}
2024-09-17 16:02:12 +01:00
2024-09-18 11:47:23 +01:00
if(!this.receiverId ) {
this.setReceiver()
}
2024-09-17 17:28:50 +01:00
}
2024-09-18 11:47:23 +01:00
2024-09-17 16:02:12 +01:00
}
}
2024-09-17 17:28:50 +01:00
// direct room only
getReceiverId() {
2024-09-17 16:02:12 +01:00
const receiver = this.members?.find((e) => e.user.wxUserId != SessionStore.user.UserId)
2024-09-18 11:47:23 +01:00
if(receiver) {
return receiver.user.wxUserId.toString()
} else {
2024-09-18 12:32:31 +01:00
// Logger.error('cant get receiver Id from Room.getReceiverId '+ this.id, this)
2024-09-18 11:47:23 +01:00
return undefined
}
2024-09-17 17:28:50 +01:00
}
2024-09-17 16:02:12 +01:00
2024-09-17 17:28:50 +01:00
// direct room only
setLocalId() {
this.$id = this.getReceiverId()
2024-09-17 16:02:12 +01:00
}
2024-09-17 17:28:50 +01:00
// direct room only
2024-09-17 16:02:12 +01:00
setReceiver() {
2024-09-18 11:47:23 +01:00
const receiverId = this.getReceiverId()
if(receiverId) {
this.receiverId = parseInt(receiverId)
} else {
2024-09-18 12:32:31 +01:00
// Logger.error('cant set receiver Id from Room.setReceiver', this)
2024-09-18 11:47:23 +01:00
}
2024-08-29 20:43:57 +01:00
}
2024-09-17 17:28:50 +01:00
// direct room only
2024-08-29 20:43:57 +01:00
setName() {
const userChatName = this.members?.find((e) => e.user.wxUserId != SessionStore.user.UserId)
if(userChatName) {
this.roomName = userChatName.user.wxFullName
}
}
2024-09-01 16:29:04 +01:00
hasLastMessage() {
2024-09-03 16:26:54 +01:00
return this.messages?.length >= 1
2024-09-01 16:29:04 +01:00
}
}