Skip to content

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

Python

Expose keep_audio_only in v1 list, v1 detail, model to_dict and to_list_dict, document in OpenAPI schema, and reject PATCH attempts to mutate it (the flag dictates how the upload was already processed and must not be retroactively changed)

Murtaza Nasir85763f3897d2
diff --git a/src/api/api_v1.py b/src/api/api_v1.py
index 7a9d9cd6..d0091621 100644
--- a/src/api/api_v1.py
+++ b/src/api/api_v1.py
@@ -114,7 +114,8 @@ OPENAPI_SPEC = {
"folder_id": {"type": "integer", "nullable": True},
"folder": {"type": "object", "nullable": True, "properties": {"id": {"type": "integer"}, "name": {"type": "string"}}},
"events": {"type": "array", "description": "Calendar events extracted from the recording (detail endpoint only)", "items": {"type": "object"}},
- "tags": {"type": "array", "items": {"$ref": "#/components/schemas/Tag"}}
+ "tags": {"type": "array", "items": {"$ref": "#/components/schemas/Tag"}},
+ "keep_audio_only": {"type": "boolean", "description": "True if the upload was processed in audio-only mode (video stream discarded). Set at upload time; immutable via PATCH."}
}
},
"Tag": {
@@ -856,7 +857,8 @@ def list_recordings():
'folder': {'id': r.folder.id, 'name': r.folder.name} if r.folder else None,
'deletion_exempt': r.deletion_exempt,
'error_message': r.error_message if r.status == 'FAILED' else None,
- 'tags': [{'id': t.id, 'name': t.name, 'color': t.color} for t in r.tags]
+ 'tags': [{'id': t.id, 'name': t.name, 'color': t.color} for t in r.tags],
+ 'keep_audio_only': r.keep_audio_only,
})
return jsonify({
@@ -922,7 +924,8 @@ def get_recording(recording_id):
'events': [e.to_dict() for e in recording.events] if hasattr(recording, 'events') else [],
'error_message': recording.error_message if recording.status == 'FAILED' else None,
'tags': [{'id': t.id, 'name': t.name, 'color': t.color} for t in recording.tags],
- 'duplicate_info': recording.get_duplicate_info()
+ 'duplicate_info': recording.get_duplicate_info(),
+ 'keep_audio_only': recording.keep_audio_only,
}
# Include large text fields based on params
@@ -1100,6 +1103,16 @@ def update_recording(recording_id):
if not data:
return jsonify({'error': 'No data provided'}), 400
+ # `keep_audio_only` is set at upload time and dictates whether the
+ # processing pipeline retained the video stream. Allowing PATCH to
+ # mutate it after the fact would create a misleading record (the
+ # file was already processed one way or the other). Reject explicitly
+ # rather than silently dropping so misuse surfaces in client logs.
+ if 'keep_audio_only' in data:
+ return jsonify({
+ 'error': 'keep_audio_only is set at upload time and cannot be changed afterwards.'
+ }), 400
+
# Track which fields actually changed so the webhook payload tells
# subscribers what was touched (recording.updated event, #275).
# Compared against the incoming key set, not the prior value, so an
diff --git a/src/models/recording.py b/src/models/recording.py
index 2d459892..3ad441b9 100644
--- a/src/models/recording.py
+++ b/src/models/recording.py
@@ -256,7 +256,9 @@ class Recording(db.Model):
'tags': [tag.to_dict() for tag in visible_tags] if visible_tags else [],
'duplicate_info': self.get_duplicate_info(),
'shared_with_count': shared_with_count,
- 'public_share_count': public_share_count
+ 'public_share_count': public_share_count,
+ 'audio_duration': self.get_audio_duration(),
+ 'keep_audio_only': self.keep_audio_only,
}
def to_dict(self, include_html=True, viewer_user=None):
@@ -317,7 +319,8 @@ class Recording(db.Model):
'prompt_variables': self.prompt_variables or {},
'duplicate_info': self.get_duplicate_info(),
'shared_with_count': shared_with_count,
- 'public_share_count': public_share_count
+ 'public_share_count': public_share_count,
+ 'keep_audio_only': self.keep_audio_only,
}
# Only compute expensive HTML conversions when explicitly requested
diff --git a/tests/test_keep_audio_only_serialization.py b/tests/test_keep_audio_only_serialization.py
new file mode 100644
index 00000000..47f8db2e
--- /dev/null
+++ b/tests/test_keep_audio_only_serialization.py
@@ -0,0 +1,143 @@
+"""Regression tests for keep_audio_only API serialization and the
+PATCH-immutability invariant.
+
+The column was added without being exposed in any to_dict() or v1 list
+builder, so external integrations and the frontend had no way to know
+whether a given recording was audio-only. PATCH also accepted mutation
+attempts silently. These tests pin the fix.
+"""
+import os
+import sys
+import uuid
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.app import app, db
+from src.models import User, Recording
+
+app.config["WTF_CSRF_ENABLED"] = False
+
+
+def _setup_user(prefix):
+ suffix = uuid.uuid4().hex[:8]
+ user = User(
+ username=f"{prefix}_{suffix}",
+ email=f"{prefix}_{suffix}@local.test",
+ password="x",
+ )
+ db.session.add(user)
+ db.session.commit()
+ return user
+
+
+def _login(client, user):
+ with client.session_transaction() as sess:
+ sess["_user_id"] = str(user.id)
+ sess["_fresh"] = True
+
+
+def _make_recording(user_id, keep_audio_only=False):
+ rec = Recording(
+ user_id=user_id,
+ title='r',
+ audio_path='/tmp/x.mp3',
+ status='COMPLETED',
+ keep_audio_only=keep_audio_only,
+ )
+ db.session.add(rec)
+ db.session.commit()
+ return rec
+
+
+def test_v1_list_includes_keep_audio_only():
+ with app.app_context():
+ user = _setup_user('keep_list')
+ rec_audio = _make_recording(user.id, keep_audio_only=True)
+ rec_video = _make_recording(user.id, keep_audio_only=False)
+
+ client = app.test_client()
+ _login(client, user)
+ resp = client.get('/api/v1/recordings')
+ assert resp.status_code == 200
+ body = resp.get_json()
+ flag_by_id = {r['id']: r.get('keep_audio_only') for r in body['recordings']}
+ assert flag_by_id.get(rec_audio.id) is True
+ assert flag_by_id.get(rec_video.id) is False
+
+ for r in [rec_audio, rec_video]:
+ db.session.delete(r)
+ db.session.delete(user)
+ db.session.commit()
+
+
+def test_v1_detail_includes_keep_audio_only():
+ with app.app_context():
+ user = _setup_user('keep_detail')
+ rec = _make_recording(user.id, keep_audio_only=True)
+ client = app.test_client()
+ _login(client, user)
+ resp = client.get(f'/api/v1/recordings/{rec.id}')
+ assert resp.status_code == 200
+ body = resp.get_json()
+ assert body.get('keep_audio_only') is True
+
+ db.session.delete(rec)
+ db.session.delete(user)
+ db.session.commit()
+
+
+def test_patch_rejects_mutating_keep_audio_only():
+ """The flag dictates how the file was already processed. Allowing
+ PATCH to flip it would create a misleading record."""
+ with app.app_context():
+ user = _setup_user('keep_immut')
+ rec = _make_recording(user.id, keep_audio_only=False)
+ client = app.test_client()
+ _login(client, user)
+ resp = client.patch(
+ f'/api/v1/recordings/{rec.id}',
+ json={'keep_audio_only': True},
+ )
+ assert resp.status_code == 400, resp.data
+
+ # And verify the DB value did not change.
+ db.session.expire_all()
+ refetched = db.session.get(Recording, rec.id)
+ assert refetched.keep_audio_only is False
+
+ db.session.delete(refetched)
+ db.session.delete(user)
+ db.session.commit()
+
+
+def test_patch_other_fields_still_works_alongside_rejection():
+ """Sending other valid fields in the same body as a rejected
+ keep_audio_only attempt fails the whole request (atomic). Without a
+ keep_audio_only key, normal fields update."""
+ with app.app_context():
+ user = _setup_user('keep_other')
+ rec = _make_recording(user.id, keep_audio_only=False)
+ client = app.test_client()
+ _login(client, user)
+
+ # With keep_audio_only present -> 400, title unchanged.
+ resp = client.patch(
+ f'/api/v1/recordings/{rec.id}',
+ json={'title': 'NEW', 'keep_audio_only': True},
+ )
+ assert resp.status_code == 400
+ db.session.expire_all()
+ assert db.session.get(Recording, rec.id).title == 'r'
+
+ # Without it -> 200, title updates.
+ resp = client.patch(
+ f'/api/v1/recordings/{rec.id}',
+ json={'title': 'NEW'},
+ )
+ assert resp.status_code == 200
+ db.session.expire_all()
+ assert db.session.get(Recording, rec.id).title == 'NEW'
+
+ db.session.delete(db.session.get(Recording, rec.id))
+ db.session.delete(user)
+ db.session.commit()