forked from retoor/devplacepy
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6392e84917 |
File diff suppressed because one or more lines are too long
+11
-3
@@ -15,19 +15,27 @@ def avatar_seed(user) -> str:
|
||||
return user.get("avatar_seed") or user.get("username") or ""
|
||||
|
||||
|
||||
def generate_avatar_svg(seed: str) -> str:
|
||||
def _svg_with_size(svg: str, size: int) -> str:
|
||||
size_attr = f' width="{size}" height="{size}"'
|
||||
if svg.startswith("<svg"):
|
||||
tag_end = svg.index(">")
|
||||
return svg[:tag_end] + size_attr + svg[tag_end:]
|
||||
return svg
|
||||
|
||||
|
||||
def generate_avatar_svg(seed: str, size: int = 128) -> str:
|
||||
try:
|
||||
from multiavatar.multiavatar import multiavatar
|
||||
|
||||
svg = multiavatar(seed, None, None)
|
||||
if svg and svg.strip().startswith("<svg"):
|
||||
return svg
|
||||
return _svg_with_size(svg, size)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
logger.warning(f"Avatar generation failed for {seed}: {e}")
|
||||
initial = seed[:1].upper() if seed else "?"
|
||||
return (
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">'
|
||||
f'<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{size}" viewBox="0 0 100 100">'
|
||||
f'<rect width="100" height="100" rx="50" fill="#ff6b35"/>'
|
||||
f'<text x="50" y="65" text-anchor="middle" fill="white" font-size="40" font-weight="700" font-family="sans-serif">{initial}</text></svg>'
|
||||
)
|
||||
|
||||
@@ -360,37 +360,6 @@ def create_comment_record(
|
||||
comment_url,
|
||||
)
|
||||
|
||||
if target_type == "post":
|
||||
posts_table = get_table("posts")
|
||||
post_record = posts_table.find_one(uid=target_uid)
|
||||
if not post_record:
|
||||
post_record = posts_table.find_one(slug=target_uid)
|
||||
|
||||
commenter_uids = set()
|
||||
for c in get_table("comments").find(
|
||||
target_type="post", target_uid=target_uid, deleted_at=None
|
||||
):
|
||||
commenter_uids.add(c["user_uid"])
|
||||
|
||||
commenter_uids.discard(user["uid"])
|
||||
if post_record:
|
||||
commenter_uids.discard(post_record["user_uid"])
|
||||
if parent_uid:
|
||||
parent_comment = get_table("comments").find_one(
|
||||
uid=parent_uid, deleted_at=None
|
||||
)
|
||||
if parent_comment and parent_comment["user_uid"] != user["uid"]:
|
||||
commenter_uids.discard(parent_comment["user_uid"])
|
||||
|
||||
for commenter_uid in commenter_uids:
|
||||
create_notification(
|
||||
commenter_uid,
|
||||
"thread_comment",
|
||||
f"{user['username']} also commented on a post you commented on",
|
||||
user["uid"],
|
||||
comment_url,
|
||||
)
|
||||
|
||||
create_mention_notifications(content, user["uid"], comment_url)
|
||||
schedule_correction(user, "comments", comment_uid, request)
|
||||
schedule_modification(user, "comments", comment_uid, request)
|
||||
|
||||
@@ -19,7 +19,6 @@ NOTIFICATION_TYPES = [
|
||||
{"key": "harvest_stolen", "label": "Farm raids", "description": "Someone steals a ready build from your Code Farm"},
|
||||
{"key": "award", "label": "Awards", "description": "Someone gives you an award on your profile"},
|
||||
{"key": "system", "label": "System alerts", "description": "Platform infrastructure alerts (e.g. the AI gateway going down)"},
|
||||
{"key": "thread_comment", "label": "Thread comments", "description": "Someone else comments on a post you commented on"},
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -17,14 +17,16 @@ _CACHE_CONTROL = f"public, max-age={SECONDS_PER_DAY}, immutable"
|
||||
|
||||
@router.get("/{style}/{seed}")
|
||||
async def avatar_proxy(request: Request, style: str, seed: str, size: int = 128):
|
||||
etag = '"' + hashlib.md5(f"{seed}:{size}".encode("utf-8")).hexdigest() + '"'
|
||||
size = max(16, min(512, size))
|
||||
cache_key = f"{seed}:{size}"
|
||||
etag = '"' + hashlib.md5(cache_key.encode("utf-8")).hexdigest() + '"'
|
||||
headers = {"ETag": etag, "Cache-Control": _CACHE_CONTROL}
|
||||
|
||||
if request.headers.get("if-none-match") == etag:
|
||||
return Response(status_code=304, headers=headers)
|
||||
|
||||
svg = _cache.get(seed)
|
||||
svg = _cache.get(cache_key)
|
||||
if svg is None:
|
||||
svg = generate_avatar_svg(seed)
|
||||
_cache.set(seed, svg)
|
||||
svg = generate_avatar_svg(seed, size)
|
||||
_cache.set(cache_key, svg)
|
||||
return Response(content=svg, media_type="image/svg+xml", headers=headers)
|
||||
|
||||
+29
-1
@@ -1,7 +1,6 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import requests
|
||||
from devplacepy.avatar import avatar_url, generate_avatar_svg
|
||||
from tests.conftest import BASE_URL
|
||||
|
||||
|
||||
@@ -10,3 +9,32 @@ def test_avatar_endpoint_serves_svg(app_server):
|
||||
assert r.status_code == 200
|
||||
assert "svg" in r.headers.get("content-type", "").lower()
|
||||
assert "<svg" in r.text
|
||||
|
||||
|
||||
def test_avatar_respects_size(app_server):
|
||||
r = requests.get(f"{BASE_URL}/avatar/multiavatar/alice_test?size=32")
|
||||
assert r.status_code == 200
|
||||
assert 'width="32"' in r.text
|
||||
assert 'height="32"' in r.text
|
||||
|
||||
|
||||
def test_avatar_different_sizes_different_response(app_server):
|
||||
r32 = requests.get(f"{BASE_URL}/avatar/multiavatar/bob_test?size=32")
|
||||
r128 = requests.get(f"{BASE_URL}/avatar/multiavatar/bob_test?size=128")
|
||||
assert r32.status_code == 200
|
||||
assert r128.status_code == 200
|
||||
etag32 = r32.headers.get("etag", "")
|
||||
etag128 = r128.headers.get("etag", "")
|
||||
assert etag32 != etag128, "same seed with different sizes must produce different ETags"
|
||||
|
||||
|
||||
def test_avatar_size_clamped_low(app_server):
|
||||
r = requests.get(f"{BASE_URL}/avatar/multiavatar/clamp_low?size=1")
|
||||
assert r.status_code == 200
|
||||
assert 'width="16"' in r.text
|
||||
|
||||
|
||||
def test_avatar_size_clamped_high(app_server):
|
||||
r = requests.get(f"{BASE_URL}/avatar/multiavatar/clamp_high?size=9999")
|
||||
assert r.status_code == 200
|
||||
assert 'width="512"' in r.text
|
||||
|
||||
@@ -384,66 +384,6 @@ def test_comment_notification_on_post(app_server, browser, seeded_db):
|
||||
ctx_b.close()
|
||||
|
||||
|
||||
def test_thread_comment_notification(app_server, browser, seeded_db):
|
||||
from tests.conftest import login_user
|
||||
|
||||
ctx_a = browser.new_context(viewport={"width": 1400, "height": 900})
|
||||
ctx_b = browser.new_context(viewport={"width": 1400, "height": 900})
|
||||
pa = ctx_a.new_page()
|
||||
pb = ctx_b.new_page()
|
||||
pa.set_default_timeout(15000)
|
||||
pb.set_default_timeout(15000)
|
||||
|
||||
login_user(pa, seeded_db["alice"])
|
||||
login_user(pb, seeded_db["bob"])
|
||||
|
||||
pa.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
|
||||
pa.locator(".feed-fab").first.wait_for(state="visible", timeout=10000)
|
||||
pa.locator(".feed-fab").first.click()
|
||||
pa.fill("#post-content", "Post for thread comment notification test")
|
||||
pa.locator("#create-post-modal button.btn-primary:has-text('Post')").click()
|
||||
pa.wait_for_url("**/posts/*", timeout=10000, wait_until="domcontentloaded")
|
||||
post_url = pa.url
|
||||
|
||||
pb.goto(post_url, wait_until="domcontentloaded")
|
||||
pb.wait_for_timeout(1000)
|
||||
comment_textarea = pb.locator("form.comment-form textarea[name='content']").first
|
||||
comment_textarea.wait_for(state="visible", timeout=10000)
|
||||
comment_textarea.fill("Bob's top-level comment")
|
||||
pb.locator("button.comment-form-submit").first.click()
|
||||
pb.wait_for_timeout(1500)
|
||||
|
||||
pb_body = pb.locator("body").text_content()
|
||||
assert "Internal Server Error" not in pb_body, f"Bob got 500: {pb_body[:300]}"
|
||||
|
||||
pa.goto(post_url, wait_until="domcontentloaded")
|
||||
pa.wait_for_timeout(1000)
|
||||
comment_textarea = pa.locator("form.comment-form textarea[name='content']").first
|
||||
comment_textarea.wait_for(state="visible", timeout=10000)
|
||||
comment_textarea.fill("Alice also comments on her own post")
|
||||
pa.locator("button.comment-form-submit").first.click()
|
||||
pa.wait_for_timeout(1500)
|
||||
|
||||
pa_body = pa.locator("body").text_content()
|
||||
assert "Internal Server Error" not in pa_body, f"Alice got 500: {pa_body[:300]}"
|
||||
|
||||
pb.goto(f"{BASE_URL}/notifications", wait_until="domcontentloaded")
|
||||
pb.wait_for_timeout(2000)
|
||||
body = pb.locator("body").text_content()
|
||||
assert "Internal Server Error" not in body, (
|
||||
f"Got 500 error on notifications: {body[:500]}"
|
||||
)
|
||||
assert "alice_test" in body, (
|
||||
f"Expected 'alice_test' in notifications, got: {body[:500]}"
|
||||
)
|
||||
assert "also commented" in body, (
|
||||
f"Expected 'also commented' in notifications, got: {body[:500]}"
|
||||
)
|
||||
|
||||
ctx_a.close()
|
||||
ctx_b.close()
|
||||
|
||||
|
||||
def test_follow_notification(app_server, browser, seeded_db):
|
||||
from tests.conftest import login_user
|
||||
|
||||
|
||||
Reference in New Issue
Block a user