152 lines
5.7 KiB
TypeScript
152 lines
5.7 KiB
TypeScript
import { RoomType } from "../mails/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);
|
|
}
|