Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message reactions POC #445

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Message reactions POC #445

wants to merge 1 commit into from

Conversation

vladvelici
Copy link
Contributor

@vladvelici vladvelici commented Jan 7, 2025

Context

  • POC for message reactions
  • For the POC, this uses the realtime HTTP endpoint instead of a chat-specific one
  • Introduction of a Message Window abstraction to simplify keeping a list of messages up to date

Does not yet have:

  • removing a reaction
  • seeing if you have reacted
  • fetching reactions from history, you only see reactions if you/someone reacts while you're online

JIRA:

Checklist

  • QA'd by the author.
  • Unit tests created (if applicable).
  • Integration tests created (if applicable).
  • Follow coding style guidelines found here.
  • TypeDoc updated (if applicable).
  • (Optional) Update documentation for new features.
  • Browser tests created (if applicable).
  • In repo demo app updated (if applicable).

Testing Instructions

  • React to messages
  • MessageWindow is used in demo app so you can see it in action

Copy link

coderabbitai bot commented Jan 7, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Jan 7, 2025

Coverage Report

Status Category Percentage Covered / Total
🔴 Lines 87.75% (🎯 92%) 3360 / 3829
🔴 Statements 87.75% (🎯 92%) 3360 / 3829
🟢 Functions 94.36% (🎯 92%) 251 / 266
🟢 Branches 93.83% (🎯 93%) 867 / 924
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/core/chat-api.ts 93.05% 90.9% 92.85% 93.05% 206-215
src/core/events.ts 100% 100% 100% 100%
src/core/message-events.ts 100% 100% 100% 100%
src/core/message-window.ts 0% 100% 100% 0% 3-187
src/core/message.ts 43.58% 100% 81.25% 43.58% 338-406, 410-424, 427-437
src/core/messages.ts 80.48% 83.82% 95.83% 80.48% 404-406, 436-440, 452-456, 560-562, 581-582, 586-587, 594-596, 610-611, 613-614, 652-663, 667-671, 675-698
src/core/utils/event-emitter.ts 100% 100% 100% 100%
src/react/hooks/use-messages.ts 96.49% 93.54% 100% 96.49% 167-168, 175-176
Generated in workflow #1893 for commit 70d7fad by the Vitest Coverage Report Action

@vladvelici vladvelici requested review from splindsay-92 and AndyTWF and removed request for splindsay-92 January 7, 2025 12:33
* (eg. reactions) this is the `serial` of the relevant chat message, usually
* found under `refSerial`.
*/
get messageSerial(): string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so sure about conflating the two here, unless we decide to call it something like parentSerial ? I'll have more of a think on this, perhaps we can do some funky union type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the word message has too many meanings. If it was an pubsub message, totally confusing. If we talk about a chat message (my intention), it should be clear it's the serial of the chat message (and for reactions it's the serial of the message it refers to).

The purpose is to be able to get the serial of the chat message for any event easily (no type-casting, no switch statements). Pseudocode:

subscribeAll(event => {
  storedMessage = findMessageBySerial(event.messageSerial)
  updateMessage(event.messageSerial, storedMessage.apply(event))
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can definitely rename to parentSerial

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with parentSerial - though does having the convenience method possibly make things a bit obscure?

Would it be more explicit to have the serial on the individual types - so MessageEventPayload simply has messageSerial and the annotation ones has parentSerial?

Im concerned we might be lumping too many principles into one here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes development easier if you can get the base message serial without any type-casting in the case of subscribeAll: you can ignore events early on in your listener, you can find the base message, and you can call message.apply() without type-casting as well.

If this isn't a use case we care about, we can just remove this field. The typed events already have either serial or refSerial which also matches realtime.

@@ -202,6 +202,18 @@ export class ChatApi {
);
}

async reactToMessage(roomId: string, serial: string, reaction: string): Promise<void> {
roomId = encodeURIComponent(roomId);
return this._makeAuthorizedRequest(`/channels/${roomId}::$chat::$chatMessages/messages`, 'POST', {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine this is temporary - but we should definitely have a dedicated chat endpoint for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's temporary, I wanted to get something working end-to-end first

return this._makeAuthorizedRequest(`/channels/${roomId}::$chat::$chatMessages/messages`, 'POST', {
action: 4,
data: reaction,
refType: 'reaction:emoji.v1',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be using the text reaction reftype - so people aren't limited to just UTF-8 emoji's

* (eg. reactions) this is the `serial` of the relevant chat message, usually
* found under `refSerial`.
*/
get messageSerial(): string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with parentSerial - though does having the convenience method possibly make things a bit obscure?

Would it be more explicit to have the serial on the individual types - so MessageEventPayload simply has messageSerial and the annotation ones has parentSerial?

Im concerned we might be lumping too many principles into one here

/**
* Payload for a message event.
*/
export interface MessageEventPayload extends EventPayload {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having a think about this. In TypeScript its quite easy for us to say "check the enum, then assume the object shape". In other languages, it's a bit less forgiving / frowned upon. Generally speaking - type safety and not relying on users to do conversions themselves is good.

Should we consider other ways to subscribe to messages, e.g. separate methods for messages themselves and annotations?

I don't have a strong view right now - but just want to make sure we consider type systems outside of TS here


apply(event: AnyMessageEvent): Message {
if (event.messageSerial !== this.serial) {
throw new ErrorInfo('apply(): Cannot apply event to message, serials do not match', 50000, 500);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be 4xx as would be user-error


switch (event.type) {
case MessageEvents.Created: {
// created events shouldn't get here, we'll treat as no-op
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should explicitly call this out as a 4xx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

}

// Clone a message, optionally replace the given fields
private static clone(source: Message, replace?: Partial<Message>): DefaultMessage {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wouldn't be a straight-up clone, as this message would share reference types such as the reactions and metadata.

For deep cloning, you can do import cloneDeep from 'lodash.clonedeep';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Will use cloneDeep for this and the reactions.

}
}

function cloneReactions(obj: Map<string, MessageReactionSummary>): Map<string, MessageReactionSummary> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use lodash.clonedeep for this?

/**
* Add a reaction to a message.
*/
react(message: Message, reaction: string): Promise<void>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest addReaction, only because eventually we'll want to remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, will change. addReaction and removeReaction are better than react and removeReaction.

subscribe(listener: MessageListener): MessageSubscriptionResponse {
this._logger.trace('Messages.subscribe();');
super.on([MessageEvents.Created, MessageEvents.Updated, MessageEvents.Deleted], listener);
subscribe(listener: MessageListener | MessageListenerObject): MessageSubscriptionResponse {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we're in the 0.x versions, I'd be comfortable with just making the breaking change - rather than having both versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to remove the MessageListener part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants