get rid of get_alt_graph

pull/98/head
Aevann 2023-01-25 03:07:30 +02:00
parent 08b00b7964
commit e2e3da4bda
7 changed files with 15 additions and 41 deletions

View File

@ -715,10 +715,6 @@ class User(Base):
def do_reddit(self):
return self.notifications_count == self.reddit_notifications_count
@property
@lazy
def alt_ids(self):
return [x.id for x in self.get_alt_graph(g.db)]
@property
@lazy
@ -1130,6 +1126,11 @@ class User(Base):
return output
@property
@lazy
def alt_ids(self):
return [x.id for x in self.alts]
if IS_FISTMAS():
@property
@lazy

View File

@ -20,7 +20,6 @@ from files.helpers.settings import get_settings, toggle_setting
from files.helpers.useractions import *
from files.routes.routehelpers import check_for_alts
from files.routes.wrappers import *
from files.routes.routehelpers import get_alt_graph, get_alt_graph_ids
from .front import frontlist
@ -226,7 +225,7 @@ def revert_actions(v:User, username):
send_repeatable_notification(user.id, f"@{v.username} (a site admin) has unbanned you!")
g.db.add(user)
for u in get_alt_graph(user.id):
for u in user.alts:
u.shadowbanned = None
u.unban_utc = 0
u.ban_reason = None
@ -642,9 +641,6 @@ def admin_add_alt(v:User, username):
g.db.add(a)
g.db.flush()
cache.delete_memoized(get_alt_graph_ids, user1.id)
cache.delete_memoized(get_alt_graph_ids, user2.id)
check_for_alts(user1, include_current_session=False)
check_for_alts(user2, include_current_session=False)
@ -811,7 +807,7 @@ def unshadowban(user_id, v):
if not user.is_banned: user.ban_reason = None
g.db.add(user)
for alt in get_alt_graph(user.id):
for alt in user.alts:
alt.shadowbanned = None
if not alt.is_banned: alt.ban_reason = None
g.db.add(alt)
@ -906,7 +902,7 @@ def ban_user(id, v):
user.ban(admin=v, reason=reason, days=days)
if request.values.get("alts"):
for x in get_alt_graph(user.id):
for x in user.alts:
if x.admin_level > v.admin_level:
continue
x.ban(admin=v, reason=reason, days=days)
@ -1067,7 +1063,7 @@ def unban_user(id, v):
send_repeatable_notification(user.id, f"@{v.username} (a site admin) has unbanned you!")
g.db.add(user)
for x in get_alt_graph(user.id):
for x in user.alts:
if x.is_banned: send_repeatable_notification(x.id, f"@{v.username} (a site admin) has unbanned you!")
x.is_banned = None
x.unban_utc = 0

View File

@ -14,7 +14,7 @@ from files.helpers.config.const import *
from files.helpers.regex import *
from files.helpers.settings import get_settings, get_setting
from files.helpers.sorting_and_time import make_age_string
from files.routes.routehelpers import get_alt_graph, get_formkey
from files.routes.routehelpers import get_formkey
from files.__main__ import app, cache
@app.template_filter("formkey")
@ -119,5 +119,5 @@ def inject_constants():
"HOUSE_JOIN_COST":HOUSE_JOIN_COST, "HOUSE_SWITCH_COST":HOUSE_SWITCH_COST, "IMAGE_FORMATS":','.join(IMAGE_FORMATS),
"PAGE_SIZES":PAGE_SIZES, "THEMES":THEMES, "COMMENT_SORTS":COMMENT_SORTS, "SORTS":SORTS,
"TIME_FILTERS":TIME_FILTERS, "HOUSES":HOUSES, "TIERS_ID_TO_NAME":TIERS_ID_TO_NAME,
"DEFAULT_CONFIG_VALUE":DEFAULT_CONFIG_VALUE, "IS_LOCALHOST":IS_LOCALHOST, "BACKGROUND_CATEGORIES":BACKGROUND_CATEGORIES, "PAGE_SIZE":PAGE_SIZE, "TAGLINES":TAGLINES, "IS_FISTMAS":IS_FISTMAS, "get_alt_graph":get_alt_graph, "current_registered_users":current_registered_users, "gitref":git_head()
"DEFAULT_CONFIG_VALUE":DEFAULT_CONFIG_VALUE, "IS_LOCALHOST":IS_LOCALHOST, "BACKGROUND_CATEGORIES":BACKGROUND_CATEGORIES, "PAGE_SIZE":PAGE_SIZE, "TAGLINES":TAGLINES, "IS_FISTMAS":IS_FISTMAS, "current_registered_users":current_registered_users, "gitref":git_head()
}

