mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-28 21:49:40 +02:00
refactor: remove read tracking from mentions (prep for notification center)
This commit is contained in:
parent
25eb240539
commit
80e19a52cb
11 changed files with 7 additions and 227 deletions
|
|
@ -37,10 +37,5 @@ def upgrade() -> None:
|
|||
|
||||
def downgrade() -> None:
|
||||
"""Remove author_id column from new_chat_messages table."""
|
||||
op.execute(
|
||||
"""
|
||||
DROP INDEX IF EXISTS ix_new_chat_messages_author_id;
|
||||
ALTER TABLE new_chat_messages
|
||||
DROP COLUMN IF EXISTS author_id;
|
||||
"""
|
||||
)
|
||||
op.execute("DROP INDEX IF EXISTS ix_new_chat_messages_author_id")
|
||||
op.execute("ALTER TABLE new_chat_messages DROP COLUMN IF EXISTS author_id")
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ def upgrade() -> None:
|
|||
id SERIAL PRIMARY KEY,
|
||||
comment_id INTEGER NOT NULL REFERENCES chat_comments(id) ON DELETE CASCADE,
|
||||
mentioned_user_id UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
read BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (comment_id, mentioned_user_id)
|
||||
)
|
||||
|
|
@ -31,9 +30,6 @@ def upgrade() -> None:
|
|||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_chat_comment_mentions_comment_id ON chat_comment_mentions(comment_id)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_chat_comment_mentions_user_unread ON chat_comment_mentions(mentioned_user_id) WHERE read = FALSE"
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
|
|
|
|||
|
|
@ -513,7 +513,6 @@ class ChatCommentMention(BaseModel, TimestampMixin):
|
|||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
read = Column(Boolean, nullable=False, default=False)
|
||||
|
||||
# Relationships
|
||||
comment = relationship("ChatComment", back_populates="mentions")
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@ from app.services.chat_comments_service import (
|
|||
delete_comment,
|
||||
get_comments_for_message,
|
||||
get_user_mentions,
|
||||
mark_all_mentions_as_read,
|
||||
mark_mention_as_read,
|
||||
update_comment,
|
||||
)
|
||||
from app.users import current_active_user
|
||||
|
|
@ -90,28 +88,8 @@ async def remove_comment(
|
|||
@router.get("/mentions", response_model=MentionListResponse)
|
||||
async def list_mentions(
|
||||
search_space_id: int | None = None,
|
||||
unread_only: bool = False,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
"""List mentions for the current user."""
|
||||
return await get_user_mentions(session, user, search_space_id, unread_only)
|
||||
|
||||
|
||||
@router.put("/mentions/{mention_id}/read")
|
||||
async def read_mention(
|
||||
mention_id: int,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
"""Mark a specific mention as read."""
|
||||
return await mark_mention_as_read(session, mention_id, user)
|
||||
|
||||
|
||||
@router.put("/mentions/read-all")
|
||||
async def read_all_mentions(
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
"""Mark all mentions as read for the current user."""
|
||||
return await mark_all_mentions_as_read(session, user)
|
||||
return await get_user_mentions(session, user, search_space_id)
|
||||
|
|
|
|||
|
|
@ -115,7 +115,6 @@ class MentionResponse(BaseModel):
|
|||
"""Schema for a mention notification."""
|
||||
|
||||
id: int
|
||||
read: bool
|
||||
created_at: datetime
|
||||
comment: MentionCommentResponse
|
||||
context: MentionContextResponse
|
||||
|
|
@ -127,4 +126,4 @@ class MentionListResponse(BaseModel):
|
|||
"""Response for listing user's mentions."""
|
||||
|
||||
mentions: list[MentionResponse]
|
||||
unread_count: int
|
||||
total_count: int
|
||||
|
|
|
|||
|
|
@ -569,7 +569,6 @@ async def get_user_mentions(
|
|||
session: AsyncSession,
|
||||
user: User,
|
||||
search_space_id: int | None = None,
|
||||
unread_only: bool = False,
|
||||
) -> MentionListResponse:
|
||||
"""
|
||||
Get mentions for the current user, optionally filtered by search space.
|
||||
|
|
@ -578,10 +577,9 @@ async def get_user_mentions(
|
|||
session: Database session
|
||||
user: The current authenticated user
|
||||
search_space_id: Optional search space ID to filter mentions
|
||||
unread_only: If True, only return unread mentions
|
||||
|
||||
Returns:
|
||||
MentionListResponse with mentions and unread count
|
||||
MentionListResponse with mentions and total count
|
||||
"""
|
||||
# Build query with joins for filtering by search_space_id
|
||||
query = (
|
||||
|
|
@ -599,9 +597,6 @@ async def get_user_mentions(
|
|||
if search_space_id is not None:
|
||||
query = query.filter(NewChatThread.search_space_id == search_space_id)
|
||||
|
||||
if unread_only:
|
||||
query = query.filter(ChatCommentMention.read.is_(False))
|
||||
|
||||
result = await session.execute(query)
|
||||
mention_records = result.scalars().all()
|
||||
|
||||
|
|
@ -617,9 +612,6 @@ async def get_user_mentions(
|
|||
else:
|
||||
threads_map = {}
|
||||
|
||||
# Count unread from fetched data
|
||||
unread_count = sum(1 for m in mention_records if not m.read)
|
||||
|
||||
mentions = []
|
||||
for mention in mention_records:
|
||||
comment = mention.comment
|
||||
|
|
@ -645,7 +637,6 @@ async def get_user_mentions(
|
|||
mentions.append(
|
||||
MentionResponse(
|
||||
id=mention.id,
|
||||
read=mention.read,
|
||||
created_at=mention.created_at,
|
||||
comment=MentionCommentResponse(
|
||||
id=comment.id,
|
||||
|
|
@ -665,75 +656,5 @@ async def get_user_mentions(
|
|||
|
||||
return MentionListResponse(
|
||||
mentions=mentions,
|
||||
unread_count=unread_count,
|
||||
total_count=len(mentions),
|
||||
)
|
||||
|
||||
|
||||
async def mark_mention_as_read(
|
||||
session: AsyncSession,
|
||||
mention_id: int,
|
||||
user: User,
|
||||
) -> dict:
|
||||
"""
|
||||
Mark a specific mention as read.
|
||||
|
||||
Args:
|
||||
session: Database session
|
||||
mention_id: ID of the mention to mark as read
|
||||
user: The current authenticated user
|
||||
|
||||
Returns:
|
||||
Dict with mention_id and read status
|
||||
|
||||
Raises:
|
||||
HTTPException: If mention not found or doesn't belong to user
|
||||
"""
|
||||
result = await session.execute(
|
||||
select(ChatCommentMention).filter(ChatCommentMention.id == mention_id)
|
||||
)
|
||||
mention = result.scalars().first()
|
||||
|
||||
if not mention:
|
||||
raise HTTPException(status_code=404, detail="Mention not found")
|
||||
|
||||
if mention.mentioned_user_id != user.id:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="You can only mark your own mentions as read",
|
||||
)
|
||||
|
||||
mention.read = True
|
||||
await session.commit()
|
||||
|
||||
return {"mention_id": mention_id, "read": True}
|
||||
|
||||
|
||||
async def mark_all_mentions_as_read(
|
||||
session: AsyncSession,
|
||||
user: User,
|
||||
) -> dict:
|
||||
"""
|
||||
Mark all mentions for the current user as read.
|
||||
|
||||
Args:
|
||||
session: Database session
|
||||
user: The current authenticated user
|
||||
|
||||
Returns:
|
||||
Dict with count of mentions marked as read
|
||||
"""
|
||||
from sqlalchemy import update
|
||||
|
||||
result = await session.execute(
|
||||
update(ChatCommentMention)
|
||||
.where(
|
||||
ChatCommentMention.mentioned_user_id == user.id,
|
||||
ChatCommentMention.read.is_(False),
|
||||
)
|
||||
.values(read=True)
|
||||
.returning(ChatCommentMention.id)
|
||||
)
|
||||
marked_ids = result.scalars().all()
|
||||
await session.commit()
|
||||
|
||||
return {"message": "All mentions marked as read", "count": len(marked_ids)}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue