接入私聊消息通知和联系人免打扰

This commit is contained in:
xhgit committed 2026-11-15 22:27:58 +08:00
1 parent 5c882ac855
commit 7e165ba9b4
23 files changed
+286 -97

No files matched your search

+47 -19
View File
@@ -52,8 +52,8 @@ async function watchRealtime(actor, t) {
}
// Opaque fixture with the MLS header. Crypto interoperability is tested separately in crypto/.
function frame() {
return Buffer.concat([Buffer.from([0, 1, 0, 2]), randomBytes(96)]).toString('base64')
function frame(kind = 2) {
return Buffer.concat([Buffer.from([0, 1, 0, kind]), randomBytes(96)]).toString('base64')
}
test('群组邀请、授权、成员移除、版本变更与实时提示', { timeout: 25_000 }, async (t) => {
@@ -333,12 +333,14 @@ test('群密文和实时提示只投递到名单设备,移除或撤销阻断
assert.deepEqual(outsiderEvents, [])
})
test('设备密文投递、幂等、离线补拉、实时提示和撤销', { timeout: 25000 }, async () => {
test('设备密文投递、幂等、离线补拉、免打扰与撤销', { timeout: 25000 }, async (t) => {
const sender = await register()
const recipient = await register()
const other = await register()
const payload = { messageId: randomUUID(), recipientDevice: recipient.device.id, ciphertext: frame() }
const preferences = '/contacts/' + sender.user.id
await json('/inbox', 'POST', sender.token, payload, 404)
await json(preferences, 'POST', recipient.token, { action: 'mute' }, 409)
await contact(sender, recipient, 'request')
await contact(recipient, sender, 'accept')
await json('/inbox', 'POST', sender.token, { ...payload, ciphertext: Buffer.from('plaintext').toString('base64') }, 400)
@@ -355,23 +357,49 @@ test('设备密文投递、幂等、离线补拉、实时提示和撤销', { tim
assert.deepEqual(await json('/inbox', 'GET', sender.token), [])
assert.deepEqual(await json('/inbox', 'GET', other.token), [])
const { ticket } = await json('/realtime/ticket', 'POST', recipient.token)
const ws = new WebSocket(base.replace(/^http/, 'ws') + '/api/v1/realtime', ['xhchat.v1', 'ticket.' + ticket], { origin: base })
try {
assert.equal(JSON.parse((await once(ws, 'message'))[0]).type, 'ready')
const changed = once(ws, 'message')
const second = { ...payload, messageId: randomUUID(), ciphertext: frame() }
await json('/inbox', 'POST', sender.token, second)
assert.equal(JSON.parse((await changed)[0]).type, 'inbox_changed')
const pending = await json('/inbox?after=' + first.sequence, 'GET', recipient.token)
assert.equal(pending.length, 1)
assert.equal(pending[0].ciphertext, second.ciphertext)
assert.equal((await call('/devices/' + recipient.device.id, 'DELETE', recipient.token)).status, 204)
await json('/inbox', 'GET', recipient.token, undefined, 401)
await json('/inbox', 'POST', sender.token, { ...payload, messageId: randomUUID() }, 404)
} finally {
ws.terminate()
const [socket, senderSocket] = await Promise.all([recipient, sender].map((actor) => watchRealtime(actor, t)))
const senderEvents = []
senderSocket.on('message', (data) => senderEvents.push(JSON.parse(data)))
async function expectEvent(type, act) {
const arrival = once(socket, 'message', { signal: AbortSignal.timeout(4000) })
const [[data], result] = await Promise.all([arrival, act()])
assert.deepEqual(JSON.parse(data), type)
return result
}
const syncOnly = { type: 'inbox_changed' }
const alert = { ...syncOnly, contactId: sender.user.id, notify: true }
const contactsChanged = { type: 'contacts_changed' }
const newPayload = (ciphertext = frame()) => ({ ...payload, messageId: randomUUID(), ciphertext })
const deliver = (expected, packet) => expectEvent(expected, () => json('/inbox', 'POST', sender.token, packet))
const second = newPayload()
const result = await deliver(alert, second)
assert.deepEqual(await deliver(syncOnly, second), result)
await deliver(syncOnly, newPayload(frame(3)))
await deliver(syncOnly, { ...newPayload(), silent: true })
await expectEvent(contactsChanged, () => contact(recipient, sender, 'mute'))
await expectEvent(contactsChanged, async () => {
const reply = await call(preferences, 'POST', recipient.token, { action: 'preferences', alias: '朋友', category: '测试' })
assert.equal(reply.status, 204, await reply.text())
})
assert.deepEqual((await json('/contacts', 'GET', recipient.token))[0], {
id: sender.user.id, username: sender.user.username, state: 'friend', alias: '朋友', category: '测试', muted: true,
})
assert.equal((await json('/contacts', 'GET', sender.token))[0].muted, false)
const muted = newPayload()
await deliver(syncOnly, muted)
const pending = await json('/inbox?after=' + first.sequence, 'GET', recipient.token)
assert.equal(pending.length, 4)
assert.equal(pending[0].ciphertext, second.ciphertext)
assert.equal(pending.at(-1).ciphertext, muted.ciphertext)
await expectEvent(contactsChanged, () => contact(recipient, sender, 'unmute'))
const unmuted = (await json('/contacts', 'GET', recipient.token))[0]
assert.deepEqual([unmuted.muted, unmuted.alias, unmuted.category], [false, '朋友', '测试'])
await deliver(alert, newPayload())
assert.deepEqual(senderEvents, [])
assert.equal((await call('/devices/' + recipient.device.id, 'DELETE', recipient.token)).status, 204)
await json('/inbox', 'GET', recipient.token, undefined, 401)
await json('/inbox', 'POST', sender.token, newPayload(), 404)
})
test('设备密钥包验证签名并只领取一次', { timeout: 25000 }, async () => {