Skip to content

Commit

Permalink
check block status before follow update
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 13, 2024
1 parent 1bbbd3c commit 231e45a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
39 changes: 37 additions & 2 deletions src/v2/routes/user/follows/follow/id/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { OpenAPIHono } from "@hono/zod-openapi"
import { followUserByIdRoute } from "./openapi"
import { getConnection } from "@/v2/db/turso"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { and, eq } from "drizzle-orm"
import { userFollowing } from "@/v2/db/schema"
import { and, eq, or } from "drizzle-orm"
import { userFollowing, userBlocked } from "@/v2/db/schema"

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()

Expand Down Expand Up @@ -55,6 +55,41 @@ handler.openapi(followUserByIdRoute, async (ctx) => {
)
}

const [blockedStatus] = await drizzle
.select({
id: userBlocked.blockedId,
blockById: userBlocked.blockedById,
})
.from(userBlocked)
.where(
or(
and(
eq(userBlocked.blockedId, user.id),
eq(userBlocked.blockedById, userId)
),
and(
eq(userBlocked.blockedId, userId),
eq(userBlocked.blockedById, user.id)
)
)
)
.limit(1)

if (blockedStatus) {
const message =
blockedStatus.blockById === user.id
? "You are blocked by this user"
: "You have blocked this user"

return ctx.json(
{
success: false,
message,
},
400
)
}

try {
await drizzle.insert(userFollowing).values({
followerId: user.id,
Expand Down
39 changes: 37 additions & 2 deletions src/v2/routes/user/follows/unfollow/id/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { OpenAPIHono } from "@hono/zod-openapi"
import { unFollowUserByIdRoute } from "./openapi"
import { getConnection } from "@/v2/db/turso"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { userFollowing } from "@/v2/db/schema"
import { and, eq } from "drizzle-orm"
import { userFollowing, userBlocked } from "@/v2/db/schema"
import { and, eq, or } from "drizzle-orm"

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()

Expand Down Expand Up @@ -56,6 +56,41 @@ handler.openapi(unFollowUserByIdRoute, async (ctx) => {
)
}

const [blockedStatus] = await drizzle
.select({
id: userBlocked.blockedId,
blockById: userBlocked.blockedById,
})
.from(userBlocked)
.where(
or(
and(
eq(userBlocked.blockedId, user.id),
eq(userBlocked.blockedById, userId)
),
and(
eq(userBlocked.blockedId, userId),
eq(userBlocked.blockedById, user.id)
)
)
)
.limit(1)

if (blockedStatus) {
const message =
blockedStatus.blockById === user.id
? "You are blocked by this user"
: "You have blocked this user"

return ctx.json(
{
success: false,
message,
},
400
)
}

try {
await drizzle
.delete(userFollowing)
Expand Down

0 comments on commit 231e45a

Please sign in to comment.