[CORL-688] Add user comment count tracking (#2744)

* feat: initial impl

* Create preliminary comment moderation slices

CORL-688

* Move slices logic into stacks

CORL-688

* Create user comment counts

CORL-688

* Create naive mutation that initializes user comment counts

CORL-688

* Use bulk updates in user counts migration

CORL-688

* fix: review

* fix: fixed issue with aggregation

* Migrate creating comment into stacks

CORL-688

* Migrate editing a comment to the stacks

CORL-688

* Break publishing comment status out of updateAllCounts

CORL-688

* review: removed variable scoping in favor of export

* revert: feb8e8196cd448f5cd24f1ca2eb0b91fe9bd43c7

* review: simplification of stacks implementation

This simplifies the stacks implementation to better reuse code related
to count management and event publishing. This can be used to great
effect with the upcomming events PR #2738.

* fix: check if authorID is null before update user counts

CORL-688

Co-authored-by: Wyatt Johnson <accounts+github@wyattjoh.ca>
This commit is contained in:
Nick Funk
2020-01-07 21:00:25 +00:00
committed by Wyatt Johnson
co-authored by Wyatt Johnson
parent 0dc3e8968a
commit e3e2e0f52e
21 changed files with 987 additions and 867 deletions
+54 -10
View File
@@ -1,4 +1,5 @@
import bcrypt from "bcryptjs";
import { identity, isEmpty, pickBy } from "lodash";
import { DateTime, DurationObject } from "luxon";
import { Db, MongoError } from "mongodb";
import uuid from "uuid";
@@ -19,16 +20,6 @@ import {
UsernameAlreadySetError,
UserNotFoundError,
} from "coral-server/errors";
import {
GQLBanStatus,
GQLDIGEST_FREQUENCY,
GQLPremodStatus,
GQLSuspensionStatus,
GQLTimeRange,
GQLUSER_ROLE,
GQLUsernameStatus,
GQLUserNotificationSettings,
} from "coral-server/graph/tenant/schema/__generated__/types";
import logger from "coral-server/logger";
import {
Connection,
@@ -40,6 +31,21 @@ import { TenantResource } from "coral-server/models/tenant";
import { DigestibleTemplate } from "coral-server/queue/tasks/mailer/templates";
import { users as collection } from "coral-server/services/mongodb/collections";
import {
GQLBanStatus,
GQLDIGEST_FREQUENCY,
GQLPremodStatus,
GQLSuspensionStatus,
GQLTimeRange,
GQLUSER_ROLE,
GQLUsernameStatus,
GQLUserNotificationSettings,
} from "coral-server/graph/tenant/schema/__generated__/types";
import {
CommentStatusCounts,
createEmptyCommentStatusCounts,
} from "../comment";
import { getLocalProfile, hasLocalProfile } from "./helpers";
export interface LocalProfile {
@@ -352,6 +358,10 @@ export interface Digest {
createdAt: Date;
}
export interface UserCommentCounts {
status: CommentStatusCounts;
}
/**
* User is someone that leaves Comments, and logs in.
*/
@@ -461,6 +471,8 @@ export interface User extends TenantResource {
* deletedAt is the time that this user was deleted from our system.
*/
deletedAt?: Date;
commentCounts: UserCommentCounts;
}
function hashPassword(password: string): Promise<string> {
@@ -514,6 +526,9 @@ async function findOrCreateUserInput(
moderatorNotes: [],
digests: [],
createdAt: now,
commentCounts: {
status: createEmptyCommentStatusCounts(),
},
};
if (input.username) {
@@ -2446,3 +2461,32 @@ export async function deleteModeratorNote(
}
return result.value;
}
export async function updateUserCommentCounts(
mongo: Db,
tenantID: string,
id: string,
commentCounts: DeepPartial<UserCommentCounts>
) {
// Update all the specific comment moderation queue counts.
const update: DeepPartial<User> = { commentCounts };
const $inc = pickBy(dotize(update), identity);
if (isEmpty($inc)) {
// Nothing needs to be incremented, just return the User.
return retrieveUser(mongo, tenantID, id);
}
logger.trace({ update: { $inc } }, "incrementing user comment counts");
const result = await collection(mongo).findOneAndUpdate(
{ id, tenantID },
{ $inc },
{
// False to return the updated document instead of the original
// document.
returnOriginal: false,
}
);
return result.value || null;
}