feat(invites): add group/channel invite links and join by token
This commit is contained in:
@@ -93,3 +93,17 @@ class ChatUserSetting(Base):
|
||||
onupdate=func.now(),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
|
||||
class ChatInviteLink(Base):
|
||||
__tablename__ = "chat_invite_links"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("token", name="uq_chat_invite_links_token"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
||||
chat_id: Mapped[int] = mapped_column(ForeignKey("chats.id", ondelete="CASCADE"), nullable=False, index=True)
|
||||
creator_user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
|
||||
token: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="true")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
|
||||
@@ -2,7 +2,7 @@ from sqlalchemy import Select, String, func, or_, select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatNotificationSetting, ChatType, ChatUserSetting
|
||||
from app.chats.models import Chat, ChatInviteLink, ChatMember, ChatMemberRole, ChatNotificationSetting, ChatType, ChatUserSetting
|
||||
from app.messages.models import Message, MessageHidden, MessageReceipt
|
||||
|
||||
|
||||
@@ -320,3 +320,25 @@ async def upsert_chat_pinned_setting(
|
||||
db.add(setting)
|
||||
await db.flush()
|
||||
return setting
|
||||
|
||||
|
||||
async def create_chat_invite_link(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
chat_id: int,
|
||||
creator_user_id: int,
|
||||
token: str,
|
||||
) -> ChatInviteLink:
|
||||
link = ChatInviteLink(chat_id=chat_id, creator_user_id=creator_user_id, token=token, is_active=True)
|
||||
db.add(link)
|
||||
await db.flush()
|
||||
return link
|
||||
|
||||
|
||||
async def get_active_chat_invite_by_token(db: AsyncSession, *, token: str) -> ChatInviteLink | None:
|
||||
result = await db.execute(
|
||||
select(ChatInviteLink)
|
||||
.where(ChatInviteLink.token == token, ChatInviteLink.is_active.is_(True))
|
||||
.limit(1)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@@ -7,6 +7,8 @@ from app.chats.schemas import (
|
||||
ChatDetailRead,
|
||||
ChatDiscoverRead,
|
||||
ChatDeleteRequest,
|
||||
ChatInviteLinkRead,
|
||||
ChatJoinByInviteRequest,
|
||||
ChatMemberAddRequest,
|
||||
ChatMemberRead,
|
||||
ChatMemberRoleUpdateRequest,
|
||||
@@ -19,6 +21,7 @@ from app.chats.schemas import (
|
||||
from app.chats.service import (
|
||||
add_chat_member_for_user,
|
||||
create_chat_for_user,
|
||||
create_chat_invite_link_for_user,
|
||||
clear_chat_for_user,
|
||||
delete_chat_for_user,
|
||||
discover_public_chats_for_user,
|
||||
@@ -27,6 +30,7 @@ from app.chats.service import (
|
||||
get_chat_notification_settings_for_user,
|
||||
get_chats_for_user,
|
||||
join_public_chat_for_user,
|
||||
join_chat_by_invite_for_user,
|
||||
leave_chat_for_user,
|
||||
pin_chat_message_for_user,
|
||||
remove_chat_member_for_user,
|
||||
@@ -278,3 +282,22 @@ async def unpin_chat(
|
||||
) -> ChatRead:
|
||||
chat = await set_chat_pinned_for_user(db, chat_id=chat_id, user_id=current_user.id, pinned=False)
|
||||
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
|
||||
|
||||
|
||||
@router.post("/{chat_id}/invite-link", response_model=ChatInviteLinkRead)
|
||||
async def create_invite_link(
|
||||
chat_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> ChatInviteLinkRead:
|
||||
return await create_chat_invite_link_for_user(db, chat_id=chat_id, user_id=current_user.id)
|
||||
|
||||
|
||||
@router.post("/join-by-invite", response_model=ChatRead)
|
||||
async def join_by_invite(
|
||||
payload: ChatJoinByInviteRequest,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> ChatRead:
|
||||
chat = await join_chat_by_invite_for_user(db, user_id=current_user.id, payload=payload)
|
||||
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
|
||||
|
||||
@@ -87,3 +87,13 @@ class ChatNotificationSettingsRead(BaseModel):
|
||||
|
||||
class ChatNotificationSettingsUpdate(BaseModel):
|
||||
muted: bool
|
||||
|
||||
|
||||
class ChatInviteLinkRead(BaseModel):
|
||||
chat_id: int
|
||||
token: str
|
||||
invite_url: str
|
||||
|
||||
|
||||
class ChatJoinByInviteRequest(BaseModel):
|
||||
token: str = Field(min_length=8, max_length=64)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import secrets
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -8,12 +10,15 @@ from app.chats.schemas import (
|
||||
ChatCreateRequest,
|
||||
ChatDeleteRequest,
|
||||
ChatDiscoverRead,
|
||||
ChatJoinByInviteRequest,
|
||||
ChatNotificationSettingsRead,
|
||||
ChatNotificationSettingsUpdate,
|
||||
ChatInviteLinkRead,
|
||||
ChatPinMessageRequest,
|
||||
ChatRead,
|
||||
ChatTitleUpdateRequest,
|
||||
)
|
||||
from app.config.settings import settings
|
||||
from app.messages.repository import (
|
||||
delete_messages_in_chat,
|
||||
get_hidden_message,
|
||||
@@ -517,3 +522,42 @@ async def set_chat_pinned_for_user(
|
||||
await db.commit()
|
||||
await db.refresh(chat)
|
||||
return chat
|
||||
|
||||
|
||||
async def create_chat_invite_link_for_user(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
chat_id: int,
|
||||
user_id: int,
|
||||
) -> ChatInviteLinkRead:
|
||||
chat, membership = await _get_chat_and_membership(db, chat_id=chat_id, user_id=user_id)
|
||||
_ensure_group_or_channel(chat.type)
|
||||
_ensure_manage_permission(membership.role)
|
||||
token = secrets.token_urlsafe(18)
|
||||
await repository.create_chat_invite_link(db, chat_id=chat_id, creator_user_id=user_id, token=token)
|
||||
await db.commit()
|
||||
invite_url = f"{settings.frontend_base_url.rstrip('/')}/join?token={token}"
|
||||
return ChatInviteLinkRead(chat_id=chat_id, token=token, invite_url=invite_url)
|
||||
|
||||
|
||||
async def join_chat_by_invite_for_user(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
user_id: int,
|
||||
payload: ChatJoinByInviteRequest,
|
||||
) -> Chat:
|
||||
link = await repository.get_active_chat_invite_by_token(db, token=payload.token.strip())
|
||||
if not link:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Invite link not found")
|
||||
chat = await repository.get_chat_by_id(db, link.chat_id)
|
||||
if not chat:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Chat not found")
|
||||
if chat.type not in {ChatType.GROUP, ChatType.CHANNEL}:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invite link is not valid for this chat")
|
||||
membership = await repository.get_chat_member(db, chat_id=chat.id, user_id=user_id)
|
||||
if membership:
|
||||
return chat
|
||||
await repository.add_chat_member(db, chat_id=chat.id, user_id=user_id, role=ChatMemberRole.MEMBER)
|
||||
await db.commit()
|
||||
await db.refresh(chat)
|
||||
return chat
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from app.auth.models import EmailVerificationToken, PasswordResetToken
|
||||
from app.chats.models import Chat, ChatMember, ChatUserSetting
|
||||
from app.chats.models import Chat, ChatInviteLink, ChatMember, ChatUserSetting
|
||||
from app.email.models import EmailLog
|
||||
from app.media.models import Attachment
|
||||
from app.messages.models import Message, MessageHidden, MessageIdempotencyKey, MessageReaction, MessageReceipt
|
||||
@@ -9,6 +9,7 @@ from app.users.models import User
|
||||
__all__ = [
|
||||
"Attachment",
|
||||
"Chat",
|
||||
"ChatInviteLink",
|
||||
"ChatMember",
|
||||
"ChatUserSetting",
|
||||
"EmailLog",
|
||||
|
||||
Reference in New Issue
Block a user