Skip to content

Commit

Permalink
feat: jump to profile when click username
Browse files Browse the repository at this point in the history
  • Loading branch information
shaokeyibb committed Sep 9, 2024
1 parent c6afdc5 commit 199de93
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions cloudflare-workers/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS `commenters`(
`oauth_user_id` TEXT NOT NULL,
`name` TEXT NOT NULL,
`avatar_url` TEXT NOT NULL,
`profile_url` TEXT,
UNIQUE(`oauth_provider`, `oauth_user_id`)
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_oauth_provider_oauth_user_id ON `commenters`(`oauth_provider`, `oauth_user_id`);
Expand Down
7 changes: 4 additions & 3 deletions cloudflare-workers/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ export async function getUserOfComment(env: Env, comment_id: number): Promise<Co
.first();
}

export async function registerUser(env: Env, name: string, oauth_provider: string, oauth_user_id: string, avatar_url: string) {
export async function registerUser(env: Env, name: string, oauth_provider: string, oauth_user_id: string, avatar_url: string, profile_url?: string) {
const db = env.DB;

await db
.prepare(
'INSERT INTO commenters (name, oauth_provider, oauth_user_id, avatar_url) VALUES (?, ?, ?, ?) ON CONFLICT(oauth_provider, oauth_user_id) DO UPDATE SET name = excluded.name',
'INSERT INTO commenters (name, oauth_provider, oauth_user_id, avatar_url, profile_url) VALUES (?, ?, ?, ?, ?) ON CONFLICT(oauth_provider, oauth_user_id) DO UPDATE SET name = excluded.name',
)
.bind(name, oauth_provider, oauth_user_id, avatar_url)
.bind(name, oauth_provider, oauth_user_id, avatar_url, profile_url)
.run();
}

Expand Down Expand Up @@ -107,6 +107,7 @@ export async function getComment(env: Env, req: GetComment): Promise<GetCommentR
oauth_provider: comment.oauth_provider as string,
oauth_user_id: comment.oauth_user_id as string,
avatar_url: comment.avatar_url as string,
profile_url: (comment.profile_url as string | null) ?? undefined,
},
comment: comment.comment as string,
created_time: comment.created_time as string,
Expand Down
2 changes: 1 addition & 1 deletion cloudflare-workers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ router.get('/oauth/callback', async (req, env, ctx) => {
env.OAUTH_JWT_SECRET,
);

await registerUser(env, userInfo.name ?? userInfo.login, 'github', userInfo.id + '', userInfo.avatar_url);
await registerUser(env, userInfo.name ?? userInfo.login, 'github', userInfo.id + '', userInfo.avatar_url, `https://github.com/${userInfo.login}`);

const redirectUrl = new URL(state.redirect);
redirectUrl.searchParams.append('oauth_token', jwt);
Expand Down
1 change: 1 addition & 0 deletions cloudflare-workers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export type Commenter = {
oauth_user_id: string;
name: string;
avatar_url: string;
profile_url?: string;
};

export type GithubOrgMembershipResp = {
Expand Down
32 changes: 23 additions & 9 deletions frontend/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Comment = {
oauth_user_id: string;
name: string | null;
avatar_url?: string;
profile_url?: string;
};
comment: string;
created_time: string;
Expand Down Expand Up @@ -532,7 +533,7 @@ const _renderComments = (comments: Comment[]) => {

const username = container.querySelector(
".comment_username",
) as HTMLDivElement;
) as HTMLSpanElement;

const commentActionsLogin = container.querySelector(
".comment_actions_login",
Expand Down Expand Up @@ -607,7 +608,7 @@ const _renderComments = (comments: Comment[]) => {
</div>
<div class="comment_base">
<div class="comment_header">
<span class="comment_commenter"></span>
<a class="comment_commenter"></a>
<span class="comment_time comment_created_time">发布于 ${dateTimeFormatter.format(new Date(comment.created_time))}</span>
<span class="comment_time comment_edited_time">最后编辑于 ${comment.last_edited_time ? dateTimeFormatter.format(new Date(comment.last_edited_time)) : ""}</span>
<div class="comment_actions">
Expand All @@ -626,18 +627,31 @@ const _renderComments = (comments: Comment[]) => {
<button class="comment_actions_item comment_expand" data-action="fold">折叠</button>
</div>
`.trim();
commentEl.querySelector(".comment_commenter")!.textContent =
comment.commenter.name;
commentEl.querySelector(".comment_main .comment_content")!.textContent =
comment.comment;
const commenter = commentEl.querySelector(
".comment_commenter",
)! as HTMLAnchorElement;
commenter.textContent = comment.commenter.name;

const userAvatar = commentEl.querySelector(
".comment_user_avatar",
) as HTMLDivElement;

if (comment.commenter.profile_url) {
commenter.target = "_blank";
commenter.href = comment.commenter.profile_url;
userAvatar.style.cursor = "pointer";
userAvatar.addEventListener("click", () => {
window.open(comment.commenter.profile_url);
})
}

if (!comment.commenter.avatar_url) {
const userAvatar = commentEl.querySelector(
".comment_user_avatar",
) as HTMLDivElement;
userAvatar.innerHTML = iconDefaultAvatar;
}

commentEl.querySelector(".comment_main .comment_content")!.textContent =
comment.comment;

const commentActionsHeader = commentEl.querySelector(
".comment_header .comment_actions",
) as HTMLDivElement;
Expand Down

0 comments on commit 199de93

Please sign in to comment.