Pyrofork: Storage: MongoStorage: Save fragment username(s) to sessions database

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2023-10-23 19:57:07 +07:00
parent 733defe650
commit db576b8a2f
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
3 changed files with 38 additions and 1 deletions

View file

@ -508,6 +508,7 @@ class Client(Methods):
async def fetch_peers(self, peers: List[Union[raw.types.User, raw.types.Chat, raw.types.Channel]]) -> bool: async def fetch_peers(self, peers: List[Union[raw.types.User, raw.types.Chat, raw.types.Channel]]) -> bool:
is_min = False is_min = False
parsed_peers = [] parsed_peers = []
usernames = []
for peer in peers: for peer in peers:
if getattr(peer, "min", False): if getattr(peer, "min", False):
@ -525,6 +526,9 @@ class Client(Methods):
else peer.usernames[0].username.lower() if peer.usernames else peer.usernames[0].username.lower() if peer.usernames
else None else None
) )
if peer.usernames is not None and len(peer.usernames) > 1:
for uname in peer.usernames:
usernames.append((peer.id, uname.username.lower()))
phone_number = peer.phone phone_number = peer.phone
peer_type = "bot" if peer.bot else "user" peer_type = "bot" if peer.bot else "user"
elif isinstance(peer, (raw.types.Chat, raw.types.ChatForbidden)): elif isinstance(peer, (raw.types.Chat, raw.types.ChatForbidden)):
@ -539,6 +543,9 @@ class Client(Methods):
else peer.usernames[0].username.lower() if peer.usernames else peer.usernames[0].username.lower() if peer.usernames
else None else None
) )
if peer.usernames is not None and len(peer.usernames) > 1:
for uname in peer.usernames:
usernames.append((peer.id, uname.username.lower()))
peer_type = "channel" if peer.broadcast else "supergroup" peer_type = "channel" if peer.broadcast else "supergroup"
elif isinstance(peer, raw.types.ChannelForbidden): elif isinstance(peer, raw.types.ChannelForbidden):
peer_id = utils.get_channel_id(peer.id) peer_id = utils.get_channel_id(peer.id)
@ -550,6 +557,7 @@ class Client(Methods):
parsed_peers.append((peer_id, access_hash, peer_type, username, phone_number)) parsed_peers.append((peer_id, access_hash, peer_type, username, phone_number))
await self.storage.update_peers(parsed_peers) await self.storage.update_peers(parsed_peers)
await self.storage.update_usernames(usernames)
return is_min return is_min

View file

@ -56,6 +56,7 @@ class MongoStorage(Storage):
self.database = database self.database = database
self._peer = database['peers'] self._peer = database['peers']
self._session = database['session'] self._session = database['session']
self._usernames = database['usernames']
self._remove_peers = remove_peers self._remove_peers = remove_peers
async def open(self): async def open(self):
@ -121,6 +122,24 @@ class MongoStorage(Storage):
bulk bulk
) )
async def update_usernames(self, usernames: List[Tuple[int, str]]):
s = int(time.time())
bulk = [
UpdateOne(
{'_id': i[1]},
{'$set': {
'peer_id': i[0],
'last_update_on': s
}},
upsert=True
) for i in usernames
]
if not bulk:
return
await self._usernames.bulk_write(
bulk
)
async def get_peer_by_id(self, peer_id: int): async def get_peer_by_id(self, peer_id: int):
# id, access_hash, type # id, access_hash, type
r = await self._peer.find_one({'_id': peer_id}, {'_id': 1, 'access_hash': 1, 'type': 1}) r = await self._peer.find_one({'_id': peer_id}, {'_id': 1, 'access_hash': 1, 'type': 1})
@ -133,6 +152,13 @@ class MongoStorage(Storage):
r = await self._peer.find_one({'username': username}, r = await self._peer.find_one({'username': username},
{'_id': 1, 'access_hash': 1, 'type': 1, 'last_update_on': 1}) {'_id': 1, 'access_hash': 1, 'type': 1, 'last_update_on': 1})
if r is None:
r2 = await self._usernames.find_one({'_id': username},
{'peer_id': 1, 'last_update_on': 1})
if r2 is None:
raise KeyError(f"Username not found: {username}")
r = await self._peer.find_one({'_id': r2['peer_id']},
{'_id': 1, 'access_hash': 1, 'type': 1, 'last_update_on': 1})
if r is None: if r is None:
raise KeyError(f"Username not found: {username}") raise KeyError(f"Username not found: {username}")

View file

@ -47,6 +47,9 @@ class Storage:
async def update_peers(self, peers: List[Tuple[int, int, str, str, str]]): async def update_peers(self, peers: List[Tuple[int, int, str, str, str]]):
raise NotImplementedError raise NotImplementedError
async def update_usernames(self, usernames: List[Tuple[int, str]]):
raise NotImplementedError
async def get_peer_by_id(self, peer_id: int): async def get_peer_by_id(self, peer_id: int):
raise NotImplementedError raise NotImplementedError