Skip to content

Commit d51a397

Browse files
committed
fix: message read status error
1 parent 11ff386 commit d51a397

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

app/api/message/read/route.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { NextRequest, NextResponse } from 'next/server'
22
import { prisma } from '~/prisma/index'
33
import { verifyHeaderCookie } from '~/middleware/_verifyHeaderCookie'
4+
import { readMessageSchema } from '~/validations/message'
5+
import { kunParsePutBody } from '~/app/api/utils/parseQuery'
46

5-
export const readMessage = async (uid: number) => {
7+
export const readMessage = async (uid: number, type?: string) => {
68
await prisma.user_message.updateMany({
7-
where: { recipient_id: uid },
9+
where: { recipient_id: uid, type },
810
data: { status: { set: 1 } }
911
})
1012
return {}
@@ -15,7 +17,13 @@ export const PUT = async (req: NextRequest) => {
1517
if (!payload) {
1618
return NextResponse.json('用户未登录')
1719
}
20+
const input = await kunParsePutBody(req, readMessageSchema)
21+
if (typeof input === 'string') {
22+
return NextResponse.json(input)
23+
}
24+
25+
const messageType = input.type === 'all' ? undefined : input.type
1826

19-
const response = await readMessage(payload.uid)
27+
const response = await readMessage(payload.uid, messageType)
2028
return NextResponse.json(response)
2129
}

components/galgame/Card.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ export const GalgameCard = ({ patch }: Props) => {
6060
{GALGAME_AGE_LIMIT_MAP[patch.content_limit]}
6161
</Chip>
6262
</div>
63-
64-
<div className="absolute z-10 rounded-full bg-background right-2 bottom-2">
65-
<Chip size="sm" variant="flat">
66-
{formatDistanceToNow(patch.created)}
67-
</Chip>
68-
</div>
6963
</div>
7064
</CardHeader>
7165
<CardBody className="justify-between space-y-2">
72-
<h2 className="font-semibold transition-colors text-medium sm:text-lg line-clamp-2 hover:text-primary-500">
73-
{patch.name}
66+
<h2 className="font-semibold transition-colors space-x-2 text-medium sm:text-lg line-clamp-2 hover:text-primary-500">
67+
<span>{patch.name}</span>
68+
<span className="font-normal text-base text-xs text-default-500">
69+
{formatDistanceToNow(patch.created)}
70+
</span>
7471
</h2>
7572
<KunCardStats patch={patch} />
7673
</CardBody>

components/kun/top-bar/User.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Button } from '@heroui/button'
88
import { Skeleton } from '@heroui/skeleton'
99
import { Tooltip } from '@heroui/tooltip'
1010
import { useUserStore } from '~/store/userStore'
11-
import { useRouter } from '@bprogress/next'
11+
import { useRouter } from 'next/navigation'
1212
import { kunFetchGet } from '~/utils/kunFetch'
1313
import { ThemeSwitcher } from './ThemeSwitcher'
1414
import { useMounted } from '~/hooks/useMounted'

components/message/MessageNav.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Bell, Globe, UserPlus, RefreshCcw, Puzzle, AtSign } from 'lucide-react'
77
import { usePathname } from 'next/navigation'
88
import Link from 'next/link'
99
import toast from 'react-hot-toast'
10+
import { READABLE_MESSAGE_MAP } from '~/constants/message'
1011

1112
const notificationTypes = [
1213
{ type: 'notice', label: '全部消息', icon: Bell, href: '/message/notice' },
@@ -39,17 +40,24 @@ const notificationTypes = [
3940

4041
export const MessageNav = () => {
4142
const pathname = usePathname()
42-
const lastSegment = pathname.split('/').filter(Boolean).pop()
43+
const lastSegment = pathname.split('/').filter(Boolean).pop() || ''
44+
45+
const readableMessageType = notificationTypes.map((msg) => msg.type)
4346

4447
useEffect(() => {
45-
const readAllMessage = async () => {
46-
const res = await kunFetchPut<KunResponse<{}>>('/message/read')
48+
const readAllMessage = async (messageType: string) => {
49+
const res = await kunFetchPut<KunResponse<{}>>('/message/read', {
50+
type: messageType
51+
})
4752
if (typeof res === 'string') {
4853
toast.error(res)
4954
}
5055
}
51-
readAllMessage()
52-
}, [])
56+
57+
if (readableMessageType.includes(lastSegment)) {
58+
readAllMessage(READABLE_MESSAGE_MAP[lastSegment])
59+
}
60+
}, [lastSegment])
5361

5462
return (
5563
<div className="flex flex-col w-full gap-3">

constants/message.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ export const MESSAGE_TYPE_MAP: Record<string, string> = {
2828
patchResourceUpdate: '更新补丁',
2929
system: '系统'
3030
}
31+
32+
export const READABLE_MESSAGE_MAP: Record<string, string> = {
33+
notice: 'all',
34+
follow: 'follow',
35+
'patch-resource-create': 'patchResourceCreate',
36+
'patch-resource-update': 'patchResourceUpdate',
37+
mention: 'mention',
38+
system: 'system'
39+
}

validations/message.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ export const getMessageSchema = z.object({
1616
page: z.coerce.number().min(1).max(9999999),
1717
limit: z.coerce.number().min(1).max(30)
1818
})
19+
20+
export const readMessageSchema = z.object({
21+
type: z.string().max(20, { message: '消息类型最多 20 个字符' })
22+
})

0 commit comments

Comments
 (0)