View File

@ -24,26 +24,6 @@ def validate_formkey(u:User, formkey:Optional[str]) -> bool:
if not formkey: return False
return validate_hash(get_raw_formkey(u), formkey)
@cache.memoize(timeout=604800)
def get_alt_graph_ids(uid:int) -> List[int]:
alt_graph_cte = g.db.query(literal(uid).label('user_id')).select_from(Alt).cte('alt_graph', recursive=True)
alt_graph_cte_inner = g.db.query(
case(
(Alt.user1 == alt_graph_cte.c.user_id, Alt.user2),
(Alt.user2 == alt_graph_cte.c.user_id, Alt.user1),
)
).select_from(Alt, alt_graph_cte).filter(
or_(alt_graph_cte.c.user_id == Alt.user1, alt_graph_cte.c.user_id == Alt.user2)
)
alt_graph_cte = alt_graph_cte.union(alt_graph_cte_inner)
return set([x[0] for x in g.db.query(User.id).filter(User.id == alt_graph_cte.c.user_id, User.id != uid).all()])
def get_alt_graph(uid:int) -> List[User]:
alt_ids = get_alt_graph_ids(uid)
return g.db.query(User).filter(User.id.in_(alt_ids)).order_by(User.username).all()
def add_alt(user1:int, user2:int):
li = [user1, user2]
existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none()
@ -51,8 +31,6 @@ def add_alt(user1:int, user2:int):
new_alt = Alt(user1=user1, user2=user2)
g.db.add(new_alt)
g.db.flush()
cache.delete_memoized(get_alt_graph_ids, user1)
cache.delete_memoized(get_alt_graph_ids, user2)
def check_for_alts(current:User, include_current_session=True):
current_id = current.id
@ -86,7 +64,7 @@ def check_for_alts(current:User, include_current_session=True):
if include_current_session:
session["history"] = list(past_accs)
g.db.flush()
for u in get_alt_graph(current.id):
for u in current.alts:
if u.shadowbanned and not current.shadowbanned and current.id not in DONT_SHADOWBAN:
current.shadowbanned = u.shadowbanned
current.ban_reason = u.ban_reason

View File

@ -3,7 +3,6 @@ from files.helpers.config.const import *
from files.helpers.get import *
from files.routes.wrappers import *
from files.__main__ import app, limiter
from files.routes.routehelpers import get_alt_graph
from math import floor
@ -75,7 +74,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls):
coin_delta = 0
alt = False
if target.author.id in [x.id for x in get_alt_graph(v.id)]:
if target.author.id in v.alt_ids:
coin_delta = -1
alt = True

View File

@ -49,7 +49,7 @@
{% if v.admin_level >= PERMS['USER_LINK'] %}
<h2>Link Accounts</h2>
{% if u2 in get_alt_graph(u1.id) %}
{% if u2.id in u1.alt_ids %}
<p>Accounts are <a href="/@{{u1.username}}/alts">known alts</a> of each other.</p>
{% else %}

View File

@ -2,7 +2,7 @@
{% set hats_total = u.hats_owned_proportion_display[1] if u else 0 %}
{% set hats_owned_percent = u.hats_owned_proportion_display[0] if u else '' %}
{% if v and (v.admin_level >= PERMS['VIEW_ALTS'] or v.alt) %}
{% set alts = get_alt_graph(u.id) %}
{% set alts = u.alts %}
{% endif %}
{% block desktopUserBanner %}
<div class="row d-mob-none">