Skip to content

Commit 5c4a21b

Browse files
committed
Separate each utterance and word into its own doc.
1 parent e054c49 commit 5c4a21b

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

functions/src/webhooks/transcription.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const transcription = functions.https.onRequest(async (req, res) => {
4242
try {
4343
const transcriptionInDb = await db
4444
.collection("transcriptions")
45-
.doc(transcript.id)
45+
.doc(id)
4646

4747
await transcriptionInDb.set({
4848
id,
@@ -51,42 +51,44 @@ export const transcription = functions.https.onRequest(async (req, res) => {
5151
audio_url
5252
})
5353

54-
await transcriptionInDb
55-
.collection("timestamps")
56-
.doc("utterances")
57-
.set({
58-
utterances: utterances?.map(
59-
({ speaker, confidence, start, end, text }) => ({
60-
speaker,
61-
confidence,
62-
start,
63-
end,
64-
text
65-
})
54+
if (utterances) {
55+
const writer = db.bulkWriter()
56+
for (let utterance of utterances) {
57+
const { speaker, confidence, start, end, text } = utterance
58+
writer.set(
59+
db.doc(
60+
`/transcriptions/${transcript.id}/utterances/${utterance.start}`
61+
),
62+
{ speaker, confidence, start, end, text }
6663
)
67-
})
64+
}
6865

69-
await transcriptionInDb
70-
.collection("timestamps")
71-
.doc("words")
72-
.set({
73-
words
74-
})
66+
await writer.close()
67+
}
7568

76-
const batch = db.batch()
69+
if (words) {
70+
const writer = db.bulkWriter()
71+
for (let word of words) {
72+
writer.set(
73+
db.doc(
74+
`/transcriptions/${transcript.id}/words/${word.start}`
75+
),
76+
word
77+
)
78+
}
79+
80+
await writer.close()
81+
}
7782

83+
const batch = db.batch()
7884
batch.set(db.collection("transcriptions").doc(transcript.id), {
7985
_timestamp: Timestamp.now(),
8086
...transcript
8187
})
82-
8388
authenticatedEventsInDb.forEach(doc => {
8489
batch.update(doc.ref, { ["x-maple-webhook"]: null })
8590
})
86-
8791
await batch.commit()
88-
89-
console.log("transcript saved in db")
9092
} catch (error) {
9193
console.log(error)
9294
}

0 commit comments

Comments
 (0)