lichobile upgrade message scripts - WIP

This commit is contained in:
Thibault Duplessis
2026-05-07 20:51:16 +02:00
parent f0a81b0395
commit dbd5a947d2
2 changed files with 112 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
function gatherRecentUsers() {
db.user4.aggregate([
{
$match: {
enabled: true,
seenAt: { $gt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30) },
},
},
{ $project: { lang: 1 } },
{ $out: 'lm_user_recent' },
]);
print('Done!');
print(db.lm_user_recent.estimatedDocumentCount());
}
function filterLichobileUsers() {
db.lm_user_recent.aggregate([
{
$lookup: {
from: 'security',
as: 'sec',
localField: '_id',
foreignField: 'user',
pipeline: [
{
$match: {
api: { $exists: 1 },
},
},
{ $limit: 1 },
],
},
},
{ $match: { sec: { $ne: [] } } },
{
$lookup: {
from: 'oauth2_access_token',
as: 'tokens',
localField: '_id',
foreignField: 'userId',
pipeline: [{ $match: { scopes: 'web:mobile' } }, { $limit: 1 }],
},
},
{ $match: { tokens: [] } },
{ $project: { lang: 1 } },
{ $out: 'lm_user_recent_nomobile' },
]);
print('Done!');
print(db.lm_user_recent_nomobile.estimatedDocumentCount());
}
function addLang() {
db.lm_user_recent_nomobile.find({ lang: { $exists: 0 } }).forEach(user => {
const has = db.user4.findOne({ _id: user._id, lang: { $exists: 1 } }, { lang: 1 });
if (has) db.lm_user_recent_nomobile.updateOne({ _id: user._id }, { $set: { lang: has.lang } });
});
}
// gatherRecentUsers();
// filterLichobileUsers();
addLang();
+51
View File
@@ -0,0 +1,51 @@
import { MongoClient } from 'mongodb';
const oauthToken = process.env.OAUTH_TOKEN;
const client = new MongoClient('mongodb://127.0.0.1:27917/lichess');
const lichessUrl = 'http://l.org';
const dryRun = true;
// hardcode translations here
const translations = {
en: `Upgrade to our new app: https://lichess.org/app`,
};
const count = client.db().collection('lm_user_recent_nomobile').estimatedDocumentCount();
console.log(`Sending messages to ${count} users...`);
const chunkSize = 100;
const chunk = [];
client.db().collection('lm_user_recent_nomobile').find().forEach(user => {
// group by chunks of 100 users
chunk.push(user._id);
if (chunk.length >= chunkSize) processChunk();
});
function processChunk() {
const users = db.user4.find({ _id: { $in: chunk } },{$project:{lang:1}}).toArray();
if (dryRun) console.log(users.map(u => `${u._id} (${u.lang})`).join('\n'));
else {
const res = await fetch(`${lichessUrl}/inbox/${username}`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${oauthToken}`,
},
body: new URLSearchParams({ text }),
});
if (!res.ok) console.error(`Failed to send message to ${username}: ${res.status} ${res.statusText}`);
}
await new Promise(resolve => setTimeout(resolve, 500)); // Avoid hitting rate limits
});
function makeMessage(langCode, url) {
const lang = langCode.slice(0, 2);
const translated = translations[lang] || translations.en;
return translated.replace('{URL}', url);
}