From 76410ee7cbfa7c4357dc5f9b18b13a4b82a0b67f Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 22 Jul 2018 17:42:29 -0400 Subject: [PATCH] Implement Matrix->Telegram presence --- mautrix_telegram/matrix.py | 5 ++++- mautrix_telegram/user.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mautrix_telegram/matrix.py b/mautrix_telegram/matrix.py index 81660a9b..28cf6796 100644 --- a/mautrix_telegram/matrix.py +++ b/mautrix_telegram/matrix.py @@ -308,7 +308,10 @@ class MatrixHandler: await portal.mark_read(user, event_id) async def handle_presence(self, user: str, presence: str): - pass + user = await User.get_by_mxid(user).ensure_started() + if not await user.is_logged_in(): + return + await user.set_presence(presence == "online") async def handle_typing(self, room_id: str, now_typing: List[str]): portal = Portal.get_by_mxid(room_id) diff --git a/mautrix_telegram/user.py b/mautrix_telegram/user.py index 110c9f8e..9c42089a 100644 --- a/mautrix_telegram/user.py +++ b/mautrix_telegram/user.py @@ -22,6 +22,7 @@ import re from telethon.tl.types import * from telethon.tl.types.contacts import ContactsNotModified from telethon.tl.functions.contacts import GetContactsRequest, SearchRequest +from telethon.tl.functions.account import UpdateStatusRequest from mautrix_appservice import MatrixRequestError from .db import User as DBUser, Contact as DBContact @@ -188,6 +189,9 @@ class User(AbstractUser): def ensure_started(self, even_if_no_session=False) -> "Awaitable[User]": return super().ensure_started(even_if_no_session) + def set_presence(self, online: bool = True): + return self.client(UpdateStatusRequest(offline=not online)) + async def update_info(self, info: User = None): info = info or await self.client.get_me() changed = False