Skip to content

Commit

Permalink
move to http endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
vladvelici committed Feb 20, 2025
1 parent e88dfc2 commit 2a13f98
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
9 changes: 5 additions & 4 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"@ably/chat": "file:..",
"ably": "https://github.com/ably/ably-js.git#support-annotations-deletes",
"ably": "github:ably/ably-js#a8f973857763975df146865b3f72e6f05ccf28bf",
"clsx": "^2.1.1",
"nanoid": "^5.0.9",
"react": "^18.3.1",
Expand Down
36 changes: 32 additions & 4 deletions src/core/chat-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ interface DeleteMessageParams {
metadata?: MessageOperationMetadata;
}

interface MessageReactionData {
refType : string;
reaction : string;
count? : number;
}

/**
* Chat SDK Backend
*/
Expand Down Expand Up @@ -162,7 +168,7 @@ export class ChatApi {
return { ...data, ...paginatedResult };
}

async deleteMessage(roomId: string, serial: string, params?: DeleteMessageParams): Promise<DeleteMessageResponse> {
deleteMessage(roomId: string, serial: string, params?: DeleteMessageParams): Promise<DeleteMessageResponse> {
const body: { description?: string; metadata?: MessageOperationMetadata } = {
description: params?.description,
metadata: params?.metadata,
Expand All @@ -177,7 +183,7 @@ export class ChatApi {
);
}

async sendMessage(roomId: string, params: SendMessageParams): Promise<CreateMessageResponse> {
sendMessage(roomId: string, params: SendMessageParams): Promise<CreateMessageResponse> {
const body: {
text: string;
metadata?: MessageMetadata;
Expand All @@ -193,7 +199,7 @@ export class ChatApi {
return this._makeAuthorizedRequest<CreateMessageResponse>(`/chat/v2/rooms/${roomId}/messages`, 'POST', body);
}

async updateMessage(roomId: string, serial: string, params: UpdateMessageParams): Promise<UpdateMessageResponse> {
updateMessage(roomId: string, serial: string, params: UpdateMessageParams): Promise<UpdateMessageResponse> {
const encodedSerial = encodeURIComponent(serial);
roomId = encodeURIComponent(roomId);
return this._makeAuthorizedRequest<UpdateMessageResponse>(
Expand All @@ -203,7 +209,29 @@ export class ChatApi {
);
}

async getOccupancy(roomId: string): Promise<OccupancyEvent> {
addMessageReaction(roomId: string, serial: string, data: MessageReactionData): Promise<void> {
const encodedSerial = encodeURIComponent(serial);
roomId = encodeURIComponent(roomId);
return this._makeAuthorizedRequest<void>(

Check failure on line 215 in src/core/chat-api.ts

View workflow job for this annotation

GitHub Actions / lint

void is only valid as a return type or generic type argument
`/chat/v2/rooms/${roomId}/messages/${encodedSerial}/reactions`,
'POST',
data,
);
}

deleteMessageReaction(roomId: string, serial: string, data: Omit<MessageReactionData, 'count'>): Promise<void> {
const encodedSerial = encodeURIComponent(serial);
roomId = encodeURIComponent(roomId);
return this._makeAuthorizedRequest<void>(

Check failure on line 225 in src/core/chat-api.ts

View workflow job for this annotation

GitHub Actions / lint

void is only valid as a return type or generic type argument
`/chat/v2/rooms/${roomId}/messages/${encodedSerial}/reactions`,
'DELETE',
undefined,
data,
);
}


getOccupancy(roomId: string): Promise<OccupancyEvent> {
roomId = encodeURIComponent(roomId);
return this._makeAuthorizedRequest<OccupancyEvent>(`/chat/v1/rooms/${roomId}/occupancy`, 'GET');
}
Expand Down
37 changes: 21 additions & 16 deletions src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,11 @@ export class DefaultMessageReactions implements Reactions {
constructor(
private readonly _logger: Logger,
private readonly _api: ChatApi,
private readonly _roomID: string,
private readonly _channel: Ably.RealtimeChannel,
) {
this._channel.annotations.subscribe(this._processAnnotationEvent.bind(this));
void _channel.subscribe(this._processMessageEvent.bind(this));
void _channel.annotations.subscribe(this._processAnnotationEvent.bind(this));

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > sending message > should be able to send message and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > sending message > should be able to delete a message and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > headers and metadata > should be able to send message with headers and metadata and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > subscribing to updates > should subscribe to all message events

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > unsubscribes from messages

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > unsubscribing from all messages

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > invalid incoming messages > should handle invalid inbound messages: unknown event name

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages.test.ts > Messages > invalid incoming messages > should handle invalid inbound messages: unknown action name

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > sending message > should be able to send message and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > sending message > should be able to delete a message and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > headers and metadata > should be able to send message with headers and metadata and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > subscribing to updates > should subscribe to all message events

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > unsubscribes from messages

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > unsubscribing from all messages

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > invalid incoming messages > should handle invalid inbound messages: unknown event name

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages.test.ts > Messages > invalid incoming messages > should handle invalid inbound messages: unknown action name

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > sending message > should be able to send message and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > sending message > should be able to delete a message and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > headers and metadata > should be able to send message with headers and metadata and get it back from response

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > subscribing to updates > should subscribe to all message events

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > unsubscribes from messages

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > unsubscribing from all messages

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > invalid incoming messages > should handle invalid inbound messages: unknown event name

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > invalid incoming messages > should handle invalid inbound messages: unknown action name

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20

Check failure on line 325 in src/core/messages.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages.test.ts > Messages > invalid incoming messages > should handle invalid inbound messages: no data

TypeError: Cannot read properties of undefined (reading 'subscribe') ❯ new DefaultMessageReactions src/core/messages.ts:325:31 ❯ new DefaultMessages src/core/messages.ts:466:22 ❯ new DefaultRoom src/core/room.ts:176:22 ❯ Module.makeRandomRoom test/helper/room.ts:49:10 ❯ test/core/messages.test.ts:58:20
}

private _processAnnotationEvent(event: Ably.Annotation) {
Expand Down Expand Up @@ -359,25 +361,28 @@ export class DefaultMessageReactions implements Reactions {
});
}

add(message: { serial: string }, refType: ReactionRefType, reaction: string, score?: number): Promise<void> {
if (score !== undefined && refType != ReactionRefType.Many) {
throw new Ably.ErrorInfo('message reaction score is only supported for reaction:many.v1', 40000, 400);
}
add(message: { serial: string }, refType: ReactionRefType, reaction: string, count?: number): Promise<void> {
return this._api.addMessageReaction(this._roomID, message.serial, {refType: refType, reaction: reaction, count: count});

if (refType === ReactionRefType.Many) {
score = score && score >= 1 ? score : 1;
return this._channel.annotations.publish(
message.serial,
refType,
JSON.stringify({ emoji: reaction, count: score }),
);
}
// if (score !== undefined && refType != ReactionRefType.Many) {
// throw new Ably.ErrorInfo('message reaction score is only supported for reaction:many.v1', 40000, 400);
// }

return this._channel.annotations.publish(message.serial, refType, reaction);
// let payload = reaction;
// if (refType === ReactionRefType.Many) {
// score = score && score >= 1 ? score : 1;
// payload = JSON.stringify({ emoji: reaction, count: score });
// }

// return this._channel.annotations.publish(message.serial, refType, payload);
}

remove(message: Message, refType: ReactionRefType, reaction: string): Promise<void> {
return (this._channel.annotations as any).delete(message.serial, refType, reaction) as Promise<void>;
let payload = reaction;
if (refType === ReactionRefType.Many) {
payload = JSON.stringify({ emoji: reaction });
}
return this._channel.annotations.delete(message.serial, refType, payload);
}

/**
Expand Down Expand Up @@ -458,7 +463,7 @@ export class DefaultMessages
this._logger = logger;
this._listenerSubscriptionPoints = new Map<MessageListener, Promise<{ fromSerial: string }>>();

this.reactions = new DefaultMessageReactions(this._logger, this._chatApi, this._channel);
this.reactions = new DefaultMessageReactions(this._logger, this._chatApi, this._roomId, this._channel);
}

/**
Expand Down

0 comments on commit 2a13f98

Please sign in to comment.