update architecture and types
This commit is contained in:
151
back/db/message/saveMessage-db.ts
Normal file
151
back/db/message/saveMessage-db.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import { RoomType } from "../../mails/message/saveMessage";
|
||||
import { hasSameElements } from "../../utils/array";
|
||||
import { transformEmojis } from "../../utils/string";
|
||||
import { execQueryAsync, execQueryAsyncWithId, execQuery } from "../db";
|
||||
import { queryFromId, queryToId, queryCcId } from "../utils/addressQueries";
|
||||
|
||||
export async function getAllMembers(messageId: number) {
|
||||
const query = `
|
||||
SELECT GROUP_CONCAT(address.address_id) AS id
|
||||
FROM address
|
||||
INNER JOIN address_field ON
|
||||
address_field.address_id = address.address_id AND
|
||||
address_field.message_id = ?
|
||||
GROUP BY address_field.message_id
|
||||
`;
|
||||
const values = [messageId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function registerMember(roomId: number, memberId: number) {
|
||||
const query = `INSERT IGNORE INTO app_room_member (room_id, member_id) VALUES (?, ?)`;
|
||||
const values = [roomId, memberId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function createRoom(
|
||||
roomName: string | null | undefined,
|
||||
ownerId: number,
|
||||
messageId: number,
|
||||
roomType: RoomType,
|
||||
): Promise<number> {
|
||||
if (!roomName) roomName = "No room name";
|
||||
roomName = transformEmojis(roomName);
|
||||
const query = `INSERT IGNORE INTO app_room (room_name, owner_id, message_id, room_type) VALUES (?, ?, ?, ?)`;
|
||||
const values = [roomName.substring(0, 255), ownerId, messageId, roomType];
|
||||
return await execQueryAsyncWithId(query, values);
|
||||
}
|
||||
|
||||
// todo date not good
|
||||
export async function registerMessageInRoom(messageId: number, roomId: number, idate: string | undefined | null) {
|
||||
if (!idate) idate = new Date().toString();
|
||||
const query = `INSERT IGNORE INTO app_room_message (message_id, room_id) VALUES (?, ?)`;
|
||||
const values = [messageId, roomId];
|
||||
await execQueryAsync(query, values);
|
||||
|
||||
updateLastUpdateRoom(roomId, idate);
|
||||
}
|
||||
|
||||
export function updateLastUpdateRoom(roomId: number, idate: string) {
|
||||
const query = `UPDATE app_room SET lastUpdate = ? WHERE room_id = ?`;
|
||||
const values = [idate, roomId];
|
||||
execQuery(query, values);
|
||||
}
|
||||
|
||||
export async function incrementNotSeenRoom(roomId: number) {
|
||||
const query = `UPDATE app_room SET notSeen = notSeen + 1 WHERE room_id = ?`;
|
||||
const values = [roomId];
|
||||
execQuery(query, values);
|
||||
}
|
||||
|
||||
export async function getThreadInfo(messageID: string): Promise<{ room_id: number; root_id: number }[]> {
|
||||
const query = `
|
||||
SELECT
|
||||
app_room.room_id,
|
||||
app_thread.root_id
|
||||
FROM app_room
|
||||
LEFT JOIN app_thread ON app_thread.room_id = app_room.room_id
|
||||
INNER JOIN app_room_message ON app_room_message.room_id = app_room.room_id
|
||||
INNER JOIN message ON message.message_id = app_room_message.message_id
|
||||
WHERE message.messageID = ?
|
||||
`;
|
||||
const values = [messageID];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function getThreadInfoOnId(threadId: number): Promise<{ room_id: number; root_id: number }[]> {
|
||||
const query = `
|
||||
SELECT
|
||||
app_room.room_id,
|
||||
app_thread.root_id
|
||||
FROM app_room
|
||||
LEFT JOIN app_thread ON app_room.room_id = app_thread.room_id
|
||||
WHERE app_room.room_id = ?
|
||||
`;
|
||||
const values = [threadId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function registerThread(roomId: number, parentId: number, rootId: number) {
|
||||
const query = `INSERT IGNORE INTO app_thread (room_id, parent_id, root_id) VALUES (?, ?, ?)`;
|
||||
const values = [roomId, parentId, rootId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function getRoomType(roomId: number): Promise<{ room_type: number }[]> {
|
||||
const query = `SELECT room_type FROM app_room WHERE room_id = ?`;
|
||||
const values = [roomId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function findRoomsFromMessage(messageID: string): Promise<{ room_id: number }[]> {
|
||||
// todo find message in room not started
|
||||
const query = `
|
||||
SELECT room_id FROM app_room_message
|
||||
INNER JOIN message ON message.message_id = app_room_message.message_id
|
||||
WHERE message.messageID = ? ORDER BY room_id
|
||||
`;
|
||||
const values = [messageID];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function hasSameMembersAsParent(messageId: number, messageID: string): Promise<boolean> {
|
||||
const query1 = `
|
||||
SELECT
|
||||
GROUP_CONCAT(fromT.address_id) AS fromA,
|
||||
GROUP_CONCAT(toT.address_id) AS toA,
|
||||
GROUP_CONCAT(ccT.address_id) AS ccA
|
||||
FROM message msg
|
||||
${queryFromId} fromT ON msg.message_id = fromT.message_id
|
||||
${queryToId} toT ON msg.message_id = toT.message_id
|
||||
${queryCcId} ccT ON msg.message_id = ccT.message_id
|
||||
WHERE msg.message_id = ?
|
||||
`;
|
||||
const values1 = [messageId];
|
||||
let addressesMsg1 = await execQueryAsync(query1, values1);
|
||||
|
||||
const query2 = `
|
||||
SELECT
|
||||
GROUP_CONCAT(fromT.address_id) AS fromA,
|
||||
GROUP_CONCAT(toT.address_id) AS toA,
|
||||
GROUP_CONCAT(ccT.address_id) AS ccA
|
||||
FROM message msg
|
||||
${queryFromId} fromT ON msg.message_id = fromT.message_id
|
||||
${queryToId} toT ON msg.message_id = toT.message_id
|
||||
${queryCcId} ccT ON msg.message_id = ccT.message_id
|
||||
WHERE msg.messageID = ?
|
||||
`;
|
||||
const values2 = [messageID];
|
||||
let addressesMsg2 = await execQueryAsync(query2, values2);
|
||||
|
||||
addressesMsg1 = addressesMsg1[0]?.fromA
|
||||
?.split(",")
|
||||
.concat(addressesMsg1[0]?.toA?.split(","))
|
||||
.concat(addressesMsg1[0]?.ccA?.split(","));
|
||||
addressesMsg2 = addressesMsg2[0]?.fromA
|
||||
?.split(",")
|
||||
.concat(addressesMsg2[0]?.toA?.split(","))
|
||||
.concat(addressesMsg2[0]?.ccA?.split(","));
|
||||
|
||||
return hasSameElements(addressesMsg1, addressesMsg2);
|
||||
}
|
||||
79
back/db/message/storeMessage-db.ts
Normal file
79
back/db/message/storeMessage-db.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { transformEmojis } from "../../utils/string";
|
||||
import { execQuery, execQueryAsync, execQueryAsyncWithId } from "../db";
|
||||
|
||||
export async function registerMessage(timestamp: string, rfc822size: number, messageID: string) {
|
||||
const query = `
|
||||
INSERT INTO message
|
||||
(idate, messageID, rfc822size) VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE message_id = LAST_INSERT_ID(message_id)
|
||||
`;
|
||||
const values = [timestamp, messageID, rfc822size];
|
||||
return await execQueryAsyncWithId(query, values);
|
||||
}
|
||||
|
||||
export function registerMailbox_message(
|
||||
mailboxId: number,
|
||||
uid: number,
|
||||
messageId: number,
|
||||
modseq: number,
|
||||
seen: boolean,
|
||||
deleted: boolean,
|
||||
) {
|
||||
const query = `
|
||||
INSERT IGNORE INTO mailbox_message
|
||||
(mailbox_id, uid, message_id, modseq, seen, deleted) VALUES (?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
const values = [mailboxId, uid, messageId, modseq, seen, deleted];
|
||||
execQuery(query, values);
|
||||
}
|
||||
|
||||
export function registerBodypart(messageId: number, part: string, bodypartId: number, bytes: number, nbLines: null) {
|
||||
const query = `
|
||||
INSERT IGNORE INTO part_number
|
||||
(message_id, part, bodypart_id, bytes, nb_lines) VALUES (?, ?, ?, ?, ?)
|
||||
`;
|
||||
const values = [messageId, part, bodypartId, bytes, nbLines];
|
||||
execQuery(query, values);
|
||||
}
|
||||
|
||||
export async function saveBodypart(bytes, hash, text, data) {
|
||||
text = transformEmojis(text);
|
||||
const query = `INSERT IGNORE INTO bodypart (bytes, hash, text, data) VALUES (?, ?, ?, ?)`;
|
||||
const values = [bytes, hash, text, data];
|
||||
return await execQueryAsyncWithId(query, values);
|
||||
}
|
||||
|
||||
export async function saveHeader_fields(
|
||||
messageId: number,
|
||||
fieldId: number,
|
||||
bodypartId: number,
|
||||
part: string,
|
||||
value: string,
|
||||
) {
|
||||
value = transformEmojis(value);
|
||||
const query = `
|
||||
INSERT IGNORE INTO header_field
|
||||
(message_id, field_id, bodypart_id, part, value) VALUES (?, ?, ?, ?, ?)
|
||||
`;
|
||||
const values = [messageId, fieldId, bodypartId, part, value];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function saveAddress_fields(messageId: number, fieldId: number, addressId: number, number: number) {
|
||||
const query = `
|
||||
INSERT IGNORE INTO address_field
|
||||
(message_id , field_id, address_id, number) VALUES (?, ?, ?, ?)
|
||||
`;
|
||||
const values = [messageId, fieldId, addressId, number];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export function saveSource(messageId: number, content: string) {
|
||||
content = transformEmojis(content);
|
||||
const query = `
|
||||
INSERT INTO source (message_id, content) VALUES (?, ?)
|
||||
ON DUPLICATE KEY UPDATE content = ?
|
||||
`;
|
||||
const values = [messageId, content, content];
|
||||
execQuery(query, values);
|
||||
}
|
||||
Reference in New Issue
Block a user