diff --git a/src/core/server/graph/tenant/resolvers/asset.ts b/src/core/server/graph/tenant/resolvers/asset.ts index e5487ba08..98c09ca88 100644 --- a/src/core/server/graph/tenant/resolvers/asset.ts +++ b/src/core/server/graph/tenant/resolvers/asset.ts @@ -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 = { @@ -6,6 +7,7 @@ const Asset: GQLAssetTypeResolver = { ctx.loaders.Comments.forAsset(asset.id, input), // TODO: implement this. isClosed: () => false, + actionCounts: asset => decodeActionCounts(asset.action_counts), }; export default Asset; diff --git a/src/core/server/graph/tenant/schema/schema.graphql b/src/core/server/graph/tenant/schema/schema.graphql index f8c00d98b..c3cdf71b4 100644 --- a/src/core/server/graph/tenant/schema/schema.graphql +++ b/src/core/server/graph/tenant/schema/schema.graphql @@ -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. """ diff --git a/src/core/server/models/asset.ts b/src/core/server/models/asset.ts index 8d412c522..0d15cf6d3 100644 --- a/src/core/server/models/asset.ts +++ b/src/core/server/models/asset.ts @@ -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; +} diff --git a/src/core/server/models/comment.ts b/src/core/server/models/comment.ts index 2fa32efc1..99780bda1 100644 --- a/src/core/server/models/comment.ts +++ b/src/core/server/models/comment.ts @@ -378,9 +378,9 @@ function applyInputToQuery(input: ConnectionInput, query: Query) { * 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, diff --git a/src/core/server/services/comments/index.ts b/src/core/server/services/comments/index.ts index 2bddddc44..7fa33fa75 100644 --- a/src/core/server/services/comments/index.ts +++ b/src/core/server/services/comments/index.ts @@ -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) {