add original message in thread
This commit is contained in:
@@ -11,7 +11,7 @@ import {
|
||||
getThreadInfoOnId,
|
||||
} from "../../db/message/saveMessage-db";
|
||||
|
||||
import { findRoomByOwner, getAddressId, getUserIdOfMailbox } from "../../db/utils/mail";
|
||||
import { findRoomByOwner, getAddressId, getMessageIdOnID, getUserIdOfMailbox } from "../../db/utils/mail";
|
||||
import { nbMembers } from "../utils/envelopeUtils";
|
||||
import logger from "../../system/Logger";
|
||||
import { Attrs, Envelope, User } from "../../interfaces/mail/attrs.interface";
|
||||
@@ -55,93 +55,6 @@ export default class RegisterMessageInApp {
|
||||
this.inReplyTo = "";
|
||||
}
|
||||
|
||||
async init() {
|
||||
if (this.envelope.from) {
|
||||
this.ownerId = await getAddressId(createAddress(this.envelope.from[0])); // todo use sender or from ?
|
||||
} else {
|
||||
throw new Error("Envelope must have a 'from' field");
|
||||
}
|
||||
}
|
||||
|
||||
isDm = () => nbMembers(this.envelope) == 2;
|
||||
|
||||
async isFromUs() {
|
||||
if (this.userId == -1) {
|
||||
await getUserIdOfMailbox(this.boxId).then((res) => {
|
||||
this.userId = res[0]?.user_id;
|
||||
});
|
||||
}
|
||||
return this.ownerId == this.userId;
|
||||
}
|
||||
|
||||
async registerMembers(roomId: number) {
|
||||
getAllMembers(this.messageId).then((res) => {
|
||||
if (res.lenght == 0) return;
|
||||
const data = res[0].id.split(",");
|
||||
data.forEach(async (memberId: number) => {
|
||||
await registerMember(roomId, memberId);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async initiateRoom(owner: number, roomType: RoomType) {
|
||||
try {
|
||||
const roomId = await createRoom(this.envelope.subject, owner, this.messageId, roomType);
|
||||
await registerMessageInRoom(this.messageId, roomId, this.envelope.date);
|
||||
await this.registerMembers(roomId);
|
||||
return roomId;
|
||||
} catch (err) {
|
||||
logger.err(err);
|
||||
}
|
||||
}
|
||||
|
||||
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.envelope.date);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async initiateThread() {
|
||||
await createRoom(this.envelope.subject, this.ownerId, this.messageId, RoomType.THREAD).then(
|
||||
async (threadId: number) => {
|
||||
// find parent room infos
|
||||
let roomId: number;
|
||||
let root_id: number;
|
||||
await getThreadInfo(this.inReplyTo).then(async (room) => {
|
||||
// todo room not lenght, reply to transfer ?
|
||||
roomId = room[0].room_id;
|
||||
root_id = room[0].root_id;
|
||||
if (root_id === undefined) root_id = roomId;
|
||||
await registerThread(threadId, roomId, root_id);
|
||||
});
|
||||
// impl register previous message or go back
|
||||
await registerMessageInRoom(this.messageId, threadId, this.envelope.date);
|
||||
await this.registerMembers(threadId);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async createOrRegisterOnMembers(roomId: number, isThread?: boolean) {
|
||||
const hasSameMembers = await hasSameMembersAsParent(this.messageId, this.inReplyTo);
|
||||
if (hasSameMembers) {
|
||||
await registerMessageInRoom(this.messageId, roomId, this.envelope.date);
|
||||
if (isThread) {
|
||||
await getThreadInfoOnId(roomId).then(async (res) => {
|
||||
let root_id = res[0].root_id;
|
||||
if (root_id == undefined) root_id = res[0].room_id;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await this.initiateThread();
|
||||
}
|
||||
}
|
||||
|
||||
async save() {
|
||||
await this.init();
|
||||
if (this.envelope.inReplyTo) {
|
||||
@@ -194,4 +107,98 @@ export default class RegisterMessageInApp {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async init() {
|
||||
if (this.envelope.from) {
|
||||
this.ownerId = await getAddressId(createAddress(this.envelope.from[0])); // todo use sender or from ?
|
||||
} else {
|
||||
throw new Error("Envelope must have a 'from' field");
|
||||
}
|
||||
}
|
||||
|
||||
isDm = () => nbMembers(this.envelope) == 2;
|
||||
|
||||
async isFromUs() {
|
||||
if (this.userId == -1) {
|
||||
await getUserIdOfMailbox(this.boxId).then((res) => {
|
||||
this.userId = res[0]?.user_id;
|
||||
});
|
||||
}
|
||||
return this.ownerId == this.userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* add all members of the message to the room
|
||||
*/
|
||||
async registerMembers(roomId: number) {
|
||||
getAllMembers(this.messageId).then((res) => {
|
||||
if (res.lenght == 0) return;
|
||||
const data = res[0].id.split(",");
|
||||
data.forEach(async (memberId: number) => {
|
||||
await registerMember(roomId, memberId);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async initiateRoom(owner: number, roomType: RoomType) {
|
||||
try {
|
||||
const roomId = await createRoom(this.envelope.subject, owner, this.messageId, roomType);
|
||||
await registerMessageInRoom(this.messageId, roomId, this.envelope.date);
|
||||
await this.registerMembers(roomId);
|
||||
return roomId;
|
||||
} catch (err) {
|
||||
logger.err(err);
|
||||
}
|
||||
}
|
||||
|
||||
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.envelope.date);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async initiateThread() {
|
||||
const inReplyToId = (await getMessageIdOnID(this.inReplyTo))[0]?.message_id;
|
||||
await createRoom(this.envelope.subject, this.ownerId, inReplyToId, RoomType.THREAD).then(
|
||||
async (threadId: number) => {
|
||||
// find parent room infos
|
||||
let roomId: number;
|
||||
let root_id: number;
|
||||
await getThreadInfo(this.inReplyTo).then(async (room) => {
|
||||
// todo room not lenght, reply to transfer ?
|
||||
roomId = room[0].room_id;
|
||||
root_id = room[0].root_id;
|
||||
if (root_id === undefined) root_id = roomId;
|
||||
await registerThread(threadId, roomId, root_id);
|
||||
});
|
||||
|
||||
// add original message
|
||||
await registerMessageInRoom(inReplyToId, threadId, this.envelope.date);
|
||||
// add reply message
|
||||
await registerMessageInRoom(this.messageId, threadId, this.envelope.date);
|
||||
await this.registerMembers(threadId);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async createOrRegisterOnMembers(roomId: number, isThread?: boolean) {
|
||||
const hasSameMembers = await hasSameMembersAsParent(this.messageId, this.inReplyTo);
|
||||
if (hasSameMembers) {
|
||||
await registerMessageInRoom(this.messageId, roomId, this.envelope.date);
|
||||
if (isThread) {
|
||||
await getThreadInfoOnId(roomId).then(async (res) => {
|
||||
let root_id = res[0].root_id;
|
||||
if (root_id == undefined) root_id = res[0].room_id;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await this.initiateThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user