mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2025-12-29 12:04:51 +00:00
Pyrofork: Storage: MongoStorage: Save fragment username(s) to sessions database
Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
parent
733defe650
commit
db576b8a2f
3 changed files with 38 additions and 1 deletions
|
|
@ -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:
|
||||
is_min = False
|
||||
parsed_peers = []
|
||||
usernames = []
|
||||
|
||||
for peer in peers:
|
||||
if getattr(peer, "min", False):
|
||||
|
|
@ -525,6 +526,9 @@ class Client(Methods):
|
|||
else peer.usernames[0].username.lower() if peer.usernames
|
||||
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
|
||||
peer_type = "bot" if peer.bot else "user"
|
||||
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 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"
|
||||
elif isinstance(peer, raw.types.ChannelForbidden):
|
||||
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))
|
||||
|
||||
await self.storage.update_peers(parsed_peers)
|
||||
await self.storage.update_usernames(usernames)
|
||||
|
||||
return is_min
|
||||
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ class MongoStorage(Storage):
|
|||
self.database = database
|
||||
self._peer = database['peers']
|
||||
self._session = database['session']
|
||||
self._usernames = database['usernames']
|
||||
self._remove_peers = remove_peers
|
||||
|
||||
async def open(self):
|
||||
|
|
@ -121,6 +122,24 @@ class MongoStorage(Storage):
|
|||
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):
|
||||
# id, access_hash, type
|
||||
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},
|
||||
{'_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:
|
||||
raise KeyError(f"Username not found: {username}")
|
||||
|
||||
|
|
|
|||
|
|
@ -47,6 +47,9 @@ class Storage:
|
|||
async def update_peers(self, peers: List[Tuple[int, int, str, str, str]]):
|
||||
raise NotImplementedError
|
||||
|
||||
async def update_usernames(self, usernames: List[Tuple[int, str]]):
|
||||
raise NotImplementedError
|
||||
|
||||
async def get_peer_by_id(self, peer_id: int):
|
||||
raise NotImplementedError
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue