| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { Message } from './api/models';
- /**
- * 派活有效性检查 —— 组长「表面上派了活」但组员根本没收到。
- *
- * ## 实测教训(2026-10-27)
- *
- * 组长在群里发了「派活消息已发到群里(event_id ...)」,
- * 但 40 分钟后交付物目录都不存在 —— 组员压根没开工。
- *
- * 查群消息发现:**整个群只有 1 条带 `m.mentions` 的消息**(用户发的),
- * 组长那条「派活」没带 mention 结构。
- *
- * 后果:`lj-test` 的 `MATRIX_REQUIRE_MENTION=true` 让它**不认为自己被叫**,
- * 于是不处理。组长的文字里写了 `@lj-test`,但那只是**纯文本**,不是 mention。
- *
- * ## 这个模块做什么
- *
- * 检查一条「派活消息」是否**真的能让对方收到**:
- * · 有 `m.mentions.user_ids` 且包含目标 → ✅ 有效
- * · 只有文本 `@名字` 但没 mention 结构 → ❌ **无效**(对方不会响应)
- */
- export type DispatchValidity = 'valid' | 'text-only' | 'no-mention';
- export interface DispatchCheck {
- validity: DispatchValidity;
- /** 给用户/组长看的说明 */
- detail: string;
- /** 是否需要提醒组长重发 */
- needsResend: boolean;
- }
- /**
- * 检查一条消息是否有效派活。
- *
- * @param msg 组长的派活消息
- * @param targetMxId 组员的 Matrix ID
- */
- export function checkDispatch(
- msg: Message | undefined,
- targetMxId: string,
- ): DispatchCheck {
- if (!msg) {
- return { validity: 'no-mention', detail: '没有找到派活消息', needsResend: true };
- }
- const mentions = msg.mentions ?? [];
- if (mentions.includes(targetMxId)) {
- return { validity: 'valid', detail: '派活消息带有正确的 @ 结构', needsResend: false };
- }
- // 文本里有 @名字 但没 mention 结构 —— 最常见的问题
- const text = msg.text ?? '';
- const local = targetMxId.replace(/^@/, '').split(':')[0];
- if (text.includes('@' + local) || text.includes(targetMxId)) {
- return {
- validity: 'text-only',
- detail:
- '派活消息只在文字里写了 @名字,没有真正的 mention 结构 —— ' +
- '对方不会认为被叫到,所以不会开工。需要带 m.mentions 重发。',
- needsResend: true,
- };
- }
- return { validity: 'no-mention', detail: '派活消息没有 @ 任何人', needsResend: true };
- }
- /**
- * 在群消息里找「组长最近一次派活」,并判断有效性。
- *
- * 用于:App 发现「组长说派了活但组员没动」时,判断是不是 mention 缺失。
- */
- export function findLastDispatch(
- messages: Message[],
- leaderMxId: string,
- targetMxId: string,
- ): { msg: Message | undefined; check: DispatchCheck } {
- for (let i = messages.length - 1; i >= 0; i--) {
- const m = messages[i];
- if (m.senderId !== leaderMxId) continue;
- const text = m.text ?? '';
- const local = targetMxId.replace(/^@/, '').split(':')[0];
- // 认「派活」的几种写法
- const looksLikeDispatch =
- /派活|分配|交给你|你来做|请做/.test(text) ||
- m.mentions?.includes(targetMxId) ||
- text.includes('@' + local);
- if (!looksLikeDispatch) continue;
- return { msg: m, check: checkDispatch(m, targetMxId) };
- }
- return { msg: undefined, check: checkDispatch(undefined, targetMxId) };
- }
|