Skip to content

Commit

Permalink
feat(core): unread message
Browse files Browse the repository at this point in the history
  • Loading branch information
aerovulpe committed Sep 26, 2023
1 parent 7f6690f commit 3bdee37
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/src/lib/chat-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export declare class StartChatSessionRequest {
onMessageDeleted?: (message: Message) => void;
onChannelUpdated?: (channel: Channel) => void;
onMessageRead?: (message: Message, receipt: ReadReceipt) => void;
onMessageUnread?: (message: Message, receipt: ReadReceipt) => void;
onMessageReactionAdded?: (message: Message, reaction: Reaction) => void;
onMessageReactionRemoved?: (message: Message, reaction: Reaction) => void;
onThreadMessageReceived?: (thread: Thread, message: Message) => void;
Expand Down
33 changes: 32 additions & 1 deletion packages/core/src/lib/chatkitty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ import {
SentFileMessageResult,
SentTextMessageResult,
TextUserMessage,
ListUsersMessagesRequest, UpdateMessagePropertiesRequest, UpdatedMessagePropertiesSucceededResult,
ListUsersMessagesRequest,
UpdateMessagePropertiesRequest,
UpdatedMessagePropertiesSucceededResult,
UnreadMessageRequest, UnreadMessageResult, UnreadMessageSucceededResult,
} from './message';
import { Notification } from './notification';
import { ChatKittyObserver, ChatKittyUnsubscribe } from './observer';
Expand Down Expand Up @@ -836,6 +839,7 @@ export class ChatKitty {
const onMessageDeleted = request.onMessageDeleted;
const onChannelUpdated = request.onChannelUpdated;
const onMessageRead = request.onMessageRead;
const onMessageUnread = request.onMessageUnread;
const onMessageReactionAdded = request.onMessageReactionAdded;
const onMessageReactionRemoved = request.onMessageReactionRemoved;
const onThreadMessageReceived = request.onThreadMessageReceived;
Expand Down Expand Up @@ -1001,6 +1005,21 @@ export class ChatKitty {
});
}

if (onMessageUnread) {
messageReadUnsubscribe = this.stompX.listenForEvent<ReadReceipt>({
topic: request.channel._topics.readReceipts,
event: 'message.read_receipt.deleted',
onSuccess: (receipt) => {
this.stompX.relayResource<Message>({
destination: receipt._relays.message,
onSuccess: (message) => {
onMessageUnread(message, receipt);
},
});
},
});
}

if (onMessageReactionAdded) {
messageReactionAddedUnsubscribe = this.stompX.listenForEvent<Reaction>({
topic: request.channel._topics.reactions,
Expand Down Expand Up @@ -1399,6 +1418,18 @@ export class ChatKitty {
});
}

unreadMessage(request: UnreadMessageRequest): Promise<UnreadMessageResult> {
return new Promise((resolve) => {
this.stompX.sendAction<never>({
destination: request.message._actions.unread,
body: {},
onSent: () =>
resolve(new UnreadMessageSucceededResult(request.message)),
onError: (error) => resolve(new ChatKittyFailedResult(error)),
});
});
}

retrieveLastReadMessage(
request: RetrieveLastReadMessageRequest
): Promise<RetrieveLastReadMessageResult> {
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/lib/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ declare class MessageRelays {

declare class MessageActions {
read: string;
unread: string;
reply: string;
deleteForMe: string;
delete: string;
Expand Down Expand Up @@ -203,6 +204,10 @@ export declare class ReadMessageRequest {
message: Message;
}

export declare class UnreadMessageRequest {
message: Message;
}

export type ReadMessageResult =
| ReadMessageSucceededResult
| ChatKittyFailedResult;
Expand All @@ -213,6 +218,16 @@ export class ReadMessageSucceededResult extends ChatKittySucceededResult {
}
}

export type UnreadMessageResult =
| UnreadMessageSucceededResult
| ChatKittyFailedResult;

export class UnreadMessageSucceededResult extends ChatKittySucceededResult {
constructor(public message: Message) {
super();
}
}

export declare class EditMessageRequest {
message: Message;
body: string;
Expand Down

0 comments on commit 3bdee37

Please sign in to comment.