2023-08-18 11:59:42 -04:00
|
|
|
/* eslint-disable no-await-in-loop */
|
2023-07-12 13:03:54 -04:00
|
|
|
|
2023-08-18 11:59:42 -04:00
|
|
|
'use strict';
|
2023-07-12 13:03:54 -04:00
|
|
|
|
|
|
|
|
const db = require('../../database');
|
|
|
|
|
const batch = require('../../batch');
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
name: 'Update chat messages to add roomId field',
|
|
|
|
|
timestamp: Date.UTC(2023, 6, 2),
|
|
|
|
|
method: async function () {
|
|
|
|
|
const { progress } = this;
|
|
|
|
|
|
|
|
|
|
const nextChatRoomId = await db.getObjectField('global', 'nextChatRoomId');
|
|
|
|
|
const allRoomIds = [];
|
|
|
|
|
for (let i = 1; i <= nextChatRoomId; i++) {
|
|
|
|
|
allRoomIds.push(i);
|
|
|
|
|
}
|
2023-08-17 19:24:01 -04:00
|
|
|
progress.total = 0;
|
|
|
|
|
|
|
|
|
|
// calculate user count and set progress.total
|
|
|
|
|
await batch.processArray(allRoomIds, async (roomIds) => {
|
2023-08-18 20:34:22 -04:00
|
|
|
const arrayOfRoomData = await db.getObjects(roomIds.map(roomId => `chat:room:${roomId}`));
|
|
|
|
|
await Promise.all(roomIds.map(async (roomId, idx) => {
|
|
|
|
|
const roomData = arrayOfRoomData[idx];
|
|
|
|
|
if (roomData) {
|
|
|
|
|
const userCount = await db.sortedSetCard(`chat:room:${roomId}:uids`);
|
|
|
|
|
await db.setObjectField(`chat:room:${roomId}`, 'userCount', userCount);
|
|
|
|
|
progress.total += userCount;
|
|
|
|
|
}
|
2023-08-17 19:24:01 -04:00
|
|
|
}));
|
|
|
|
|
}, {
|
|
|
|
|
batch: 500,
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
await batch.processArray(allRoomIds, async (roomIds) => {
|
2023-08-17 19:24:01 -04:00
|
|
|
const arrayOfRoomData = await db.getObjects(roomIds.map(roomId => `chat:room:${roomId}`));
|
2023-08-18 11:59:42 -04:00
|
|
|
for (const roomData of arrayOfRoomData) {
|
|
|
|
|
if (roomData) {
|
2023-08-18 16:17:19 -04:00
|
|
|
const midsSeen = {};
|
2023-08-18 11:59:42 -04:00
|
|
|
const { roomId } = roomData;
|
|
|
|
|
await batch.processSortedSet(`chat:room:${roomId}:uids`, async (uids) => {
|
|
|
|
|
for (const uid of uids) {
|
|
|
|
|
await batch.processSortedSet(`uid:${uid}:chat:room:${roomId}:mids`, async (mids) => {
|
|
|
|
|
const uniqMids = mids.filter(mid => !midsSeen.hasOwnProperty(mid));
|
|
|
|
|
if (!uniqMids.length) {
|
|
|
|
|
return;
|
2023-08-17 19:24:01 -04:00
|
|
|
}
|
2023-07-12 13:03:54 -04:00
|
|
|
|
2023-08-18 11:59:42 -04:00
|
|
|
let messageData = await db.getObjects(uniqMids.map(mid => `message:${mid}`));
|
|
|
|
|
messageData.forEach((m, idx) => {
|
|
|
|
|
if (m) {
|
|
|
|
|
m.mid = parseInt(uniqMids[idx], 10);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
messageData = messageData.filter(Boolean);
|
|
|
|
|
|
|
|
|
|
const bulkSet = messageData.map(
|
|
|
|
|
msg => [`message:${msg.mid}`, { roomId: roomId }]
|
|
|
|
|
);
|
2023-08-17 19:24:01 -04:00
|
|
|
|
2023-08-18 11:59:42 -04:00
|
|
|
await db.setObjectBulk(bulkSet);
|
|
|
|
|
await db.sortedSetAdd(
|
|
|
|
|
`chat:room:${roomId}:mids`,
|
|
|
|
|
messageData.map(m => m.timestamp),
|
|
|
|
|
messageData.map(m => m.mid),
|
|
|
|
|
);
|
|
|
|
|
uniqMids.forEach((mid) => {
|
|
|
|
|
midsSeen[mid] = 1;
|
|
|
|
|
});
|
|
|
|
|
}, {
|
|
|
|
|
batch: 500,
|
2023-08-17 19:24:01 -04:00
|
|
|
});
|
2023-08-18 11:59:42 -04:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
await db.deleteAll(`uid:${uid}:chat:room:${roomId}:mids`);
|
|
|
|
|
}
|
|
|
|
|
progress.incr(uids.length);
|
|
|
|
|
}, {
|
|
|
|
|
batch: 500,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-12 13:03:54 -04:00
|
|
|
}, {
|
|
|
|
|
batch: 500,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|