hide chat_id from path

This commit is contained in:
odysseusmax 2020-08-13 03:58:55 +00:00
parent c2ace9d089
commit 8ae8df2323
4 changed files with 30 additions and 15 deletions

View File

@ -21,6 +21,7 @@ except (KeyError, ValueError):
try:
chat_id_raw = os.environ["CHAT_ID"]
chat_ids = [int(chat_id) for chat_id in chat_id_raw.split(' ')]
alias_ids = []
except (KeyError, ValueError):
print("Please set the CHAT_ID environment variable correctly")
sys.exit(1)

View File

@ -1,6 +1,9 @@
import random
import string
from aiohttp import web
from .config import chat_ids
from .config import chat_ids, alias_ids
def setup_routes(app, handler):
@ -9,7 +12,13 @@ def setup_routes(app, handler):
web.get('/', h.home, name='home')
]
for chat_id in chat_ids:
p = f"/{chat_id}"
while True:
alias_id = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(len(str(chat_id)))])
if alias_id in alias_ids:
continue
alias_ids.append(alias_id)
break
p = f"/{alias_id}"
r = [
web.get(p, h.index),
web.get(p + r"/{id:\d+}/view", h.info),

View File

@ -3,10 +3,12 @@
<h1 class=" my-2 text-2xl text-center font-bold text-green-400">
Available Sources
</h1>
<div class="mx-auto my-2 px-4 w-full">
{% for chat in chats %}
<li class="w-full border border-blue-300 rounded bg-blue-100 hover:bg-blue-200 p-4 my-3 shadow-md"> <a href="/{{chat.id}}" > {{chat.name}}</a> </li>
{% endfor %}
<div class="mx-auto my-2 p-2 w-full break-words">
<div class="block md:flex flex-wrap justify-center w-full">
{% for chat in chats %}
<li class="w-full md:w-2/5 lg:w-1/4 border border-blue-300 rounded bg-blue-100 hover:bg-blue-200 p-3 my-3 md:mx-1 shadow-md"> <a href="/{{chat.id}}" > {{chat.name}} </a> </li>
{% endfor %}
</div>
</div>
{% include 'footer.html' %}

View File

@ -7,7 +7,7 @@ from telethon.tl import types
from telethon.tl.custom import Message
from .util import get_file_name, get_human_size
from .config import chat_ids
from .config import chat_ids, alias_ids
log = logging.getLogger(__name__)
@ -22,12 +22,12 @@ class Views:
@aiohttp_jinja2.template('home.html')
async def home(self, req):
if len(chat_ids) == 1:
raise web.HTTPFound(f"{chat_ids[0]}")
raise web.HTTPFound(f"{alias_ids[0]}")
chats = []
for chat_id in chat_ids:
for chat_id, alias_id in zip(chat_ids, alias_ids):
chat = await self.client.get_entity(chat_id)
chats.append({
'id': chat_id,
'id': alias_id,
'name': chat.title
})
return {'chats':chats}
@ -35,7 +35,8 @@ class Views:
@aiohttp_jinja2.template('index.html')
async def index(self, req):
chat_id = int(req.rel_url.path.split('/')[1])
alias_id = req.rel_url.path.split('/')[1]
chat_id = chat_ids[alias_ids.index(alias_id)]
chat = await self.client.get_entity(chat_id)
log_msg = ''
try:
@ -74,7 +75,7 @@ class Views:
insight = get_file_name(m)[:55],
date = m.date.isoformat(),
size=get_human_size(m.file.size),
url=req.rel_url.with_path(f"/{chat_id}/{m.id}/view")
url=req.rel_url.with_path(f"/{alias_id}/{m.id}/view")
)
elif m.message:
entry = dict(
@ -84,7 +85,7 @@ class Views:
insight = m.raw_text[:55],
date = m.date.isoformat(),
size=get_human_size(len(m.raw_text)),
url=req.rel_url.with_path(f"/{chat_id}/{m.id}/view")
url=req.rel_url.with_path(f"/{alias_id}/{m.id}/view")
)
results.append(entry)
prev_page = False
@ -120,7 +121,8 @@ class Views:
@aiohttp_jinja2.template('info.html')
async def info(self, req):
file_id = int(req.match_info["id"])
chat_id = int(req.rel_url.path.split('/')[1])
alias_id = req.rel_url.path.split('/')[1]
chat_id = chat_ids[alias_ids.index(alias_id)]
message = await self.client.get_messages(entity=chat_id, ids=file_id)
if not message or not isinstance(message, Message):
log.debug(f"no valid entry for {file_id} in {chat_id}")
@ -193,7 +195,8 @@ class Views:
async def handle_request(self, req, head=False, thumb=False):
file_id = int(req.match_info["id"])
chat_id = int(req.rel_url.path.split('/')[1])
alias_id = req.rel_url.path.split('/')[1]
chat_id = chat_ids[alias_ids.index(alias_id)]
message = await self.client.get_messages(entity=chat_id, ids=file_id)
if not message or not message.file:
log.info(f"no result for {file_id} in {chat_id}")