implement database as a class for tests
This commit is contained in:
@@ -7,7 +7,8 @@ import {
|
||||
registerThread,
|
||||
registerMember,
|
||||
getAllMembers,
|
||||
getRoomInfo,
|
||||
getThreadInfo,
|
||||
incrementNotSeenRoom,
|
||||
} from "../db/saveMessage-db";
|
||||
|
||||
import { findRoomByOwner, getAddresseId, getUserIdOfMailbox } from "../db/utils/mail";
|
||||
@@ -22,13 +23,13 @@ function createAddress(elt: User): string {
|
||||
return `${elt.mailbox}@${elt.host}`;
|
||||
}
|
||||
|
||||
export const roomType = {
|
||||
ROOM: 0,
|
||||
CHANNEL: 1,
|
||||
GROUP: 2,
|
||||
DM: 3,
|
||||
THREAD: 4,
|
||||
};
|
||||
export enum RoomType {
|
||||
ROOM = 0,
|
||||
CHANNEL = 1,
|
||||
GROUP = 2,
|
||||
DM = 3,
|
||||
THREAD = 4,
|
||||
}
|
||||
|
||||
export default class RegisterMessageInApp {
|
||||
messageId: number;
|
||||
@@ -73,6 +74,12 @@ export default class RegisterMessageInApp {
|
||||
return this.ownerId == this.userId;
|
||||
}
|
||||
|
||||
async incrementNotSeen(roomId: number) {
|
||||
if (!this.isSeen) {
|
||||
await incrementNotSeenRoom(roomId);
|
||||
}
|
||||
}
|
||||
|
||||
async registerMembers(roomId: number) {
|
||||
getAllMembers(this.messageId).then((res) => {
|
||||
const data = res[0].id.split(",");
|
||||
@@ -82,42 +89,49 @@ export default class RegisterMessageInApp {
|
||||
});
|
||||
}
|
||||
|
||||
async initiateRoom(owner: number, roomType: number) {
|
||||
async initiateRoom(owner: number, roomType: RoomType) {
|
||||
try {
|
||||
const roomId = await createRoom(this.envelope.subject, owner, this.messageId, roomType);
|
||||
await registerMessageInRoom(this.messageId, roomId, this.isSeen, this.envelope.date);
|
||||
this.registerMembers(roomId);
|
||||
await registerMessageInRoom(this.messageId, roomId, this.envelope.date);
|
||||
await this.incrementNotSeen(roomId);
|
||||
await this.registerMembers(roomId);
|
||||
return roomId;
|
||||
} catch (err) {
|
||||
logger.err(err);
|
||||
}
|
||||
}
|
||||
|
||||
async createOrRegisterOnExistence(owner: number, roomType: number) {
|
||||
async createOrRegisterOnExistence(owner: number, roomType: RoomType) {
|
||||
await findRoomByOwner(owner).then(async (res) => {
|
||||
if (res.length == 0) {
|
||||
// first message with this sender
|
||||
await this.initiateRoom(owner, roomType);
|
||||
} else {
|
||||
// not a reply, add to the list of message if this sender
|
||||
await registerMessageInRoom(this.messageId, res[0].room_id, this.isSeen, this.envelope.date);
|
||||
await registerMessageInRoom(this.messageId, res[0].room_id, this.envelope.date);
|
||||
await this.incrementNotSeen(res[0].room_id);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async initiateThread() {
|
||||
await createRoom(this.envelope.subject, this.ownerId, this.messageId, roomType.THREAD).then(
|
||||
async (roomId: number) => {
|
||||
await createRoom(this.envelope.subject, this.ownerId, this.messageId, RoomType.THREAD).then(
|
||||
async (threadId: number) => {
|
||||
// find parent room infos
|
||||
await getRoomInfo(this.inReplyTo).then(async (room) => {
|
||||
let roomId: number;
|
||||
await getThreadInfo(this.inReplyTo).then(async (room) => {
|
||||
// todo room not lenght, reply to transfer ?
|
||||
roomId = room[0].room_id;
|
||||
let root_id = room[0].root_id;
|
||||
if (!root_id) root_id = room[0].room_id;
|
||||
await registerThread(roomId, room[0].room_id, root_id);
|
||||
if (!root_id) root_id = roomId;
|
||||
await registerThread(threadId, room[0].room_id, root_id);
|
||||
});
|
||||
// impl register previous message ?
|
||||
await registerMessageInRoom(this.messageId, roomId, this.isSeen, this.envelope.date);
|
||||
await this.registerMembers(roomId);
|
||||
// impl register previous message or go back
|
||||
await registerMessageInRoom(this.messageId, threadId, this.envelope.date);
|
||||
await this.incrementNotSeen(roomId);
|
||||
await this.incrementNotSeen(threadId);
|
||||
await this.registerMembers(threadId);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -125,7 +139,8 @@ export default class RegisterMessageInApp {
|
||||
async createOrRegisterOnMembers(roomId: number) {
|
||||
const hasSameMembers = await hasSameMembersAsParent(this.messageId, this.inReplyTo);
|
||||
if (hasSameMembers) {
|
||||
await registerMessageInRoom(this.messageId, roomId, this.isSeen, this.envelope.date);
|
||||
await registerMessageInRoom(this.messageId, roomId, this.envelope.date);
|
||||
await this.incrementNotSeen(roomId);
|
||||
} else {
|
||||
await this.initiateThread();
|
||||
}
|
||||
@@ -142,21 +157,21 @@ export default class RegisterMessageInApp {
|
||||
// create or add new message to DM
|
||||
if (!this.envelope.to) throw new Error("Who send a DM and put the recipient in cc ?");
|
||||
const userTo = await getAddresseId(createAddress(this.envelope.to[0]));
|
||||
await this.createOrRegisterOnExistence(userTo, roomType.DM);
|
||||
await this.createOrRegisterOnExistence(userTo, RoomType.DM);
|
||||
} else {
|
||||
// it is not a reply and not a dm
|
||||
// so it is a channel, which can be possibly a group
|
||||
this.initiateRoom(this.ownerId, roomType.ROOM);
|
||||
this.initiateRoom(this.ownerId, RoomType.ROOM);
|
||||
}
|
||||
} else {
|
||||
await this.createOrRegisterOnExistence(this.ownerId, roomType.ROOM);
|
||||
await this.createOrRegisterOnExistence(this.ownerId, RoomType.ROOM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async saveReply() {
|
||||
await findRoomsFromMessage(this.inReplyTo).then(async (rooms) => {
|
||||
if (rooms.length == 0) {
|
||||
if (rooms.length < 1) {
|
||||
// no rooms, so is a transfer
|
||||
// todo test if members of transferred message are included
|
||||
} else if (rooms.length === 1) {
|
||||
|
||||
Reference in New Issue
Block a user