Matrix: Refactor function 'matrix_to_zulip'
This commit is contained in:
parent
c467b5fd03
commit
bb39ce981d
|
@ -41,7 +41,9 @@ def die(signal: int, frame: FrameType) -> None:
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
|
|
||||||
def zulip_to_matrix_username(full_name: str, site: str) -> str:
|
def zulip_to_matrix_username(full_name: str, site: str) -> str:
|
||||||
return "@**{0}**:{1}".format(full_name, site)
|
# Strip spaces from the full_name
|
||||||
|
full_name = "".join(full_name.split(' '))
|
||||||
|
return "@zulip_{0}:{1}".format(full_name, site)
|
||||||
|
|
||||||
def matrix_to_zulip(zulip_client: zulip.Client, zulip_config: Dict[str, Any],
|
def matrix_to_zulip(zulip_client: zulip.Client, zulip_config: Dict[str, Any],
|
||||||
matrix_config: Dict[str, Any]) -> Callable[[Any, Dict[str, Any]], None]:
|
matrix_config: Dict[str, Any]) -> Callable[[Any, Dict[str, Any]], None]:
|
||||||
|
@ -95,18 +97,26 @@ def zulip_to_matrix(config: Dict[str, Any], room: Any) -> Callable[[Dict[str, An
|
||||||
"""
|
"""
|
||||||
Zulip -> Matrix
|
Zulip -> Matrix
|
||||||
"""
|
"""
|
||||||
isa_stream = msg["type"] == "stream"
|
message_valid = check_zulip_message_validity(msg, config)
|
||||||
not_from_bot = msg["sender_email"] != config["email"]
|
if message_valid:
|
||||||
in_the_specified_stream = msg["display_recipient"] == config["stream"]
|
|
||||||
at_the_specified_subject = msg["subject"] == config["subject"]
|
|
||||||
if isa_stream and not_from_bot and in_the_specified_stream and at_the_specified_subject:
|
|
||||||
matrix_username = zulip_to_matrix_username(msg["sender_full_name"], site_without_http)
|
matrix_username = zulip_to_matrix_username(msg["sender_full_name"], site_without_http)
|
||||||
matrix_text = "{0}: {1}".format(matrix_username,
|
matrix_text = "{0}: {1}".format(matrix_username, msg["content"])
|
||||||
msg["content"])
|
# Forward Zulip message to Matrix
|
||||||
print(matrix_text)
|
|
||||||
room.send_text(matrix_text)
|
room.send_text(matrix_text)
|
||||||
return _zulip_to_matrix
|
return _zulip_to_matrix
|
||||||
|
|
||||||
|
def check_zulip_message_validity(msg: Dict[str, Any], config: Dict[str, Any]) -> bool:
|
||||||
|
is_a_stream = msg["type"] == "stream"
|
||||||
|
in_the_specified_stream = msg["display_recipient"] == config["stream"]
|
||||||
|
at_the_specified_subject = msg["subject"] == config["subject"]
|
||||||
|
|
||||||
|
# We do this to identify the messages generated from Matrix -> Zulip
|
||||||
|
# and we make sure we don't forward it again to the Matrix.
|
||||||
|
not_from_zulip_bot = msg["sender_email"] != config["email"]
|
||||||
|
if is_a_stream and not_from_zulip_bot and in_the_specified_stream and at_the_specified_subject:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
signal.signal(signal.SIGINT, die)
|
signal.signal(signal.SIGINT, die)
|
||||||
logging.basicConfig(level=logging.WARNING)
|
logging.basicConfig(level=logging.WARNING)
|
||||||
|
|
Loading…
Reference in a new issue