Skip to content

Commit

Permalink
Merge branch 'main' into compile_clips_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dam2452 authored Dec 27, 2024
2 parents 8926b3d + c9ebe45 commit 8c23909
Show file tree
Hide file tree
Showing 33 changed files with 616 additions and 504 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ video_data_1
/bot/tests/e2e_tests/expected_files
/bot/tests/expected
/bot/tests/expected/expected_files
*.session-journal
Test Results - .html
*.html
bot/tests/received_file.txt
*.session-journal
Test Results - .html
*.html
bot/tests/received_file.txt
6 changes: 2 additions & 4 deletions bot/database/database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _row_to_video_clip(row: asyncpg.Record) -> VideoClip:
)

@staticmethod
async def get_saved_clips(user_id: int) -> Optional[List[VideoClip]]:
async def get_saved_clips(user_id: int) -> List[VideoClip]:
async with DatabaseManager.get_db_connection() as conn:
rows = await conn.fetch(
"SELECT id, chat_id, user_id, clip_name, video_data, start_time, end_time, duration, season, episode_number, is_compilation "
Expand All @@ -275,9 +275,7 @@ async def get_saved_clips(user_id: int) -> Optional[List[VideoClip]]:
user_id,
)

if rows:
return [DatabaseManager._row_to_video_clip(row) for row in rows]
return None
return [DatabaseManager._row_to_video_clip(row) for row in rows] if rows else []

@staticmethod
async def save_clip(
Expand Down
39 changes: 22 additions & 17 deletions bot/handlers/administration/add_whitelist_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from typing import List

from aiogram.exceptions import TelegramBadRequest
from aiogram.types import Message

from bot.database.database_manager import DatabaseManager
Expand All @@ -11,9 +10,9 @@
)
from bot.responses.administration.add_whitelist_handler_responses import (
get_log_user_added_message,
get_no_user_id_provided_message,
get_no_username_provided_message,
get_user_added_message,
get_user_not_found_message,
)


Expand All @@ -24,33 +23,39 @@ def get_commands(self) -> List[str]:
def _get_validator_functions(self) -> ValidatorFunctions:
return [
self.__check_argument_count,
self.__check_user_id_is_digit,
]

async def __check_argument_count(self, message: Message) -> bool:
return await self._validate_argument_count(
message, 2, get_no_username_provided_message(),
)

async def __check_user_id_is_digit(self, message: Message) -> bool:
user_input = message.text.split()[1]
if not user_input.isdigit():
await self.__reply_user_not_found(message)
return False
return True

async def _do_handle(self, message: Message) -> None:
user_input = message.text.split()[1]
try:
user_data = await self._bot.get_chat(int(user_input) if user_input.isdigit() else user_input)
except TelegramBadRequest as e:
if "chat not found" in str(e).lower():
await self._answer(message, get_user_not_found_message())
return
raise

await DatabaseManager.add_user(
user_id=user_data.id,
username=user_data.username,
full_name=user_data.full_name,
user_id=int(user_input),
username="",
full_name="",
note=None,
)

username_or_name = user_data.username or user_data.full_name or str(user_data.id)
await self.__reply_user_added(message, username_or_name)
await self.__reply_user_added(message, user_input)

async def __reply_user_added(self, message: Message, username: str) -> None:
await self._answer(message,get_user_added_message(username))
await self._log_system_message(logging.INFO, get_log_user_added_message(username, message.from_user.username))
await self._answer(message, get_user_added_message(username))
await self._log_system_message(
logging.INFO,
get_log_user_added_message(username, message.from_user.username),
)

async def __reply_user_not_found(self, message: Message) -> None:
await self._answer(message, get_no_user_id_provided_message())
await self._log_system_message(logging.INFO, get_no_user_id_provided_message())
41 changes: 38 additions & 3 deletions bot/handlers/administration/create_key_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import List

from aiogram.types import Message
Expand All @@ -10,6 +11,7 @@
from bot.responses.administration.create_key_handler_responses import (
get_create_key_success_message,
get_create_key_usage_message,
get_key_already_exists_message,
)


Expand All @@ -20,16 +22,49 @@ def get_commands(self) -> List[str]:
def _get_validator_functions(self) -> ValidatorFunctions:
return [
self.__check_argument_count,
self.__check_days_is_digit,
self.__check_key_is_unique,
]

async def __check_argument_count(self, message: Message) -> bool:
return await self._validate_argument_count(
message, 3, get_create_key_usage_message(),
)

async def __check_days_is_digit(self, message: Message) -> bool:
args = message.text.split()
if not args[1].isdigit():
await self.__reply_wrong_argument(message)
return False
return True

async def __check_key_is_unique(self, message: Message) -> bool:
args = message.text.split()
key = " ".join(args[2:])
key_exists = await DatabaseManager.get_subscription_days_by_key(key)
if key_exists is not None:
await self.__reply_key_already_exists(message, key)
return False
return True

async def _do_handle(self, message: Message) -> None:
args = message.text.split()
days = int(args[1])
name = " ".join(args[2:])
key = " ".join(args[2:])

await DatabaseManager.create_subscription_key(days, key)
await self.__reply_key_added(message, days, key)

async def __reply_key_added(self, message: Message, days: int, key: str) -> None:
await self._answer(message, get_create_key_success_message(days, key))
await self._log_system_message(
logging.INFO, get_create_key_success_message(days, key),
)

async def __reply_wrong_argument(self, message: Message) -> None:
await self._answer(message, get_create_key_usage_message())
await self._log_system_message(logging.INFO, get_create_key_usage_message())

await DatabaseManager.create_subscription_key(days, name)
await self._answer(message,get_create_key_success_message(days, name))
async def __reply_key_already_exists(self, message: Message, key: str) -> None:
await self._answer(message, get_key_already_exists_message(key))
await self._log_system_message(logging.INFO, get_key_already_exists_message(key))
32 changes: 24 additions & 8 deletions bot/handlers/administration/remove_subscription_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from bot.responses.administration.remove_subscription_handler_responses import (
get_log_subscription_removed_message,
get_no_username_provided_message,
get_no_user_id_provided_message,
get_subscription_removed_message,
)

Expand All @@ -22,18 +22,34 @@ def get_commands(self) -> List[str]:
def _get_validator_functions(self) -> ValidatorFunctions:
return [
self.__check_argument_count,
self.__check_user_id_is_digit,
]

async def __check_argument_count(self, message: Message) -> bool:
return await self._validate_argument_count(
message, 2, get_no_username_provided_message(),
message, 2, get_no_user_id_provided_message(),
)

async def __check_user_id_is_digit(self, message: Message) -> bool:
user_input = message.text.split()[1]
if not user_input.isdigit():
await self.__reply_invalid_user_id(message)
return False
return True

async def _do_handle(self, message: Message) -> None:
username = message.text.split()[1]
await DatabaseManager.remove_subscription(message.from_user.id)
await self.__reply_subscription_removed(message, username)
user_id = int(message.text.split()[1])

await DatabaseManager.remove_subscription(user_id)
await self.__reply_subscription_removed(message, user_id)

async def __reply_subscription_removed(self, message: Message, user_id: int) -> None:
await self._answer(message, get_subscription_removed_message(str(user_id)))
await self._log_system_message(
logging.INFO,
get_log_subscription_removed_message(str(user_id), message.from_user.username),
)

async def __reply_subscription_removed(self, message: Message, username: str) -> None:
await self._answer(message,get_subscription_removed_message(username))
await self._log_system_message(logging.INFO, get_log_subscription_removed_message(username, message.from_user.username))
async def __reply_invalid_user_id(self, message: Message) -> None:
await self._answer(message, get_no_user_id_provided_message())
await self._log_system_message(logging.WARNING, get_no_user_id_provided_message())
22 changes: 20 additions & 2 deletions bot/handlers/administration/remove_whitelist_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
ValidatorFunctions,
)
from bot.responses.administration.remove_whitelist_handler_responses import (
get_log_user_not_in_whitelist_message,
get_log_user_removed_message,
get_no_user_id_provided_message,
get_user_not_in_whitelist_message,
get_user_removed_message,
)

Expand All @@ -23,6 +25,7 @@ def _get_validator_functions(self) -> ValidatorFunctions:
return [
self.__check_argument_count,
self.__check_user_id_digit,
self.__check_user_exists,
]

async def __check_argument_count(self, message: Message) -> bool:
Expand All @@ -37,12 +40,27 @@ async def __check_user_id_digit(self, message: Message) -> bool:
return False
return True

async def __check_user_exists(self, message: Message) -> bool:
user_id = int(message.text.split()[1])
user_exists = await DatabaseManager.is_user_in_db(user_id)
if not user_exists:
await self._reply_user_not_found(message, user_id)
return False
return True

async def _do_handle(self, message: Message) -> None:
user_id = int(message.text.split()[1])

await DatabaseManager.remove_user(user_id)
await self.__reply_user_removed(message, user_id)

async def __reply_user_removed(self, message: Message, user_id: int) -> None:
await self._answer(message,get_user_removed_message(str(user_id)))
await self._log_system_message(logging.INFO, get_log_user_removed_message(str(user_id), message.from_user.username))
await self._answer(message, get_user_removed_message(str(user_id)))
await self._log_system_message(
logging.INFO,
get_log_user_removed_message(str(user_id), message.from_user.username),
)

async def _reply_user_not_found(self, message: Message, user_id: int) -> None:
await self._answer(message, get_user_not_in_whitelist_message(user_id))
await self._log_system_message(logging.WARNING, get_log_user_not_in_whitelist_message(user_id))
39 changes: 25 additions & 14 deletions bot/handlers/not_sending_videos/delete_clip_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _get_validator_functions(self) -> ValidatorFunctions:
return [
self.__check_argument_count,
self.__check_clip_number_format,
self.__check_clip_exists,
]

async def __check_argument_count(self, message: Message) -> bool:
Expand All @@ -34,30 +35,40 @@ async def __check_argument_count(self, message: Message) -> bool:
async def __check_clip_number_format(self, message: Message) -> bool:
content = message.text.split()
if not content[1].isdigit():
await self._reply_invalid_args_count(message, get_invalid_args_count_message())
await self.__reply_invalid_args_count(message)
return False
return True

async def _do_handle(self, message: Message) -> None:
clip_number = int(message.text.split()[1])
async def __check_clip_exists(self, message: Message) -> bool:
content = message.text.split()
clip_number = int(content[1])
user_clips = await DatabaseManager.get_saved_clips(message.from_user.id)

user_clips = await DatabaseManager.get_saved_clips(message.chat.id)
if clip_number not in range(1, len(user_clips) + 1):
await self._answer(message,get_clip_not_exist_message(clip_number))
await self._log_system_message(
logging.INFO,
get_log_clip_not_exist_message(clip_number, message.from_user.username),
)
return
await self.__reply_clip_not_exist(message, clip_number)
return False
return True

clip_to_delete = user_clips[clip_number - 1]
async def _do_handle(self, message: Message) -> None:
clip_number = int(message.text.split()[1])

await DatabaseManager.delete_clip(message.chat.id, clip_to_delete.name)
user_clips = await DatabaseManager.get_saved_clips(message.from_user.id)
clip_to_delete = user_clips[clip_number - 1]
await DatabaseManager.delete_clip(message.from_user.id, clip_to_delete.name)

await self._answer(message,get_clip_deleted_message(clip_to_delete.name))
await self._answer(message, get_clip_deleted_message(clip_to_delete.name))
await self._log_system_message(
logging.INFO, get_log_clip_deleted_message(
logging.INFO,
get_log_clip_deleted_message(
clip_to_delete.name,
message.from_user.username,
),
)

async def __reply_invalid_args_count(self, message: Message) -> None:
await self._answer(message, get_invalid_args_count_message())
await self._log_system_message(logging.INFO, get_invalid_args_count_message())

async def __reply_clip_not_exist(self, message: Message, clip_number: int) -> None:
await self._answer(message, get_clip_not_exist_message(clip_number))
await self._log_system_message(logging.INFO, get_log_clip_not_exist_message(clip_number, message.from_user.username))
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ def get_commands(self) -> List[str]:
def _get_validator_functions(self) -> ValidatorFunctions:
return [
self.__check_argument_count,
self.__check_user_has_clips,
]

async def __check_argument_count(self, message: Message) -> bool:
return await self._validate_argument_count(message, 2, get_invalid_args_count_message())

async def __check_user_has_clips(self, message: Message) -> bool:
user_clips = await DatabaseManager.get_saved_clips(message.from_user.id)
if not user_clips:
await self.__reply_no_matching_clips_found(message)
return False
return True

async def _do_handle(self, message: Message) -> None:
content = message.text.split()
Expand Down Expand Up @@ -78,7 +85,6 @@ async def _do_handle(self, message: Message) -> None:

compiled_output = await ClipsCompiler.compile(message, selected_segments, self._logger)
await process_compiled_clip(message, compiled_output, ClipType.COMPILED)

await self._answer_video(message, compiled_output)

await self._log_system_message(logging.INFO, get_compiled_clip_sent_message(message.from_user.username))
Expand Down
Loading

0 comments on commit 8c23909

Please sign in to comment.