doctor-mail/apps/server/src/models/post/utils.ts

74 lines
1.8 KiB
TypeScript
Raw Normal View History

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;
2025-01-24 00:19:02 +08:00
// 在函数开始时计算一次时间
const thirtyMinutesAgo = new Date(Date.now() - 30 * 60 * 1000);
const clientIp = (data?.meta as any)?.ip;
2025-01-22 18:56:27 +08:00
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;
2025-01-24 00:19:02 +08:00
const liked = await db.visit.count({
where: {
postId: data.id,
type: VisitType?.LIKE,
...(staff?.id
? // 如果有 staff查找对应的 visitorId
{ visitorId: staff.id }
: // 如果没有 staff查找相同 IP 且 visitorId 为 null 且 30 分钟内的记录
{
visitorId: null,
meta: { path: ['ip'], equals: clientIp },
updatedAt: {
gte: thirtyMinutesAgo,
},
}),
},
});
2025-01-22 18:56:27 +08:00
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,
2025-01-24 00:19:02 +08:00
liked,
// limitedComments,
2025-01-22 18:56:27 +08:00
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 || '';
}