Improved /nearby

This commit is contained in:
Profitroll 2022-12-30 23:29:17 +01:00
parent e6589fc3e5
commit 25be843cd8
4 changed files with 23 additions and 14 deletions

View File

@ -392,7 +392,7 @@ class HoloUser():
try: try:
progress["application"][str(stage)] = find_location(query) progress["application"][str(stage)] = find_location(query)
if ("lat" in progress["application"][str(stage)] and "lng" in progress["application"][str(stage)]): if ("lat" in progress["application"][str(stage)] and "lng" in progress["application"][str(stage)]):
progress["application"][str(stage)]["loc"] = [progress["application"][str(stage)]["lng"], progress["application"][str(stage)]["lat"]] progress["application"][str(stage)]["loc"] = [float(progress["application"][str(stage)]["lng"]), float(progress["application"][str(stage)]["lat"])]
del progress["application"][str(stage)]["lat"] del progress["application"][str(stage)]["lat"]
del progress["application"][str(stage)]["lng"] del progress["application"][str(stage)]["lng"]
col_tmp.update_one({"user": {"$eq": self.id}, "type": {"$eq": "application"}}, {"$set": {"application": progress["application"], "stage": progress["stage"]+1}}) col_tmp.update_one({"user": {"$eq": self.id}, "type": {"$eq": "application"}}, {"$set": {"application": progress["application"], "stage": progress["stage"]+1}})

View File

@ -9,6 +9,7 @@
"admin_group": 0, "admin_group": 0,
"destination_group": 0, "destination_group": 0,
"remove_application_time": -1, "remove_application_time": -1,
"search_radius": 50,
"admins": [], "admins": [],
"bot": { "bot": {
"api_id": 0, "api_id": 0,

View File

@ -5,8 +5,9 @@ from pyrogram.types import Message
from pyrogram.client import Client from pyrogram.client import Client
from classes.holo_user import HoloUser from classes.holo_user import HoloUser
from modules import custom_filters from modules import custom_filters
from modules.logging import logWrite
from modules.utils import configGet, locale, should_quote, find_location from modules.utils import configGet, locale, should_quote, find_location
from modules.database import col_applications from modules.database import col_applications, col_users
from classes.errors.geo import PlaceNotFoundError from classes.errors.geo import PlaceNotFoundError
# Nearby command =============================================================================================================== # Nearby command ===============================================================================================================
@ -16,16 +17,17 @@ async def cmd_nearby(app: Client, msg: Message):
holo_user = HoloUser(msg.from_user) holo_user = HoloUser(msg.from_user)
# Check if any place provided # Check if any place provided
if len(msg.command) < 2: # Action if no place provided if len(msg.command) == 1: # Action if no place provided
application = col_applications.find_one({"user": msg.from_user}) application = col_applications.find_one({"user": msg.from_user.id})
if application is None: if application is None:
await msg.reply_text(locale("nearby_user_empty", "message", locale=holo_user)) await msg.reply_text(locale("nearby_user_empty", "message", locale=holo_user))
return return
location = application["application"]["3"]["loc"][0], application["application"]["3"]["loc"][1] location = application["application"]["3"]["loc"][0], application["application"]["3"]["loc"][1]
else: # Find a place from input query else: # Find a place from input query
logWrite(f"Looking for the location by query '{' '.join(msg.command[1:])}'")
try: try:
location_coordinates = find_location(" ".join(msg.command[2:])) location_coordinates = find_location(" ".join(msg.command[1:]))
location = location_coordinates["lng"], location_coordinates["lat"] location = float(location_coordinates["lng"]), float(location_coordinates["lat"])
except PlaceNotFoundError: # Place is not found except PlaceNotFoundError: # Place is not found
await msg.reply_text(locale("nearby_invalid", "message", locale=holo_user), quote=should_quote(msg)) await msg.reply_text(locale("nearby_invalid", "message", locale=holo_user), quote=should_quote(msg))
return return
@ -35,13 +37,19 @@ async def cmd_nearby(app: Client, msg: Message):
# Find all users registered in the area provided # Find all users registered in the area provided
output = [] output = []
users_nearby = col_applications.find( {"application.loc": {"$near": { "$geometry": { "type": "Point", "coordinates": location }, "$maxDistance": 30000 }} } ) applications_nearby = col_applications.find( {"application.3.loc": { "$nearSphere": {"$geometry": {"type": "Point", "coordinates": [location[0], location[1]]}, "$maxDistance": configGet("search_radius")*1000} } } )
# {"application": {"3": {"loc": {"$near": { "$geometry": { "type": "Point", "coordinates": location }, "$maxDistance": 30000 }} } } } )
for user in users_nearby: for entry in applications_nearby:
if user["tg_username"] not in [None, "None", ""]: # Check if user has any name if not entry["user"] == msg.from_user.id:
output.append(f'{user["tg_name"]} (@{user["tg_username"]}):\n {user["application"]["3"]["Name"]}, {user["application"]["3"]["adminName1"]}') user = col_users.find_one( {"user": entry["user"]} )
else: if user is not None:
output.append(f'{user["tg_name"]}:\n {user["application"]["3"]["Name"]}, {user["application"]["3"]["adminName1"]}') if user["tg_username"] not in [None, "None", ""]: # Check if user has any name
output.append(f'{user["tg_name"]} (@{user["tg_username"]}):\n {entry["application"]["3"]["name"]}, {entry["application"]["3"]["adminName1"]}')
else:
output.append(f'{user["tg_name"]}:\n {entry["application"]["3"]["name"]}, {entry["application"]["3"]["adminName1"]}')
logWrite(f"{holo_user.id} tried to find someone nearby {location[1]} {location[0]} in the radius of {configGet('search_radius')} kilometers")
# Check if any users found # Check if any users found
if len(output) > 0: if len(output) > 0:

View File

@ -1,4 +1,4 @@
from pymongo import MongoClient, GEO2D from pymongo import MongoClient, GEOSPHERE
from ujson import loads from ujson import loads
with open("config.json", "r", encoding="utf-8") as f: with open("config.json", "r", encoding="utf-8") as f:
@ -37,4 +37,4 @@ col_warnings = db.get_collection("warnings")
col_applications = db.get_collection("applications") col_applications = db.get_collection("applications")
col_sponsorships = db.get_collection("sponsorships") col_sponsorships = db.get_collection("sponsorships")
col_applications.create_index([("application.3.loc", GEO2D)]) col_applications.create_index([("application.3.loc", GEOSPHERE)])