feat: 扩展接收组模型,支持应用信息;优化消息请求验证逻辑

This commit is contained in:
zhaoyingbo 2025-01-17 12:51:25 +00:00
parent e36c3c8496
commit 68f2e322a4
2 changed files with 44 additions and 21 deletions

View File

@ -1,5 +1,6 @@
import { RecordModel } from "pocketbase" import { RecordModel } from "pocketbase"
import { AppInfoModel } from "../../constant/config"
import { managePbError } from "../../utils/pbTools" import { managePbError } from "../../utils/pbTools"
import pbClient from "../pbClient" import pbClient from "../pbClient"
@ -7,23 +8,28 @@ const DB_NAME = "message_group"
export interface ReceiveGroup { export interface ReceiveGroup {
name: string name: string
email?: string[]
chatId?: string[] chatId?: string[]
openId?: string[]
unionId?: string[]
userId?: string[] userId?: string[]
} }
export type ReceiveGroupModel = ReceiveGroup & RecordModel export type ReceiveGroupModel = ReceiveGroup & RecordModel
export interface ReceiveGroupModelWithApp extends ReceiveGroupModel {
expand: {
app: AppInfoModel
}
}
/** /**
* ID获取指定消息组 * ID获取指定消息组
* @param id * @param id
* @returns * @returns
*/ */
const getOne = (id: string) => const getOne = (id: string) =>
managePbError<ReceiveGroupModel>(() => managePbError<ReceiveGroupModelWithApp>(() =>
pbClient.collection(DB_NAME).getOne(id) pbClient.collection(DB_NAME).getOne(id, {
expand: "app",
})
) )
const receiveGroup = { const receiveGroup = {

View File

@ -1,4 +1,5 @@
import { stringifyJson } from "@egg/hooks" import { stringifyJson } from "@egg/hooks"
import { LarkService } from "@egg/net-tool"
import db from "../../db" import db from "../../db"
import { Log } from "../../db/log" import { Log } from "../../db/log"
@ -19,8 +20,8 @@ const ID_TYPE_MAP = {
* @returns {false | Response} false * @returns {false | Response} false
*/ */
const validateMessageReq = ({ body, genResp }: Context): false | Response => { const validateMessageReq = ({ body, genResp }: Context): false | Response => {
if (!body.api_key) { if (!body.api_key && !body.group_id) {
return genResp.badRequest("api_key is required") return genResp.badRequest("api_key or group_id is required")
} }
if (!body.group_id && !body.receive_id) { if (!body.group_id && !body.receive_id) {
return genResp.badRequest("group_id or receive_id is required") return genResp.badRequest("group_id or receive_id is required")
@ -79,16 +80,20 @@ export const manageMessageReq = async (ctx: Context): Promise<Response> => {
finalContent, finalContent,
} }
// 校验 api_key let larkService: LarkService | null = null
const apiKeyInfo = await db.apiKey.getOne(body.api_key)
if (!apiKeyInfo) {
const error = "api key not found"
await db.log.create({ ...baseLog, error })
return genResp.notFound(error)
}
// 生成 Lark 服务 if (body.api_key) {
const larkService = genLarkService(apiKeyInfo.expand.app.name, requestId) // 校验 api_key
const apiKeyInfo = await db.apiKey.getOne(body.api_key)
if (!apiKeyInfo) {
const error = "api key not found"
await db.log.create({ ...baseLog, error })
return genResp.notFound(error)
}
// 生成 Lark 服务
larkService = genLarkService(apiKeyInfo.expand.app.name, requestId)
}
// 如果有 group_id则发送给所有 group_id 中的人 // 如果有 group_id则发送给所有 group_id 中的人
if (body.group_id) { if (body.group_id) {
@ -100,13 +105,22 @@ export const manageMessageReq = async (ctx: Context): Promise<Response> => {
return genResp.notFound(error) return genResp.notFound(error)
} }
const { chatId, openId, unionId, userId, email } = group if (!larkService) {
if (!group.expand?.app?.name) {
const error = "api key not found"
await db.log.create({ ...baseLog, error })
return genResp.notFound(error)
}
larkService = genLarkService(group.expand.app.name, requestId)
}
const { chatId, userId } = group
// 构造发送消息函数 // 构造发送消息函数
const makeSendFunc = (receive_id_type: LarkServer.ReceiveIDType) => { const makeSendFunc = (receive_id_type: LarkServer.ReceiveIDType) => {
return (receive_id: string) => { return (receive_id: string) => {
sendList.push( sendList.push(
larkService.message larkService!.message
.send(receive_id_type, receive_id, body.msg_type, finalContent) .send(receive_id_type, receive_id, body.msg_type, finalContent)
.then((res) => { .then((res) => {
sendResult[ID_TYPE_MAP[receive_id_type]][receive_id] = res sendResult[ID_TYPE_MAP[receive_id_type]][receive_id] = res
@ -117,10 +131,13 @@ export const manageMessageReq = async (ctx: Context): Promise<Response> => {
// 创建消息列表 // 创建消息列表
if (chatId) chatId.map(makeSendFunc("chat_id")) if (chatId) chatId.map(makeSendFunc("chat_id"))
if (openId) openId.map(makeSendFunc("open_id"))
if (unionId) unionId.map(makeSendFunc("union_id"))
if (userId) userId.map(makeSendFunc("user_id")) if (userId) userId.map(makeSendFunc("user_id"))
if (email) email.map(makeSendFunc("email")) }
if (!larkService) {
const error = "api key not found"
await db.log.create({ ...baseLog, error })
return genResp.notFound(error)
} }
// 如果有 receive_id则发送给所有 receive_id 中的人 // 如果有 receive_id则发送给所有 receive_id 中的人