Page MenuHomePhabricator

D4071.id12857.diff
No OneTemporary

D4071.id12857.diff

diff --git a/keyserver/src/database/migrations.js b/keyserver/src/database/migrations.js
--- a/keyserver/src/database/migrations.js
+++ b/keyserver/src/database/migrations.js
@@ -5,6 +5,7 @@
import { getMessageForException } from 'lib/utils/errors';
+import { updateRolesAndPermissionsForAllThreads } from '../updaters/thread-permission-updaters';
import { dbQuery, SQL } from './database';
async function makeSureBaseRoutePathExists(filePath: string): Promise<void> {
@@ -85,6 +86,12 @@
await fixBaseRoutePathForLocalhost('facts/squadcal_url.json');
},
],
+ [
+ 3,
+ async () => {
+ await updateRolesAndPermissionsForAllThreads();
+ },
+ ],
]);
async function migrate(): Promise<boolean> {
diff --git a/keyserver/src/updaters/thread-permission-updaters.js b/keyserver/src/updaters/thread-permission-updaters.js
--- a/keyserver/src/updaters/thread-permission-updaters.js
+++ b/keyserver/src/updaters/thread-permission-updaters.js
@@ -38,6 +38,7 @@
import { rescindPushNotifs } from '../push/rescind';
import { createScriptViewer } from '../session/scripts';
import type { Viewer } from '../session/viewer';
+import { updateRoles } from '../updaters/role-updaters';
import DepthQueue from '../utils/depth-queue';
import RelationshipChangeset from '../utils/relationship-changeset';
import { updateChangedUndirectedRelationships } from './relationship-updaters';
@@ -1238,6 +1239,42 @@
}
}
+async function updateRolesAndPermissionsForAllThreads() {
+ const batchSize = 10;
+
+ const fetchThreads = SQL`SELECT id, type FROM threads`;
+ const [result] = await dbQuery(fetchThreads);
+ const threads = result.map(row => {
+ return { id: row.id.toString(), type: assertThreadType(row.type) };
+ });
+
+ const viewer = createScriptViewer(bots.commbot.userID);
+
+ while (threads.length > 0) {
+ const batch = threads.splice(0, batchSize);
+ const membershipRows = [];
+ const relationshipChangeset = new RelationshipChangeset();
+ await Promise.all(
+ batch.map(async thread => {
+ console.log(`updating roles for ${thread.id}`);
+ await updateRoles(viewer, thread.id, thread.type);
+ console.log(`recalculating permissions for ${thread.id}`);
+ const {
+ membershipRows: threadMembershipRows,
+ relationshipChangeset: threadRelationshipChangeset,
+ } = await recalculateThreadPermissions(thread.id);
+ membershipRows.push(...threadMembershipRows);
+ relationshipChangeset.addAll(threadRelationshipChangeset);
+ }),
+ );
+ console.log(`committing batch ${JSON.stringify(batch)}`);
+ await commitMembershipChangeset(viewer, {
+ membershipRows,
+ relationshipChangeset,
+ });
+ }
+}
+
export {
changeRole,
recalculateThreadPermissions,
@@ -1245,4 +1282,5 @@
saveMemberships,
commitMembershipChangeset,
recalculateAllThreadPermissions,
+ updateRolesAndPermissionsForAllThreads,
};

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 30, 10:26 AM (21 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2600572
Default Alt Text
D4071.id12857.diff (2 KB)

Event Timeline