From b582e59eee092ef463fc2e53c7e056f4e885fe2f Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 12 Oct 2022 11:24:51 +0300 Subject: [PATCH] Add option to mark old chats as read even if they're unread on Telegram --- mautrix_telegram/config.py | 1 + mautrix_telegram/example-config.yaml | 4 ++++ mautrix_telegram/user.py | 16 ++++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 0d66c646..03edd641 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -162,6 +162,7 @@ class Config(BaseBridgeConfig): copy("bridge.backfill.msc2716") copy("bridge.backfill.double_puppet_backfill") copy("bridge.backfill.normal_groups") + copy("bridge.backfill.unread_hours_threshold") copy("bridge.backfill.forward.initial_limit") copy("bridge.backfill.forward.sync_limit") copy("bridge.backfill.incremental.messages_per_batch") diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index 3daf9dd0..ba9c5723 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -354,6 +354,10 @@ bridge: # will likely cause problems if there are multiple Matrix users in the group. normal_groups: false + # If a backfilled chat is older than this number of hours, mark it as read even if it's unread on Telegram. + # Set to -1 to let any chat be unread. + unread_hours_threshold: 720 + # Forward backfilling limits. These apply to both MSC2716 and legacy backfill. # # Using a negative initial limit is not recommended, as it would try to backfill everything in a single batch. diff --git a/mautrix_telegram/user.py b/mautrix_telegram/user.py index 3638602d..62ed8f28 100644 --- a/mautrix_telegram/user.py +++ b/mautrix_telegram/user.py @@ -16,7 +16,7 @@ from __future__ import annotations from typing import TYPE_CHECKING, Any, AsyncGenerator, AsyncIterable, Awaitable, NamedTuple, cast -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone import asyncio import time @@ -695,10 +695,22 @@ class User(DBUser, AbstractUser, BaseUser): self.log.exception(f"Error while creating {portal.tgid_log}") if portal.mxid and puppet and puppet.is_real_user: tg_space = portal.tgid if portal.peer_type == "channel" else self.tgid - if dialog.unread_count == 0: + last_message_date: datetime = cast(datetime, dialog.date) + unread_threshold_hours = self.config["bridge.backfill.unread_hours_threshold"] + force_read = ( + was_created + and unread_threshold_hours >= 0 + and last_message_date + timedelta(hours=unread_threshold_hours) < datetime.now() + ) + if dialog.unread_count == 0 or force_read: # This is usually more reliable than finding a specific message # e.g. if the last read message is a service message that isn't in the message db last_read = await DBMessage.find_last(portal.mxid, tg_space) + if force_read: + self.log.debug( + f"Marking {portal.tgid_log} as read because the last message is from " + f"{last_message_date} (unread threshold is {unread_threshold_hours} hours)" + ) else: last_read = await DBMessage.get_one_by_tgid( portal.tgid, tg_space, dialog.dialog.read_inbox_max_id