feat: websockets (comment posting + liking)
This commit is contained in:
parent
251609d7b8
commit
3f137e5c3c
15 changed files with 2200 additions and 5 deletions
|
|
@ -3,6 +3,7 @@ import { error, json } from '@sveltejs/kit';
|
|||
import { db } from '$lib/server/db';
|
||||
import { comment, coin, user, commentLike } from '$lib/server/db/schema';
|
||||
import { eq, and, desc, sql } from 'drizzle-orm';
|
||||
import { redis } from '$lib/server/redis';
|
||||
|
||||
export async function GET({ params, request }) {
|
||||
const session = await auth.api.getSession({
|
||||
|
|
@ -117,6 +118,14 @@ export async function POST({ request, params }) {
|
|||
.where(eq(comment.id, newComment.id))
|
||||
.limit(1);
|
||||
|
||||
await redis.publish(
|
||||
`comments:${normalizedSymbol}`,
|
||||
JSON.stringify({
|
||||
type: 'new_comment',
|
||||
data: commentWithUser
|
||||
})
|
||||
);
|
||||
|
||||
return json({ comment: commentWithUser });
|
||||
} catch (e) {
|
||||
console.error('Error creating comment:', e);
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { comment, commentLike, coin } from '$lib/server/db/schema';
|
|||
import { eq, and, sql } from 'drizzle-orm';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { auth } from '$lib/auth';
|
||||
import { redis } from '$lib/server/redis';
|
||||
|
||||
export const POST: RequestHandler = async ({ request, params }) => {
|
||||
const session = await auth.api.getSession({
|
||||
|
|
@ -54,6 +55,24 @@ export const POST: RequestHandler = async ({ request, params }) => {
|
|||
.where(eq(comment.id, commentId));
|
||||
});
|
||||
|
||||
const [updatedComment] = await db
|
||||
.select({ likesCount: comment.likesCount })
|
||||
.from(comment)
|
||||
.where(eq(comment.id, commentId));
|
||||
|
||||
await redis.publish(
|
||||
`comments:${coinSymbol!.toUpperCase()}`,
|
||||
JSON.stringify({
|
||||
type: 'comment_liked',
|
||||
data: {
|
||||
commentId: Number(commentId),
|
||||
likesCount: updatedComment.likesCount,
|
||||
isLikedByUser: true,
|
||||
userId
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Failed to like comment:', error);
|
||||
|
|
@ -112,6 +131,24 @@ export const DELETE: RequestHandler = async ({ request, params }) => {
|
|||
.where(eq(comment.id, commentId));
|
||||
});
|
||||
|
||||
const [updatedComment] = await db
|
||||
.select({ likesCount: comment.likesCount })
|
||||
.from(comment)
|
||||
.where(eq(comment.id, commentId));
|
||||
|
||||
await redis.publish(
|
||||
`comments:${coinSymbol.toUpperCase()}`,
|
||||
JSON.stringify({
|
||||
type: 'comment_liked',
|
||||
data: {
|
||||
commentId: Number(commentId),
|
||||
likesCount: updatedComment.likesCount,
|
||||
isLikedByUser: false,
|
||||
userId
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Failed to unlike comment:', error);
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@
|
|||
{#if leaderboardData.biggestLosers.length === 0}
|
||||
<div class="py-8 text-center">
|
||||
<p class="text-muted-foreground">
|
||||
Everyone's in profit today! 📈 (This won't last...)
|
||||
No major losses recorded today
|
||||
</p>
|
||||
</div>
|
||||
{:else}
|
||||
|
|
|
|||
Reference in a new issue