feat: add likes
+ add ability for user to like a posts + add ability for user ot remove a like + add display of reply and like count
This commit is contained in:
Vendored
+2
@@ -15,6 +15,7 @@ import type {
|
||||
FunctionReference,
|
||||
} from "convex/server";
|
||||
import type * as http from "../http";
|
||||
import type * as likes from "../likes";
|
||||
import type * as posts from "../posts";
|
||||
import type * as users from "../users";
|
||||
|
||||
@@ -28,6 +29,7 @@ import type * as users from "../users";
|
||||
*/
|
||||
declare const fullApi: ApiFromModules<{
|
||||
http: typeof http;
|
||||
likes: typeof likes;
|
||||
posts: typeof posts;
|
||||
users: typeof users;
|
||||
}>;
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import { v } from 'convex/values';
|
||||
import { mutation, query } from './_generated/server';
|
||||
import { userQuery } from './users';
|
||||
|
||||
export const addLike = mutation({
|
||||
args: { postId: v.id('posts') },
|
||||
handler: async (ctx, args) => {
|
||||
const clerkUser = await ctx.auth.getUserIdentity();
|
||||
|
||||
if (clerkUser === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await userQuery(ctx, clerkUser.subject);
|
||||
|
||||
if (user === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingLike = await ctx.db
|
||||
.query('likes')
|
||||
.withIndex('by_user_post_id', q =>
|
||||
q.eq('userId', user._id).eq('postId', args.postId)
|
||||
)
|
||||
.unique();
|
||||
|
||||
if (existingLike !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
await ctx.db.insert('likes', { postId: args.postId, userId: user._id });
|
||||
},
|
||||
});
|
||||
|
||||
export const removeLike = mutation({
|
||||
args: { postId: v.id('posts') },
|
||||
handler: async (ctx, args) => {
|
||||
const clerkUser = await ctx.auth.getUserIdentity();
|
||||
|
||||
if (clerkUser === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await userQuery(ctx, clerkUser.subject);
|
||||
|
||||
if (user === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingLike = await ctx.db
|
||||
.query('likes')
|
||||
.withIndex('by_user_post_id', q =>
|
||||
q.eq('userId', user._id).eq('postId', args.postId)
|
||||
)
|
||||
.unique();
|
||||
|
||||
if (existingLike === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
await ctx.db.delete(existingLike._id);
|
||||
},
|
||||
});
|
||||
|
||||
export const getLike = query({
|
||||
args: { postId: v.id('posts') },
|
||||
handler: async (ctx, args) => {
|
||||
const clerkUser = await ctx.auth.getUserIdentity();
|
||||
|
||||
if (clerkUser === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await userQuery(ctx, clerkUser.subject);
|
||||
|
||||
if (user === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
return await ctx.db
|
||||
.query('likes')
|
||||
.withIndex('by_post_user_id', q =>
|
||||
q.eq('postId', args.postId).eq('userId', user._id)
|
||||
)
|
||||
.unique();
|
||||
},
|
||||
});
|
||||
|
||||
export const getPostLikeCount = query({
|
||||
args: { postId: v.id('posts') },
|
||||
handler: async (ctx, args) => {
|
||||
const likes = await ctx.db
|
||||
.query('likes')
|
||||
.withIndex('by_post_user_id', q => q.eq('postId', args.postId))
|
||||
.collect();
|
||||
|
||||
return likes.length;
|
||||
},
|
||||
});
|
||||
@@ -135,3 +135,15 @@ export const getRepliesByParentId = query({
|
||||
return { ...replies, page: repliesWithUserData };
|
||||
},
|
||||
});
|
||||
|
||||
export const getPostReplyCount = query({
|
||||
args: { postId: v.id('posts') },
|
||||
handler: async (ctx, args) => {
|
||||
const replies = await ctx.db
|
||||
.query('posts')
|
||||
.withIndex('by_parent_id', q => q.eq('parentPostId', args.postId))
|
||||
.collect();
|
||||
|
||||
return replies.length;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -42,6 +42,12 @@ export default defineSchema(
|
||||
})
|
||||
.index('by_user_id', ['userId'])
|
||||
.index('by_parent_id', ['parentPostId']),
|
||||
likes: defineTable({
|
||||
postId: v.id('posts'),
|
||||
userId: v.id('users'),
|
||||
})
|
||||
.index('by_user_post_id', ['userId', 'postId'])
|
||||
.index('by_post_user_id', ['postId', 'userId']),
|
||||
},
|
||||
{ schemaValidation: false }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user