Skip to content

Commit

Permalink
validate reaction icons message params
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 committed May 8, 2024
1 parent d5f5749 commit 6330c59
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
15 changes: 10 additions & 5 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..layout.card import Card
from ..layout.spacer import VSpacer
from ..pane.image import SVG
from .icon import ChatReactionIcons
from .message import ChatMessage

if TYPE_CHECKING:
Expand Down Expand Up @@ -291,6 +292,15 @@ def _cleanup(self, root: Model | None = None) -> None:
self._card._cleanup(root)
super()._cleanup(root)

@param.depends("message_params", watch=True, on_init=True)
def _validate_message_params(self):
reaction_icons = self.message_params.get("reaction_icons")
if isinstance(reaction_icons, ChatReactionIcons):
raise ValueError(
"Cannot pass a ChatReactionIcons instance to message_params; "
"use a dict of the options instead."
)

@param.depends("load_buffer", "auto_scroll_limit", "scroll_button_threshold", watch=True)
def _update_chat_log_params(self):
self._chat_log.load_buffer = self.load_buffer
Expand Down Expand Up @@ -350,11 +360,6 @@ def _build_message(
f"e.g. {{'object': 'Hello World'}}; got {value!r}"
)
message_params = dict(value, renderers=self.renderers, **self.message_params)
for param_key, param_value in message_params.items():
if hasattr(param_value, "clone"):
# fixes chat reaction icons being linked across all messages
message_params[param_key] = param_value.clone()

if user:
message_params["user"] = user
if avatar:
Expand Down
12 changes: 4 additions & 8 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,10 @@ def test_forward_message_params(self, chat_feed):
assert chat_message.reactions == ["like"]
assert chat_message.reaction_icons.options == {"like": "thumb-up"}

def test_message_params_cloned(self, chat_feed):
chat_feed.reaction_icons = ChatReactionIcons(options={"like": "thumb-up", "dislike": "thumb-down"})
msg1 = chat_feed.send("Hello")
msg2 = chat_feed.send("Hey")

msg1.reactions = ["like"]
assert msg1.reactions == ["like"]
assert msg2.reactions == []
def test_message_params_no_chat_reaction_icons_instance(self, chat_feed):
with pytest.raises(ValueError, match="Cannot pass"):
chat_feed.message_params = {"reaction_icons": ChatReactionIcons(
options={"like": "thumb-up", "dislike": "thumb-down"})}

def test_update_chat_log_params(self, chat_feed):
chat_feed = ChatFeed(load_buffer=5, scroll_button_threshold=5, auto_scroll_limit=5)
Expand Down

0 comments on commit 6330c59

Please sign in to comment.