mirror of
https://github.com/OHV-IT/collabrix.git
synced 2025-12-15 16:48:36 +01:00
- Complete chat application similar to Microsoft Teams - Code snippet library with syntax highlighting - Real-time messaging with WebSockets - File upload with Office integration - Department-based permissions - Dark/Light theme support - Production deployment with SSL/Reverse Proxy - Docker containerization - PostgreSQL database with SQLModel ORM
113 lines
3.6 KiB
Python
113 lines
3.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlmodel import Session, select
|
|
from typing import List
|
|
from app.database import get_session
|
|
from app.models import Channel, Department, User
|
|
from app.schemas import ChannelCreate, ChannelResponse
|
|
from app.auth import get_current_user
|
|
|
|
router = APIRouter(prefix="/channels", tags=["Channels"])
|
|
|
|
|
|
@router.post("/", response_model=ChannelResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_channel(
|
|
channel_data: ChannelCreate,
|
|
session: Session = Depends(get_session),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Create a new channel"""
|
|
# Check if department exists
|
|
department = session.get(Department, channel_data.department_id)
|
|
if not department:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Department not found"
|
|
)
|
|
|
|
new_channel = Channel(
|
|
name=channel_data.name,
|
|
description=channel_data.description,
|
|
department_id=channel_data.department_id
|
|
)
|
|
|
|
session.add(new_channel)
|
|
session.commit()
|
|
session.refresh(new_channel)
|
|
|
|
return new_channel
|
|
|
|
|
|
@router.get("/", response_model=List[ChannelResponse])
|
|
def get_my_channels(
|
|
session: Session = Depends(get_session),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Get all channels that current user has access to (based on departments)"""
|
|
# Get user with departments
|
|
statement = select(User).where(User.id == current_user.id)
|
|
user = session.exec(statement).first()
|
|
|
|
if not user or not user.departments:
|
|
return []
|
|
|
|
# Get all channels from user's departments
|
|
channels = []
|
|
for dept in user.departments:
|
|
statement = select(Channel).where(Channel.department_id == dept.id)
|
|
dept_channels = session.exec(statement).all()
|
|
channels.extend(dept_channels)
|
|
|
|
return channels
|
|
|
|
|
|
@router.get("/{channel_id}", response_model=ChannelResponse)
|
|
def get_channel(
|
|
channel_id: int,
|
|
session: Session = Depends(get_session),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Get a specific channel"""
|
|
channel = session.get(Channel, channel_id)
|
|
if not channel:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Channel not found"
|
|
)
|
|
|
|
# Check if user has access to this channel (via department)
|
|
statement = select(User).where(User.id == current_user.id)
|
|
user = session.exec(statement).first()
|
|
|
|
user_dept_ids = [dept.id for dept in user.departments] if user else []
|
|
if channel.department_id not in user_dept_ids:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="You don't have access to this channel"
|
|
)
|
|
|
|
return channel
|
|
|
|
|
|
@router.get("/department/{department_id}", response_model=List[ChannelResponse])
|
|
def get_channels_by_department(
|
|
department_id: int,
|
|
session: Session = Depends(get_session),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Get all channels in a department"""
|
|
# Check if user has access to this department
|
|
statement = select(User).where(User.id == current_user.id)
|
|
user = session.exec(statement).first()
|
|
|
|
user_dept_ids = [dept.id for dept in user.departments] if user else []
|
|
if department_id not in user_dept_ids:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="You don't have access to this department"
|
|
)
|
|
|
|
statement = select(Channel).where(Channel.department_id == department_id)
|
|
channels = session.exec(statement).all()
|
|
|
|
return channels
|