Skip to content

Commit cb9a926

Browse files
committed
Use authenticated ownership for group message alignment
1 parent 5f83e58 commit cb9a926

5 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎flybot_workspace/static/chat/app.mjs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { countMentionedGroupMembers, hasEveryoneMention, hasGroupAllMention, mou
33
import { createGroupBotIdentities, groupBotIdentity, createGroupBotAvatar } from './group-identity.mjs';
44
import { groupMentionPainter } from './group-mentions-display.mjs';
55
import { AGENT_FILE_ACCEPT, chatFileDownload, chatUploadProgress, deferModule, localChatFile, mountChatFiles,
6-
ownSocialReceipt, renderChatFile, sendPrivateAttachmentMessage, sendSocialAttachmentMessage, unclearSocialDelivery } from './files.mjs';
6+
ownSocialMessage, ownSocialReceipt, renderChatFile, sendPrivateAttachmentMessage, sendSocialAttachmentMessage, unclearSocialDelivery } from './files.mjs';
77
import { mountGroupTranslation } from './translation.mjs';
88
import { mountChatActivity, mountChatApprovalNotice } from './activity.mjs';
99
import { meetingCodeFromHash, meetingDraftFromCallHref, stageMeetingFromChat } from '../shell/meeting-link.mjs';
@@ -1460,7 +1460,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} =
14601460
function socialMessageAuthor(item, people = currentConversation?.members || members) {
14611461
if (item.role === 'agent') return item.reply_job_id ? groupReplyAuthor(item)
14621462
: (item.agent_name || uiLabel('智能体', 'Agent')) + uiLabel(' · AI 智能体 · 运行时回复', ' · AI agent · DS-Bot');
1463-
if (item.user_id === currentUser?.id) return uiLabel('我', 'Me');
1463+
if (ownSocialMessage(item, currentUser?.id)) return uiLabel('我', 'Me');
14641464
return people.find(entry => entry.user_id === item.user_id)?.username || uiLabel('成员', 'Member');
14651465
}
14661466

