Skip to content

Commit e2440cd

Browse files
committed
refactor: update user profile photo handling and improve article feed query structure
1 parent 020b88e commit e2440cd

File tree

11 files changed

+47
-33
lines changed

11 files changed

+47
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"recharts": "^2.15.1",
6666
"schema-dts": "^1.1.5",
6767
"sonner": "^2.0.5",
68-
"sqlkit": "^1.0.15",
68+
"sqlkit": "^1.0.17",
6969
"tailwind-merge": "^3.0.2",
7070
"tw-animate-css": "^1.2.4",
7171
"use-immer": "^0.11.0",

src/app/[username]/(profile-page)/articles/UserArticleFeed.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@ const UserArticleFeed: React.FC<UserArticleFeedProps> = ({ userId }) => {
1616
const feedInfiniteQuery = useInfiniteQuery({
1717
queryKey: ["user-article-feed", userId],
1818
queryFn: ({ pageParam }) =>
19-
articleActions.userArticleFeed({
20-
user_id: userId,
21-
limit: 5,
22-
page: pageParam,
23-
}),
19+
articleActions.userArticleFeed(
20+
{
21+
user_id: userId,
22+
limit: 5,
23+
page: pageParam,
24+
},
25+
[
26+
"id",
27+
"title",
28+
"handle",
29+
"cover_image",
30+
"body",
31+
"created_at",
32+
"excerpt",
33+
]
34+
),
2435
initialPageParam: 1,
2536
getNextPageParam: (lastPage) => {
2637
const _page = lastPage?.meta?.currentPage ?? 1;
@@ -46,6 +57,8 @@ const UserArticleFeed: React.FC<UserArticleFeedProps> = ({ userId }) => {
4657
</div>
4758
)}
4859

60+
{/* <pre>{JSON.stringify(feedArticles, null, 2)}</pre> */}
61+
4962
{feedArticles?.map((article) => (
5063
<ArticleCard
5164
key={article?.id}
@@ -59,7 +72,7 @@ const UserArticleFeed: React.FC<UserArticleFeedProps> = ({ userId }) => {
5972
avatar: getFileUrl(article?.user?.profile_photo) ?? "",
6073
username: article?.user?.username ?? "",
6174
}}
62-
publishedAt={article?.created_at.toDateString() ?? ""}
75+
publishedAt={article?.created_at?.toDateString() ?? ""}
6376
readingTime={readingTime(article?.body ?? "")}
6477
/>
6578
))}

src/app/[username]/(profile-page)/articles/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { getUserByUsername } from "@/backend/services/user.action";
12
import { sanitizedUsername } from "@/lib/utils";
23
import React from "react";
34
import UserArticleFeed from "./UserArticleFeed";
4-
import { getUserByUsername } from "@/backend/services/user.action";
55

66
interface PageProps {
77
params: Promise<{ username: string }>;
@@ -13,6 +13,7 @@ const Page: React.FC<PageProps> = async ({ params }) => {
1313

1414
return (
1515
<main className="border rounded-bl-2xl rounded-br-2xl md:col-span-9 col-span-full">
16+
{/* <pre>{JSON.stringify(profile, null, 2)}</pre> */}
1617
<UserArticleFeed userId={profile?.id!} />
1718
</main>
1819
);

src/backend/persistence/persistence-repositories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { pgClient } from "./clients";
1616
import { DatabaseTableName } from "./persistence-contracts";
1717

1818
const repositoryConfig = {
19-
// logging: true,
19+
logging: true,
2020
};
2121

2222
const userRepository = new Repository<User>(

src/backend/services/action-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface ISessionUser {
1515
name: string;
1616
username: string;
1717
email: string;
18-
profile_photo: string;
18+
profile_photo_url: string;
1919
}
2020

2121
export type SessionResult =

src/backend/services/article.actions.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ export async function userArticleFeed(
359359
const input = await ArticleRepositoryInput.userFeedInput.parseAsync(_input);
360360

361361
const response = await persistenceRepository.article.paginate({
362+
operationName: "userArticleFeed",
362363
where: and(
363364
eq("is_published", true),
364365
neq("approved_at", null),
@@ -367,19 +368,12 @@ export async function userArticleFeed(
367368
page: input.page,
368369
limit: input.limit,
369370
orderBy: [desc("published_at")],
370-
columns: columns ?? [
371-
"id",
372-
"title",
373-
"handle",
374-
"cover_image",
375-
"body",
376-
"created_at",
377-
"excerpt",
378-
],
371+
columns,
379372
joins: [
380373
{
381-
as: "tags",
374+
as: "user",
382375
table: DatabaseTableName.users,
376+
type: "left",
383377
on: {
384378
foreignField: "id",
385379
localField: "author_id",

src/backend/services/session.actions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { persistenceRepository } from "../persistence/persistence-repositories";
1111
import { handleActionException } from "./RepositoryException";
1212
import { SessionResult, USER_SESSION_KEY } from "./action-type";
1313
import { UserSessionInput } from "./inputs/session.input";
14+
import getFileUrl from "@/utils/getFileUrl";
1415

1516
/**
1617
* Creates a new login session for a user and sets a session cookie.
@@ -64,6 +65,7 @@ export const validateSessionToken = async (
6465
token: string
6566
): Promise<SessionResult> => {
6667
const [session] = await persistenceRepository.userSession.find({
68+
operationName: "validateSessionToken/userSession.find",
6769
limit: 1,
6870
where: eq("token", token),
6971
columns: ["id", "user_id", "token", "device"],
@@ -73,13 +75,15 @@ export const validateSessionToken = async (
7375
}
7476

7577
await persistenceRepository.userSession.update({
78+
operationName: "validateSessionToken/userSession.update--",
7679
where: eq("id", session.id),
7780
data: {
7881
last_action_at: new Date(),
7982
},
8083
});
8184

8285
const [user] = await persistenceRepository.user.find({
86+
operationName: "validateSessionToken/user.find",
8387
limit: 1,
8488
where: eq("id", session.user_id),
8589
columns: ["id", "name", "username", "email", "profile_photo"],
@@ -96,7 +100,7 @@ export const validateSessionToken = async (
96100
name: user?.name,
97101
username: user?.username,
98102
email: user?.email,
99-
profile_photo: user?.profile_photo,
103+
profile_photo_url: getFileUrl(user?.profile_photo),
100104
},
101105
};
102106
};

src/backend/services/user.action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export async function getUserByUsername(
147147
): Promise<User | null> {
148148
try {
149149
const [user] = await persistenceRepository.user.find({
150+
operationName: "getUserByUsername",
150151
where: eq("username", username),
151152
limit: 1,
152153
columns: columns ? columns : undefined,

src/components/ArticleCard.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ const ArticleCard = ({
111111
)}
112112
</div>
113113

114-
<pre>{JSON.stringify(author, null, 2)}</pre>
115-
116114
<div className="mt-4 flex items-center justify-between">
117115
<ResourceReaction resource_type="ARTICLE" resource_id={id} />
118116
<ResourceBookmark resource_type="ARTICLE" resource_id={id} />

src/components/Navbar/AuthenticatedUserMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const AuthenticatedUserMenu = () => {
3333
<DropdownMenu>
3434
<DropdownMenuTrigger>
3535
<Avatar>
36-
<AvatarImage src={authSession?.user?.profile_photo ?? ""} />
36+
<AvatarImage src={authSession?.user?.profile_photo_url ?? ""} />
3737
<AvatarFallback>{authSession?.user?.name.charAt(0)}</AvatarFallback>
3838
</Avatar>
3939
</DropdownMenuTrigger>

0 commit comments

Comments
 (0)