2025-01-22 18:56:27 +08:00
|
|
|
import { db, Post, PostType, UserProfile, VisitType } from '@nice/common';
|
2024-12-30 09:22:38 +08:00
|
|
|
|
2025-01-22 18:56:27 +08:00
|
|
|
export async function setPostRelation(params: {
|
|
|
|
data: Post;
|
|
|
|
staff?: UserProfile;
|
|
|
|
}) {
|
|
|
|
const { data, staff } = params;
|
|
|
|
const limitedComments = await db.post.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: data.id,
|
|
|
|
type: PostType.POST_COMMENT,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
author: true,
|
|
|
|
},
|
|
|
|
take: 5,
|
|
|
|
});
|
|
|
|
const commentsCount = await db.post.count({
|
|
|
|
where: {
|
|
|
|
parentId: data.id,
|
|
|
|
type: PostType.POST_COMMENT,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const readed =
|
|
|
|
(await db.visit.count({
|
|
|
|
where: {
|
|
|
|
postId: data.id,
|
|
|
|
type: VisitType.READED,
|
|
|
|
visitorId: staff?.id,
|
|
|
|
},
|
|
|
|
})) > 0;
|
|
|
|
const readedCount = await db.visit.count({
|
|
|
|
where: {
|
|
|
|
postId: data.id,
|
|
|
|
type: VisitType.READED,
|
|
|
|
},
|
|
|
|
});
|
2024-12-31 15:57:32 +08:00
|
|
|
|
2025-01-22 18:56:27 +08:00
|
|
|
Object.assign(data, {
|
|
|
|
readed,
|
|
|
|
readedCount,
|
|
|
|
limitedComments,
|
|
|
|
commentsCount,
|
|
|
|
// trouble
|
|
|
|
});
|
|
|
|
}
|
2024-12-30 08:26:40 +08:00
|
|
|
|
2025-01-22 18:56:27 +08:00
|
|
|
export function getClientIp(req: any): string {
|
|
|
|
let ip =
|
|
|
|
req.ip ||
|
|
|
|
(Array.isArray(req.headers['x-forwarded-for'])
|
|
|
|
? req.headers['x-forwarded-for'][0]
|
|
|
|
: req.headers['x-forwarded-for']) ||
|
|
|
|
req.socket.remoteAddress;
|
|
|
|
|
|
|
|
// 如果是 IPv4-mapped IPv6 地址,转换为 IPv4
|
|
|
|
if (typeof ip === 'string' && ip.startsWith('::ffff:')) {
|
|
|
|
ip = ip.substring(7);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip || '';
|
|
|
|
}
|