Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python
29.0 KB
Raw Blame
"""
Sharing routes for public and internal recording shares.

This blueprint handles:
- Public sharing (shareable links)
- Internal sharing (user-to-user sharing)
- Share management (CRUD operations)
"""

import os
import re
import json
from flask import Blueprint, render_template, request, redirect, url_for, jsonify, send_file, current_app
from flask_login import login_required, current_user
from werkzeug.exceptions import HTTPException

from src.database import db
from src.models import Recording, Share, InternalShare, SharedRecordingState, User, TranscriptChunk, ShareAuditLog, Folder
from src.utils import md_to_html
from src.services.storage import get_storage_service
from src.services.public_shares import active_share_or_404
from src.utils.mime import served_media_mime, sandbox_media_response
from src.utils.search import LIKE_ESCAPE, contains_pattern
from src.utils.localization import api_error, translate, request_language

# Configuration from environment
ENABLE_PUBLIC_SHARING = os.environ.get('ENABLE_PUBLIC_SHARING', 'true').lower() == 'true'
ENABLE_INTERNAL_SHARING = os.environ.get('ENABLE_INTERNAL_SHARING', 'false').lower() == 'true'
SHOW_USERNAMES_IN_UI = os.environ.get('SHOW_USERNAMES_IN_UI', 'false').lower() == 'true'
ENABLE_INQUIRE_MODE = os.environ.get('ENABLE_INQUIRE_MODE', 'false').lower() == 'true'
READABLE_PUBLIC_LINKS = os.environ.get('READABLE_PUBLIC_LINKS', 'false').lower() == 'true'

# Create blueprint
shares_bp = Blueprint('shares', __name__)

# Import has_recording_access from app context
has_recording_access = None

def init_shares_helpers(_has_recording_access):
    """Initialize helper functions from app."""
    global has_recording_access
    has_recording_access = _has_recording_access


def process_transcription_for_template(transcription_str):
    """
    Process transcription JSON into a format ready for server-side rendering.

    Returns a dict with:
    - is_json: bool - whether transcription is valid JSON
    - has_speakers: bool - whether diarization data exists
    - segments: list - processed segments with speaker info and colors
    - speakers: list - unique speakers with colors
    - plain_text: str - plain text version for non-JSON or fallback
    """
    if not transcription_str:
        return {'is_json': False, 'has_speakers': False, 'segments': [], 'speakers': [], 'plain_text': ''}

    try:
        data = json.loads(transcription_str)
    except (json.JSONDecodeError, TypeError):
        # Plain text transcription
        return {
            'is_json': False,
            'has_speakers': False,
            'segments': [],
            'speakers': [],
            'plain_text': transcription_str
        }

    if not isinstance(data, list):
        return {
            'is_json': False,
            'has_speakers': False,
            'segments': [],
            'speakers': [],
            'plain_text': transcription_str
        }

    # Check if diarized (has speaker info)
    has_speakers = any(seg.get('speaker') for seg in data)

    # Get unique speakers and assign colors
    speakers = []
    speaker_colors = {}
    if has_speakers:
        unique_speakers = list(dict.fromkeys(seg.get('speaker') for seg in data if seg.get('speaker')))
        for i, speaker in enumerate(unique_speakers):
            color = f'speaker-color-{(i % 8) + 1}'
            speaker_colors[speaker] = color
            speakers.append({'name': speaker, 'color': color})

    # Process segments
    segments = []
    last_speaker = None
    for seg in data:
        speaker = seg.get('speaker', '')
        segment = {
            'text': seg.get('sentence', ''),
            'speaker': speaker,
            'start_time': seg.get('start_time') or seg.get('startTime', ''),
            'end_time': seg.get('end_time') or seg.get('endTime', ''),
            'color': speaker_colors.get(speaker, 'speaker-color-1'),
            'show_speaker': speaker != last_speaker
        }
        segments.append(segment)
        last_speaker = speaker

    # Build plain text version
    if has_speakers:
        plain_text = '\n'.join(f"[{seg['speaker']}]: {seg['text']}" for seg in segments)
    else:
        plain_text = '\n'.join(seg['text'] for seg in segments)

    return {
        'is_json': True,
        'has_speakers': has_speakers,
        'segments': segments,
        'speakers': speakers,
        'plain_text': plain_text
    }


# --- Public Sharing Routes ---

