Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/platforms/mattermost/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,8 @@ async def _handle_ws_event(self, event: Dict[str, Any]) -> None:
# Determine message type.
file_ids = post.get("file_ids") or []
msg_type = MessageType.TEXT
if message_text[:1].isspace() and message_text.lstrip().startswith("/"):
message_text = message_text.lstrip()
if message_text.startswith("/"):
msg_type = MessageType.COMMAND

Expand Down
50 changes: 50 additions & 0 deletions tests/gateway/test_mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from unittest.mock import MagicMock, patch, AsyncMock

from gateway.config import Platform, PlatformConfig
from gateway.platforms.base import MessageType


# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -366,6 +367,55 @@ async def test_channel_type_mapping(self):
msg_event = self.adapter.handle_message.call_args[0][0]
assert msg_event.source.chat_type == "dm"

@pytest.mark.asyncio
async def test_leading_space_slash_command_is_command(self):
"""Mattermost mobile suggests leading-space slash commands."""
post_data = {
"id": "post_cmd",
"user_id": "user_123",
"channel_id": "chan_dm",
"message": " /new",
}
event = {
"event": "posted",
"data": {
"post": json.dumps(post_data),
"channel_type": "D",
"sender_name": "@bob",
},
}

await self.adapter._handle_ws_event(event)
assert self.adapter.handle_message.called
msg_event = self.adapter.handle_message.call_args[0][0]
assert msg_event.text == "/new"
assert msg_event.message_type is MessageType.COMMAND
assert msg_event.get_command() == "new"

@pytest.mark.asyncio
async def test_leading_space_normal_text_is_preserved(self):
"""Only command-shaped mobile messages should be normalized."""
post_data = {
"id": "post_text",
"user_id": "user_123",
"channel_id": "chan_dm",
"message": " hello",
}
event = {
"event": "posted",
"data": {
"post": json.dumps(post_data),
"channel_type": "D",
"sender_name": "@bob",
},
}

await self.adapter._handle_ws_event(event)
assert self.adapter.handle_message.called
msg_event = self.adapter.handle_message.call_args[0][0]
assert msg_event.text == " hello"
assert msg_event.message_type is MessageType.TEXT

@pytest.mark.asyncio
async def test_thread_id_from_root_id(self):
"""Post with root_id should have thread_id set."""
Expand Down