-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add user blocking system, username editing, and mention display #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1001add
feat: add user blocking system, username editing, and mention display
BuckyMcYolo 8cf4d7f
fix: fixed block users various bugs
BuckyMcYolo 887b18f
feat: add privacy settings system and profile popover DM button
BuckyMcYolo 00d39ca
fix: fix various user block settings page
BuckyMcYolo ce5f393
Update index.ts
BuckyMcYolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { and, db, desc, eq, or, schema } from "@repo/db" | ||
| import * as HttpStatusCodes from "@/lib/helpers/http/status-codes" | ||
| import type { AppRouteHandler } from "@/lib/types/app-types" | ||
| import type { | ||
| BlockUserRoute, | ||
| ListBlockedUsersRoute, | ||
| UnblockUserRoute, | ||
| } from "./routes" | ||
|
BuckyMcYolo marked this conversation as resolved.
|
||
|
|
||
| export const blockUser: AppRouteHandler<BlockUserRoute> = async (c) => { | ||
| const currentUser = c.var.user | ||
| const { userId: targetUserId } = c.req.valid("json") | ||
|
|
||
| if (currentUser.id === targetUserId) { | ||
| return c.json( | ||
| { success: false, message: "Cannot block yourself" }, | ||
| HttpStatusCodes.BAD_REQUEST | ||
| ) | ||
| } | ||
|
|
||
| // Check target user exists | ||
| const targetUser = await db | ||
| .select({ id: schema.user.id }) | ||
| .from(schema.user) | ||
| .where(eq(schema.user.id, targetUserId)) | ||
| .limit(1) | ||
| .then((rows) => rows[0]) | ||
|
|
||
| if (!targetUser) { | ||
| return c.json( | ||
| { success: false, message: "User not found" }, | ||
| HttpStatusCodes.NOT_FOUND | ||
| ) | ||
| } | ||
|
|
||
| // Atomically: insert block + remove any ally relationship | ||
| const result = await db.transaction(async (tx) => { | ||
| const inserted = await tx | ||
| .insert(schema.userBlock) | ||
| .values({ | ||
| blockerId: currentUser.id, | ||
| blockedId: targetUserId, | ||
| }) | ||
| .onConflictDoNothing() | ||
| .returning() | ||
|
|
||
| if (inserted.length === 0) { | ||
| return { alreadyBlocked: true } | ||
| } | ||
|
|
||
| // Delete any ally request between the two users (in either direction) | ||
| await tx | ||
| .delete(schema.allyRequest) | ||
| .where( | ||
| or( | ||
| and( | ||
| eq(schema.allyRequest.senderId, currentUser.id), | ||
| eq(schema.allyRequest.receiverId, targetUserId) | ||
| ), | ||
| and( | ||
| eq(schema.allyRequest.senderId, targetUserId), | ||
| eq(schema.allyRequest.receiverId, currentUser.id) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| return { alreadyBlocked: false } | ||
| }) | ||
|
|
||
| if (result.alreadyBlocked) { | ||
| return c.json( | ||
| { success: false, message: "User is already blocked" }, | ||
| HttpStatusCodes.BAD_REQUEST | ||
| ) | ||
| } | ||
|
|
||
| return c.json({ success: true }, HttpStatusCodes.OK) | ||
| } | ||
|
|
||
| export const unblockUser: AppRouteHandler<UnblockUserRoute> = async (c) => { | ||
| const currentUser = c.var.user | ||
| const { userId: targetUserId } = c.req.valid("param") | ||
|
|
||
| const deleted = await db | ||
| .delete(schema.userBlock) | ||
| .where( | ||
| and( | ||
| eq(schema.userBlock.blockerId, currentUser.id), | ||
| eq(schema.userBlock.blockedId, targetUserId) | ||
| ) | ||
| ) | ||
| .returning() | ||
|
|
||
| if (deleted.length === 0) { | ||
| return c.json( | ||
| { success: false, message: "Block not found" }, | ||
| HttpStatusCodes.NOT_FOUND | ||
| ) | ||
| } | ||
|
|
||
| return c.json({ success: true }, HttpStatusCodes.OK) | ||
| } | ||
|
|
||
| export const listBlockedUsers: AppRouteHandler<ListBlockedUsersRoute> = async ( | ||
| c | ||
| ) => { | ||
| const currentUser = c.var.user | ||
|
|
||
| const blocks = await db | ||
| .select({ | ||
| id: schema.user.id, | ||
| name: schema.user.name, | ||
| username: schema.user.username, | ||
| displayUsername: schema.user.displayUsername, | ||
| image: schema.user.image, | ||
| blockedAt: schema.userBlock.createdAt, | ||
| }) | ||
| .from(schema.userBlock) | ||
| .innerJoin(schema.user, eq(schema.userBlock.blockedId, schema.user.id)) | ||
| .where(eq(schema.userBlock.blockerId, currentUser.id)) | ||
| .orderBy(desc(schema.userBlock.createdAt)) | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return c.json( | ||
| { | ||
| blockedUsers: blocks.map((b) => ({ | ||
| id: b.id, | ||
| name: b.name, | ||
| username: b.username, | ||
| displayUsername: b.displayUsername, | ||
| image: b.image, | ||
| blockedAt: b.blockedAt.toISOString(), | ||
| })), | ||
| }, | ||
| HttpStatusCodes.OK | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { createRouter } from "@/lib/helpers/app/create-app" | ||
| import * as handlers from "@/routes/v1/blocks/handlers" | ||
| import * as routes from "@/routes/v1/blocks/routes" | ||
|
|
||
| const blocksRouter = createRouter() | ||
| .openapi(routes.blockUser, handlers.blockUser) | ||
| .openapi(routes.unblockUser, handlers.unblockUser) | ||
| .openapi(routes.listBlockedUsers, handlers.listBlockedUsers) | ||
|
|
||
| export default blocksRouter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { createRoute } from "@hono/zod-openapi" | ||
| import * as HttpStatusCodes from "@/lib/helpers/http/status-codes" | ||
| import jsonContent from "@/lib/helpers/openapi/json-content" | ||
| import { | ||
| badRequestSchema, | ||
| internalServerErrorSchema, | ||
| notFoundSchema, | ||
| unauthorizedSchema, | ||
| } from "@/lib/helpers/openapi/schemas" | ||
| import { sessionAuthMiddleware } from "@/middleware/session-auth" | ||
| import { | ||
| blockUserBodySchema, | ||
| blockUserIdParamsSchema, | ||
| blockUserResponseSchema, | ||
| listBlockedUsersResponseSchema, | ||
| unblockUserResponseSchema, | ||
| } from "./schema" | ||
|
|
||
| export const blockUser = createRoute({ | ||
| path: "/blocks", | ||
| method: "post", | ||
| summary: "Block a user", | ||
| description: | ||
| "Blocks a user. Removes any existing ally relationship between the users.", | ||
| tags: ["Blocks"], | ||
| middleware: [sessionAuthMiddleware] as const, | ||
| request: { | ||
| body: jsonContent({ | ||
| schema: blockUserBodySchema, | ||
| description: "User to block", | ||
| }), | ||
| }, | ||
| responses: { | ||
| [HttpStatusCodes.OK]: jsonContent({ | ||
| schema: blockUserResponseSchema, | ||
| description: "User blocked", | ||
| }), | ||
| [HttpStatusCodes.BAD_REQUEST]: badRequestSchema, | ||
| [HttpStatusCodes.UNAUTHORIZED]: unauthorizedSchema, | ||
| [HttpStatusCodes.NOT_FOUND]: notFoundSchema, | ||
| [HttpStatusCodes.INTERNAL_SERVER_ERROR]: internalServerErrorSchema, | ||
| }, | ||
| }) | ||
|
|
||
| export type BlockUserRoute = typeof blockUser | ||
|
|
||
| export const unblockUser = createRoute({ | ||
| path: "/blocks/{userId}", | ||
| method: "delete", | ||
| summary: "Unblock a user", | ||
| description: "Removes a block on the specified user.", | ||
| tags: ["Blocks"], | ||
| middleware: [sessionAuthMiddleware] as const, | ||
| request: { | ||
| params: blockUserIdParamsSchema, | ||
| }, | ||
| responses: { | ||
| [HttpStatusCodes.OK]: jsonContent({ | ||
| schema: unblockUserResponseSchema, | ||
| description: "User unblocked", | ||
| }), | ||
| [HttpStatusCodes.UNAUTHORIZED]: unauthorizedSchema, | ||
| [HttpStatusCodes.NOT_FOUND]: notFoundSchema, | ||
| [HttpStatusCodes.INTERNAL_SERVER_ERROR]: internalServerErrorSchema, | ||
| }, | ||
| }) | ||
|
|
||
| export type UnblockUserRoute = typeof unblockUser | ||
|
|
||
| export const listBlockedUsers = createRoute({ | ||
| path: "/blocks", | ||
| method: "get", | ||
| summary: "List blocked users", | ||
| description: "Returns all users blocked by the current user.", | ||
| tags: ["Blocks"], | ||
| middleware: [sessionAuthMiddleware] as const, | ||
| responses: { | ||
| [HttpStatusCodes.OK]: jsonContent({ | ||
| schema: listBlockedUsersResponseSchema, | ||
| description: "List of blocked users", | ||
| }), | ||
| [HttpStatusCodes.UNAUTHORIZED]: unauthorizedSchema, | ||
| [HttpStatusCodes.INTERNAL_SERVER_ERROR]: internalServerErrorSchema, | ||
| }, | ||
| }) | ||
|
|
||
| export type ListBlockedUsersRoute = typeof listBlockedUsers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { z } from "@hono/zod-openapi" | ||
|
|
||
| // ── Path Params ────────────────────────────────────────── | ||
|
|
||
| export const blockUserIdParamsSchema = z.object({ | ||
| userId: z | ||
| .string() | ||
| .uuid() | ||
| .openapi({ | ||
| param: { name: "userId", in: "path", required: true }, | ||
| example: "00000000-0000-0000-0000-000000000000", | ||
| }), | ||
| }) | ||
|
|
||
| // ── Request Schemas ────────────────────────────────────── | ||
|
|
||
| export const blockUserBodySchema = z.object({ | ||
| userId: z.string().uuid(), | ||
| }) | ||
|
|
||
| // ── Response Schemas ────────────────────────────────────── | ||
|
|
||
| const blockedUserSchema = z.object({ | ||
| id: z.string().uuid(), | ||
| name: z.string(), | ||
| username: z.string().nullable(), | ||
| displayUsername: z.string().nullable(), | ||
| image: z.string().nullable(), | ||
| blockedAt: z.string().datetime(), | ||
| }) | ||
|
|
||
| export const blockUserResponseSchema = z.object({ | ||
| success: z.literal(true), | ||
| }) | ||
|
|
||
| export const unblockUserResponseSchema = z.object({ | ||
| success: z.literal(true), | ||
| }) | ||
|
|
||
| export const listBlockedUsersResponseSchema = z.object({ | ||
| blockedUsers: z.array(blockedUserSchema), | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.