comms: update numeric guest rename logic
This commit is contained in:
parent
1cce304872
commit
41a762d6a6
@ -257,8 +257,9 @@ spec:
|
|||||||
return not display or display.isdigit() or display.startswith("guest-")
|
return not display or display.isdigit() or display.startswith("guest-")
|
||||||
|
|
||||||
def db_rename_numeric(existing_names):
|
def db_rename_numeric(existing_names):
|
||||||
user_ids = []
|
profile_rows = []
|
||||||
profiles = {}
|
profile_index = {}
|
||||||
|
users = []
|
||||||
conn = psycopg2.connect(
|
conn = psycopg2.connect(
|
||||||
host=os.environ["PGHOST"],
|
host=os.environ["PGHOST"],
|
||||||
port=int(os.environ["PGPORT"]),
|
port=int(os.environ["PGPORT"]),
|
||||||
@ -270,19 +271,12 @@ spec:
|
|||||||
with conn:
|
with conn:
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"SELECT name FROM users WHERE name ~ %s",
|
"SELECT user_id, full_user_id, displayname FROM profiles WHERE full_user_id ~ %s",
|
||||||
(f"^@\\d+:{SERVER_NAME}$",),
|
(f"^@\\d+:{SERVER_NAME}$",),
|
||||||
)
|
)
|
||||||
user_ids = [row[0] for row in cur.fetchall()]
|
profile_rows = cur.fetchall()
|
||||||
if not user_ids:
|
profile_index = {row[1]: row for row in profile_rows}
|
||||||
return
|
for user_id, full_user_id, display in profile_rows:
|
||||||
cur.execute(
|
|
||||||
"SELECT user_id, displayname FROM profiles WHERE user_id = ANY(%s)",
|
|
||||||
(user_ids,),
|
|
||||||
)
|
|
||||||
profiles = {row[0]: row[1] for row in cur.fetchall()}
|
|
||||||
for user_id in user_ids:
|
|
||||||
display = profiles.get(user_id)
|
|
||||||
if display and not needs_rename_display(display):
|
if display and not needs_rename_display(display):
|
||||||
continue
|
continue
|
||||||
new = None
|
new = None
|
||||||
@ -295,8 +289,40 @@ spec:
|
|||||||
if not new:
|
if not new:
|
||||||
continue
|
continue
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO profiles (user_id, displayname, full_user_id) VALUES (%s, %s, %s) ON CONFLICT (user_id) DO UPDATE SET displayname = EXCLUDED.displayname",
|
"UPDATE profiles SET displayname = %s WHERE full_user_id = %s",
|
||||||
(user_id, new, user_id),
|
(new, full_user_id),
|
||||||
|
)
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"SELECT name FROM users WHERE name ~ %s",
|
||||||
|
(f"^@\\d+:{SERVER_NAME}$",),
|
||||||
|
)
|
||||||
|
users = [row[0] for row in cur.fetchall()]
|
||||||
|
if not users:
|
||||||
|
return
|
||||||
|
cur.execute(
|
||||||
|
"SELECT user_id, full_user_id FROM profiles WHERE full_user_id = ANY(%s)",
|
||||||
|
(users,),
|
||||||
|
)
|
||||||
|
for existing_full in cur.fetchall():
|
||||||
|
profile_index.setdefault(existing_full[1], existing_full)
|
||||||
|
|
||||||
|
for full_user_id in users:
|
||||||
|
if full_user_id in profile_index:
|
||||||
|
continue
|
||||||
|
localpart = full_user_id.split(":", 1)[0].lstrip("@")
|
||||||
|
new = None
|
||||||
|
for _ in range(30):
|
||||||
|
candidate = f"{random.choice(ADJ)}-{random.choice(NOUN)}"
|
||||||
|
if candidate not in existing_names:
|
||||||
|
new = candidate
|
||||||
|
existing_names.add(candidate)
|
||||||
|
break
|
||||||
|
if not new:
|
||||||
|
continue
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO profiles (user_id, displayname, full_user_id) VALUES (%s, %s, %s)",
|
||||||
|
(localpart, new, full_user_id),
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user