Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/msgd/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4425,23 +4425,26 @@ def _reindex_inbox(self, post_id: int) -> None:
events.add((target, "reply"))

text = post.title + "\n" + post.body
alias_keys: set[str] = set()
for token in set(MENTION_RE.findall(text)):
lowered = token.lower()
targets: set[str] = set()
if AUTHOR_ID_RE.fullmatch(lowered):
targets.add(lowered)
if lowered != actor_id:
events.add((lowered, "mention"))
else:
rows = self._conn.execute(
"""
SELECT author_id
FROM name_claims
WHERE name_key = ?
""",
(self.normalize_identity_name(token),),
).fetchall()
targets.update(str(item["author_id"]) for item in rows)
alias_keys.add(self.normalize_identity_name(token))

for target in targets:
if alias_keys:
rows = self._conn.execute(
"""
SELECT author_id
FROM name_claims
WHERE name_key IN (SELECT value FROM json_each(?))
""",
(json.dumps(sorted(alias_keys), separators=(",", ":")),),
).fetchall()
for item in rows:
target = str(item["author_id"])
if target != actor_id:
events.add((target, "mention"))

Expand Down
27 changes: 27 additions & 0 deletions tests/test_msgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,33 @@ def test_private_inbox_collects_replies_and_mentions(self) -> None:
self.assertIn(f"[mention] #{direct_id}", inbox)
self.assertIn("latest_id=", inbox)

def test_alias_mentions_are_resolved_in_one_query(self) -> None:
alice = Ed25519PrivateKey.generate()
self.issue_member(alice)
self.signed_create(alice, "hello", name="Alice")

statements: list[str] = []
self.server.board.store._conn.set_trace_callback(statements.append)
try:
status, body = self.c.post(
"/publish",
board="main",
text="ping @Alice " + " ".join(f"@missing{index}" for index in range(1_000)),
)
finally:
self.server.board.store._conn.set_trace_callback(None)

self.assertEqual(status, 201, body)
alias_queries = [
statement
for statement in statements
if "FROM name_claims" in statement and "json_each" in statement
]
self.assertEqual(len(alias_queries), 1)
status, inbox, _ = self.read_inbox(alice)
self.assertEqual(status, 200, inbox)
self.assertIn("[mention]", inbox)

def test_inbox_signature_nonce_and_cursor_are_enforced(self) -> None:
alice = Ed25519PrivateKey.generate()
mallory = Ed25519PrivateKey.generate()
Expand Down