-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into subscribed-tab-improvements-20240606
- Loading branch information
Showing
12 changed files
with
234 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/lesswrong/server/manualMigrations/2024-06-04-backfillUserFeedSubscriptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { randomId } from "@/lib/random"; | ||
import { createAdminContext } from "../vulcan-lib"; | ||
import { registerMigration } from "./migrationUtils"; | ||
import groupBy from "lodash/groupBy"; | ||
import uniq from "lodash/uniq"; | ||
|
||
function chunkWithoutSplitting<T>(dict: Record<string, T[]>, limit: number): T[][] { | ||
const result: T[][] = []; | ||
let currentChunk: T[] = []; | ||
|
||
for (const key in dict) { | ||
const values = dict[key]; | ||
|
||
if (currentChunk.length + values.length <= limit) { | ||
currentChunk.push(...values); | ||
} else { | ||
if (currentChunk.length > 0) { | ||
result.push(currentChunk); | ||
currentChunk = []; | ||
} | ||
|
||
currentChunk.push(...values); | ||
|
||
if (currentChunk.length > limit) { | ||
// eslint-disable-next-line no-console | ||
console.log(`Chunked ${currentChunk.length} values for id ${key}, which is over the ${limit} limit`); | ||
result.push(currentChunk); | ||
currentChunk = []; | ||
} | ||
} | ||
} | ||
|
||
if (currentChunk.length > 0) { | ||
result.push(currentChunk); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
|
||
registerMigration({ | ||
name: "backfillUserFeedSubscriptions", | ||
dateWritten: "2024-06-04", | ||
idempotent: true, | ||
action: async () => { | ||
const adminContext = createAdminContext(); | ||
const { Subscriptions } = adminContext; | ||
|
||
const subscriptions = await Subscriptions.find({ | ||
collectionName: 'Users', | ||
type: { $in: ['newPosts', 'newUserComments'] | ||
} }).fetch(); | ||
|
||
const currentSubscriptionStates = new Map<string, DbSubscription>(); | ||
|
||
// Get the most recent subscription record for each (userId, type, documentId) tuple | ||
subscriptions.forEach(sub => { | ||
const key = `${sub.userId}-${sub.type}-${sub.documentId}`; | ||
const existingSub = currentSubscriptionStates.get(key); | ||
|
||
if (!existingSub || existingSub.createdAt < sub.createdAt) { | ||
currentSubscriptionStates.set(key, sub); | ||
} | ||
}); | ||
|
||
// Use only those most-recent subscriptions which are "active" | ||
const activeSubscriptions = Array.from(currentSubscriptionStates.values()).filter(({ state, deleted }) => !deleted && state === 'subscribed'); | ||
|
||
const subscriptionsByType = groupBy(activeSubscriptions, ({ type }) => type); | ||
|
||
const postAndCommentSubscriptions = uniq([...subscriptionsByType['newPosts'], ...subscriptionsByType['newUserComments']].map(({ userId, documentId }) => ({ userId, documentId }))); | ||
|
||
const now = new Date(); | ||
|
||
const bulkWriteOperations = postAndCommentSubscriptions.map(({ userId, documentId }) => ({ | ||
insertOne: { | ||
document: { | ||
_id: randomId(), | ||
collectionName: 'Users', | ||
type: 'newActivityForFeed', | ||
state: 'subscribed', | ||
deleted: false, | ||
userId, | ||
documentId, | ||
createdAt: now, | ||
schemaVersion: 1, | ||
legacyData: null, | ||
} | ||
} | ||
} as const)); | ||
|
||
const writeOperationsById = groupBy(bulkWriteOperations, ({ insertOne: { document: { userId } } }) => userId); | ||
|
||
for (const batch of chunkWithoutSplitting(writeOperationsById, 1000)) { | ||
await Subscriptions.rawCollection().bulkWrite(batch); | ||
} | ||
}, | ||
}); |
55 changes: 55 additions & 0 deletions
55
packages/lesswrong/server/manualMigrations/2024-06-05-rewriteOldReviewBotComments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Comments } from "@/lib/collections/comments"; | ||
import { getSqlClientOrThrow } from "../sql/sqlClient"; | ||
import { createAdminContext, updateMutator } from "../vulcan-lib"; | ||
import { registerMigration } from "./migrationUtils"; | ||
|
||
registerMigration({ | ||
name: "rewriteOldReviewBotComments", | ||
dateWritten: "2024-06-05", | ||
idempotent: true, | ||
action: async () => { | ||
const sql = getSqlClientOrThrow() | ||
|
||
const context = createAdminContext() | ||
|
||
const oldReviewBotComments: DbComment[] = await sql.many(` | ||
SELECT * FROM "Comments" | ||
WHERE "userId" = 'tBchiz3RM7rPwujrJ' AND "deleted" = FALSE | ||
`); | ||
|
||
const reviewBotUser: DbUser = await sql.one(` | ||
SELECT * FROM "Users" | ||
WHERE "_id" = 'tBchiz3RM7rPwujrJ' | ||
`); | ||
|
||
for (const comment of oldReviewBotComments) { | ||
if (!comment.contents?.html) throw new Error("oh no!", String(comment.contents)); | ||
const manifoldUrl = comment.contents.html.match(/https:\/\/manifold.markets\/LessWrong\/([A-Za-z\d-])+/)?.[0]; | ||
const year = comment.contents.html.match(`but will be at the end of (\\d+)`)?.[1]; | ||
if (!manifoldUrl || !year) { | ||
// eslint-disable-next-line no-console | ||
console.warn("Skipping comment", comment._id, "because we didn't find a manifold URL or year", comment.contents.html) | ||
continue | ||
} | ||
|
||
const newContent = `<p>The <a href="https://www.lesswrong.com/bestoflesswrong">LessWrong Review</a> runs every year to select the posts that have most stood the test of time. This post is not yet eligible for review, but will be at the end of ${year}. The top fifty or so posts are featured prominently on the site throughout the year.</p><p>Hopefully, the review is better than karma at judging enduring value. If we have accurate prediction markets on the review results, maybe we can have better incentives on LessWrong today. <a href="${manifoldUrl}">Will this post make the top fifty?</a></p>`; | ||
|
||
if (newContent === comment.contents.html) continue; | ||
|
||
await updateMutator({ | ||
collection: Comments, | ||
documentId: comment._id, | ||
currentUser: reviewBotUser, | ||
context, | ||
data: { | ||
contents: { | ||
originalContents: { | ||
type: "html", | ||
data: newContent, | ||
} | ||
}, | ||
} | ||
}) | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters