-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to disable notifications during initial backfill
- Loading branch information
Showing
5 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__version__ = "0.5.2" | ||
__version__ = "0.5.3" | ||
__author__ = "Tulir Asokan <[email protected]>" | ||
__all__ = ["api", "appservice", "bridge", "client", "errors", "util", "types"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright (c) 2020 Tulir Asokan | ||
# | ||
# 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/. | ||
from typing import Optional, Type | ||
import logging | ||
|
||
from mautrix.types import RoomID | ||
from mautrix.appservice import IntentAPI | ||
from mautrix.api import Method, Path, PathBuilder | ||
from mautrix.util.logging import TraceLogger | ||
|
||
from .puppet import BasePuppet | ||
from .user import BaseUser | ||
|
||
|
||
class NotificationDisabler: | ||
puppet_cls: Type[BasePuppet] | ||
config_enabled: bool = False | ||
log: TraceLogger = logging.getLogger("mau.notification_disabler") | ||
|
||
room_id: RoomID | ||
intent: Optional[IntentAPI] | ||
enabled: bool | ||
|
||
def __init__(self, room_id: RoomID, user: BaseUser) -> None: | ||
self.room_id = room_id | ||
self.enabled = False | ||
puppet = self.puppet_cls.get_by_custom_mxid(user.mxid) | ||
if puppet and puppet.is_real_user: | ||
self.intent = puppet.intent | ||
|
||
@property | ||
def _path(self) -> PathBuilder: | ||
return Path.pushrules["global"].override["net.maunium.silence_while_backfilling"] | ||
|
||
@property | ||
def _rule(self) -> dict: | ||
return { | ||
"actions": ["dont_notify"], | ||
"conditions": [{ | ||
"kind": "event_match", | ||
"key": "room_id", | ||
"pattern": self.room_id, | ||
}] | ||
} | ||
|
||
async def __aenter__(self) -> None: | ||
if not self.intent or not self.config_enabled: | ||
return | ||
self.enabled = True | ||
try: | ||
self.log.debug(f"Disabling notifications in {self.room_id} for {self.intent.mxid}") | ||
await self.intent.api.request(Method.PUT, self._path, content=self._rule) | ||
except Exception: | ||
self.log.warning(f"Failed to disable notifications in {self.room_id} " | ||
f"for {self.intent.mxid} while backfilling", exc_info=True) | ||
raise | ||
|
||
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: | ||
if not self.enabled: | ||
return | ||
try: | ||
self.log.debug(f"Re-enabling notifications in {self.room_id} for {self.intent.mxid}") | ||
await self.intent.api.request(Method.DELETE, self._path) | ||
except Exception: | ||
self.log.warning(f"Failed to re-enable notifications in {self.room_id} " | ||
f"for {self.intent.mxid} after backfilling", exc_info=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters