Skip to content

Commit bc4cc78

Browse files
committed
Revert thread_to_pb / total_num_responses to take CouchersContext
1 parent 6f03e2c commit bc4cc78

5 files changed

Lines changed: 15 additions & 19 deletions

File tree

app/backend/src/couchers/servicers/discussions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def discussion_to_pb(session: Session, discussion: Discussion, context: Couchers
4141
owner_title=discussion.owner_cluster.name,
4242
title=discussion.title,
4343
content=discussion.content,
44-
thread=thread_to_pb(session, context.user_id, discussion.thread_id),
44+
thread=thread_to_pb(session, context, discussion.thread_id),
4545
can_moderate=can_moderate,
4646
)
4747

app/backend/src/couchers/servicers/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def event_to_pb(session: Session, occurrence: EventOccurrence, context: Couchers
204204
owner_user_id=event.owner_user_id,
205205
owner_community_id=owner_community_id,
206206
owner_group_id=owner_group_id,
207-
thread=thread_to_pb(session, context.user_id, event.thread_id),
207+
thread=thread_to_pb(session, context, event.thread_id),
208208
can_edit=can_edit,
209209
can_moderate=can_moderate,
210210
)

app/backend/src/couchers/servicers/pages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def page_to_pb(session: Session, page: Page, context: CouchersContext) -> pages_
7878
owner_user_id=page.owner_user_id,
7979
owner_community_id=owner_community_id,
8080
owner_group_id=owner_group_id,
81-
thread=thread_to_pb(session, context.user_id, page.thread_id),
81+
thread=thread_to_pb(session, context, page.thread_id),
8282
title=current_version.title,
8383
content=current_version.content,
8484
photo_url=current_version.photo.full_url if current_version.photo_key else None,

app/backend/src/couchers/servicers/threads.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
from sqlalchemy.orm import Session
77
from sqlalchemy.sql import func
88

9-
from couchers.context import CouchersContext, make_background_user_context, make_logged_out_context
9+
from couchers.context import CouchersContext, make_background_user_context
1010
from couchers.db import session_scope
11-
from couchers.i18n import LocalizationContext
1211
from couchers.jobs.enqueue import queue_job
1312
from couchers.models import (
1413
Comment,
@@ -48,14 +47,7 @@ def unpack_thread_id(thread_id: int) -> tuple[int, int]:
4847
return divmod(thread_id, 10)
4948

5049

51-
def _viewing_context(viewing_user_id: int | None) -> CouchersContext:
52-
if viewing_user_id is not None:
53-
return make_background_user_context(user_id=viewing_user_id)
54-
return make_logged_out_context(LocalizationContext.en_utc())
55-
56-
57-
def total_num_responses(session: Session, viewing_user_id: int | None, database_id: int) -> int:
58-
context = _viewing_context(viewing_user_id)
50+
def total_num_responses(session: Session, context: CouchersContext, database_id: int) -> int:
5951
comments = where_moderated_content_visible(
6052
select(func.count()).select_from(Comment).where(Comment.thread_id == database_id),
6153
context,
@@ -74,10 +66,10 @@ def total_num_responses(session: Session, viewing_user_id: int | None, database_
7466
return session.execute(comments).scalar_one() + session.execute(replies).scalar_one()
7567

7668

77-
def thread_to_pb(session: Session, viewing_user_id: int | None, database_id: int) -> threads_pb2.Thread:
69+
def thread_to_pb(session: Session, context: CouchersContext, database_id: int) -> threads_pb2.Thread:
7870
return threads_pb2.Thread(
7971
thread_id=pack_thread_id(database_id, 0),
80-
num_responses=total_num_responses(session, viewing_user_id, database_id),
72+
num_responses=total_num_responses(session, context, database_id),
8173
)
8274

8375

app/backend/src/tests/test_threads.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,18 @@ def test_admin_can_hide_comment(db):
338338

339339

340340
def test_total_num_responses_excludes_shadowed(db):
341+
from couchers.context import make_background_user_context # noqa: PLC0415
341342
from couchers.servicers.threads import total_num_responses # noqa: PLC0415
342343

343344
author, author_token = generate_user()
344345
viewer, _ = generate_user()
345346
parent_thread_id, _ = _make_thread_and_comment(author_token, content="one")
347+
viewer_context = make_background_user_context(user_id=viewer.id)
346348

347349
parent_db_id, _ = divmod(parent_thread_id, 10)
348350

349351
with session_scope() as session:
350-
assert total_num_responses(session, viewer.id, parent_db_id) == 0
352+
assert total_num_responses(session, viewer_context, parent_db_id) == 0
351353

352354
with session_scope() as session:
353355
state = session.execute(
@@ -359,18 +361,20 @@ def test_total_num_responses_excludes_shadowed(db):
359361
state.visibility = ModerationVisibility.visible
360362

361363
with session_scope() as session:
362-
assert total_num_responses(session, viewer.id, parent_db_id) == 1
364+
assert total_num_responses(session, viewer_context, parent_db_id) == 1
363365

364366

365367
def test_total_num_responses_includes_own_shadowed(db):
366-
"""The count uses the viewer's id so authors see their own shadowed content in the total,
368+
"""The count uses the viewer's context so authors see their own shadowed content in the total,
367369
matching what GetThread shows them in the list."""
370+
from couchers.context import make_background_user_context # noqa: PLC0415
368371
from couchers.servicers.threads import total_num_responses # noqa: PLC0415
369372

370373
author, author_token = generate_user()
371374
parent_thread_id, _ = _make_thread_and_comment(author_token, content="one")
375+
author_context = make_background_user_context(user_id=author.id)
372376

373377
parent_db_id, _ = divmod(parent_thread_id, 10)
374378

375379
with session_scope() as session:
376-
assert total_num_responses(session, author.id, parent_db_id) == 1
380+
assert total_num_responses(session, author_context, parent_db_id) == 1

0 commit comments

Comments
 (0)