-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use unique users.json list and manage welcome message when ther…
…e haven't been any previous activity
- Loading branch information
1 parent
628d44d
commit fe4c815
Showing
10 changed files
with
94 additions
and
48 deletions.
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
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
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
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
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,6 +1,7 @@ | ||
# SPDX-FileCopyrightText: 2023 Pôle d'Expertise de la Régulation Numérique <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import json | ||
import logging | ||
from pathlib import Path | ||
|
||
|
@@ -21,9 +22,6 @@ class BotLibConfig(BaseSettings): | |
description="The maximum time that the server should wait for new events before " | ||
"it should return the request anyways, in milliseconds", | ||
) | ||
join_on_invite: bool = Field( | ||
default=False, description="Do the bot automatically join when invited" | ||
) | ||
encryption_enabled: bool = Field(default=ENCRYPTION_ENABLED) | ||
ignore_unverified_devices: bool = Field(default=True, description="True by default in Element") | ||
store_path: Path = Field( | ||
|
@@ -32,6 +30,10 @@ class BotLibConfig(BaseSettings): | |
session_path: Path = Field( | ||
default="/data/session.txt", description="path of the file to store session identifier" | ||
) | ||
users_path: Path = Field( | ||
default="/data/users.json", description="path of the file to store pending users" | ||
) | ||
users: dict = Field(default={}, description="pending users") | ||
log_level: int = Field(default=logging.INFO, description="log level for the library") | ||
salt: bytes = Field( | ||
default=b"\xce,\xa1\xc6lY\x80\xe3X}\x91\xa60m\xa8N", | ||
|
@@ -43,6 +45,20 @@ class BotLibConfig(BaseSettings): | |
|
||
model_config = SettingsConfigDict(env_file=Path(".matrix_bot_env")) | ||
|
||
def __init__(self): | ||
super().__init__() | ||
if not self.users_path.exists(): | ||
with open(self.users_path, "w") as f: | ||
json.dump({}, f) | ||
self.users = {} | ||
else: | ||
with open(self.users_path, "r") as f: | ||
self.users: dict = json.load(f) | ||
|
||
def save_users(self): | ||
with open(self.users_path, "w") as f: | ||
json.dump(self.users, f, indent=2) | ||
|
||
|
||
bot_lib_config = BotLibConfig() | ||
|
||
|
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
from nio import Event, MatrixRoom, RoomMessageText | ||
|
||
from .client import MatrixClient | ||
from .config import logger | ||
from .config import bot_lib_config, logger | ||
from .room_utils import room_is_direct_message | ||
|
||
|
||
|
@@ -40,7 +40,7 @@ def is_from_userid(self, userid: str) -> bool: | |
def is_from_this_bot(self) -> bool: | ||
return self.is_from_userid(self.matrix_client.user_id) | ||
|
||
def is_sender_allowed(self) -> bool: | ||
def is_sender_domain_allowed(self) -> bool: | ||
if "*" in env_config.user_allowed_domains: | ||
return True | ||
return self.sender_domain() in env_config.user_allowed_domains | ||
|
@@ -94,14 +94,22 @@ def only_on_join(self) -> None: | |
if not self.event.source.get("content", {}).get("membership") == "invite": | ||
raise EventNotConcerned | ||
|
||
async def only_allowed_sender(self) -> None: | ||
async def only_allowed_sender(self, user_config) -> None: | ||
""" | ||
:raise EventNotConcerned: if the sender is not allowed to send messages | ||
""" | ||
if not self.is_sender_allowed(): | ||
if not self.is_sender_domain_allowed(): | ||
await self.matrix_client.send_text_message( | ||
self.room.room_id, | ||
"Albert n'est pas encore disponible pour votre domaine. Merci de rester en contact, il sera disponible après un beta test.", | ||
msgtype="m.notice", | ||
) | ||
raise EventNotConcerned | ||
if not user_config.is_authorized(self.sender): | ||
await self.matrix_client.send_markdown_message( | ||
self.room.room_id, | ||
"Albert n'est pas encore disponible pour votre domaine. Merci de rester en contact, il sera disponible après un beta test !", | ||
"Albert est en phase de test et n'est pas encore disponible pour votre utilisateur. Contactez [email protected] pour demander un accès.", | ||
msgtype="m.notice", | ||
) | ||
raise EventNotConcerned | ||
|
||
|
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