-
-
Notifications
You must be signed in to change notification settings - Fork 17
Add mention detection to update_message #302
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -118,6 +118,20 @@ def get_messages(self) -> list[Message]: | |||||
| message_dicts = self._get_messages() | ||||||
| return [Message(**message_dict) for message_dict in message_dicts] | ||||||
|
|
||||||
| def _find_mentions(self, body: str) -> list[str]: | ||||||
| """ | ||||||
| Extract mentioned usernames from a message body. | ||||||
| Finds all @mentions in the body and returns the corresponding usernames. | ||||||
| """ | ||||||
| mention_pattern = re.compile(r"@([\w-]+):?") | ||||||
| mentioned_names: Set[str] = set(re.findall(mention_pattern, body)) | ||||||
| users = self.get_users() | ||||||
| mentioned_usernames = [] | ||||||
| for username, user in users.items(): | ||||||
| if user.mention_name in mentioned_names and user.username not in mentioned_usernames: | ||||||
| mentioned_usernames.append(username) | ||||||
| return mentioned_usernames | ||||||
|
|
||||||
| def _get_messages(self) -> list[dict]: | ||||||
| """ | ||||||
| Returns the messages of the document as dict. | ||||||
|
|
@@ -137,14 +151,7 @@ def add_message(self, new_message: NewMessage) -> str: | |||||
| ) | ||||||
|
|
||||||
| # find all mentioned users and add them as message mentions | ||||||
| mention_pattern = re.compile("@([\w-]+):?") | ||||||
| mentioned_names: Set[str] = set(re.findall(mention_pattern, message.body)) | ||||||
| users = self.get_users() | ||||||
| mentioned_usernames = [] | ||||||
| for username, user in users.items(): | ||||||
| if user.mention_name in mentioned_names and user.username not in mentioned_usernames: | ||||||
| mentioned_usernames.append(username) | ||||||
| message.mentions = mentioned_usernames | ||||||
| message.mentions = self._find_mentions(message.body) | ||||||
|
|
||||||
| with self._ydoc.transaction(): | ||||||
| index = len(self._ymessages) - next((i for i, v in enumerate(self._get_messages()[::-1]) if v["time"] < timestamp), len(self._ymessages)) | ||||||
|
|
@@ -155,17 +162,23 @@ def add_message(self, new_message: NewMessage) -> str: | |||||
|
|
||||||
| return uid | ||||||
|
|
||||||
| def update_message(self, message: Message, append: bool = False): | ||||||
| def update_message(self, message: Message, append: bool = False, find_mentions: bool = False): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC the expectation is to find the mentions for the last update of a streaming message.
Suggested change
What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized that it has been discussed and updated in another comment, with the opposite argument 😄 I still think that the default could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😆 I think you make a great point though, @brichet. Maybe this should be more generic... like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I wrestled with this a bit too. I think it should be But the deeper reason is that if the developer is not explicit when using this API, it has the potential to trigger a bunch of actions repeatedly, unintendedly. While it's a bit more verbose in the downstream code, I think it's best to force folks to be explicit when triggering actions. |
||||||
| """ | ||||||
| Update a message of the document. | ||||||
| If append is True, the content will be append to the previous content. | ||||||
| If append is True, the content will be appended to the previous content. | ||||||
| If find_mentions is True, mentions will be extracted and notifications triggered (use for streaming completion). | ||||||
| """ | ||||||
| with self._ydoc.transaction(): | ||||||
| index = self._indexes_by_id[message.id] | ||||||
| initial_message = self._ymessages[index] | ||||||
| message.time = initial_message["time"] # type:ignore[index] | ||||||
| if append: | ||||||
| message.body = initial_message["body"] + message.body # type:ignore[index] | ||||||
|
|
||||||
| # Extract and update mentions from the message body | ||||||
| if find_mentions: | ||||||
| message.mentions = self._find_mentions(message.body) | ||||||
|
|
||||||
| self._ymessages[index] = asdict(message, dict_factory=message_asdict_factory) | ||||||
|
|
||||||
| def get_attachments(self) -> dict[str, Union[FileAttachment, NotebookAttachment]]: | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK the
setfunction returns unique elements, which would no prove thatUSER2is not mentioned several times.