Skip to content

Commit

Permalink
fix multiple user with same username problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ayanamists committed Mar 10, 2024
1 parent 8ffcfd2 commit d1014f5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build
dist
packages/omot-server/service
packages/omot-server/deploy
packages/omot-server/deploy.zip

1 change: 1 addition & 0 deletions packages/omot-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ service/db-data
service/temp-data

deploy/
deploy.zip
15 changes: 14 additions & 1 deletion packages/omot-server/scripts/generate-deploy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from 'fs';
import { exec } from 'child_process';

// clean all files in the deploy folder
if (fs.existsSync('deploy')) {
fs.rmdirSync('deploy', { recursive: true });
}
fs.mkdirSync('deploy');

fs.mkdirSync('deploy/db-init');
Expand All @@ -14,3 +17,13 @@ fs.copyFileSync('service/nginx/nginx.prod.conf', 'deploy/nginx/nginx.conf');

fs.copyFileSync('.env.user', 'deploy/.env');
fs.copyFileSync('service/docker-compose.prod.yml', 'deploy/docker-compose.yml');

// zip the deploy folder
exec('zip -r deploy.zip deploy', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
console.error(stderr);
});
2 changes: 1 addition & 1 deletion packages/omot-server/service/docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'

services:
omot:
image: omot-temp
image: ghcr.io/ayanamists/oh-my-old-tweet:release
volumes:
- "${MEDIA_STORAGE}:/opt/media"

Expand Down
2 changes: 1 addition & 1 deletion packages/omot-server/src/componets/UserList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function UserList({ users }: UserListProps) {
sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
>
<TableCell scope="row">
<Link href={`/user/${row.userName}`}>{row.userName}</Link>
<Link href={`/user/${row.userName}?originalId=${row.originalId}`}>{row.userName}</Link>
</TableCell>
<TableCell align="left">{row.fullName}</TableCell>
<TableCell align="left">{
Expand Down
24 changes: 19 additions & 5 deletions packages/omot-server/src/pages/user/[name].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Timeline, { DisplayTweet } from "@/componets/TimeLine";
import MainLayout from "@/layouts/MainLayout";
import { logger } from "@/logger";
import { getImageUrl, processImages, ssrConvert } from "@/util";
import prisma from "@/util/db";
import { GetServerSideProps, InferGetServerSidePropsType } from "next";
Expand Down Expand Up @@ -39,9 +38,18 @@ function processShowReply(showReply: string | string[] | undefined) {
}
}

function processOriginalId(originalId: string | string[] | undefined) {
if (typeof originalId === "string") {
return originalId;
} else {
return undefined;
}
}

export const getServerSideProps = (async (context) => {
const userName = context.params?.name;
const showReply = processShowReply(context.query.showReply);
const originalId = processOriginalId(context.query.originalId);
if (userName == null) {
throw new Error("userName is null");
} else if (typeof userName !== "string") {
Expand All @@ -53,9 +61,10 @@ export const getServerSideProps = (async (context) => {
some: {
userName: {
userName: userName
}
},
}
}
},
...(originalId && { originalId })
},
});
if (users.length === 0) {
Expand All @@ -65,9 +74,14 @@ export const getServerSideProps = (async (context) => {
destination: fallback,
permanent: false
}
}
};
} else if (users.length > 1) {
logger.warn(`Many user found: ${users.map(u => u.id)}`)
return {
redirect: {
destination: `/user?userName=${userName}`,
permanent: false
}
};
}
const user = users[0];
const userId = user.id;
Expand Down
25 changes: 20 additions & 5 deletions packages/omot-server/src/pages/user/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import UserList, { UserListProps } from "@/componets/UserList";
import MainLayout from "@/layouts/MainLayout";
import prisma from '@/util/db'
import { ssrConvert } from '@/util'
import { GetServerSideProps } from "next";

export default function allUser({ users } : UserListProps) {
export default function allUser({ users }: UserListProps) {
return (<MainLayout>
<UserList users={users}/>
<UserList users={users} />
</MainLayout>)
}


export async function getServerSideProps() {
export const getServerSideProps = (async (context) => {
const userNameQuery = context.query.name;
let userName = undefined;
if (typeof userNameQuery === "string") {
userName = userNameQuery;
}
const userList = await prisma.user.findMany({
orderBy: {
lastModified: "desc"
Expand All @@ -23,7 +29,16 @@ export async function getServerSideProps() {
userName: true
},
}
}
},
...(userName && {
where: {
posts: {
some: {
userName: { userName }
}
}
}
})
});
return {
props: {
Expand All @@ -33,4 +48,4 @@ export async function getServerSideProps() {
}))
}
}
}
}) satisfies GetServerSideProps<UserListProps>;

0 comments on commit d1014f5

Please sign in to comment.