38290-vm/backend/src/db/api/messages.js
2026-02-08 14:35:52 +00:00

838 lines
21 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class MessagesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const messages = await db.messages.create(
{
id: data.id || undefined,
message_key: data.message_key
||
null
,
message_type: data.message_type
||
null
,
text: data.text
||
null
,
sent_at: data.sent_at
||
null
,
edited_at: data.edited_at
||
null
,
deleted_at_time: data.deleted_at_time
||
null
,
is_deleted_for_all: data.is_deleted_for_all
||
false
,
is_pinned: data.is_pinned
||
false
,
pinned_at: data.pinned_at
||
null
,
forwarded_at: data.forwarded_at
||
null
,
has_attachments: data.has_attachments
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await messages.setChat( data.chat || null, {
transaction,
});
await messages.setSender_user( data.sender_user || null, {
transaction,
});
await messages.setReply_to_message( data.reply_to_message || null, {
transaction,
});
await messages.setForwarded_from_chat( data.forwarded_from_chat || null, {
transaction,
});
await messages.setForwarded_from_user( data.forwarded_from_user || null, {
transaction,
});
return messages;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const messagesData = data.map((item, index) => ({
id: item.id || undefined,
message_key: item.message_key
||
null
,
message_type: item.message_type
||
null
,
text: item.text
||
null
,
sent_at: item.sent_at
||
null
,
edited_at: item.edited_at
||
null
,
deleted_at_time: item.deleted_at_time
||
null
,
is_deleted_for_all: item.is_deleted_for_all
||
false
,
is_pinned: item.is_pinned
||
false
,
pinned_at: item.pinned_at
||
null
,
forwarded_at: item.forwarded_at
||
null
,
has_attachments: item.has_attachments
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const messages = await db.messages.bulkCreate(messagesData, { transaction });
// For each item created, replace relation files
return messages;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const messages = await db.messages.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.message_key !== undefined) updatePayload.message_key = data.message_key;
if (data.message_type !== undefined) updatePayload.message_type = data.message_type;
if (data.text !== undefined) updatePayload.text = data.text;
if (data.sent_at !== undefined) updatePayload.sent_at = data.sent_at;
if (data.edited_at !== undefined) updatePayload.edited_at = data.edited_at;
if (data.deleted_at_time !== undefined) updatePayload.deleted_at_time = data.deleted_at_time;
if (data.is_deleted_for_all !== undefined) updatePayload.is_deleted_for_all = data.is_deleted_for_all;
if (data.is_pinned !== undefined) updatePayload.is_pinned = data.is_pinned;
if (data.pinned_at !== undefined) updatePayload.pinned_at = data.pinned_at;
if (data.forwarded_at !== undefined) updatePayload.forwarded_at = data.forwarded_at;
if (data.has_attachments !== undefined) updatePayload.has_attachments = data.has_attachments;
updatePayload.updatedById = currentUser.id;
await messages.update(updatePayload, {transaction});
if (data.chat !== undefined) {
await messages.setChat(
data.chat,
{ transaction }
);
}
if (data.sender_user !== undefined) {
await messages.setSender_user(
data.sender_user,
{ transaction }
);
}
if (data.reply_to_message !== undefined) {
await messages.setReply_to_message(
data.reply_to_message,
{ transaction }
);
}
if (data.forwarded_from_chat !== undefined) {
await messages.setForwarded_from_chat(
data.forwarded_from_chat,
{ transaction }
);
}
if (data.forwarded_from_user !== undefined) {
await messages.setForwarded_from_user(
data.forwarded_from_user,
{ transaction }
);
}
return messages;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const messages = await db.messages.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of messages) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of messages) {
await record.destroy({transaction});
}
});
return messages;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const messages = await db.messages.findByPk(id, options);
await messages.update({
deletedBy: currentUser.id
}, {
transaction,
});
await messages.destroy({
transaction
});
return messages;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const messages = await db.messages.findOne(
{ where },
{ transaction },
);
if (!messages) {
return messages;
}
const output = messages.get({plain: true});
output.message_attachments_message = await messages.getMessage_attachments_message({
transaction
});
output.message_reactions_message = await messages.getMessage_reactions_message({
transaction
});
output.read_receipts_message = await messages.getRead_receipts_message({
transaction
});
output.message_reports_message = await messages.getMessage_reports_message({
transaction
});
output.chat = await messages.getChat({
transaction
});
output.sender_user = await messages.getSender_user({
transaction
});
output.reply_to_message = await messages.getReply_to_message({
transaction
});
output.forwarded_from_chat = await messages.getForwarded_from_chat({
transaction
});
output.forwarded_from_user = await messages.getForwarded_from_user({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.chats,
as: 'chat',
where: filter.chat ? {
[Op.or]: [
{ id: { [Op.in]: filter.chat.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.chat.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'sender_user',
where: filter.sender_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.sender_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.sender_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.messages,
as: 'reply_to_message',
where: filter.reply_to_message ? {
[Op.or]: [
{ id: { [Op.in]: filter.reply_to_message.split('|').map(term => Utils.uuid(term)) } },
{
message_key: {
[Op.or]: filter.reply_to_message.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.chats,
as: 'forwarded_from_chat',
where: filter.forwarded_from_chat ? {
[Op.or]: [
{ id: { [Op.in]: filter.forwarded_from_chat.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.forwarded_from_chat.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'forwarded_from_user',
where: filter.forwarded_from_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.forwarded_from_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.forwarded_from_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.message_key) {
where = {
...where,
[Op.and]: Utils.ilike(
'messages',
'message_key',
filter.message_key,
),
};
}
if (filter.text) {
where = {
...where,
[Op.and]: Utils.ilike(
'messages',
'text',
filter.text,
),
};
}
if (filter.sent_atRange) {
const [start, end] = filter.sent_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
sent_at: {
...where.sent_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
sent_at: {
...where.sent_at,
[Op.lte]: end,
},
};
}
}
if (filter.edited_atRange) {
const [start, end] = filter.edited_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
edited_at: {
...where.edited_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
edited_at: {
...where.edited_at,
[Op.lte]: end,
},
};
}
}
if (filter.deleted_at_timeRange) {
const [start, end] = filter.deleted_at_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
deleted_at_time: {
...where.deleted_at_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
deleted_at_time: {
...where.deleted_at_time,
[Op.lte]: end,
},
};
}
}
if (filter.pinned_atRange) {
const [start, end] = filter.pinned_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
pinned_at: {
...where.pinned_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
pinned_at: {
...where.pinned_at,
[Op.lte]: end,
},
};
}
}
if (filter.forwarded_atRange) {
const [start, end] = filter.forwarded_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
forwarded_at: {
...where.forwarded_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
forwarded_at: {
...where.forwarded_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.message_type) {
where = {
...where,
message_type: filter.message_type,
};
}
if (filter.is_deleted_for_all) {
where = {
...where,
is_deleted_for_all: filter.is_deleted_for_all,
};
}
if (filter.is_pinned) {
where = {
...where,
is_pinned: filter.is_pinned,
};
}
if (filter.has_attachments) {
where = {
...where,
has_attachments: filter.has_attachments,
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.messages.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'messages',
'message_key',
query,
),
],
};
}
const records = await db.messages.findAll({
attributes: [ 'id', 'message_key' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['message_key', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.message_key,
}));
}
};