139 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import db from "../../db"
import service from "../../services"
import netTool from "../../services/netTool"
import { DB, LarkServer, MsgProxy } from "../../types"
import { safeJsonStringify } from "../../utils/pathTools"
const LOG_COLLECTION = "message_log"
const validateMessageReq = (body: MsgProxy.Body) => {
if (!body.api_key) {
return netTool.badRequest("api_key is required")
}
if (!body.group_id && !body.receive_id) {
return netTool.badRequest("group_id or receive_id is required")
}
if (body.receive_id && !body.receive_id_type) {
return netTool.badRequest("receive_id_type is required")
}
if (!body.msg_type) {
return netTool.badRequest("msg_type is required")
}
if (!body.content) {
return netTool.badRequest("content is required")
}
return false
}
export const manageMessageReq = async (req: Request) => {
const body = (await req.json()) as MsgProxy.Body
// 校验参数
const validateRes = validateMessageReq(body)
if (validateRes) return validateRes
// 处理消息内容
const finalContent =
typeof body.content !== "string"
? safeJsonStringify(body.content)
: body.content
// 遍历所有id发送消息保存所有对应的messageId
const sendRes = {
chat_id: {} as Record<string, any>,
open_id: {} as Record<string, any>,
union_id: {} as Record<string, any>,
user_id: {} as Record<string, any>,
email: {} as Record<string, any>,
}
// 发送消息列表
const sendList = [] as Promise<any>[]
// 构造消息记录
const baseLog: DB.MessageLogCreate = {
...body,
final_content: finalContent,
}
// 校验api_key
const apiKeyInfo = await db.apiKey.getOne(body.api_key)
if (!apiKeyInfo) {
const error = "api key not found"
db.log.create(LOG_COLLECTION, { ...baseLog, error })
return netTool.notFound(error)
}
// 获取app name
const appName = apiKeyInfo.expand?.app?.name
if (!appName) {
const error = "app name not found"
db.log.create(LOG_COLLECTION, { ...baseLog, error })
return netTool.notFound(error)
}
// 如果有group_id则发送给所有group_id中的人
if (body.group_id) {
// 获取所有接收者
const group = await db.messageGroup.getOne(body.group_id!)
if (!group) {
const error = "message group not found"
db.log.create(LOG_COLLECTION, { ...baseLog, error })
return netTool.notFound(error)
}
const { chat_id, open_id, union_id, user_id, email } = group
// 构造发送消息函数
const makeSendFunc = (receive_id_type: LarkServer.ReceiveIDType) => {
return (receive_id: string) => {
sendList.push(
service.lark.message
.send(appName)(
receive_id_type,
receive_id,
body.msg_type,
finalContent
)
.then((res) => {
sendRes[receive_id_type][receive_id] = res
})
)
}
}
// 创建消息列表
if (chat_id) chat_id.map(makeSendFunc("chat_id"))
if (open_id) open_id.map(makeSendFunc("open_id"))
if (union_id) union_id.map(makeSendFunc("union_id"))
if (user_id) user_id.map(makeSendFunc("user_id"))
if (email) email.map(makeSendFunc("email"))
}
if (body.receive_id && body.receive_id_type) {
sendList.push(
service.lark.message
.send(appName)(
body.receive_id_type,
body.receive_id,
body.msg_type,
finalContent
)
.then((res) => {
sendRes[body.receive_id_type][body.receive_id] = res
})
)
}
try {
// 里边有错误处理,这里不用担心执行不完
await Promise.all(sendList)
// 保存消息记录
db.log.create(LOG_COLLECTION, { ...baseLog, send_result: sendRes })
return netTool.ok(sendRes)
} catch {
const error = "send msg failed"
db.log.create(LOG_COLLECTION, { ...baseLog, error })
return netTool.serverError(error, sendRes)
}
}