diff --git a/flybot_workspace/static/chat/app.mjs b/flybot_workspace/static/chat/app.mjs index 7ea5b72af..8309a61b8 100644 --- a/flybot_workspace/static/chat/app.mjs +++ b/flybot_workspace/static/chat/app.mjs @@ -3,7 +3,7 @@ import { countMentionedGroupMembers, hasEveryoneMention, hasGroupAllMention, mou import { createGroupBotIdentities, groupBotIdentity, createGroupBotAvatar } from './group-identity.mjs'; import { groupMentionPainter } from './group-mentions-display.mjs'; import { AGENT_FILE_ACCEPT, chatFileDownload, chatUploadProgress, deferModule, localChatFile, mountChatFiles, - ownSocialReceipt, renderChatFile, sendPrivateAttachmentMessage, sendSocialAttachmentMessage, unclearSocialDelivery } from './files.mjs'; + ownSocialMessage, ownSocialReceipt, renderChatFile, sendPrivateAttachmentMessage, sendSocialAttachmentMessage, unclearSocialDelivery } from './files.mjs'; import { mountGroupTranslation } from './translation.mjs'; import { mountChatActivity, mountChatApprovalNotice } from './activity.mjs'; import { meetingCodeFromHash, meetingDraftFromCallHref, stageMeetingFromChat } from '../shell/meeting-link.mjs'; @@ -1460,7 +1460,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} = function socialMessageAuthor(item, people = currentConversation?.members || members) { if (item.role === 'agent') return item.reply_job_id ? groupReplyAuthor(item) : (item.agent_name || uiLabel('智能体', 'Agent')) + uiLabel(' · AI 智能体 · 运行时回复', ' · AI agent · DS-Bot'); - if (item.user_id === currentUser?.id) return uiLabel('我', 'Me'); + if (ownSocialMessage(item, currentUser?.id)) return uiLabel('我', 'Me'); return people.find(entry => entry.user_id === item.user_id)?.username || uiLabel('成员', 'Member'); } @@ -5040,7 +5040,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} = const receipt = await historyPage(history,query,AbortSignal.timeout(4500)).catch(() => null); const original = receipt?.status === 200 && Array.isArray(receipt.body.items) && receipt.body.items.find(message => message.id === item.id && !message.retracted && message.conversation_id === cid && - message.workspace_id === history.wid && message.user_id === history.owner); + message.workspace_id === history.wid && ownSocialMessage(message, history.owner)); next = Array.isArray(original?.reply_jobs) && original.reply_jobs.find(advanced); } if (groupReplyRetries.get(job.id) !== retry) return; @@ -5303,7 +5303,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} = } li.dataset.messageId = item.id; if (Number.isSafeInteger(item.seq)) li.dataset.messageSeq = String(item.seq); - const mine = item.role !== 'agent' && item.user_id === (currentUser && currentUser.id); + const mine = ownSocialMessage(item, currentUser?.id); li.dataset.chatAuthor = item.role === 'agent' ? 'agent' : 'person'; li.className = mine ? 'mine' : 'other'; const role = document.createElement('p'); @@ -5405,7 +5405,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} = confirm.removeAttribute('aria-busy'); if (currentUser !== owner || workspaceId() !== wid) return; const message = result.body?.message; - if (result.status !== 200 || message?.id !== item.id || message.user_id !== owner.id || + if (result.status !== 200 || message?.id !== item.id || !ownSocialMessage(message, owner.id) || message.workspace_id !== wid || message.conversation_id !== cid || !Number.isSafeInteger(message.revision) || message.revision <= payload.revision || message.retracted !== (operation === 'retract')) { if (scope === sendScope()) { @@ -8615,7 +8615,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} = const start = Math.max(0, index - 48); return { kind: 'social', conversation_id: conversationId, message_id: item.id, created: item.created, - role: item.role === 'agent' ? 'assistant' : item.user_id && item.user_id === viewer ? 'user' : '', + role: item.role === 'agent' ? 'assistant' : ownSocialMessage(item, viewer) ? 'user' : '', agent_name: item.agent_name, author: item.user_id && people.find(member => member.user_id === item.user_id)?.username, snippet: (start > 0 ? '…' : '') + body.slice(start, start + 160) + (body.length > start + 160 ? '…' : ''), diff --git a/flybot_workspace/static/chat/files.mjs b/flybot_workspace/static/chat/files.mjs index 52fbeee5a..2265e4921 100644 --- a/flybot_workspace/static/chat/files.mjs +++ b/flybot_workspace/static/chat/files.mjs @@ -193,8 +193,14 @@ export function unclearSocialDelivery(status) { return !status || status === 408 || status >= 500 || [200, 201].includes(status); } +export function ownSocialMessage(message, viewer) { + if (!viewer || !message || message.role === 'agent') return false; + // Bot Social checks the authenticated room identity, which can differ from the platform ID. + return typeof message.mine === 'boolean' ? message.mine : message.user_id === viewer; +} + export function ownSocialReceipt(message, destination, key) { - return Boolean(message?.id && message.client_key === key && message.user_id === destination.owner && + return Boolean(message?.id && message.client_key === key && ownSocialMessage(message, destination.owner) && message.workspace_id === destination.wid && message.conversation_id === destination.conversationId); } diff --git a/runtime/flybot-workspace-web/lib/c/legacy-v1.js b/runtime/flybot-workspace-web/lib/c/legacy-v1.js index c241c949a..a46ebf2a0 100644 --- a/runtime/flybot-workspace-web/lib/c/legacy-v1.js +++ b/runtime/flybot-workspace-web/lib/c/legacy-v1.js @@ -159,6 +159,7 @@ function messageItem(row, me, env, workspaceId) { return { id: row.id, seq: row.seq, + ...(!agent && typeof row.mine === 'boolean' ? {mine: row.mine} : {}), ...(row.mine && row.client_key ? {client_key: row.client_key} : {}), conversation_id: row.conversation_id, workspace_id: workspaceId, diff --git a/runtime/flybot-workspace-web/test/social-messenger.test.js b/runtime/flybot-workspace-web/test/social-messenger.test.js index 420dc8203..2f58ee3f8 100644 --- a/runtime/flybot-workspace-web/test/social-messenger.test.js +++ b/runtime/flybot-workspace-web/test/social-messenger.test.js @@ -51,6 +51,33 @@ test('messenger endpoints map to the shared conversation routes', async () => { assert.deepEqual(f.calls[0].body, { kind: 'dm', peer: 'bea.home' }) }) +test('group message receipts and history retain the authenticated Bot Social ownership', async () => { + const dir = fixture() + const workspace = 'a'.repeat(32) + const route = '/v1/ot/conversations/otc_group' + const author = {kind:'member', owner_id:'social-owner', handle:'ann.home'} + const mine = {id:'otm_mine', author, mine:true, client_key:'group-message-key', body:'Hey'} + const peer = {id:'otm_peer', author:{kind:'member', owner_id:'social-peer', handle:'bea.home'}, + mine:false, client_key:'not-the-viewers-key', body:'Hi'} + const f = fake({ + 'GET /v1/ot/me': () => author, + ['GET ' + route]: () => ({id:'otc_group', kind:'group', account_group:true}), + ['GET ' + route + '/messages']: () => ({messages:[mine, peer]}), + ['POST ' + route + '/messages']: () => mine, + }) + setSharedBotSocial(createBotSocial({server:SERVER, credentialsPath:join(dir,'bot','flybot-social.json'), fetch:f.fetch})) + const {v1} = messenger({dataDir:dir, async call() {return {workspace_id:workspace}}}).endpoints + const path = `/v1/workspaces/${workspace}/social/conversations/otc_group/messages` + const receipt = await v1({method:'POST', path, body:{body:mine.body}, idempotency_key:mine.client_key}) + const history = await v1({method:'GET', path}) + assert.equal(receipt.status, 201) + assert.deepEqual(history.body.items[0], receipt.body.message) + assert.equal(receipt.body.message.mine, true) + assert.equal(receipt.body.message.client_key, mine.client_key) + assert.equal(history.body.items[1].mine, false) + assert.equal(history.body.items[1].client_key, undefined) +}) + test('call consent forwards only the exact lease fields through either signed native call route', async () => { const dir = fixture() const workspace = 'a'.repeat(32) diff --git a/tests/chat_page.test.mjs b/tests/chat_page.test.mjs index 829661662..8d4620da5 100644 --- a/tests/chat_page.test.mjs +++ b/tests/chat_page.test.mjs @@ -2712,7 +2712,7 @@ test('own message editing and confirmed retraction call versioned endpoints', as const {doc, el} = fakeChatDocument(chatHtml()); const {fetcher: original} = sessionFetcher(); let text = 'Original', revision = 1, retracted = false; - const message = () => ({id:'mine-1', seq:1, user_id:alice, workspace_id:'ws-1', conversation_id:'conv-1', + const message = () => ({id:'mine-1', seq:1, user_id:'social-owner', mine:true, workspace_id:'ws-1', conversation_id:'conv-1', body:text, revision, retracted, edited:revision > 1 ? 15 : null}); const waiting = Promise.withResolvers(); let holdDirectory = false; @@ -4899,7 +4899,8 @@ test('a native group send releases the next message only after activity confirms routes.set(activity, () => snapshot('other-message', 9)); routes.set(activity + '&since=other-message', () => snapshot('own-message', 10)); routes.set(receiptPath, () => json(200, { - items:[{...first, client_key:keys[0], user_id:calls.filter(call => call.path === receiptPath).length === 1 ? bob : alice}], + items:[{...first, client_key:keys[0], user_id:'social-owner', + mine:calls.filter(call => call.path === receiptPath).length === 2}], })); const fetcher = async (url, options = {}) => { if (url !== path || options.method !== 'POST') return fixture.fetcher(url, options); @@ -4925,6 +4926,7 @@ test('a native group send releases the next message only after activity confirms assert.equal(keys.length, 2); assert.notEqual(keys[0],keys[1]); assert.deepEqual(fixture.messages(), [linkSocialLatest,first.id,second.id]); + assert.equal(el('message-list').children.find(row => row.dataset.messageId === first.id).className, 'mine'); heldResponse.resolve(json(502,{})); await settle(); assert.deepEqual(fixture.messages(), [linkSocialLatest,first.id,second.id]);