2023-08-28 16:41:20 +03:00
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
from convopyro import listen_message
|
|
|
|
from pykeyboard import ReplyButton, ReplyKeyboard
|
|
|
|
from pyrogram.types import Message, ReplyKeyboardRemove
|
|
|
|
|
|
|
|
from classes.location import Location
|
|
|
|
from classes.pyroclient import PyroClient
|
2023-11-05 15:20:01 +02:00
|
|
|
from modules.database_api import col_locations
|
2023-08-28 16:41:20 +03:00
|
|
|
from modules.search_name import search_name
|
|
|
|
|
|
|
|
|
|
|
|
async def search_nearby(app: PyroClient, message: Message) -> Union[Location, None]:
|
2023-08-29 17:32:37 +03:00
|
|
|
user = await app.find_user(message.from_user)
|
|
|
|
|
2023-08-28 16:41:20 +03:00
|
|
|
query = {
|
|
|
|
"location": {
|
|
|
|
"$within": {
|
|
|
|
"$center": [
|
|
|
|
[message.location.longitude, message.location.latitude],
|
|
|
|
app.config["search"]["radius"],
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:03:08 +03:00
|
|
|
locations = await col_locations.find(query).limit(6).to_list()
|
2023-08-28 16:41:20 +03:00
|
|
|
|
2023-08-30 15:06:46 +03:00
|
|
|
if len(locations) == 0:
|
2023-08-28 16:41:20 +03:00
|
|
|
await message.reply_text(
|
2023-08-29 17:32:37 +03:00
|
|
|
app._("search_nearby_empty", "messages", locale=user.locale)
|
2023-08-28 16:41:20 +03:00
|
|
|
)
|
|
|
|
return await search_name(app, message)
|
|
|
|
|
2023-08-30 15:06:46 +03:00
|
|
|
locations.reverse()
|
2023-08-30 15:03:08 +03:00
|
|
|
|
2023-08-28 16:41:20 +03:00
|
|
|
keyboard = ReplyKeyboard(resize_keyboard=True, row_width=2)
|
|
|
|
keyboard.add(*[ReplyButton(db_record["name"]) for db_record in locations])
|
|
|
|
|
|
|
|
await message.reply_text(
|
2023-08-29 17:32:37 +03:00
|
|
|
app._("location_select", "messages", locale=user.locale),
|
|
|
|
reply_markup=keyboard,
|
2023-08-28 16:41:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
while True:
|
2023-08-30 15:41:34 +03:00
|
|
|
app.contexts.append(message.from_user.id)
|
2023-08-28 16:41:20 +03:00
|
|
|
answer = await listen_message(app, message.chat.id, 300)
|
2023-08-30 15:41:34 +03:00
|
|
|
app.contexts.remove(message.from_user.id)
|
|
|
|
|
2023-08-28 16:41:20 +03:00
|
|
|
location: Union[Location, None] = None
|
|
|
|
|
|
|
|
if answer is None or answer.text == "/cancel":
|
2023-08-29 17:32:37 +03:00
|
|
|
await message.reply_text(
|
|
|
|
app._("cancelled", "messages", locale=user.locale),
|
|
|
|
reply_markup=ReplyKeyboardRemove(),
|
|
|
|
)
|
2023-08-28 16:41:20 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
for db_record in locations:
|
|
|
|
if answer.text == db_record["name"]:
|
|
|
|
location = Location(**db_record)
|
|
|
|
break
|
|
|
|
|
|
|
|
if answer.text is None or location is None:
|
|
|
|
await answer.reply_text(
|
2023-08-29 17:32:37 +03:00
|
|
|
app._("selection_invalid", "messages", locale=user.locale).format(
|
|
|
|
cancel_notice=app._("cancel", "messages", locale=user.locale)
|
|
|
|
)
|
2023-08-28 16:41:20 +03:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
return location or await search_name(app, message)
|