This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues/pull-requests.
uniborg/uniborg/util.py

46 lines
1.5 KiB
Python
Raw Normal View History

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
2019-02-26 22:27:01 +00:00
import functools
import re
2019-02-26 22:27:01 +00:00
import signal
from telethon import events
2018-04-08 12:43:23 +00:00
from telethon.tl.functions.messages import GetPeerDialogsRequest
2018-04-08 14:44:09 +00:00
2018-04-08 12:43:23 +00:00
async def is_read(borg, entity, message, is_out=None):
"""
Returns True if the given message (or id) has been read
if a id is given, is_out needs to be a bool
"""
2018-04-08 14:44:09 +00:00
is_out = getattr(message, "out", is_out)
2018-04-08 12:43:23 +00:00
if not isinstance(is_out, bool):
2018-04-08 14:44:09 +00:00
raise ValueError(
2018-06-10 17:12:53 +00:00
"Message was id but is_out not provided or not a bool")
2018-04-08 14:44:09 +00:00
message_id = getattr(message, "id", message)
2018-04-08 12:43:23 +00:00
if not isinstance(message_id, int):
2018-04-08 14:44:09 +00:00
raise ValueError("Failed to extract id from message")
2018-04-08 12:43:23 +00:00
dialog = (await borg(GetPeerDialogsRequest([entity]))).dialogs[0]
max_id = dialog.read_outbox_max_id if is_out else dialog.read_inbox_max_id
return message_id <= max_id
2019-02-26 22:27:01 +00:00
def _handle_timeout(signum, frame):
raise TimeoutError("Execution took too long")
def sync_timeout(seconds):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.setitimer(signal.ITIMER_REAL, seconds)
try:
r = func(*args, **kwargs)
finally:
signal.alarm(0)
return r
return wrapper
return decorator