@shares_bp.route('/share/<string:public_id>', methods=['GET'])
def view_shared_recording(public_id):
    """View a publicly shared recording."""
    share = active_share_or_404(public_id)
    recording = share.recording

    # Process transcription for server-side rendering (only if READABLE_PUBLIC_LINKS is enabled)
    processed_transcript = None
    if READABLE_PUBLIC_LINKS:
        processed_transcript = process_transcription_for_template(recording.transcription)

    # Create a limited dictionary for the public view
    recording_data = {
        'id': recording.id,
        'public_id': share.public_id,
        'title': recording.title,
        'participants': recording.participants,
        'transcription': recording.transcription,
        'summary': md_to_html(recording.summary) if share.share_summary else None,
        'summary_raw': recording.summary if share.share_summary else None,
        'notes': md_to_html(recording.notes) if share.share_notes else None,
        'notes_raw': recording.notes if share.share_notes else None,
        'meeting_date': recording.meeting_date.isoformat() if recording.meeting_date else None,
        'mime_type': recording.mime_type,
        'audio_deleted_at': recording.audio_deleted_at.isoformat() if recording.audio_deleted_at else None,
        'audio_duration': recording.get_audio_duration()
    }

    # The viewer's language for the embedded translations: their interface
    # language when signed in, otherwise the browser's Accept-Language.
    return render_template('share.html', recording=recording_data, transcript=processed_transcript,
                           readable_mode=READABLE_PUBLIC_LINKS, user_language=request_language())


@shares_bp.route('/share/audio/<string:public_id>')
def get_shared_audio(public_id):
    """Serve audio file for a publicly shared recording."""
    try:
        share = active_share_or_404(public_id)
        recording = share.recording
        if not recording or not recording.audio_path:
            return jsonify(api_error('recording_or_audio_not_found')), 404
        serve_mime = served_media_mime(recording.mime_type, recording.audio_path)
        delivery = get_storage_service().get_audio_delivery(
            recording.audio_path,
            download=False,
            mime_type=serve_mime,
            download_name=recording.original_filename,
            is_public=True,
        )
        if delivery.mode == 'redirect_url':
            return redirect(delivery.url, code=302)
        if not delivery.local_path or not os.path.exists(delivery.local_path):
            current_app.logger.error(f"Audio file missing from server: {recording.audio_path}")
            return jsonify(api_error('audio_file_missing')), 404
        return sandbox_media_response(send_file(delivery.local_path, mimetype=serve_mime, conditional=True))
    except HTTPException:
        # Let first_or_404() (and any other HTTP error) keep its real status —
        # an unknown public_id must stay a 404, not be masked as a 500.
        raise
    except Exception as e:
        current_app.logger.error(f"Error serving shared audio for public_id {public_id}: {e}", exc_info=True)
        return jsonify(api_error('unexpected_error')), 500


@shares_bp.route('/api/recording/<int:recording_id>/share', methods=['GET'])
@login_required
def get_existing_share(recording_id):
    """Check if a share already exists for this recording."""
    recording = db.session.get(Recording, recording_id)
    if not recording or recording.user_id != current_user.id:
        return jsonify(api_error('recording_not_found_or_no_view')), 404

    existing_share = Share.query.filter_by(
        recording_id=recording.id,
        user_id=current_user.id
    ).order_by(Share.created_at.desc()).first()

    if existing_share:
        share_url = url_for('shares.view_shared_recording', public_id=existing_share.public_id, _external=True)
        return jsonify({
            'success': True,
            'exists': True,
            'share_url': share_url,
            'share': existing_share.to_dict()
        }), 200
    else:
        return jsonify({
            'success': True,
            'exists': False
        }), 200


