Skip to content

Commit

Permalink
track forum and proposal events in the public api (#3599)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcasey authored Feb 7, 2024
1 parent 94cf6df commit f15fce8
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 140 deletions.
20 changes: 8 additions & 12 deletions lib/forums/comments/createPostComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@ export async function createPostComment({
}
});

const category = post.category;

if (category) {
trackUserAction('create_comment', {
categoryName: category.name,
commentedOn: parentId === postId ? 'post' : 'comment',
postId,
resourceId: comment.id,
spaceId: post.spaceId,
userId
});
}
trackUserAction('create_comment', {
categoryName: post.category.name,
commentedOn: parentId === postId ? 'post' : 'comment',
postId,
resourceId: comment.id,
spaceId: post.spaceId,
userId
});

return comment;
}
2 changes: 1 addition & 1 deletion lib/forums/comments/deletePostComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function deletePostComment({
postId: postComment.post.id,
resourceId: commentId,
spaceId: postComment.post.spaceId,
userId: postComment.createdBy
userId
});
}

Expand Down
18 changes: 7 additions & 11 deletions lib/forums/posts/voteForumComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ export async function voteForumComment({
}
});

const category = post.category;

if (category) {
trackUserAction(upvoted ? 'upvote_comment' : 'downvote_comment', {
resourceId: commentId,
spaceId: post.spaceId,
userId,
categoryName: category.name,
postId: post.id
});
}
trackUserAction(upvoted ? 'upvote_comment' : 'downvote_comment', {
resourceId: commentId,
spaceId: post.spaceId,
userId,
categoryName: post.category.name,
postId: post.id
});

const commentVote = await prisma.postCommentUpDownVote.upsert({
create: {
Expand Down
3 changes: 3 additions & 0 deletions lib/metrics/mixpanel/interfaces/ProposalEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ export interface ProposalEventMap {
new_proposal_stage: ProposalStatusUpdatedEvent;
new_vote_created: ProposalVoteEvent;
user_cast_a_vote: ProposalVoteEvent;
create_proposal_comment: ProposalEvent;
upvote_proposal_comment: ProposalEvent;
downvote_proposal_comment: ProposalEvent;
}
26 changes: 13 additions & 13 deletions pages/api/v1/forum/comments/[commentId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import { log } from '@charmverse/core/log';
import { prisma } from '@charmverse/core/prisma-client';
import type { NextApiRequest, NextApiResponse } from 'next';

import { deletePostComment } from 'lib/forums/comments/deletePostComment';
import { requireKeys } from 'lib/middleware';
import { parseMarkdown } from 'lib/prosemirror/plugins/markdown/parseMarkdown';
import { superApiHandler } from 'lib/public-api/handler';
import { getUserProfile, userProfileSelect } from 'lib/public-api/searchUserProfile';
import { withSessionRoute } from 'lib/session/withSession';
import { InvalidInputError } from 'lib/utilities/errors';

import type { PublicApiPostComment } from '../../posts/[postId]/comments';

const handler = superApiHandler();

handler.delete(deletePostComment).put(requireKeys(['contentMarkdown'], 'body'), updatePostComment);

/**
* @swagger
* /forum/comments/{commentId}:
Expand All @@ -29,8 +27,15 @@ handler.delete(deletePostComment).put(requireKeys(['contentMarkdown'], 'body'),
* description: ID of the comment to update
*
*/
async function deletePostComment(req: NextApiRequest, res: NextApiResponse) {

const handler = superApiHandler();
handler.delete(deletePostComment).put(requireKeys(['contentMarkdown'], 'body'), updatePostCommentEndpoint);

async function updatePostCommentEndpoint(req: NextApiRequest, res: NextApiResponse) {
// This should never be undefined, but adding this safeguard for future proofing
if (!req.query.commentId) {
throw new InvalidInputError('Comment Id is undefined');
}

const result = await prisma.postComment.findFirstOrThrow({
where: {
Expand All @@ -44,17 +49,12 @@ async function deletePostComment(req: NextApiRequest, res: NextApiResponse) {
}
},
select: {
id: true
id: true,
createdBy: true
}
});

if (result) {
await prisma.postComment.delete({
where: {
id: result.id
}
});
}
await deletePostComment({ commentId: result.id, userId: result.createdBy });

log.debug('[public-api] Deleted comment', { query: req.query, result });

Expand Down
40 changes: 12 additions & 28 deletions pages/api/v1/forum/comments/[commentId]/upvote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { UnauthorisedActionError } from '@charmverse/core/errors';
import { prisma } from '@charmverse/core/prisma-client';
import type { NextApiRequest, NextApiResponse } from 'next';

import { voteForumComment } from 'lib/forums/posts/voteForumComment';
import { requireKeys } from 'lib/middleware';
import { generateMarkdown } from 'lib/prosemirror/plugins/markdown/generateMarkdown';
import { superApiHandler } from 'lib/public-api/handler';
import { userProfileSelect, getUserProfile } from 'lib/public-api/searchUserProfile';
import { withSessionRoute } from 'lib/session/withSession';
import { InvalidInputError } from 'lib/utilities/errors';

import type { PublicApiPostComment } from '../../posts/[postId]/comments/index';

Expand Down Expand Up @@ -50,6 +52,10 @@ async function upvoteOnComment(req: NextApiRequest, res: NextApiResponse<PublicA
const userId = req.body.userId as string;
const commentId = req.query.commentId as string;

if (!userId || !commentId) {
throw new InvalidInputError('User ID or comment ID is undefined');
}

const { postId } = await prisma.postComment.findFirstOrThrow({
where: {
id: commentId,
Expand Down Expand Up @@ -77,34 +83,12 @@ async function upvoteOnComment(req: NextApiRequest, res: NextApiResponse<PublicA
throw new UnauthorisedActionError('User does not have access to this space');
}

if (req.body.upvoted === null) {
await prisma.postCommentUpDownVote.delete({
where: {
createdBy_commentId: {
commentId,
createdBy: userId
}
}
});
} else {
await prisma.postCommentUpDownVote.upsert({
where: {
createdBy_commentId: {
commentId,
createdBy: userId
}
},
create: {
createdBy: userId,
upvoted: req.body.upvoted,
comment: { connect: { id: commentId } },
post: { connect: { id: postId } }
},
update: {
upvoted: req.body.upvoted
}
});
}
await voteForumComment({
upvoted: !!req.body.upvoted,
commentId,
postId,
userId
});

const comment = await prisma.postComment.findUniqueOrThrow({
where: {
Expand Down
30 changes: 17 additions & 13 deletions pages/api/v1/forum/posts/[postId]/comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PostComment } from '@charmverse/core/prisma-client';
import { prisma } from '@charmverse/core/prisma-client';
import type { NextApiRequest, NextApiResponse } from 'next';

import { createPostComment } from 'lib/forums/comments/createPostComment';
import type { PostCommentVote } from 'lib/forums/comments/interface';
import { InvalidStateError, requireApiKey, requireKeys, requireSuperApiKey } from 'lib/middleware';
import { generateMarkdown } from 'lib/prosemirror/plugins/markdown/generateMarkdown';
Expand All @@ -17,7 +18,12 @@ const handler = defaultHandler();

handler.get(requireApiKey, logApiRequest, getPostComments);

handler.post(requireSuperApiKey, logApiRequest, requireKeys(['userId', 'contentMarkdown'], 'body'), createPostComment);
handler.post(
requireSuperApiKey,
logApiRequest,
requireKeys(['userId', 'contentMarkdown'], 'body'),
createPostCommentEndpoint
);

/**
* @swagger
Expand Down Expand Up @@ -287,7 +293,7 @@ async function getPostComments(req: NextApiRequest, res: NextApiResponse<PublicA
* $ref: '#/components/schemas/ForumPostComment'
*
*/
async function createPostComment(req: NextApiRequest, res: NextApiResponse<PublicApiPostComment>) {
async function createPostCommentEndpoint(req: NextApiRequest, res: NextApiResponse<PublicApiPostComment>) {
// This should never be undefined, but adding this safeguard for future proofing
if (!req.spaceIdRange) {
throw new InvalidStateError('Space ID is undefined');
Expand Down Expand Up @@ -322,14 +328,12 @@ async function createPostComment(req: NextApiRequest, res: NextApiResponse<Publi

const commentContent = parseMarkdown(req.body.contentMarkdown);

const postComment = await prisma.postComment.create({
data: {
post: { connect: { id: postId } },
parentId: req.body.parentId,
contentText: req.body.contentMarkdown,
user: { connect: { id: userId } },
content: commentContent
}
const result = await createPostComment({
postId,
userId,
content: commentContent,
contentText: req.body.contentMarkdown,
parentId: req.body.parentId
});

const user = await prisma.user.findUniqueOrThrow({
Expand All @@ -343,16 +347,16 @@ async function createPostComment(req: NextApiRequest, res: NextApiResponse<Publi
});

const apiComment: PublicApiPostComment = {
id: postComment.id,
createdAt: postComment.createdAt.toISOString(),
id: result.id,
createdAt: result.createdAt.toISOString(),
content: {
markdown: req.body.contentMarkdown,
text: req.body.contentMarkdown
},
author: getUserProfile(user),
downvotes: 0,
upvotes: 0,
parentId: postComment.parentId,
parentId: result.parentId,
children: []
};

Expand Down
Loading

0 comments on commit f15fce8

Please sign in to comment.