Private
Public Access
Mirrors the existing @-mention system's shape: a regex finds #roomname tokens, extract_referenced_room_ids validates them against rooms the *sender* actually belongs to (mirrors mentions' "must be a real member" rule -- referencing a private room the sender isn't in would otherwise leak its existence), and a MessageRoomReference join row is stored per match in create_message. No notification/unread layer, unlike mentions -- referencing a room has no "you were referenced" semantics. Rendering is the same markdown-link rewrite trick MessageContent.tsx already uses for mentions (#username -> [#username](mention:username)), but resolved against the *viewer's* own room list (threaded down from ChatShellPage's room state through ChatPane/MessageList) rather than the stored server-side reference -- a reference to a room the current viewer isn't in quietly renders as plain text instead of a link, same as an @mention of someone outside the room does. The href scheme renders a real react-router Link instead of mentions' inert span, since a room reference is meant to be navigable. mention_service.strip_code_spans (was _strip_code_spans) is now shared between both extraction paths rather than private to one module. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
import re
|
|
import uuid
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models import RoomMembership, User
|
|
|
|
MENTION_PATTERN = re.compile(r"@([a-zA-Z0-9_.-]+)")
|
|
|
|
|
|
def strip_code_spans(content: str) -> str:
|
|
"""Blanks out fenced code blocks and inline code spans (replacing with
|
|
equal-length whitespace, so a bare '@'/'#' in pasted code -- a
|
|
decorator, an email fragment, a shell comment -- doesn't trigger a
|
|
mention or room reference. Mirrors the same skip logic
|
|
frontend/src/components/MessageContent.tsx already uses for emoji
|
|
shortcode conversion. Shared with room_reference_service, not private
|
|
to this module anymore."""
|
|
lines = content.split("\n")
|
|
in_fence = False
|
|
out = []
|
|
for line in lines:
|
|
if re.match(r"^\s*```", line):
|
|
in_fence = not in_fence
|
|
out.append(line)
|
|
continue
|
|
if in_fence:
|
|
out.append(line)
|
|
continue
|
|
parts = re.split(r"(`+[^`]*`+)", line)
|
|
out.append("".join(part if i % 2 == 0 else " " * len(part) for i, part in enumerate(parts)))
|
|
return "\n".join(out)
|
|
|
|
|
|
async def extract_mentioned_user_ids(
|
|
db: AsyncSession, room_id: uuid.UUID, content: str
|
|
) -> set[uuid.UUID]:
|
|
"""Resolves `@username` tokens in `content` against this room's actual
|
|
members -- a bare '@' followed by prose that happens to not match
|
|
anyone's username is just text, not a mention."""
|
|
usernames = set(MENTION_PATTERN.findall(strip_code_spans(content)))
|
|
if not usernames:
|
|
return set()
|
|
|
|
result = await db.execute(
|
|
select(RoomMembership.user_id)
|
|
.join(User, User.id == RoomMembership.user_id)
|
|
.where(RoomMembership.room_id == room_id, User.username.in_(usernames))
|
|
)
|
|
return {row[0] for row in result.all()}
|