[FIX] Purged threads still show as unread (#18944)
Co-authored-by: Diego Sampaio <chinello@gmail.com> Co-authored-by: Rodrigo Nascimento <rodrigoknascimento@gmail.com>pull/18882/head^2
parent
14a2de551a
commit
995b4e3ff8
@ -0,0 +1,62 @@ |
||||
import { Migrations } from '../../../app/migrations'; |
||||
import { Subscriptions, Messages } from '../../../app/models/server/raw'; |
||||
|
||||
|
||||
async function migrate() { |
||||
const subs = await Subscriptions.find({ |
||||
$or: [{ |
||||
'tunread.0': { $exists: true }, |
||||
}, { |
||||
'tunreadUser.0': { $exists: true }, |
||||
}, { |
||||
'tunreadGroup.0': { $exists: true }, |
||||
}], |
||||
}, { |
||||
projection: { |
||||
_id: 0, |
||||
tunread: 1, |
||||
tunreadUser: 1, |
||||
tunreadGroup: 1, |
||||
}, |
||||
}).toArray(); |
||||
|
||||
// Get unique thread ids
|
||||
const tunreads = new Set(); |
||||
for (const { tunread = [], tunreadUser = [], tunreadGroup = [] } of subs) { |
||||
tunread.forEach((i) => tunreads.add(i)); |
||||
tunreadUser.forEach((i) => tunreads.add(i)); |
||||
tunreadGroup.forEach((i) => tunreads.add(i)); |
||||
} |
||||
|
||||
const inexistentThreads = new Set(); |
||||
for await (const tunread of tunreads) { |
||||
if (!await Messages.findOne({ _id: tunread }, { _id: 1 })) { |
||||
inexistentThreads.add(tunread); |
||||
} |
||||
} |
||||
|
||||
const inexistentThreadsArr = [...inexistentThreads]; |
||||
|
||||
await Subscriptions.update({ |
||||
$or: [{ |
||||
tunread: { $in: inexistentThreadsArr }, |
||||
}, { |
||||
tunreadUser: { $in: inexistentThreadsArr }, |
||||
}, { |
||||
tunreadGroup: { $in: inexistentThreadsArr }, |
||||
}], |
||||
}, { |
||||
$pullAll: { |
||||
tunread: inexistentThreadsArr, |
||||
tunreadUser: inexistentThreadsArr, |
||||
tunreadGroup: inexistentThreadsArr, |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
Migrations.add({ |
||||
version: 206, |
||||
up() { |
||||
Promise.await(migrate()); |
||||
}, |
||||
}); |
||||
Loading…
Reference in new issue