diff --git a/keyserver/src/cron/cron.js b/keyserver/src/cron/cron.js index 2b83481e33..6540ed0afc 100644 --- a/keyserver/src/cron/cron.js +++ b/keyserver/src/cron/cron.js @@ -6,6 +6,7 @@ import schedule from 'node-schedule'; import { backupDB } from './backups.js'; import { createDailyUpdatesThread } from './daily-updates.js'; import { updateAndReloadGeoipDB } from './update-geoip-db.js'; +import { updateIdentityReservedUsernames } from './update-identity-reserved-usernames.js'; import { deleteOrphanedActivity } from '../deleters/activity-deleters.js'; import { deleteExpiredCookies } from '../deleters/cookie-deleters.js'; import { deleteOrphanedDays } from '../deleters/day-deleters.js'; @@ -92,7 +93,20 @@ if (cluster.isMaster) { } }, ); - + schedule.scheduleJob( + '0 5 * * *', // every day at 5:00 AM in the keyserver's timezone + async () => { + try { + await updateIdentityReservedUsernames(); + } catch (e) { + console.warn( + 'encountered error while trying to update reserved usernames on ' + + 'identity service', + e, + ); + } + }, + ); schedule.scheduleJob( '0 0 * * *', // every day at midnight in the keyserver's timezone async () => { diff --git a/keyserver/src/cron/update-identity-reserved-usernames.js b/keyserver/src/cron/update-identity-reserved-usernames.js new file mode 100644 index 0000000000..dfdd15aa4a --- /dev/null +++ b/keyserver/src/cron/update-identity-reserved-usernames.js @@ -0,0 +1,28 @@ +// @flow + +import { getRustAPI } from 'rust-node-addon'; + +import type { ReservedUsernameMessage } from 'lib/types/crypto-types.js'; + +import { fetchAllUsernames } from '../fetchers/user-fetchers.js'; +import { fetchOlmAccount } from '../updaters/olm-account-updater.js'; + +async function updateIdentityReservedUsernames(): Promise { + const [usernames, rustAPI, accountInfo] = await Promise.all([ + fetchAllUsernames(), + getRustAPI(), + fetchOlmAccount('content'), + ]); + const issuedAt = new Date().toISOString(); + const reservedUsernameMessage: ReservedUsernameMessage = { + statement: 'Add the following usernames to reserved list', + payload: usernames, + issuedAt, + }; + const stringifiedMessage = JSON.stringify(reservedUsernameMessage); + const signature = accountInfo.account.sign(stringifiedMessage); + + await rustAPI.addReservedUsernames(stringifiedMessage, signature); +} + +export { updateIdentityReservedUsernames };