@@ -5040,7 +5040,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} =
50405040
const receipt = await historyPage(history,query,AbortSignal.timeout(4500)).catch(() => null);
50415041
const original = receipt?.status === 200 && Array.isArray(receipt.body.items) && receipt.body.items.find(message =>
50425042
message.id === item.id && !message.retracted && message.conversation_id === cid &&
5043-
message.workspace_id === history.wid && message.user_id === history.owner);
5043+
message.workspace_id === history.wid && ownSocialMessage(message, history.owner));
50445044
next = Array.isArray(original?.reply_jobs) && original.reply_jobs.find(advanced);
50455045
}
50465046
if (groupReplyRetries.get(job.id) !== retry) return;
@@ -5303,7 +5303,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} =
53035303
}
53045304
li.dataset.messageId = item.id;
53055305
if (Number.isSafeInteger(item.seq)) li.dataset.messageSeq = String(item.seq);
5306-
const mine = item.role !== 'agent' && item.user_id === (currentUser && currentUser.id);
5306+
const mine = ownSocialMessage(item, currentUser?.id);
53075307
li.dataset.chatAuthor = item.role === 'agent' ? 'agent' : 'person';
53085308
li.className = mine ? 'mine' : 'other';
53095309
const role = document.createElement('p');
@@ -5405,7 +5405,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} =
54055405
confirm.removeAttribute('aria-busy');
54065406
if (currentUser !== owner || workspaceId() !== wid) return;
54075407
const message = result.body?.message;
5408-
if (result.status !== 200 || message?.id !== item.id || message.user_id !== owner.id ||
5408+
if (result.status !== 200 || message?.id !== item.id || !ownSocialMessage(message, owner.id) ||
54095409
message.workspace_id !== wid || message.conversation_id !== cid || !Number.isSafeInteger(message.revision) ||
54105410
message.revision <= payload.revision || message.retracted !== (operation === 'retract')) {
54115411
if (scope === sendScope()) {
@@ -8615,7 +8615,7 @@ export function mount(document, {fetcher = globalThis.fetch.bind(globalThis)} =
86158615
const start = Math.max(0, index - 48);
86168616
return {
86178617
kind: 'social', conversation_id: conversationId, message_id: item.id, created: item.created,
8618-
role: item.role === 'agent' ? 'assistant' : item.user_id && item.user_id === viewer ? 'user' : '',
8618+
role: item.role === 'agent' ? 'assistant' : ownSocialMessage(item, viewer) ? 'user' : '',
86198619
agent_name: item.agent_name,
86208620
author: item.user_id && people.find(member => member.user_id === item.user_id)?.username,
86218621
snippet: (start > 0 ? '…' : '') + body.slice(start, start + 160) + (body.length > start + 160 ? '…' : ''),

‎flybot_workspace/static/chat/files.mjs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,14 @@ export function unclearSocialDelivery(status) {
193193
return !status || status === 408 || status >= 500 || [200, 201].includes(status);
194194
}
195195

196+
export function ownSocialMessage(message, viewer) {
197+
if (!viewer || !message || message.role === 'agent') return false;
198+
// Bot Social checks the authenticated room identity, which can differ from the platform ID.
199+
return typeof message.mine === 'boolean' ? message.mine : message.user_id === viewer;
200+
}
201+
196202
export function ownSocialReceipt(message, destination, key) {
197-
return Boolean(message?.id && message.client_key === key && message.user_id === destination.owner &&
203+
return Boolean(message?.id && message.client_key === key && ownSocialMessage(message, destination.owner) &&
198204
message.workspace_id === destination.wid && message.conversation_id === destination.conversationId);
199205
}
200206

‎runtime/flybot-workspace-web/lib/c/legacy-v1.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ function messageItem(row, me, env, workspaceId) {
159159
return {
160160
id: row.id,
161161
seq: row.seq,
162+
...(!agent && typeof row.mine === 'boolean' ? {mine: row.mine} : {}),
162163
...(row.mine && row.client_key ? {client_key: row.client_key} : {}),
163164
conversation_id: row.conversation_id,
164165
workspace_id: workspaceId,

‎runtime/flybot-workspace-web/test/social-messenger.test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ test('messenger endpoints map to the shared conversation routes', async () => {
5151
assert.deepEqual(f.calls[0].body, { kind: 'dm', peer: 'bea.home' })
5252
})
5353

54+
test('group message receipts and history retain the authenticated Bot Social ownership', async () => {
55+
const dir = fixture()
56+
const workspace = 'a'.repeat(32)
57+
const route = '/v1/ot/conversations/otc_group'
58+
const author = {kind:'member', owner_id:'social-owner', handle:'ann.home'}
59+
const mine = {id:'otm_mine', author, mine:true, client_key:'group-message-key', body:'Hey'}
60+
const peer = {id:'otm_peer', author:{kind:'member', owner_id:'social-peer', handle:'bea.home'},
61+
mine:false, client_key:'not-the-viewers-key', body:'Hi'}
62+
const f = fake({
63+
'GET /v1/ot/me': () => author,
64+
['GET ' + route]: () => ({id:'otc_group', kind:'group', account_group:true}),
65+
['GET ' + route + '/messages']: () => ({messages:[mine, peer]}),
66+
['POST ' + route + '/messages']: () => mine,
67+
})
68+
setSharedBotSocial(createBotSocial({server:SERVER, credentialsPath:join(dir,'bot','flybot-social.json'), fetch:f.fetch}))
69+
const {v1} = messenger({dataDir:dir, async call() {return {workspace_id:workspace}}}).endpoints
70+
const path = `/v1/workspaces/${workspace}/social/conversations/otc_group/messages`
71+
const receipt = await v1({method:'POST', path, body:{body:mine.body}, idempotency_key:mine.client_key})
72+
const history = await v1({method:'GET', path})
73+
assert.equal(receipt.status, 201)
74+
assert.deepEqual(history.body.items[0], receipt.body.message)
75+
assert.equal(receipt.body.message.mine, true)
76+
assert.equal(receipt.body.message.client_key, mine.client_key)
77+
assert.equal(history.body.items[1].mine, false)
78+
assert.equal(history.body.items[1].client_key, undefined)
79+
})
80+
5481
test('call consent forwards only the exact lease fields through either signed native call route', async () => {
5582
const dir = fixture()
5683
const workspace = 'a'.repeat(32)

‎tests/chat_page.test.mjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,7 +2712,7 @@ test('own message editing and confirmed retraction call versioned endpoints', as
27122712
const {doc, el} = fakeChatDocument(chatHtml());
27132713
const {fetcher: original} = sessionFetcher();
27142714
let text = 'Original', revision = 1, retracted = false;
2715-
const message = () => ({id:'mine-1', seq:1, user_id:alice, workspace_id:'ws-1', conversation_id:'conv-1',
2715+
const message = () => ({id:'mine-1', seq:1, user_id:'social-owner', mine:true, workspace_id:'ws-1', conversation_id:'conv-1',
27162716
body:text, revision, retracted, edited:revision > 1 ? 15 : null});
27172717
const waiting = Promise.withResolvers();
27182718
let holdDirectory = false;
@@ -4899,7 +4899,8 @@ test('a native group send releases the next message only after activity confirms
48994899
routes.set(activity, () => snapshot('other-message', 9));
49004900
routes.set(activity + '&since=other-message', () => snapshot('own-message', 10));
49014901
routes.set(receiptPath, () => json(200, {
4902-
items:[{...first, client_key:keys[0], user_id:calls.filter(call => call.path === receiptPath).length === 1 ? bob : alice}],
4902+
items:[{...first, client_key:keys[0], user_id:'social-owner',
4903+
mine:calls.filter(call => call.path === receiptPath).length === 2}],
49034904
}));
49044905
const fetcher = async (url, options = {}) => {
49054906
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
49254926
assert.equal(keys.length, 2);
49264927
assert.notEqual(keys[0],keys[1]);
49274928
assert.deepEqual(fixture.messages(), [linkSocialLatest,first.id,second.id]);
4929+
assert.equal(el('message-list').children.find(row => row.dataset.messageId === first.id).className, 'mine');
49284930
heldResponse.resolve(json(502,{}));
49294931
await settle();
49304932
assert.deepEqual(fixture.messages(), [linkSocialLatest,first.id,second.id]);

0 commit comments

Comments
 (0)