mirror of
https://github.com/wassname/talk.git
synced 2026-07-13 17:45:56 +08:00
feat: introduce asset action count caching
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { GQLAssetTypeResolver } from "talk-server/graph/tenant/schema/__generated__/types";
|
||||
import { decodeActionCounts } from "talk-server/models/actions";
|
||||
import { Asset } from "talk-server/models/asset";
|
||||
|
||||
const Asset: GQLAssetTypeResolver<Asset> = {
|
||||
@@ -6,6 +7,7 @@ const Asset: GQLAssetTypeResolver<Asset> = {
|
||||
ctx.loaders.Comments.forAsset(asset.id, input),
|
||||
// TODO: implement this.
|
||||
isClosed: () => false,
|
||||
actionCounts: asset => decodeActionCounts(asset.action_counts),
|
||||
};
|
||||
|
||||
export default Asset;
|
||||
|
||||
@@ -930,6 +930,11 @@ type Asset {
|
||||
after: Cursor
|
||||
): CommentsConnection!
|
||||
|
||||
"""
|
||||
actionCounts stores the counts of all the actions for the Comment.
|
||||
"""
|
||||
actionCounts: ActionCounts!
|
||||
|
||||
"""
|
||||
author is the authors listed in the meta tags for the Asset.
|
||||
"""
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { defaults } from "lodash";
|
||||
import { Db } from "mongodb";
|
||||
import uuid from "uuid";
|
||||
|
||||
import { Omit } from "talk-common/types";
|
||||
import { dotize } from "talk-common/utils/dotize";
|
||||
import { EncodedActionCounts } from "talk-server/models/actions";
|
||||
import { ModerationSettings } from "talk-server/models/settings";
|
||||
import { TenantResource } from "talk-server/models/tenant";
|
||||
|
||||
@@ -27,6 +27,11 @@ export interface Asset extends TenantResource {
|
||||
modified_date?: Date;
|
||||
created_at: Date;
|
||||
|
||||
/**
|
||||
* action_counts stores all the action counts for all Comment's on this Asset.
|
||||
*/
|
||||
action_counts: EncodedActionCounts;
|
||||
|
||||
/**
|
||||
* settings provides a point where the settings can be overriden for a
|
||||
* specific Asset.
|
||||
@@ -51,17 +56,13 @@ export async function upsertAsset(
|
||||
// Create the asset, optionally sourcing the id from the input, additionally
|
||||
// porting in the tenant_id.
|
||||
const update: { $setOnInsert: Asset } = {
|
||||
$setOnInsert: defaults(
|
||||
{
|
||||
url,
|
||||
tenant_id: tenantID,
|
||||
created_at: now,
|
||||
},
|
||||
{ id },
|
||||
{
|
||||
id: uuid.v4(),
|
||||
}
|
||||
),
|
||||
$setOnInsert: {
|
||||
id: id ? id : uuid.v4(),
|
||||
url,
|
||||
tenant_id: tenantID,
|
||||
created_at: now,
|
||||
action_counts: {},
|
||||
},
|
||||
};
|
||||
|
||||
// Perform the find and update operation to try and find and or create the
|
||||
@@ -193,3 +194,31 @@ export async function updateAsset(
|
||||
|
||||
return result.value || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* updateAssetActionCounts will update the given comment's action counts on
|
||||
* the Asset.
|
||||
*
|
||||
* @param mongo the database handle
|
||||
* @param tenantID the id of the Tenant
|
||||
* @param id the id of the Asset being updated
|
||||
* @param actionCounts the action counts to merge into the Asset
|
||||
*/
|
||||
export async function updateAssetActionCounts(
|
||||
mongo: Db,
|
||||
tenantID: string,
|
||||
id: string,
|
||||
actionCounts: EncodedActionCounts
|
||||
) {
|
||||
const result = await collection(mongo).findOneAndUpdate(
|
||||
{ id, tenant_id: tenantID },
|
||||
// Update all the specific action counts that are associated with each of
|
||||
// the counts.
|
||||
{ $inc: dotize({ action_counts: actionCounts }) },
|
||||
// False to return the updated document instead of the original
|
||||
// document.
|
||||
{ returnOriginal: false }
|
||||
);
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@@ -378,9 +378,9 @@ function applyInputToQuery(input: ConnectionInput, query: Query<Comment>) {
|
||||
* updateCommentActionCounts will update the given comment's action counts.
|
||||
*
|
||||
* @param mongo the database handle
|
||||
* @param tenantID the id of the tenant
|
||||
* @param id the id of the comment being updated
|
||||
* @param actionCounts the action counts to merge into the comment
|
||||
* @param tenantID the id of the Tenant
|
||||
* @param id the id of the Comment being updated
|
||||
* @param actionCounts the action counts to merge into the Comment
|
||||
*/
|
||||
export async function updateCommentActionCounts(
|
||||
mongo: Db,
|
||||
|
||||
@@ -7,7 +7,10 @@ import {
|
||||
createActions,
|
||||
encodeActionCounts,
|
||||
} from "talk-server/models/actions";
|
||||
import { retrieveAsset } from "talk-server/models/asset";
|
||||
import {
|
||||
retrieveAsset,
|
||||
updateAssetActionCounts,
|
||||
} from "talk-server/models/asset";
|
||||
import {
|
||||
Comment,
|
||||
createComment,
|
||||
@@ -85,6 +88,7 @@ export async function create(
|
||||
|
||||
// Insert and handle creating the actions.
|
||||
comment = await addCommentActions(mongo, tenant, comment, inputs);
|
||||
// asse
|
||||
}
|
||||
|
||||
if (input.parent_id) {
|
||||
@@ -196,6 +200,14 @@ async function addCommentActions(
|
||||
actionCounts
|
||||
);
|
||||
|
||||
// Update the Asset with the updated action counts.
|
||||
await updateAssetActionCounts(
|
||||
mongo,
|
||||
tenant.id,
|
||||
comment.asset_id,
|
||||
actionCounts
|
||||
);
|
||||
|
||||
// Check to see if there was an actual comment returned (there should
|
||||
// have been, we just created it!).
|
||||
if (!updatedComment) {
|
||||
|
||||
Reference in New Issue
Block a user