Skip to content

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

Python

Speaker merge and rename: recording.updated for each recording changed, and recordings_updated in the merge answer (mailr spec note, 1 Oct 2026)

Murtaza Nasird26a1d6f7a58
diff --git a/src/api/speakers.py b/src/api/speakers.py
index 639791cb..613c384f 100644
--- a/src/api/speakers.py
+++ b/src/api/speakers.py
@@ -608,7 +608,8 @@ def merge_speaker_profiles():
return jsonify({
'success': True,
'message': f'Successfully merged {len(source_ids)} speaker(s) into {merged_speaker.name}',
- 'speaker': merged_speaker.to_dict()
+ 'speaker': merged_speaker.to_dict(),
+ 'recordings_updated': getattr(merged_speaker, 'recordings_updated', 0)
})
except ValueError as e:
diff --git a/src/services/speaker_merge.py b/src/services/speaker_merge.py
index ef039426..ca184c37 100644
--- a/src/services/speaker_merge.py
+++ b/src/services/speaker_merge.py
@@ -104,10 +104,12 @@ def merge_speakers(target_id, source_ids, user_id):
# Recordings keep speaker names, not speaker ids: without this the
# recordings of a merged speaker kept showing the old name.
- renamed = set()
+ renamed = {}
for source in sources:
- _, recording_ids = rename_speaker_in_recordings(user_id, source.name, target.name)
- renamed.update(recording_ids)
+ _, changed = rename_speaker_in_recordings(user_id, source.name, target.name)
+ for rid, fields in changed.items():
+ renamed.setdefault(rid, set()).update(fields)
+ target.recordings_updated = len(renamed) # for the route's answer, not a column
# Delete source speakers
for source in sources:
@@ -130,7 +132,7 @@ def rename_speaker_in_recordings(user_id, old_name, new_name):
Covers the transcript segments, the participants list, the diarization
label map and the Inquire chunks' speaker column. Names match ignoring
case and surrounding spaces, as saved speakers do. Does not commit.
- Returns (chunks_updated, ids of the recordings changed).
+ Returns (chunks_updated, {recording id: set of the fields changed}).
"""
from src.models import Recording, TranscriptChunk
@@ -142,9 +144,9 @@ def rename_speaker_in_recordings(user_id, old_name, new_name):
chunk.speaker_name = new_name
chunks_updated += 1
- changed = []
+ changed = {}
for recording in Recording.query.filter_by(user_id=user_id).all():
- updated = False
+ fields = set()
if recording.participants:
names = [p.strip() for p in recording.participants.split(',') if p.strip()]
@@ -155,7 +157,7 @@ def rename_speaker_in_recordings(user_id, old_name, new_name):
if not any(_same_name(p, m) for m in merged):
merged.append(p)
recording.participants = ', '.join(merged)
- updated = True
+ fields.add('participants')
if recording.transcription:
try:
@@ -170,7 +172,7 @@ def rename_speaker_in_recordings(user_id, old_name, new_name):
hit = True
if hit:
recording.transcription = json.dumps(segments)
- updated = True
+ fields.add('transcription')
# The label map records which name each diarization label shows;
# without this the next save would think the voice was removed.
@@ -178,23 +180,29 @@ def rename_speaker_in_recordings(user_id, old_name, new_name):
if isinstance(label_map, dict) and any(_same_name(n, old_name) for n in label_map.values()):
recording.speaker_label_map = {
label: (new_name if _same_name(n, old_name) else n) for label, n in label_map.items()}
- updated = True
+ fields.add('speaker_label_map')
- if updated:
- changed.append(recording.id)
+ if fields:
+ changed.setdefault(recording.id, set()).update(fields)
return chunks_updated, changed
-def refresh_renamed_recordings(recording_ids):
+def refresh_renamed_recordings(changed):
"""After names change in committed recordings: rebuild their Inquire
- chunks (the chunk text holds the names) and rewrite their auto-exports."""
- if not recording_ids:
+ chunks (the chunk text holds the names), rewrite their auto-exports and
+ send recording.updated for each. changed: {recording id: fields}."""
+ if not changed:
return
from flask import current_app
from src.api import recordings as recordings_api
from src.file_exporter import ENABLE_AUTO_EXPORT, export_recording
- for rid in sorted(recording_ids):
+ from src.models import Recording
+ from src.services.webhook_dispatch import emit_recording_updated
+ for rid in sorted(changed):
recordings_api.reindex_recording_chunks_async(rid)
+ recording = db.session.get(Recording, rid)
+ if recording is not None:
+ emit_recording_updated(recording, sorted(changed[rid]))
if ENABLE_AUTO_EXPORT:
try:
export_recording(rid)
diff --git a/src/services/webhook_dispatch.py b/src/services/webhook_dispatch.py
index 9aae3f4b..55fd4598 100644
--- a/src/services/webhook_dispatch.py
+++ b/src/services/webhook_dispatch.py
@@ -681,6 +681,25 @@ def start_dispatcher_thread(app):
app.logger.info("✅ Webhook dispatcher thread initialized")
+def emit_recording_updated(recording, fields_changed):
+ """recording.updated for a change made outside PATCH /recordings/{id}.
+
+ Same payload as the PATCH route. Best-effort: never raises.
+ """
+ try:
+ emit_webhook_event(
+ user_id=recording.user_id,
+ event_type='recording.updated',
+ data={
+ 'recording_id': recording.id,
+ 'title': recording.title,
+ 'fields_changed': list(fields_changed),
+ },
+ )
+ except Exception as e:
+ logger.warning(f"Webhook emit (recording.updated) failed for {getattr(recording, 'id', None)}: {e}")
+
+
def emit_recording_created(recording):
"""recording.created, from every path that creates a recording (#412 audit P11).
diff --git a/tests/test_speaker_merge_renames.py b/tests/test_speaker_merge_renames.py
index 74320e78..6c2e710f 100644
--- a/tests/test_speaker_merge_renames.py
+++ b/tests/test_speaker_merge_renames.py
@@ -4,7 +4,8 @@ Recordings keep speaker names, not speaker ids. A merge in Speaker Management
moved the voice samples and snippets to the kept speaker, but every recording
of a merged speaker went on showing the old name. Merge and rename now share
rename_speaker_in_recordings, and both rebuild the Inquire chunks and rewrite
-the exports of the recordings they change.
+the exports of the recordings they change, and send recording.updated for
+each (mailr spec, note of 1 Oct 2026).
SHARED-DB: users, speakers, recordings and chunks are removed afterwards.
"""
@@ -99,10 +100,16 @@ def _speakers(rid):
def test_a_merge_renames_the_merged_speaker_in_every_recording(world):
reindex, flag, export = _effects()
- with reindex as m_reindex, flag, export as m_export:
+ with reindex as m_reindex, flag, export as m_export, \
+ patch("src.services.webhook_dispatch.emit_webhook_event") as emit:
resp = _client(world["me"]).post("/speakers/merge",
json={"target_id": world["keep"], "source_ids": [world["old"]]})
assert resp.status_code == 200, resp.get_json()
+ assert resp.get_json()["recordings_updated"] == 2
+ updates = {c.kwargs["data"]["recording_id"]: c.kwargs["data"]["fields_changed"]
+ for c in emit.call_args_list if c.kwargs["event_type"] == "recording.updated"}
+ assert updates == {world["only_old"]: ["participants", "speaker_label_map", "transcription"],
+ world["both"]: ["participants", "transcription"]}
with app.app_context():
segs, parts, label_map = _speakers(world["only_old"])
assert segs == ["Murtaza", "Bob"] and parts == "Bob, Murtaza"