|
|
|
@ -1,12 +1,10 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const db = require('../../database');
|
|
|
|
|
const batch = require('../../batch');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
name: 'Update chat messages to add roomId field',
|
|
|
|
|
timestamp: Date.UTC(2023, 6, 2),
|
|
|
|
@ -18,47 +16,71 @@ module.exports = {
|
|
|
|
|
for (let i = 1; i <= nextChatRoomId; i++) {
|
|
|
|
|
allRoomIds.push(i);
|
|
|
|
|
}
|
|
|
|
|
progress.total = allRoomIds.length;
|
|
|
|
|
await batch.processArray(allRoomIds, async (roomIds) => {
|
|
|
|
|
progress.incr(roomIds.length);
|
|
|
|
|
const [arrayOfUids, arrayOfRoomData] = await Promise.all([
|
|
|
|
|
db.getSortedSetsMembers(roomIds.map(roomId => `chat:room:${roomId}:uids`)),
|
|
|
|
|
db.getObjects(roomIds.map(roomId => `chat:room:${roomId}`)),
|
|
|
|
|
]);
|
|
|
|
|
progress.total = 0;
|
|
|
|
|
|
|
|
|
|
await Promise.all(roomIds.map(async (roomId, index) => {
|
|
|
|
|
const uids = arrayOfUids[index];
|
|
|
|
|
const roomData = arrayOfRoomData[index];
|
|
|
|
|
if (!uids.length && !roomData) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (roomData && roomData.owner && !uids.includes(String(roomData.owner))) {
|
|
|
|
|
uids.push(roomData.owner);
|
|
|
|
|
// calculate user count and set progress.total
|
|
|
|
|
await batch.processArray(allRoomIds, async (roomIds) => {
|
|
|
|
|
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`);
|
|
|
|
|
progress.total += userCount;
|
|
|
|
|
}
|
|
|
|
|
const userKeys = uids.map(uid => `uid:${uid}:chat:room:${roomId}:mids`);
|
|
|
|
|
const mids = await db.getSortedSetsMembers(userKeys);
|
|
|
|
|
const uniqMids = _.uniq(_.flatten(mids));
|
|
|
|
|
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);
|
|
|
|
|
}));
|
|
|
|
|
}, {
|
|
|
|
|
batch: 500,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await batch.processArray(allRoomIds, async (roomIds) => {
|
|
|
|
|
const arrayOfRoomData = await db.getObjects(roomIds.map(roomId => `chat:room:${roomId}`));
|
|
|
|
|
for (const roomData of arrayOfRoomData) {
|
|
|
|
|
if (roomData) {
|
|
|
|
|
const midsSeen = {};
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bulkSet = messageData.map(
|
|
|
|
|
msg => [`message:${msg.mid}`, { roomId: roomId }]
|
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
await db.setObjectBulk(bulkSet);
|
|
|
|
|
await db.setObjectField(`chat:room:${roomId}`, 'userCount', uids.length);
|
|
|
|
|
await db.sortedSetAdd(
|
|
|
|
|
`chat:room:${roomId}:mids`,
|
|
|
|
|
messageData.map(m => m.timestamp),
|
|
|
|
|
messageData.map(m => m.mid),
|
|
|
|
|
);
|
|
|
|
|
await db.deleteAll(userKeys);
|
|
|
|
|
}));
|
|
|
|
|
const bulkSet = messageData.map(
|
|
|
|
|
msg => [`message:${msg.mid}`, { roomId: roomId }]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
await db.deleteAll(`uid:${uid}:chat:room:${roomId}:mids`);
|
|
|
|
|
}
|
|
|
|
|
progress.incr(uids.length);
|
|
|
|
|
}, {
|
|
|
|
|
batch: 500,
|
|
|
|
|
});
|
|
|
|
|
const userCount = await db.sortedSetCard(`chat:room:${roomId}:uids`);
|
|
|
|
|
await db.setObjectField(`chat:room:${roomId}`, 'userCount', userCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
batch: 500,
|
|
|
|
|
});
|
|
|
|
|