Spaces:
Running
Running
File size: 4,140 Bytes
71601b1 26aeb58 71601b1 410987a 71601b1 c4341fd 71601b1 c4341fd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | const User = require('../models/User');
const Message = require('../models/Message');
const Dialog = require('../models/Dialog');
const { logError } = require('../utils/logger');
const runMigration = async () => {
try {
const needsMigration = await Message.exists({ dialog_id: { $exists: false }, receiver: { $not: /^room_/ } });
if (!needsMigration) return;
const messagesWithoutDialogId = await Message.find({
dialog_id: { $exists: false },
receiver: { $not: /^room_/ }
}).lean();
const bulkOps = [];
for (const msg of messagesWithoutDialogId) {
const dialogId = [String(msg.sender).toLowerCase(), String(msg.receiver).toLowerCase()].sort().join('_');
bulkOps.push({
updateOne: {
filter: { _id: msg._id },
update: { $set: { dialog_id: dialogId } }
}
});
if (bulkOps.length === 1000) {
await Message.bulkWrite(bulkOps);
bulkOps.length = 0;
}
}
if (bulkOps.length > 0) {
await Message.bulkWrite(bulkOps);
}
const latestMessages = await Message.aggregate([
{ $match: { receiver: { $not: /^room_/ }, dialog_id: { $exists: true } } },
{ $sort: { timestamp: -1 } },
{ $group: {
_id: "$dialog_id",
lastMsg: { $first: "$$ROOT" }
}}
]);
for (const group of latestMessages) {
const msg = group.lastMsg;
const u1 = msg.sender.toLowerCase();
const u2 = msg.receiver.toLowerCase();
const unreadU1 = await Message.countDocuments({ dialog_id: msg.dialog_id, receiver: u1, is_read: false });
const unreadU2 = await Message.countDocuments({ dialog_id: msg.dialog_id, receiver: u2, is_read: false });
await Dialog.findOneAndUpdate(
{ dialog_id: msg.dialog_id },
{
$set: {
participants: [u1, u2],
lastMessageText: msg.text,
lastSender: msg.sender,
lastMessageTimestamp: msg.timestamp,
lastIsAudio: msg.isAudio || false,
lastIsMusic: msg.isMusic || false,
lastIsVideoNote: msg.isVideoNote || false,
lastFileUrl: msg.fileUrl || null,
lastFileName: msg.fileName || null,
[`unreadCounts.${u1}`]: unreadU1,
[`unreadCounts.${u2}`]: unreadU2
}
},
{ upsert: true }
);
}
} catch (err) {}
};
if (!global.privacyMap) global.privacyMap = new Map();
const dedupPins = async () => {
try {
const Pin = require('../models/Pin');
await Pin.updateMany({ pin_type: 'self' }, { $set: { pin_type: 'me' } });
const dupes = await Pin.aggregate([
{ $sort: { timestamp: -1 } },
{ $group: {
_id: { m: '$message_id', c: '$chat_id', p: '$pinner_id' },
ids: { $push: '$_id' },
n: { $sum: 1 }
} },
{ $match: { n: { $gt: 1 } } }
]);
const toDelete = [];
for (const d of dupes) toDelete.push(...d.ids.slice(1));
for (let i = 0; i < toDelete.length; i += 500) {
await Pin.deleteMany({ _id: { $in: toDelete.slice(i, i + 500) } });
}
await Pin.syncIndexes();
} catch (err) {
logError('dedup_pins', err);
}
};
const loadPrivacyMap = async () => {
try {
const users = await User.find({ privacy: { $exists: true, $ne: '{}' } }).select('username privacy').lean();
users.forEach(u => {
try { global.privacyMap.set(u.username.toLowerCase(), JSON.parse(u.privacy)); } catch (e) {}
});
} catch (err) { logError('privacy_load', err); }
};
module.exports = { runMigration, loadPrivacyMap, dedupPins };
|