Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adb5def4da | ||
|
|
cbd3f697fd | ||
|
|
d14bb4c671 |
@@ -360,6 +360,29 @@ def create_comment_record(
|
||||
comment_url,
|
||||
)
|
||||
|
||||
# Notify previous commenters on this post (participation)
|
||||
posts = get_table("posts")
|
||||
post = posts.find_one(uid=target_uid)
|
||||
if not post:
|
||||
post = posts.find_one(slug=target_uid)
|
||||
if post:
|
||||
post_owner_uid = post["user_uid"]
|
||||
previous_commenters = set()
|
||||
for c in get_table("comments").find(
|
||||
target_type="post", target_uid=target_uid, deleted_at=None
|
||||
):
|
||||
cu = c["user_uid"]
|
||||
if cu != user["uid"] and cu != post_owner_uid:
|
||||
previous_commenters.add(cu)
|
||||
for cu in previous_commenters:
|
||||
create_notification(
|
||||
cu,
|
||||
"participation",
|
||||
f"{user['username']} also commented on this post",
|
||||
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)
|
||||
@@ -718,3 +741,4 @@ def enrich_items(
|
||||
)
|
||||
enriched.append(entry)
|
||||
return enriched
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ NOTIFICATION_TYPES = [
|
||||
{"key": "vote", "label": "Upvotes", "description": "Someone ++'d your content"},
|
||||
{"key": "follow", "label": "Followers", "description": "Someone starts following you"},
|
||||
{"key": "message", "label": "Direct messages", "description": "Someone sends you a message"},
|
||||
{"key": "participation", "label": "Post participation", "description": "Someone else comments on a post you also commented on"},
|
||||
{"key": "badge", "label": "Badges", "description": "You earn a badge"},
|
||||
{"key": "level", "label": "Level-ups", "description": "You reach a new level"},
|
||||
{"key": "issue", "label": "Issue tracker", "description": "Updates on issue reports you filed"},
|
||||
@@ -199,3 +200,4 @@ def mark_notifications_read_by_target(user_uid: str, target_url: str) -> int:
|
||||
|
||||
clear_unread_cache(user_uid)
|
||||
return len(ids)
|
||||
|
||||
|
||||
@@ -41,12 +41,9 @@ four ways to sign requests.
|
||||
method="GET",
|
||||
path="/profile/{username}",
|
||||
title="View a profile",
|
||||
summary="Render a user profile, including an online-presence indicator (JSON exposes profile_online, profile_user.last_seen, xp_next_level, and xp_progress_pct). Returns an HTML page.",
|
||||
summary="Render a user profile, including an online-presence indicator (JSON exposes profile_online and profile_user.last_seen). Returns an HTML page.",
|
||||
auth="public",
|
||||
interactive=True,
|
||||
notes=[
|
||||
"Level progress: `xp_next_level = level * 100` (total XP needed), `xp_progress_pct = xp % 100` (percentage towards next level). Both are also embedded in `profile_user`.",
|
||||
],
|
||||
params=[
|
||||
field(
|
||||
"username",
|
||||
@@ -428,7 +425,7 @@ four ways to sign requests.
|
||||
method="POST",
|
||||
path="/profile/{username}/notifications",
|
||||
title="Toggle a notification preference",
|
||||
summary="Enable or disable one notification type on one channel (in-app or push). Admins may target any user. Types: comment, reply, mention, vote, follow, message, badge, level, issue, reminder, harvest_stolen.",
|
||||
summary="Enable or disable one notification type on one channel (in-app or push). Admins may target any user. Types: comment, reply, mention, vote, follow, message, participation, badge, level, issue, reminder, harvest_stolen.",
|
||||
auth="user",
|
||||
encoding="form",
|
||||
destructive=True,
|
||||
@@ -447,7 +444,7 @@ four ways to sign requests.
|
||||
"string",
|
||||
True,
|
||||
"vote",
|
||||
"One of: comment, reply, mention, vote, follow, message, badge, level, issue, reminder, harvest_stolen.",
|
||||
"One of: comment, reply, mention, vote, follow, message, participation, badge, level, issue, reminder, harvest_stolen.",
|
||||
),
|
||||
field(
|
||||
"channel",
|
||||
@@ -797,3 +794,4 @@ four ways to sign requests.
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +46,6 @@ from devplacepy.utils import (
|
||||
track_action,
|
||||
build_achievements,
|
||||
)
|
||||
from devplacepy.utils.rewards import LEVEL_XP
|
||||
from devplacepy.responses import respond, action_result, wants_json
|
||||
from devplacepy.schemas import ProfileOut
|
||||
from devplacepy.avatar import avatar_url, avatar_seed
|
||||
@@ -140,10 +139,6 @@ async def profile_page(
|
||||
current_user["uid"], f"/profile/{profile_user['username']}"
|
||||
)
|
||||
profile_user["stars"] = get_user_stars(profile_user["uid"])
|
||||
xp_raw = profile_user.get("xp") or 0
|
||||
level_raw = profile_user.get("level") or 1
|
||||
xp_progress_pct = xp_raw % LEVEL_XP
|
||||
xp_next_level = level_raw * LEVEL_XP
|
||||
rank = get_user_rank(profile_user["uid"])
|
||||
follow_counts = get_follow_counts(profile_user["uid"])
|
||||
|
||||
@@ -445,8 +440,6 @@ async def profile_page(
|
||||
"awards_count": awards_count,
|
||||
"prominent_award": prominent_award,
|
||||
"can_give_award": can_give,
|
||||
"xp_next_level": xp_next_level,
|
||||
"xp_progress_pct": xp_progress_pct,
|
||||
},
|
||||
model=ProfileOut,
|
||||
)
|
||||
@@ -506,7 +499,3 @@ async def regenerate_api_key(request: Request):
|
||||
links=[audit.target("user", user["uid"], user["username"])],
|
||||
)
|
||||
return JSONResponse({"api_key": new_key})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@ class UserOut(_Out):
|
||||
website: Optional[str] = None
|
||||
level: Optional[int] = None
|
||||
xp: Optional[int] = None
|
||||
xp_progress_pct: Optional[int] = None
|
||||
xp_next_level: Optional[int] = None
|
||||
stars: Optional[int] = None
|
||||
created_at: Optional[str] = None
|
||||
last_seen: Optional[str] = None
|
||||
@@ -204,4 +202,3 @@ class MessageOut(_Out):
|
||||
|
||||
|
||||
CommentItemOut.model_rebuild()
|
||||
|
||||
|
||||
@@ -72,8 +72,6 @@ class ProfileOut(_Out):
|
||||
followers_count: Optional[int] = None
|
||||
following_count: Optional[int] = None
|
||||
viewer_is_admin: bool = False
|
||||
xp_next_level: int = 0
|
||||
xp_progress_pct: int = 0
|
||||
media: list[MediaItemOut] = []
|
||||
media_pagination: Optional[Any] = None
|
||||
notification_prefs: list[Any] = []
|
||||
@@ -90,4 +88,3 @@ class TelegramPairOut(_Out):
|
||||
code: Optional[str] = None
|
||||
expires_at: Optional[str] = None
|
||||
ttl_minutes: Optional[int] = None
|
||||
|
||||
|
||||
@@ -42,10 +42,10 @@
|
||||
<div class="profile-level-bar">
|
||||
<div class="level-label">
|
||||
<span>Progress to next level</span>
|
||||
<span>{{ xp_progress_pct }}%</span>
|
||||
<span>{{ (profile_user.get('xp') or 0) % 100 }}%</span>
|
||||
</div>
|
||||
<div class="bar">
|
||||
<div class="bar-fill" style="--bar-pct: {{ xp_progress_pct }}%;"></div>
|
||||
<div class="bar-fill" style="--bar-pct: {{ (profile_user.get('xp') or 0) % 100 }}%;"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -765,6 +765,3 @@ import { AwardGiver } from "{{ static_url('/static/js/AwardGiver.js') }}";
|
||||
new AwardGiver();
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,263 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import time
|
||||
import pytest
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot, set_setting
|
||||
|
||||
_COUNTER = [0]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _participation_settings(app_server):
|
||||
for key, value in {
|
||||
"rate_limit_per_minute": "1000000",
|
||||
"rate_limit_window_seconds": "60",
|
||||
"registration_open": "1",
|
||||
"maintenance_mode": "0",
|
||||
"max_upload_size_mb": "10",
|
||||
"allowed_file_types": "",
|
||||
"max_attachments_per_resource": "10",
|
||||
"session_max_age_days": "7",
|
||||
"session_remember_days": "30",
|
||||
"news_service_interval": "3600",
|
||||
"news_grade_threshold": "7",
|
||||
}.items():
|
||||
set_setting(key, value)
|
||||
yield
|
||||
|
||||
|
||||
def _db_user(username):
|
||||
refresh_snapshot()
|
||||
return get_table("users").find_one(username=username)
|
||||
|
||||
|
||||
def _unique(prefix="pn"):
|
||||
_COUNTER[0] += 1
|
||||
return f"{prefix}{int(time.time() * 1000)}{_COUNTER[0]}"
|
||||
|
||||
|
||||
def _signup():
|
||||
name = _unique("pnuser")
|
||||
s = requests.Session()
|
||||
s.post(
|
||||
f"{BASE_URL}/auth/signup",
|
||||
data={
|
||||
"username": name,
|
||||
"email": f"{name}@t.dev",
|
||||
"password": "secret123",
|
||||
"confirm_password": "secret123",
|
||||
},
|
||||
allow_redirects=True,
|
||||
)
|
||||
return s, name
|
||||
|
||||
|
||||
def _create_post(session):
|
||||
r = session.post(
|
||||
f"{BASE_URL}/posts/create",
|
||||
data={
|
||||
"title": _unique("pnpost"),
|
||||
"content": "Post for participation notification test.",
|
||||
"topic": "devlog",
|
||||
},
|
||||
allow_redirects=False,
|
||||
)
|
||||
slug = r.headers["location"].split("/posts/")[-1]
|
||||
refresh_snapshot()
|
||||
return get_table("posts").find_one(slug=slug)["uid"]
|
||||
|
||||
|
||||
def _comment_on_post(session, post_uid, content):
|
||||
r = session.post(
|
||||
f"{BASE_URL}/comments/create",
|
||||
data={
|
||||
"content": content,
|
||||
"target_type": "post",
|
||||
"post_uid": post_uid,
|
||||
"target_uid": post_uid,
|
||||
},
|
||||
allow_redirects=False,
|
||||
)
|
||||
assert r.status_code in (302, 303), (
|
||||
f"Comment creation failed: {r.status_code} {r.text[:300]}"
|
||||
)
|
||||
|
||||
|
||||
def _reply_to_comment(session, post_uid, parent_uid, content):
|
||||
r = session.post(
|
||||
f"{BASE_URL}/comments/create",
|
||||
data={
|
||||
"content": content,
|
||||
"target_type": "post",
|
||||
"post_uid": post_uid,
|
||||
"target_uid": post_uid,
|
||||
"parent_uid": parent_uid,
|
||||
},
|
||||
allow_redirects=False,
|
||||
)
|
||||
assert r.status_code in (302, 303), (
|
||||
f"Reply creation failed: {r.status_code} {r.text[:300]}"
|
||||
)
|
||||
|
||||
|
||||
def _find_comment(post_uid, content):
|
||||
refresh_snapshot()
|
||||
return get_table("comments").find_one(target_uid=post_uid, content=content)
|
||||
|
||||
|
||||
def _notifications_for(user_uid):
|
||||
refresh_snapshot()
|
||||
return list(
|
||||
get_table("notifications").find(
|
||||
user_uid=user_uid, order_by=["-created_at"]
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _participation_notifications_for(user_uid):
|
||||
refresh_snapshot()
|
||||
return list(
|
||||
get_table("notifications").find(
|
||||
user_uid=user_uid, type="participation", order_by=["-created_at"]
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_participation_notification_sent(app_server):
|
||||
"""User A receives a participation notification when User B comments
|
||||
on a post that User A previously commented on."""
|
||||
owner_session, owner_name = _signup()
|
||||
post_uid = _create_post(owner_session)
|
||||
|
||||
a_session, a_name = _signup()
|
||||
b_session, b_name = _signup()
|
||||
|
||||
_comment_on_post(a_session, post_uid, "User A first comment")
|
||||
_comment_on_post(b_session, post_uid, "User B comment")
|
||||
|
||||
a_user = _db_user(a_name)
|
||||
assert a_user is not None
|
||||
|
||||
participation_notifs = _participation_notifications_for(a_user["uid"])
|
||||
assert len(participation_notifs) >= 1, (
|
||||
f"User {a_name} should have at least one participation notification, "
|
||||
f"got {len(participation_notifs)}"
|
||||
)
|
||||
latest = participation_notifs[0]
|
||||
assert latest["type"] == "participation"
|
||||
assert b_name in latest["message"], (
|
||||
f"Expected notification message to contain {b_name!r}, "
|
||||
f"got {latest['message']!r}"
|
||||
)
|
||||
assert "also commented" in latest["message"], (
|
||||
f"Expected 'also commented' in message, got {latest['message']!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_post_owner_no_participation_duplicate(app_server):
|
||||
"""Post owner does NOT receive a participation notification.
|
||||
They already receive the 'comment' notification."""
|
||||
owner_session, owner_name = _signup()
|
||||
post_uid = _create_post(owner_session)
|
||||
|
||||
a_session, a_name = _signup()
|
||||
b_session, b_name = _signup()
|
||||
|
||||
_comment_on_post(a_session, post_uid, "User A comment for owner test")
|
||||
_comment_on_post(b_session, post_uid, "User B comment for owner test")
|
||||
|
||||
owner = _db_user(owner_name)
|
||||
assert owner is not None
|
||||
|
||||
participation_notifs = _participation_notifications_for(owner["uid"])
|
||||
assert len(participation_notifs) == 0, (
|
||||
f"Post owner {owner_name} should have zero participation notifications, "
|
||||
f"got {len(participation_notifs)}: "
|
||||
f"{[n['message'] for n in participation_notifs]}"
|
||||
)
|
||||
|
||||
all_notifs = _notifications_for(owner["uid"])
|
||||
comment_notifs = [n for n in all_notifs if n["type"] == "comment"]
|
||||
assert len(comment_notifs) >= 1, (
|
||||
f"Post owner should have at least one 'comment' notification, "
|
||||
f"got {len(comment_notifs)}"
|
||||
)
|
||||
|
||||
|
||||
def test_commenter_no_self_notification(app_server):
|
||||
"""Commenter does NOT receive a participation notification
|
||||
for their own comment."""
|
||||
owner_session, _ = _signup()
|
||||
post_uid = _create_post(owner_session)
|
||||
|
||||
a_session, a_name = _signup()
|
||||
_comment_on_post(a_session, post_uid, "User A self-test comment")
|
||||
|
||||
b_session, b_name = _signup()
|
||||
_comment_on_post(b_session, post_uid, "User B self-test comment")
|
||||
|
||||
b_user = _db_user(b_name)
|
||||
assert b_user is not None
|
||||
|
||||
participation_notifs = _participation_notifications_for(b_user["uid"])
|
||||
self_notifs = [
|
||||
n for n in participation_notifs if b_name in n["message"]
|
||||
]
|
||||
assert len(self_notifs) == 0, (
|
||||
f"User {b_name} should not have a participation notification "
|
||||
f"about themselves, got {len(self_notifs)}"
|
||||
)
|
||||
|
||||
|
||||
def test_reply_triggers_participation(app_server):
|
||||
"""A reply to a comment triggers participation notifications
|
||||
for other previous commenters (excluding the reply author and post owner).
|
||||
The implementation sends participation for every comment on a post,
|
||||
both top-level and replies."""
|
||||
owner_session, owner_name = _signup()
|
||||
post_uid = _create_post(owner_session)
|
||||
|
||||
a_session, a_name = _signup()
|
||||
_comment_on_post(a_session, post_uid, "User A top-level comment")
|
||||
a_comment = _find_comment(post_uid, "User A top-level comment")
|
||||
|
||||
c_session, c_name = _signup()
|
||||
_comment_on_post(c_session, post_uid, "User C third participant comment")
|
||||
|
||||
b_session, b_name = _signup()
|
||||
_reply_to_comment(b_session, post_uid, a_comment["uid"], "User B reply")
|
||||
|
||||
a_user = _db_user(a_name)
|
||||
c_user = _db_user(c_name)
|
||||
|
||||
a_participation = _participation_notifications_for(a_user["uid"])
|
||||
c_participation = _participation_notifications_for(c_user["uid"])
|
||||
|
||||
a_has_participation = any(
|
||||
b_name in n["message"] for n in a_participation
|
||||
)
|
||||
c_has_participation = any(
|
||||
b_name in n["message"] for n in c_participation
|
||||
)
|
||||
|
||||
assert a_has_participation, (
|
||||
f"User {a_name} (parent commenter) should receive a participation "
|
||||
f"notification when a reply is posted on the same post. "
|
||||
f"Notifications: {[n['message'] for n in a_participation]}"
|
||||
)
|
||||
assert c_has_participation, (
|
||||
f"User {c_name} (previous commenter) should receive a participation "
|
||||
f"notification when a reply is posted on the same post. "
|
||||
f"Notifications: {[n['message'] for n in c_participation]}"
|
||||
)
|
||||
|
||||
b_user = _db_user(b_name)
|
||||
b_participation = _participation_notifications_for(b_user["uid"])
|
||||
b_self = [n for n in b_participation if b_name in n["message"]]
|
||||
assert len(b_self) == 0, (
|
||||
f"Reply author {b_name} should not receive a participation "
|
||||
f"notification about themselves."
|
||||
)
|
||||
|
||||
@@ -407,96 +407,3 @@ def test_viewing_profile_marks_notification_read(app_server):
|
||||
|
||||
refresh_snapshot()
|
||||
assert bool(get_table("notifications").find_one(uid=notif_uid)["read"]) is True
|
||||
|
||||
|
||||
def test_own_profile_json_exposes_xp_fields(app_server, seeded_db):
|
||||
"""GET /profile (own) with Accept: application/json includes xp_next_level and xp_progress_pct."""
|
||||
import requests
|
||||
|
||||
session = requests.Session()
|
||||
session.post(
|
||||
f"{BASE_URL}/auth/login",
|
||||
data={
|
||||
"username": seeded_db["alice"]["username"],
|
||||
"password": seeded_db["alice"]["password"],
|
||||
},
|
||||
allow_redirects=True,
|
||||
)
|
||||
r = session.get(f"{BASE_URL}/profile", headers={"Accept": "application/json"})
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
pu = data["profile_user"]
|
||||
assert "xp_next_level" in pu, "xp_next_level missing from own profile JSON"
|
||||
assert "xp_progress_pct" in pu, "xp_progress_pct missing from own profile JSON"
|
||||
assert isinstance(pu["xp_next_level"], int)
|
||||
assert isinstance(pu["xp_progress_pct"], int)
|
||||
|
||||
|
||||
def test_other_profile_json_exposes_xp_fields(app_server):
|
||||
"""GET /profile/{username} with Accept: application/json includes xp_next_level and xp_progress_pct."""
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
|
||||
owner = _seed_owner()
|
||||
username = get_table("users").find_one(uid=owner)["username"]
|
||||
refresh_snapshot()
|
||||
|
||||
r = requests.get(
|
||||
f"{BASE_URL}/profile/{username}", headers={"Accept": "application/json"}
|
||||
)
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
pu = data["profile_user"]
|
||||
assert "xp_next_level" in pu, "xp_next_level missing from other profile JSON"
|
||||
assert "xp_progress_pct" in pu, "xp_progress_pct missing from other profile JSON"
|
||||
assert isinstance(pu["xp_next_level"], int)
|
||||
assert isinstance(pu["xp_progress_pct"], int)
|
||||
|
||||
|
||||
def test_profile_json_xp_fields_zero_xp(app_server):
|
||||
"""User with 0 XP returns xp_next_level=100 (level 1) and xp_progress_pct=0."""
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
from devplacepy.utils.rewards import LEVEL_XP
|
||||
|
||||
owner = _seed_owner()
|
||||
username = get_table("users").find_one(uid=owner)["username"]
|
||||
refresh_snapshot()
|
||||
|
||||
r = requests.get(
|
||||
f"{BASE_URL}/profile/{username}", headers={"Accept": "application/json"}
|
||||
)
|
||||
assert r.status_code == 200
|
||||
pu = r.json()["profile_user"]
|
||||
assert pu["xp"] == 0 or pu["xp"] is None, f"expected 0 xp, got {pu['xp']}"
|
||||
assert pu["xp_next_level"] == LEVEL_XP, (
|
||||
f"expected xp_next_level={LEVEL_XP} for level 1, got {pu['xp_next_level']}"
|
||||
)
|
||||
assert pu["xp_progress_pct"] == 0, (
|
||||
f"expected xp_progress_pct=0 for 0 XP, got {pu['xp_progress_pct']}"
|
||||
)
|
||||
|
||||
|
||||
def test_profile_json_xp_fields_boundary_xp(app_server):
|
||||
"""User with exactly 100 XP (level 2) returns xp_next_level=200 and xp_progress_pct=0."""
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
from devplacepy.utils.rewards import award_xp
|
||||
|
||||
owner = _seed_owner()
|
||||
username = get_table("users").find_one(uid=owner)["username"]
|
||||
award_xp(owner, 100)
|
||||
refresh_snapshot()
|
||||
|
||||
r = requests.get(
|
||||
f"{BASE_URL}/profile/{username}", headers={"Accept": "application/json"}
|
||||
)
|
||||
assert r.status_code == 200
|
||||
pu = r.json()["profile_user"]
|
||||
assert pu["xp"] == 100, f"expected 100 xp, got {pu['xp']}"
|
||||
assert pu["level"] == 2, f"expected level 2, got {pu['level']}"
|
||||
assert pu["xp_next_level"] == 200, (
|
||||
f"expected xp_next_level=200 for level 2, got {pu['xp_next_level']}"
|
||||
)
|
||||
assert pu["xp_progress_pct"] == 0, (
|
||||
f"expected xp_progress_pct=0 at boundary, got {pu['xp_progress_pct']}"
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user