diff --git a/server/messages.py b/server/messages.py index 26d8834..e2144e5 100644 --- a/server/messages.py +++ b/server/messages.py @@ -470,19 +470,23 @@ async def mark_read(db: Connection, reader: Address, ids: list[int]) -> int: async def conversations(db: Connection, person: int) -> dict: """person's conversations, newest first: each with its last message, the addresses of their side in it and what waits there.""" + # The sides are totalled once per conversation, not once per message, so the + # time grows with the messages and not with their square. Each message of mine + # was sent by person or delivered to them, so every key has totals. rows = await db.fetch( f"with mine as ({_MINE}), " "sides as (select x.key, address(d.recipient, d.recipient_agent) as side, " "d.read_at is null as waits, x.created_at from mine x " "join deliveries d on d.message_id = x.id and d.recipient = $1 " "union all select key, address(sender, sender_agent), false, created_at " - "from mine where sender = $1) " + "from mine where sender = $1), " + "totals as (select key, array_agg(distinct side) as sides, " + "count(*) filter (where waits) as waiting, " + "min(created_at) filter (where waits) as waiting_since from sides group by key) " 'select distinct on (x.key) x.key, x."from", x.body, x.created_at, g.name, ' - "(select array_agg(distinct side) from sides s where s.key = x.key) as sides, " - "(select count(*) from sides s where s.key = x.key and waits) as waiting, " - "(select min(created_at) from sides s where s.key = x.key and waits) " - "as waiting_since " - "from mine x left join groups g on group_address(g.id) = x.key " + "t.sides, t.waiting, t.waiting_since " + "from mine x join totals t on t.key = x.key " + "left join groups g on group_address(g.id) = x.key " "order by x.key, x.id desc", person, ) diff --git a/server/tests/test_inbox.py b/server/tests/test_inbox.py index 165f2b3..3798350 100644 --- a/server/tests/test_inbox.py +++ b/server/tests/test_inbox.py @@ -1497,6 +1497,33 @@ async def test_two_of_your_agents_talk_in_one_conversation(db): assert [m["text"] for m in transcript] == ["run the suite", "412 passed"] +async def test_each_conversation_counts_only_what_waits_in_it(db): + turing, church = signed_in("turing"), signed_in("church") + kleene = signed_in("kleene") + await inbox(agent="bot", token=turing, db=db) + await whoami(token=church, db=db) + await send("church", "hello", token=turing, db=db) + await send("turing", "first", token=church, db=db) + await send("turing/bot", "second", token=church, db=db) + seen = await send("turing", "old news", token=kleene, db=db) + await mark_read([seen["id"]], token=turing, db=db) + await send("turing", "news", token=kleene, db=db) + + with browser(await account_id(db, "turing")) as page: + listed = page.get("/api/conversations").json()["conversations"] + sent = { + m["text"]: m["sent_at"] + for key in ("church", "kleene") + for m in page.get(f"/api/conversations/{key}").json()["messages"] + } + assert [ + (c["key"], c["sides"], c["waiting"], c["waiting_since"]) for c in listed + ] == [ + ("kleene", ["turing"], 1, sent["news"]), + ("church", ["turing", "turing/bot"], 2, sent["first"]), + ] + + async def test_a_group_conversation_shows_every_members_messages(db): turing, church = signed_in("turing"), signed_in("church") group = (await create_group("compilers", token=turing, db=db))["group"]