Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chat-message-edit-timestamp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/rtc-node': patch
---

Fix `ChatMessage.editTimestamp` being `NaN` instead of `undefined` for a message that was never edited.
18 changes: 3 additions & 15 deletions packages/livekit-rtc/src/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import type { RemoteTrackPublication, TrackPublication } from './track_publicati
import { LocalTrackPublication } from './track_publication.js';
import type { Transcription } from './transcription.js';
import type { ChatMessage } from './types.js';
import { byteStreamInfoFromProto, textStreamInfoFromProto } from './utils.js';
import { byteStreamInfoFromProto, chatMessageFromProto, textStreamInfoFromProto } from './utils.js';
import { numberToBigInt } from './utils.js';

export abstract class Participant {
Expand Down Expand Up @@ -651,13 +651,7 @@ export class LocalParticipant extends Participant {

switch (cb.message.case) {
case 'chatMessage':
const { id, timestamp, editTimestamp, message } = cb.message.value!;
return {
id: id!,
timestamp: Number(timestamp),
editTimestamp: Number(editTimestamp),
message: message!,
};
return chatMessageFromProto(cb.message.value!);
case 'error':
default:
throw new Error(cb.message.value);
Expand Down Expand Up @@ -698,13 +692,7 @@ export class LocalParticipant extends Participant {

switch (cb.message.case) {
case 'chatMessage':
const { id, timestamp, editTimestamp, message } = cb.message.value!;
return {
id: id!,
timestamp: Number(timestamp),
editTimestamp: Number(editTimestamp),
message: message!,
};
return chatMessageFromProto(cb.message.value!);
case 'error':
default:
throw new Error(cb.message.value);
Expand Down
10 changes: 2 additions & 8 deletions packages/livekit-rtc/src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import type { ChatMessage } from './types.js';
import {
bigIntToNumber,
byteStreamInfoFromProto,
chatMessageFromProto,
numberToBigInt,
textStreamInfoFromProto,
} from './utils.js';
Expand Down Expand Up @@ -825,14 +826,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
}
} else if (ev.case == 'chatMessage') {
const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
const { id, message: messageText, timestamp, editTimestamp, generated } = ev.value.message!;
const message: ChatMessage = {
id: id!,
message: messageText!,
timestamp: Number(timestamp),
editTimestamp: Number(editTimestamp),
generated,
};
const message: ChatMessage = chatMessageFromProto(ev.value.message!);
this.emit(RoomEvent.ChatMessage, message, participant);
} else if (ev.case == 'dataPacketReceived') {
// Can be undefined if the data is sent from a Server SDK
Expand Down
34 changes: 33 additions & 1 deletion packages/livekit-rtc/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { ChatMessage as ProtoChatMessage } from '@livekit/rtc-ffi-bindings';
import { describe, expect, it } from 'vitest';
import { splitUtf8 } from './utils.js';
import { chatMessageFromProto, splitUtf8 } from './utils.js';

describe('splitUtf8', () => {
it('splits a string into chunks of the given size', () => {
Expand Down Expand Up @@ -49,3 +50,34 @@ describe('splitUtf8', () => {
expect(splitUtf8('', 5)).toEqual([]);
});
});

describe('chatMessageFromProto', () => {
it('leaves editTimestamp undefined for a message that was never edited', () => {
const message = chatMessageFromProto(
new ProtoChatMessage({ id: 'msg-1', message: 'hello', timestamp: 1700000000000n }),
);

expect(message).toEqual({
id: 'msg-1',
message: 'hello',
timestamp: 1700000000000,
editTimestamp: undefined,
generated: undefined,
});
});

it('converts editTimestamp and generated for an edited message', () => {
const message = chatMessageFromProto(
new ProtoChatMessage({
id: 'msg-2',
message: 'hello again',
timestamp: 1700000000000n,
editTimestamp: 1700000005000n,
generated: true,
}),
);

expect(message.editTimestamp).toBe(1700000005000);
expect(message.generated).toBe(true);
});
});
13 changes: 13 additions & 0 deletions packages/livekit-rtc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// SPDX-License-Identifier: Apache-2.0
import type {
ByteStreamInfo as ProtoByteStreamInfo,
ChatMessage as ProtoChatMessage,
TextStreamInfo as ProtoTextStreamInfo,
} from '@livekit/rtc-ffi-bindings';
import type { ByteStreamInfo, TextStreamInfo } from './data_streams/types.js';
import type { ChatMessage } from './types.js';

/** convert bigints to numbers preserving undefined values */
export function bigIntToNumber<T extends bigint | undefined>(
Expand Down Expand Up @@ -70,3 +72,14 @@ export function byteStreamInfoFromProto(info: ProtoByteStreamInfo): ByteStreamIn
attributes: info.attributes,
};
}

/** @internal */
export function chatMessageFromProto(info: ProtoChatMessage): ChatMessage {
return {
id: info.id!,
message: info.message!,
timestamp: bigIntToNumber(info.timestamp!),
editTimestamp: bigIntToNumber(info.editTimestamp),
generated: info.generated,
};
}
Loading