@shares_bp.route('/api/recording/<int:recording_id>/share', methods=['POST'])
@login_required
def create_share(recording_id):
    """Create a public share link for a recording."""
    # Check if public sharing is globally enabled
    if not ENABLE_PUBLIC_SHARING:
        return jsonify(api_error('public_sharing_disabled')), 403

    # Check if user has permission to create public shares
    if not current_user.can_share_publicly:
        return jsonify(api_error('public_share_not_permitted')), 403

    if not request.is_secure:
        return jsonify(api_error('sharing_https_required')), 403

    recording = db.session.get(Recording, recording_id)
    if not recording or recording.user_id != current_user.id:
        return jsonify(api_error('recording_not_found_or_no_share')), 404

    data = request.json
    share_summary = data.get('share_summary', True)
    share_notes = data.get('share_notes', True)
    force_new = data.get('force_new', False)

    # The web dialog sets the flags of a reused link to what it sent.
    from src.services.public_shares import get_or_create
    share, created, _ = get_or_create(recording, current_user, share_summary, share_notes,
                                      force_new=force_new, update_existing=True)
    if not created:
        share_url = url_for('shares.view_shared_recording', public_id=share.public_id, _external=True)
        return jsonify({
            'success': True,
            'share_url': share_url,
            'share': share.to_dict(),
            'existing': True,
            'message': translate('server.apiMessage.existingShareLinkUsed')
        }), 200

    share_url = url_for('shares.view_shared_recording', public_id=share.public_id, _external=True)

    return jsonify({
        'success': True,
        'share_url': share_url,
        'share': share.to_dict(),
        'existing': False
    }), 201


@shares_bp.route('/api/shares', methods=['GET'])
@login_required
def get_shares():
    """Get all public shares for the current user."""
    shares = Share.query.filter_by(user_id=current_user.id).order_by(Share.created_at.desc()).all()
    return jsonify([share.to_dict() for share in shares])


@shares_bp.route('/api/shares/overview', methods=['GET'])
@login_required
def get_shares_overview():
    """Everything the Shared Transcripts list shows (#416).

    The list predates internal sharing and showed public links only. It now
    has what the user shared (public links, and recordings shared with people
    and groups) and what was shared with them.

    The recordings, users, source tags and source folders of the shares are
    read once per list (D#483), with only the recording columns shown here.
    """
    from sqlalchemy.orm import joinedload, load_only, selectinload
    folders = {}

    def _source(share):
        if share.source_type == 'group_tag' and share.source_tag is not None:
            return {'type': 'group_tag', 'name': share.source_tag.name}
        if share.source_type == 'group_folder' and share.source_folder_id:
            folder = folders.get(share.source_folder_id)
            return {'type': 'group_folder', 'name': folder.name if folder else None}
        return {'type': 'manual', 'name': None}

    def _internal(*criteria):
        shares = (InternalShare.query.filter(*criteria)
                  .options(selectinload(InternalShare.recording).load_only(
                               Recording.id, Recording.title, Recording.original_filename, Recording.status),
                           joinedload(InternalShare.shared_with), joinedload(InternalShare.owner),
                           joinedload(InternalShare.source_tag))
                  .order_by(InternalShare.created_at.desc(), InternalShare.id.desc()).all())
        wanted = {s.source_folder_id for s in shares
                  if s.source_type == 'group_folder' and s.source_folder_id and s.source_folder_id not in folders}
        if wanted:
            folders.update({f.id: f for f in Folder.query.filter(Folder.id.in_(sorted(wanted)))})
        return shares

    public_links = [s.to_dict() for s in
                    Share.query.filter_by(user_id=current_user.id)
                    .options(selectinload(Share.recording).load_only(
                        Recording.id, Recording.title, Recording.audio_deleted_at))
                    .order_by(Share.created_at.desc(), Share.id.desc()).all()]
    shared_by_me, shared_with_me = [], []
    if ENABLE_INTERNAL_SHARING:
        by_recording = {}
        for share in _internal(InternalShare.owner_id == current_user.id):
            recording = share.recording
            if recording is None:
                continue
            entry = by_recording.setdefault(recording.id, {
                'recording_id': recording.id,
                'recording_title': recording.title or recording.original_filename or f'#{recording.id}',
                'recipients': [],
            })
            source = _source(share)
            entry['recipients'].append({
                'share_id': share.id,
                'user_id': share.shared_with_user_id,
                'username': share.shared_with.username if share.shared_with else None,
                'can_edit': bool(share.can_edit),
                'can_reshare': bool(share.can_reshare),
                'source': source,
                # A group share follows its tag or folder; removing the tag or
                # folder ends it, so only a manual share is revoked here.
                'revocable': source['type'] == 'manual',
                'created_at': share.created_at.isoformat() if share.created_at else None,
            })
        shared_by_me = list(by_recording.values())

        for share in _internal(InternalShare.shared_with_user_id == current_user.id):
            recording = share.recording
            if recording is None:
                continue
            shared_with_me.append({
                'share_id': share.id,
                'recording_id': recording.id,
                'recording_title': recording.title or recording.original_filename or f'#{recording.id}',
                'owner_username': share.owner.username if SHOW_USERNAMES_IN_UI and share.owner else None,
                'can_edit': bool(share.can_edit),
                'can_reshare': bool(share.can_reshare),
                'source': _source(share),
                'status': recording.status,
                'created_at': share.created_at.isoformat() if share.created_at else None,
            })

    return jsonify({
        'public_sharing_enabled': ENABLE_PUBLIC_SHARING,
        'internal_sharing_enabled': ENABLE_INTERNAL_SHARING,
        'public_links': public_links,
        'shared_by_me': shared_by_me,
        'shared_with_me': shared_with_me,
    })


