Skip to content
Merged
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
26 changes: 24 additions & 2 deletions chatom/backend.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
from typing import Literal, Union

DISCORD = "discord"
EMAIL = "email"
IRC = "irc"
MATRIX = "matrix"
MATTERMOST = "mattermost"
MESSENGER = "messenger"
SLACK = "slack"
SYMPHONY = "symphony"
TEAMS = "teams"
TELEGRAM = "telegram"
WHATSAPP = "whatsapp"
ZULIP = "zulip"

Backend = Union[
BACKEND = Union[
Literal[DISCORD],
Literal[EMAIL],
Literal[IRC],
Literal[MATRIX],
Literal[MATTERMOST],
Literal[MESSENGER],
Literal[SLACK],
Literal[SYMPHONY],
Literal[TEAMS],
Literal[TELEGRAM],
Literal[WHATSAPP],
Literal[ZULIP],
str,
]
ALL_BACKENDS = [
DISCORD,
EMAIL,
IRC,
MATRIX,
MATTERMOST,
MESSENGER,
SLACK,
SYMPHONY,
TEAMS,
TELEGRAM,
WHATSAPP,
ZULIP,
]

__all__ = ("Backend", "DISCORD", "MATTERMOST", "MESSENGER", "SLACK", "TEAMS", "TELEGRAM", "WHATSAPP", "ZULIP")
__all__ = ("BACKEND", "DISCORD", "MATTERMOST", "MESSENGER", "SLACK", "SYMPHONY", "TEAMS", "TELEGRAM", "WHATSAPP", "ZULIP")
1 change: 1 addition & 0 deletions chatom/format/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .message import *
from .table import *
from .text import *
from .variant import *
42 changes: 42 additions & 0 deletions chatom/mention.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from .backend import BACKEND
from .user import User


def mention_user(user: User, backend: BACKEND) -> str:
"""Generate a mention string for a user based on the backend platform.

Args:
user (User): The user to mention.
backend (BACKEND): The backend platform.

Returns:
str: The formatted mention string.
"""
match backend:
case "discord":
return f"<@!{user.id}>"
case "email":
return f"<a href='mailto:{user.email}'>{user.name}</a>" if user.email else user.name
case "irc":
return f"{user.handle}" if user.handle else user.name
case "matrix":
return f"@{user.handle}:matrix.org" if user.handle else user.name
case "mattermost":
return f"@{user.handle}" if user.handle else user.name
case "messenger":
return f"@{user.name}"
case "slack":
return f"<@{user.id}>"
case "teams":
return f"<at>{user.name}</at>"
case "telegram":
return f"@{user.handle}" if user.handle else user.name
case "symphony":
return f"@{user.name}"
case "whatsapp":
return f"@{user.name}"
case "zulip":
return f"@**{user.name}**"
case _:
# Fallback to full name if no specific format is defined
return user.name
10 changes: 10 additions & 0 deletions chatom/tests/test_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Literal


class TestBackend:
def test_all_backends_listed(self):
from chatom.backend import ALL_BACKENDS, BACKEND

# Ensure that ALL_BACKENDS actually has all backends
for backend in ALL_BACKENDS:
assert Literal[backend] in BACKEND.__args__
32 changes: 32 additions & 0 deletions chatom/tests/test_mention.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

from chatom.mention import mention_user
from chatom.user import User


class TestMention:
@pytest.mark.parametrize(
"user,backend,expected",
[
(User(handle="jane_doe", id="456", name="Jane Doe"), "discord", "<@!456>"),
(User(handle="", id="789", name="Alice"), "email", "Alice"),
(User(handle="", id="101", name="Bob", email="bob@example.com"), "email", "<a href='mailto:bob@example.com'>Bob</a>"),
(User(handle="charlie", id="112", name="Charlie"), "irc", "charlie"),
(User(handle="", id="131", name="David"), "irc", "David"),
(User(handle="eve", id="415", name="Eve"), "matrix", "@eve:matrix.org"),
(User(handle="charlie", id="112", name="Charlie"), "mattermost", "@charlie"),
(User(handle="", id="131", name="David"), "mattermost", "David"),
(User(handle="eve", id="415", name="Eve"), "messenger", "@Eve"),
(User(handle="john_doe", id="123", name="John Doe"), "slack", "<@123>"),
(User(handle="frank", id="161", name="Frank"), "symphony", "@Frank"),
(User(handle="eve", id="415", name="Eve"), "teams", "<at>Eve</at>"),
(User(handle="alice", id="789", name="Alice"), "telegram", "@alice"),
(User(handle="", id="101", name="Bob"), "telegram", "Bob"),
(User(handle="grace", id="718", name="Grace"), "whatsapp", "@Grace"),
(User(handle="heidi", id="192", name="Heidi"), "zulip", "@**Heidi**"),
(User(handle="ivan", id="202", name="Ivan"), "unknown", "Ivan"),
],
)
def test_mention(self, user, backend, expected):
result = mention_user(user, backend)
assert result == expected
6 changes: 5 additions & 1 deletion chatom/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class User(BaseModel):
user: str = Field(
handle: str = Field(
description="The username of the author of the message.",
)
email: str = Field(
Expand All @@ -15,3 +15,7 @@ class User(BaseModel):
default="",
description="uid of the author, for mentions",
)
name: str = Field(
default="",
description="The display name of the author of the message.",
)
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "Minimum viable chat components"
readme = "README.md"
license = { text = "Apache-2.0" }
version = "0.1.0"
requires-python = ">=3.9"
requires-python = ">=3.10"
keywords = [
"chat",
"chatbot",
Expand All @@ -27,7 +27,6 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand Down