[CORL-836] Create activeStories endpoint (#2787)

* Create activeStories GraphQL query endpoint

Set lastCommentedAt on stories when they are
commented upon.
Use lastCommentedAt to retrieve the activeStories.
Create a migration to partial index
lastCommentedAt on stories to make retrieval fast.

CORL-836

* fix: adjusted query to use index, more @auth directives

Co-authored-by: Wyatt Johnson <accounts+github@wyattjoh.ca>
This commit is contained in:
Nick Funk
2020-01-10 16:55:22 -07:00
committed by Wyatt Johnson
parent 745fb4056c
commit a88644d98e
8 changed files with 143 additions and 31 deletions
+7 -6
View File
@@ -6,12 +6,6 @@ import uuid from "uuid";
import { Omit, Sub } from "coral-common/types";
import { dotize } from "coral-common/utils/dotize";
import { CommentNotFoundError } from "coral-server/errors";
import {
GQLCOMMENT_SORT,
GQLCOMMENT_STATUS,
GQLCommentTagCounts,
GQLTAG,
} from "coral-server/graph/schema/__generated__/types";
import logger from "coral-server/logger";
import {
EncodedCommentActionCounts,
@@ -31,6 +25,13 @@ import {
import { TenantResource } from "coral-server/models/tenant";
import { comments as collection } from "coral-server/services/mongodb/collections";
import {
GQLCOMMENT_SORT,
GQLCOMMENT_STATUS,
GQLCommentTagCounts,
GQLTAG,
} from "coral-server/graph/schema/__generated__/types";
import { PUBLISHED_STATUSES } from "./constants";
import {
CommentStatusCounts,
+50 -4
View File
@@ -7,10 +7,6 @@ import {
DuplicateStoryIDError,
DuplicateStoryURLError,
} from "coral-server/errors";
import {
GQLStoryMetadata,
GQLStorySettings,
} from "coral-server/graph/schema/__generated__/types";
import {
Connection,
ConnectionInput,
@@ -21,6 +17,11 @@ import { GlobalModerationSettings } from "coral-server/models/settings";
import { TenantResource } from "coral-server/models/tenant";
import { stories as collection } from "coral-server/services/mongodb/collections";
import {
GQLStoryMetadata,
GQLStorySettings,
} from "coral-server/graph/schema/__generated__/types";
import { createEmptyCommentStatusCounts } from "../comment/helpers";
import {
createEmptyCommentModerationQueueCounts,
@@ -75,6 +76,11 @@ export interface Story extends TenantResource {
* createdAt is the date that the Story was added to the Coral database.
*/
createdAt: Date;
/**
* lastCommentedAt is the last time someone commented on this story.
*/
lastCommentedAt?: Date;
}
export interface UpsertStoryInput {
@@ -455,3 +461,43 @@ async function retrieveConnection(
// Return a connection.
return resolveConnection(query, input, story => story.createdAt);
}
export async function retrieveActiveStories(
mongo: Db,
tenantID: string,
limit: number
) {
const stories = await collection(mongo)
.find({
tenantID,
// We limit this query to stories that have the following field. This
// allows us to use the index.
lastCommentedAt: {
$exists: true,
},
})
.sort({ lastCommentedAt: -1 })
.limit(limit)
.toArray();
return stories;
}
export async function updateStoryLastCommentedAt(
mongo: Db,
tenantID: string,
storyID: string,
now: Date
) {
await collection(mongo).updateOne(
{
tenantID,
id: storyID,
},
{
$set: {
lastCommentedAt: now,
},
}
);
}