124 lines
3.7 KiB
TypeScript
124 lines
3.7 KiB
TypeScript
import { RoomType } from "../../../mails/saveMessage";
|
|
import { generateUsers } from "../test-attrsUtils";
|
|
|
|
interface Room {
|
|
room_id: number;
|
|
room_name: string;
|
|
owner_id: number;
|
|
message_id: number;
|
|
room_type: RoomType;
|
|
notSeen: number;
|
|
lastUpdate: string;
|
|
is_thread?: boolean;
|
|
parent_id?: number;
|
|
root_id?: number;
|
|
}
|
|
|
|
export default class saveMessageDatabase {
|
|
rooms: Room[];
|
|
roomId: number;
|
|
messages: { room_id: number; message_id: number }[];
|
|
users: any[];
|
|
|
|
constructor(_users) {
|
|
this.rooms = [];
|
|
this.messages = [];
|
|
this.users = generateUsers(5);
|
|
this.roomId = 0;
|
|
}
|
|
|
|
clear() {
|
|
this.rooms = [];
|
|
this.messages = [];
|
|
this.roomId = 0;
|
|
}
|
|
|
|
_findRoomById(roomId: number): Room {
|
|
return this.rooms.find((room) => room.room_id === roomId);
|
|
}
|
|
|
|
createRoom(roomName: string | null | undefined, ownerId: number, messageId: number, roomType: RoomType) {
|
|
this.rooms.push({
|
|
room_id: this.roomId,
|
|
room_name: roomName,
|
|
owner_id: ownerId,
|
|
message_id: messageId,
|
|
room_type: roomType,
|
|
notSeen: 0,
|
|
lastUpdate: "0",
|
|
});
|
|
this.roomId++;
|
|
}
|
|
|
|
registerMessageInRoom = (messageId: number, roomId: number, idate: string | undefined | null) => {
|
|
this.messages.push({ message_id: messageId, room_id: roomId });
|
|
};
|
|
|
|
isRoomGroup(roomId: number): Promise<boolean> {
|
|
return new Promise((resolve, reject) => {
|
|
resolve(this.rooms.find((room) => room.room_id == roomId).room_type === RoomType.GROUP);
|
|
});
|
|
}
|
|
|
|
findRoomsFromMessage = (messageID: string): Promise<{ room_id: number }[]> => {
|
|
return new Promise((resolve, reject) => {
|
|
const rooms = this.rooms.filter((room) => room.message_id.toString() === messageID);
|
|
const res: { room_id: number }[] = [];
|
|
rooms.forEach((room) => {
|
|
res.push({ room_id: room.room_id });
|
|
});
|
|
resolve(res);
|
|
});
|
|
};
|
|
|
|
hasSameMembersAsParent() {
|
|
// test_todo
|
|
}
|
|
|
|
registerThread = async (roomId: number, parentId: number, rootId: number) => {
|
|
const room = this._findRoomById(roomId);
|
|
room.parent_id = parentId;
|
|
room.room_id = rootId;
|
|
};
|
|
|
|
registerMember() {
|
|
// test_todo
|
|
}
|
|
|
|
getAllMembers() {
|
|
// test_todo
|
|
}
|
|
|
|
getThreadInfo = (messageID: string): Promise<{ room_id: number; root_id: number }[]> => {
|
|
return new Promise((resolve, reject) => {
|
|
const room = this.rooms.find((room) => room.message_id.toString() === messageID);
|
|
resolve([{ room_id: room.room_id, root_id: room.root_id }]);
|
|
});
|
|
};
|
|
|
|
incrementNotSeenRoom = (roomId: number) => {
|
|
const room = this._findRoomById(roomId);
|
|
room.notSeen++;
|
|
}
|
|
|
|
findRoomByOwner = (ownerId: number): Promise<{ room_id: number }[]> => {
|
|
return new Promise((resolve, reject) => {
|
|
const rooms = this.rooms.filter((room) => room.owner_id === ownerId);
|
|
const res = [];
|
|
rooms.forEach((room) => {
|
|
res.push({ room_id: room.room_id });
|
|
});
|
|
resolve(res);
|
|
});
|
|
};
|
|
|
|
getAddresseId = (email: string, name?: string): Promise<number> => {
|
|
const match = this.users.find((user) => user.user.mailbox + "@" + user.user.host == email);
|
|
return new Promise((resolve, reject) => resolve(match?.id));
|
|
};
|
|
|
|
getUserIdOfMailbox = (boxId: number): Promise<{ user_id: number }[]> => {
|
|
return new Promise((resolve, rejects) => resolve([{ user_id: this.users[0].id }]));
|
|
};
|
|
}
|