@shares_bp.route('/api/share/<int:share_id>', methods=['PUT'])
@login_required
def update_share(share_id):
    """Update a public share's settings."""
    share = Share.query.filter_by(id=share_id, user_id=current_user.id).first_or_404()
    data = request.json

    if 'share_summary' in data:
        share.share_summary = data['share_summary']
    if 'share_notes' in data:
        share.share_notes = data['share_notes']

    db.session.commit()
    return jsonify({'success': True, 'share': share.to_dict()})


@shares_bp.route('/api/share/<int:share_id>', methods=['DELETE'])
@login_required
def delete_share(share_id):
    """Delete a public share."""
    share = Share.query.filter_by(id=share_id, user_id=current_user.id).first_or_404()
    from src.services.public_shares import revoke
    revoke(share)
    return jsonify({'success': True})


# --- Internal Sharing Routes ---

@shares_bp.route('/api/users/search', methods=['GET'])
@login_required
def search_users():
    """Search for users by username (for internal sharing)."""
    if not ENABLE_INTERNAL_SHARING:
        return jsonify(api_error('internal_sharing_disabled')), 403

    query = request.args.get('q', '').strip()

    # If SHOW_USERNAMES_IN_UI is enabled and no query, return all users for quick selection
    if SHOW_USERNAMES_IN_UI and len(query) < 2:
        users = User.query.filter(User.id != current_user.id).order_by(User.username).all()
    elif len(query) < 2:
        # If usernames are hidden and no query, return empty
        return jsonify([])
    else:
        if SHOW_USERNAMES_IN_UI:
            # If usernames are shown, allow partial match (autocomplete)
            users = User.query.filter(
                User.id != current_user.id,
                User.username.ilike(contains_pattern(query), escape=LIKE_ESCAPE)
            ).limit(10).all()
        else:
            # If usernames are hidden (privacy mode), require exact match only
            users = User.query.filter(
                User.id != current_user.id,
                User.username == query
            ).all()

    return jsonify([{
        'id': user.id,
        'username': user.username,
        'email': user.email if SHOW_USERNAMES_IN_UI else None
    } for user in users])


@shares_bp.route('/api/recordings/<int:recording_id>/share-internal', methods=['POST'])
@login_required
def share_recording_internal(recording_id):
    """Share a recording with another user internally."""
    if not ENABLE_INTERNAL_SHARING:
        return jsonify(api_error('internal_sharing_disabled')), 403

    try:
        data = request.json
        shared_with_user_id = data.get('user_id')
        can_edit = data.get('can_edit', False)
        can_reshare = data.get('can_reshare', False)

        if not shared_with_user_id:
            return jsonify(api_error('user_id_required')), 400

        # Check recording exists and user has permission to share it
        recording = db.session.get(Recording, recording_id)
        if not recording:
            return jsonify(api_error('recording_not_found')), 404

        if not has_recording_access(recording, current_user, require_reshare=True):
            return jsonify(api_error('recording_share_denied')), 403

        # Check target user exists
        target_user = db.session.get(User, shared_with_user_id)
        if not target_user:
            return jsonify(api_error('target_user_not_found')), 404

        # Prevent sharing back to owner (circular share)
        if shared_with_user_id == recording.user_id:
            return jsonify(api_error('share_with_owner')), 400

        # Prevent sharing with self
        if shared_with_user_id == current_user.id:
            return jsonify(api_error('share_with_self')), 400

        # Check if already shared
        existing_share = InternalShare.query.filter_by(
            recording_id=recording_id,
            shared_with_user_id=shared_with_user_id
        ).first()

        if existing_share:
            return jsonify(api_error('already_shared')), 409

        # PERMISSION VALIDATION: Validate that current user can grant the requested permissions
        requested_permissions = {'can_edit': can_edit, 'can_reshare': can_reshare}
        is_valid, error_code = InternalShare.validate_reshare_permissions(
            recording, current_user, requested_permissions
        )

        if not is_valid:
            return jsonify(api_error(error_code)), 403

        # Get current user's permissions for audit log
        actor_permissions = InternalShare.get_user_max_permissions(recording, current_user)

        # Create share
        share = InternalShare(
            recording_id=recording_id,
            owner_id=current_user.id,
            shared_with_user_id=shared_with_user_id,
            can_edit=can_edit,
            can_reshare=can_reshare
        )
        db.session.add(share)

        # Create or update SharedRecordingState for the recipient
        state = SharedRecordingState.query.filter_by(
            recording_id=recording_id,
            user_id=shared_with_user_id
        ).first()

        if not state:
            # Create new state if it doesn't exist
            state = SharedRecordingState(
                recording_id=recording_id,
                user_id=shared_with_user_id,
                is_inbox=True,  # New shares appear in inbox by default
                is_highlighted=False  # Not favorited by default
            )
            db.session.add(state)
        else:
            # Reset to inbox if it already exists (e.g., from previous share that was deleted)
            state.is_inbox = True

        db.session.commit()

        # AUDIT LOGGING: Log the share creation
        try:
            ShareAuditLog.log_share_created(
                recording_id=recording_id,
                actor_id=current_user.id,
                target_user_id=shared_with_user_id,
                permissions={'can_edit': can_edit, 'can_reshare': can_reshare},
                actor_permissions=actor_permissions,
                notes=f"Shared by {'owner' if recording.user_id == current_user.id else 'delegated user'}",
                ip_address=request.remote_addr
            )
            db.session.commit()
        except Exception as audit_error:
            # Don't fail the share if audit logging fails
            current_app.logger.error(f"Failed to log share creation: {audit_error}")

        return jsonify({
            'success': True,
            'share': share.to_dict()
        }), 201

    except Exception as e:
        db.session.rollback()
        current_app.logger.error(f"Error sharing recording internally: {e}", exc_info=True)
        return jsonify(api_error('unexpected_error')), 500


@shares_bp.route('/api/recordings/<int:recording_id>/shares-internal', methods=['GET'])
@login_required
def get_internal_shares(recording_id):
    """Get list of users a recording is shared with, including owner."""
    if not ENABLE_INTERNAL_SHARING:
        return jsonify(api_error('internal_sharing_disabled')), 403

    # Check recording exists and user has permission to view shares
    recording = db.session.get(Recording, recording_id)
    if not recording:
        return jsonify(api_error('recording_not_found')), 404

    if not has_recording_access(recording, current_user, require_reshare=True):
        return jsonify(api_error('shares_view_denied')), 403

    # Get all internal shares
    shares = InternalShare.query.filter_by(recording_id=recording_id).all()
    shares_list = [share.to_dict() for share in shares]

    # Add owner as first entry (owner always has full permissions)
    owner = db.session.get(User, recording.user_id)
    if owner:
        owner_entry = {
            'id': None,  # No share ID for owner
            'recording_id': recording_id,
            'owner_id': owner.id,
            'owner_username': owner.username,
            'user_id': owner.id,
            'username': owner.username,
            'can_edit': True,
            'can_reshare': True,
            'is_owner': True,  # Mark as owner
            'source_type': 'owner',
            'source_tag_id': None,
            'created_at': recording.created_at.isoformat() if recording.created_at else None
        }
        # Insert owner at the beginning
        shares_list.insert(0, owner_entry)

    return jsonify({'shares': shares_list})


@shares_bp.route('/api/internal-shares/<int:share_id>', methods=['DELETE'])
@login_required
def revoke_internal_share(share_id):
    """Revoke an internal share with cascade revocation."""
    if not ENABLE_INTERNAL_SHARING:
        return jsonify(api_error('internal_sharing_disabled')), 403

    share = db.session.get(InternalShare, share_id)
    if not share:
        return jsonify(api_error('share_not_found')), 404

    # Only owner can revoke
    if share.owner_id != current_user.id:
        return jsonify(api_error('share_revoke_denied')), 403

    recording_id = share.recording_id
    revoked_user_id = share.shared_with_user_id
    revoked_count = 0

    try:
        # CASCADE REVOCATION: Find downstream shares created by the user losing access
        downstream_shares = InternalShare.find_downstream_shares(recording_id, revoked_user_id)

        # Recursively revoke downstream shares that don't have alternate paths
        for downstream in downstream_shares:
            # Check for alternate access paths (diamond pattern protection)
            has_alternate = InternalShare.has_alternate_access_path(
                recording_id,
                downstream.shared_with_user_id,
                excluding_grantor_id=revoked_user_id
            )

            if not has_alternate:
                # No alternate path - cascade revoke
                # Audit log cascade revocation
                try:
                    ShareAuditLog.log_share_revoked(
                        share_id=downstream.id,
                        recording_id=recording_id,
                        actor_id=current_user.id,
                        target_user_id=downstream.shared_with_user_id,
                        was_cascade=True,
                        notes=f"Cascaded from revoking user {revoked_user_id}",
                        ip_address=request.remote_addr
                    )
                except Exception as audit_error:
                    current_app.logger.error(f"Failed to log cascade revocation: {audit_error}")

                db.session.delete(downstream)
                revoked_count += 1

        # Audit log the primary revocation
        try:
            ShareAuditLog.log_share_revoked(
                share_id=share.id,
                recording_id=recording_id,
                actor_id=current_user.id,
                target_user_id=revoked_user_id,
                was_cascade=False,
                notes=f"Revoked by user {current_user.id}, cascaded to {revoked_count} downstream shares",
                ip_address=request.remote_addr
            )
        except Exception as audit_error:
            current_app.logger.error(f"Failed to log revocation: {audit_error}")

        # Delete the primary share
        db.session.delete(share)
        db.session.commit()

        return jsonify({
            'success': True,
            'revoked_count': revoked_count + 1,  # Include primary share
            'cascaded': revoked_count
        })

    except Exception as e:
        db.session.rollback()
        current_app.logger.error(f"Error revoking internal share: {e}", exc_info=True)
        return jsonify(api_error('unexpected_error')), 500


@shares_bp.route('/api/recordings/shared-with-me', methods=['GET'])
@login_required
def get_shared_with_me():
    """Get recordings that have been shared with the current user."""
    if not ENABLE_INTERNAL_SHARING:
        return jsonify(api_error('internal_sharing_disabled')), 403

    try:
        # Get shares where current user is the recipient, with their
        # recordings and owners, and the rows' lookups read once (D#483)
        from sqlalchemy.orm import selectinload
        from src.services.recording_list import RecordingListBatch, list_load_options
        shares = InternalShare.query.filter_by(shared_with_user_id=current_user.id).options(
            selectinload(InternalShare.recording).options(*list_load_options()),
            selectinload(InternalShare.owner),
        ).all()
        listed = [s for s in shares if s.recording and s.recording.status == 'COMPLETED']
        batch = RecordingListBatch([s.recording for s in listed], current_user,
                                   {s.recording_id: s for s in listed})

        result = []
        for share in listed:
            recording = share.recording
            rec_data = recording.to_list_dict(viewer_user=current_user, batch=batch)
            # Mark as shared recording with owner info
            rec_data['is_shared'] = True
            rec_data['owner_username'] = share.owner.username if SHOW_USERNAMES_IN_UI else None
            # Don't show outgoing share counts for recordings you don't own
            rec_data['shared_with_count'] = 0
            rec_data['public_share_count'] = 0
            # Check if recording has group tags (among visible tags)
            visible_tags = batch.visible_tags(recording)
            rec_data['has_group_tags'] = any(tag.is_group_tag for tag in visible_tags) if visible_tags else False
            rec_data['share_info'] = {
                'share_id': share.id,
                'owner_username': share.owner.username if SHOW_USERNAMES_IN_UI else None,
                'can_edit': share.can_edit,
                'can_reshare': share.can_reshare,
                'shared_at': share.created_at.isoformat()
            }
            result.append(rec_data)

        return jsonify(result)

    except Exception as e:
        current_app.logger.error(f"Error fetching shared recordings: {e}")
        return jsonify({'error': str(e)}), 500


@shares_bp.route('/api/permissions/can-share-publicly', methods=['GET'])
@login_required
def can_share_publicly():
    """Check if the current user has permission to create public shares."""
    return jsonify({
        'can_share_publicly': current_user.can_share_publicly and ENABLE_PUBLIC_SHARING
    })