Skip to content

Commit

Permalink
update user routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 090856c commit 6a1568c
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 339 deletions.
1 change: 0 additions & 1 deletion src/v2/db/schema/asset/asset-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
text,
// uniqueIndex,
index,
integer,
foreignKey,
} from "drizzle-orm/sqlite-core"
import { authUser } from "../user/user"
Expand Down
1 change: 0 additions & 1 deletion src/v2/db/schema/asset/asset-likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
text,
// uniqueIndex,
index,
integer,
} from "drizzle-orm/sqlite-core"
import { authUser } from "../user/user"
import { asset } from "./asset"
Expand Down
1 change: 0 additions & 1 deletion src/v2/db/schema/tags/asset-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { relations } from "drizzle-orm"
import {
sqliteTable,
text,
integer,
// uniqueIndex,
index,
} from "drizzle-orm/sqlite-core"
Expand Down
194 changes: 96 additions & 98 deletions src/v2/routes/user/follow-user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { and, eq, or } from "drizzle-orm"
Expand All @@ -23,7 +23,7 @@ const followUserByIdResponseSchema = z.object({
})

export const followUserByIdRoute = createRoute({
path: "/{id}",
path: "/{id}/follow",
method: "post",
description: "Follow a user from their ID.",
tags: ["User"],
Expand All @@ -43,113 +43,111 @@ export const followUserByIdRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const FollowUserRoute = (handler: AppHandler) => {
handler.openapi(followUserByIdRoute, async (ctx) => {
const userId = ctx.req.valid("param").id

handler.openapi(followUserByIdRoute, async (ctx) => {
const userId = ctx.req.valid("param").id
const { drizzle } = await getConnection(ctx.env)

const { drizzle } = await getConnection(ctx.env)
const authSessionManager = new AuthSessionManager(ctx)
const { user } = await authSessionManager.validateSession()

const authSessionManager = new AuthSessionManager(ctx)
const { user } = await authSessionManager.validateSession()

if (!user) {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}

if (userId == user.id) {
return ctx.json(
{
success: false,
message: "You cannot follow yourself",
},
400
)
}
const [followStatus] = await drizzle
.select({ id: userFollowing.followerId })
.from(userFollowing)
.where(
and(
eq(userFollowing.followerId, user.id),
eq(userFollowing.followingId, userId)
if (!user) {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
)
.limit(1)
}

if (followStatus) {
return ctx.json(
{
success: false,
message: "You are already following this user",
},
400
)
}

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)
),
if (userId == user.id) {
return ctx.json(
{
success: false,
message: "You cannot follow yourself",
},
400
)
}
const [followStatus] = await drizzle
.select({ id: userFollowing.followerId })
.from(userFollowing)
.where(
and(
eq(userBlocked.blockedId, userId),
eq(userBlocked.blockedById, user.id)
eq(userFollowing.followerId, user.id),
eq(userFollowing.followingId, userId)
)
)
)
.limit(1)

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

return ctx.json(
{
success: false,
message,
},
400
)
}
if (followStatus) {
return ctx.json(
{
success: false,
message: "You are already following this user",
},
400
)
}

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,
followingId: userId,
createdAt: new Date().toISOString(),
})
} catch (e) {
return ctx.json(
{
success: false,
message: "Failed to follow user.",
},
500
)
}

try {
await drizzle.insert(userFollowing).values({
followerId: user.id,
followingId: userId,
createdAt: new Date().toISOString(),
})
} catch (e) {
return ctx.json(
{
success: false,
message: "Failed to follow user.",
success: true,
},
500
200
)
}

return ctx.json(
{
success: true,
},
200
)
})

export default handler
})
}
62 changes: 30 additions & 32 deletions src/v2/routes/user/get-user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { eq } from "drizzle-orm"
import { authUser } from "@/v2/db/schema"
Expand Down Expand Up @@ -56,37 +56,35 @@ const getUserByIdRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const GetUserByIdRoute = (handler: AppHandler) => {
handler.openapi(getUserByIdRoute, async (ctx) => {
const userId = ctx.req.valid("param").id

handler.openapi(getUserByIdRoute, async (ctx) => {
const userId = ctx.req.valid("param").id
const { drizzle } = await getConnection(ctx.env)

const { drizzle } = await getConnection(ctx.env)
const [user] = await drizzle
.select({
id: authUser.id,
avatarUrl: authUser.avatarUrl,
displayName: authUser.displayName,
username: authUser.username,
usernameColour: authUser.usernameColour,
pronouns: authUser.pronouns,
verified: authUser.verified,
bio: authUser.bio,
dateJoined: authUser.dateJoined,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
.where(eq(authUser.id, userId))

const [user] = await drizzle
.select({
id: authUser.id,
avatarUrl: authUser.avatarUrl,
displayName: authUser.displayName,
username: authUser.username,
usernameColour: authUser.usernameColour,
pronouns: authUser.pronouns,
verified: authUser.verified,
bio: authUser.bio,
dateJoined: authUser.dateJoined,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
.where(eq(authUser.id, userId))

return ctx.json(
{
success: true,
user,
},
200
)
})

export default handler
return ctx.json(
{
success: true,
user,
},
200
)
})
}
26 changes: 14 additions & 12 deletions src/v2/routes/user/handler.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { OpenAPIHono } from "@hono/zod-openapi"

import GetUserRoute from "./get-user"
import SearchUserRoute from "./search-users"
import { GetUserByIdRoute } from "./get-user"
import { SearchUsersByUsernameRoute } from "./search-users"

import FollowUserRoute from "./follow-user"
import UnfollowUserRoute from "./unfollow-user"
import { ViewUsersFollowersRoute } from "./user-followers"
import { ViewUsersFollowingRoute } from "./user-following"

import UserFollowingRoute from "./user-following"
import UserFollowersRoute from "./user-followers"
import { FollowUserRoute } from "./follow-user"
import { UnfollowUserRoute } from "./unfollow-user"

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

handler.route("/get", GetUserRoute)
handler.route("/search", SearchUserRoute)
handler.route("/follow", FollowUserRoute)
handler.route("/unfollow", UnfollowUserRoute)
handler.route("/following", UserFollowingRoute)
handler.route("/followers", UserFollowersRoute)
GetUserByIdRoute(handler)
SearchUsersByUsernameRoute(handler)

ViewUsersFollowersRoute(handler)
ViewUsersFollowingRoute(handler)

FollowUserRoute(handler)
UnfollowUserRoute(handler)

export default handler
Loading

0 comments on commit 6a1568c

Please sign in to comment.