mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 09:26:37 +03:00
Evict old cached usernames in case of collision
This commit is contained in:
parent
233daafd96
commit
67b87a0ea0
|
@ -1,5 +1,6 @@
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
from telethon.tl import types
|
from telethon.tl import types
|
||||||
from .memory import MemorySession, _SentFileType
|
from .memory import MemorySession, _SentFileType
|
||||||
|
@ -17,7 +18,7 @@ except ImportError as e:
|
||||||
sqlite3_err = type(e)
|
sqlite3_err = type(e)
|
||||||
|
|
||||||
EXTENSION = '.session'
|
EXTENSION = '.session'
|
||||||
CURRENT_VERSION = 6 # database version
|
CURRENT_VERSION = 7 # database version
|
||||||
|
|
||||||
|
|
||||||
class SQLiteSession(MemorySession):
|
class SQLiteSession(MemorySession):
|
||||||
|
@ -84,7 +85,8 @@ class SQLiteSession(MemorySession):
|
||||||
hash integer not null,
|
hash integer not null,
|
||||||
username text,
|
username text,
|
||||||
phone integer,
|
phone integer,
|
||||||
name text
|
name text,
|
||||||
|
date integer
|
||||||
)"""
|
)"""
|
||||||
,
|
,
|
||||||
"""sent_files (
|
"""sent_files (
|
||||||
|
@ -148,6 +150,9 @@ class SQLiteSession(MemorySession):
|
||||||
# hashes for User and Channel are wrong, so drop them off.
|
# hashes for User and Channel are wrong, so drop them off.
|
||||||
old += 1
|
old += 1
|
||||||
c.execute('delete from entities')
|
c.execute('delete from entities')
|
||||||
|
if old == 6:
|
||||||
|
old += 1
|
||||||
|
c.execute("alter table entities add column date integer")
|
||||||
|
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
|
@ -264,10 +269,9 @@ class SQLiteSession(MemorySession):
|
||||||
# Entity processing
|
# Entity processing
|
||||||
|
|
||||||
def process_entities(self, tlo):
|
def process_entities(self, tlo):
|
||||||
"""Processes all the found entities on the given TLObject,
|
"""
|
||||||
unless .enabled is False.
|
Processes all the found entities on the given TLObject,
|
||||||
|
unless .save_entities is False.
|
||||||
Returns True if new input entities were added.
|
|
||||||
"""
|
"""
|
||||||
if not self.save_entities:
|
if not self.save_entities:
|
||||||
return
|
return
|
||||||
|
@ -278,8 +282,10 @@ class SQLiteSession(MemorySession):
|
||||||
|
|
||||||
c = self._cursor()
|
c = self._cursor()
|
||||||
try:
|
try:
|
||||||
|
now_tup = (int(time.time()),)
|
||||||
|
rows = [row + now_tup for row in rows]
|
||||||
c.executemany(
|
c.executemany(
|
||||||
'insert or replace into entities values (?,?,?,?,?)', rows)
|
'insert or replace into entities values (?,?,?,?,?,?)', rows)
|
||||||
finally:
|
finally:
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
|
@ -288,8 +294,25 @@ class SQLiteSession(MemorySession):
|
||||||
'select id, hash from entities where phone = ?', phone)
|
'select id, hash from entities where phone = ?', phone)
|
||||||
|
|
||||||
def get_entity_rows_by_username(self, username):
|
def get_entity_rows_by_username(self, username):
|
||||||
return self._execute(
|
c = self._cursor()
|
||||||
'select id, hash from entities where username = ?', username)
|
try:
|
||||||
|
results = c.execute(
|
||||||
|
'select id, hash, date from entities where username = ?',
|
||||||
|
(username,)
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
if not results:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# If there is more than one result for the same username, evict the oldest one
|
||||||
|
if len(results) > 1:
|
||||||
|
results.sort(key=lambda t: t[2] or 0)
|
||||||
|
c.executemany('update entities set username = null where id = ?',
|
||||||
|
[(t[0],) for t in results[:-1]])
|
||||||
|
|
||||||
|
return results[-1][0], results[-1][1]
|
||||||
|
finally:
|
||||||
|
c.close()
|
||||||
|
|
||||||
def get_entity_rows_by_name(self, name):
|
def get_entity_rows_by_name(self, name):
|
||||||
return self._execute(
|
return self._execute(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user