2021-06-06 13:48:17 +02:00
|
|
|
import base64
|
|
|
|
import hashlib
|
2021-11-01 05:02:46 +01:00
|
|
|
from typing import Dict, Union
|
2020-12-12 15:16:44 +01:00
|
|
|
|
2021-11-01 05:02:46 +01:00
|
|
|
from telethon.tl.types import Chat, User, Channel
|
2021-06-06 13:48:17 +02:00
|
|
|
|
2021-11-01 05:02:46 +01:00
|
|
|
from ..config import SHORT_URL_LEN
|
|
|
|
from ..telegram import Client
|
2020-12-12 15:16:44 +01:00
|
|
|
from .home_view import HomeView
|
|
|
|
from .wildcard_view import WildcardView
|
|
|
|
from .download import Download
|
|
|
|
from .index_view import IndexView
|
|
|
|
from .info_view import InfoView
|
|
|
|
from .logo_view import LogoView
|
|
|
|
from .thumbnail_view import ThumbnailView
|
2021-05-22 16:00:08 +02:00
|
|
|
from .login_view import LoginView
|
|
|
|
from .logout_view import LogoutView
|
2021-06-14 17:53:33 +02:00
|
|
|
from .faviconicon_view import FaviconIconView
|
2021-05-22 16:00:08 +02:00
|
|
|
from .middlewhere import middleware_factory
|
2020-12-12 15:16:44 +01:00
|
|
|
|
2021-11-01 05:02:46 +01:00
|
|
|
TELEGRAM_CHAT = Union[Chat, User, Channel]
|
|
|
|
|
2021-05-22 16:00:08 +02:00
|
|
|
class Views(
|
|
|
|
HomeView,
|
|
|
|
Download,
|
|
|
|
IndexView,
|
|
|
|
InfoView,
|
|
|
|
LogoView,
|
|
|
|
ThumbnailView,
|
|
|
|
WildcardView,
|
|
|
|
LoginView,
|
|
|
|
LogoutView,
|
2021-06-14 17:53:33 +02:00
|
|
|
FaviconIconView,
|
2021-05-22 16:00:08 +02:00
|
|
|
):
|
2021-11-01 05:02:46 +01:00
|
|
|
def __init__(self, client: Client):
|
2020-12-12 15:16:44 +01:00
|
|
|
self.client = client
|
2021-06-10 10:55:44 +02:00
|
|
|
self.url_len = SHORT_URL_LEN
|
2021-11-01 05:02:46 +01:00
|
|
|
self.chat_ids: Dict[str, Dict[str, str]] = {}
|
2020-12-12 15:16:44 +01:00
|
|
|
|
2021-11-01 05:02:46 +01:00
|
|
|
def generate_alias_id(self, chat: TELEGRAM_CHAT) -> str:
|
2020-12-12 15:16:44 +01:00
|
|
|
chat_id = chat.id
|
|
|
|
title = chat.title
|
2021-06-11 20:07:48 +02:00
|
|
|
|
2020-12-12 15:16:44 +01:00
|
|
|
while True:
|
2021-06-14 07:30:52 +02:00
|
|
|
orig_id = f"{chat_id}" # the original id
|
2021-06-11 20:07:48 +02:00
|
|
|
unique_hash = hashlib.md5(orig_id.encode()).digest()
|
2021-06-14 17:53:33 +02:00
|
|
|
alias_id = base64.b64encode(unique_hash, b"__").decode()[: self.url_len]
|
2021-06-14 07:30:52 +02:00
|
|
|
|
2021-05-22 16:00:08 +02:00
|
|
|
if alias_id in self.chat_ids:
|
2021-06-14 07:30:52 +02:00
|
|
|
self.url_len += (
|
|
|
|
1 # increment url_len just incase the hash is already used.
|
|
|
|
)
|
2020-12-12 15:16:44 +01:00
|
|
|
continue
|
2021-06-14 07:30:52 +02:00
|
|
|
elif (
|
|
|
|
self.url_len > SHORT_URL_LEN
|
|
|
|
): # reset url_len to initial if hash was unique.
|
2021-06-10 10:55:44 +02:00
|
|
|
self.url_len = SHORT_URL_LEN
|
2021-05-22 16:00:08 +02:00
|
|
|
|
|
|
|
self.chat_ids[alias_id] = {
|
|
|
|
"chat_id": chat_id,
|
|
|
|
"alias_id": alias_id,
|
|
|
|
"title": title,
|
|
|
|
}
|
|
|
|
|
2020-12-12 15:16:44 +01:00
|
|
|
return alias_id
|