Skip to content

Commit

Permalink
Strip displayname format in Matrix->Telegram non-username mentions. F…
Browse files Browse the repository at this point in the history
…ixes #138
  • Loading branch information
tulir committed Dec 20, 2018
1 parent 1bebdcb commit 75b6e4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 2 additions & 3 deletions mautrix_telegram/formatter/from_matrix/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

from .html_reader import HTMLNode, read_html


ParsedMessage = Tuple[str, List[TypeMessageEntity]]


Expand Down Expand Up @@ -155,8 +154,8 @@ def link_to_tstring(cls, node: HTMLNode, ctx: RecursionContext) -> TelegramMessa
if user.username:
return TelegramMessage(f"@{user.username}").format(Mention)
elif user.tgid:
return TelegramMessage(user.displayname or msg.text).format(MentionName,
user_id=user.tgid)
displayname = user.plain_displayname or msg.text
return TelegramMessage(displayname).format(MentionName, user_id=user.tgid)
return msg

room = cls.room_regex.match(href)
Expand Down
15 changes: 12 additions & 3 deletions mautrix_telegram/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ async def is_logged_in() -> bool:
""" Is True if the puppet is logged in. """
return True

@property
def plain_displayname(self) -> str:
tpl = config["bridge.displayname_template"]
if tpl == "{displayname}":
# Template has no extra stuff, no need to parse.
return self.displayname
regex = re.compile("^" + re.escape(tpl).replace(re.escape("{displayname}"), "(.+?)") + "$")
match = regex.match(self.displayname)
return match.group(1) or self.displayname

# region Custom puppet management
def _fresh_intent(self) -> IntentAPI:
return (self.az.intent.user(self.custom_mxid, self.access_token)
Expand Down Expand Up @@ -312,8 +322,7 @@ def get_displayname(info: User, enable_format: bool = True) -> str:
"first name": info.first_name,
"last name": info.last_name,
}
preferences = config.get("bridge.displayname_preference",
["full name", "username", "phone"])
preferences = config["bridge.displayname_preference"]
name = None
for preference in preferences:
name = data[preference]
Expand All @@ -327,7 +336,7 @@ def get_displayname(info: User, enable_format: bool = True) -> str:

if not enable_format:
return name
return config.get("bridge.displayname_template", "{displayname} (Telegram)").format(
return config["bridge.displayname_template"].format(
displayname=name)

async def update_info(self, source: 'AbstractUser', info: User) -> None:
Expand Down
4 changes: 4 additions & 0 deletions mautrix_telegram/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def human_tg_id(self) -> str:
def displayname(self) -> str:
return self.mxid_localpart

@property
def plain_displayname(self) -> str:
return self.displayname

@property
def db_contacts(self) -> List[DBContact]:
return [self.db.merge(DBContact(user=self.tgid, contact=puppet.id))
Expand Down

0 comments on commit 75b6e4f

Please sign in to comment.