Skip to content

Commit

Permalink
[keyserver] cron job to sync reserved usernames
Browse files Browse the repository at this point in the history
Summary:
daily cron job to send all the usernames from ashoat's keyserver to the identity service.

the logic here is very similar to the logic added to the account-creator.js

Depends on D8330

Test Plan: Modified cron job schedule to force a run and observed that usernames were added to the identity service's reserved usernames table locally

Reviewers: ashoat

Reviewed By: ashoat

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D8331
  • Loading branch information
vdhanan committed Jul 4, 2023
1 parent 979c4b4 commit 9a75d88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
16 changes: 15 additions & 1 deletion keyserver/src/cron/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 () => {
Expand Down
31 changes: 31 additions & 0 deletions keyserver/src/cron/update-identity-reserved-usernames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @flow

import { getRustAPI } from 'rust-node-addon';

import type { ReservedUsernameMessage } from 'lib/types/crypto-types.js';

import { fetchAllUsernames } from '../fetchers/user-fetchers.js';
import { addReservedUsernamesStatement } from '../shared/message-statements.js';
import { fetchOlmAccount } from '../updaters/olm-account-updater.js';

async function updateIdentityReservedUsernames(): Promise<void> {
const [usernames, rustAPI, accountInfo] = await Promise.all([
fetchAllUsernames(),
getRustAPI(),
fetchOlmAccount('content'),
]);
const issuedAt = new Date().toISOString();
const reservedUsernameMessage: ReservedUsernameMessage<
$ReadOnlyArray<string>,
> = {
statement: addReservedUsernamesStatement,
payload: usernames,
issuedAt,
};
const stringifiedMessage = JSON.stringify(reservedUsernameMessage);
const signature = accountInfo.account.sign(stringifiedMessage);

await rustAPI.addReservedUsernames(stringifiedMessage, signature);
}

export { updateIdentityReservedUsernames };

0 comments on commit 9a75d88

Please sign in to comment.